package org.jeecg.modules.stm.problem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.stm.act.collection.entity.StCaMainRelCollection;
import org.jeecg.modules.stm.act.collection.service.IStCaMainRelCollectionService;
import org.jeecg.modules.stm.act.main.entity.StCaMain;
import org.jeecg.modules.stm.act.main.service.IStCaMainService;
import org.jeecg.modules.stm.problem.entity.StProblem;
import org.jeecg.modules.stm.problem.entity.StProblemCa;
import org.jeecg.modules.stm.problem.mapper.StProblemMapper;
import org.jeecg.modules.stm.problem.service.IStProblemService;
import org.jeecg.modules.stm.problem.vo.StProblemVo;
import org.jeecg.modules.stm.project.create.entity.StProjectAct;
import org.jeecg.modules.stm.project.create.service.IStProjectActService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;
import org.jeecg.modules.stm.base.domain.vo.StDomainVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.utils.QueryCaManParamsUtlis;
import org.jeecg.modules.stm.utils.StmConstans;
import org.jeecg.modules.stm.utils.UserUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: st_problem
 * @Author: yzh
 * @Date:   2023-03-04
 * @Version: V1.0
 */
@Service
public class StProblemServiceImpl extends ServiceImpl<StProblemMapper, StProblem> implements IStProblemService {

    @Autowired
    private IStCaMainService iStCaMainService;

    @Autowired
    private IStDomainService iStDomainService;


    @Override
    public <E extends IPage<StProblem>> E page(E page, Wrapper<StProblem> queryWrapper) {
        IPage<StProblem> iPage =  super.page(page, queryWrapper);
        List<StProblem> data = iPage.getRecords();
        if(CollectionUtils.isEmpty(data)){
            return (E) iPage;
        }
        List<StProblem> records = new ArrayList<>();
        for(StProblem stProblem:data){
            StProblemVo stProblemVo = new StProblemVo();
            BeanUtils.copyProperties(stProblem,stProblemVo);
            StCaMain stCaMain = iStCaMainService.queryByCaNo(stProblem.getCaNo());
            if(stCaMain!=null) {
                stProblemVo.setCaName(stCaMain.getCaName());
                StDomainVo stDomain = iStDomainService.queryEntityByCode(stCaMain.getDomainCode());
                if(stDomain!=null) {
                    stProblemVo.setDomainName(stDomain.getPathDomainName());
                }else{
                    stProblemVo.setDomainName("-");
                }
            }else{
                stProblemVo.setCaName("-");
                stProblemVo.setDomainName("-");
            }
            records.add(stProblemVo);
        }
        iPage.setRecords(records);
        return (E) iPage;
    }

    private void checkName(StProblem entity, boolean isSave){
        if(entity==null){
            return;
        }
        QueryWrapper<StProblem> wapper = new QueryWrapper<>();
        wapper.eq("PROBLEM_ITEM",entity.getProblemItem());
        if(!isSave){
            wapper.ne("ID",entity.getId());
        }
        long count = super.count(wapper);
        if(count>0){
            throw new JeecgBootException("问题项已存在");
        }
    }

    @Override
    public boolean save(StProblem entity) {
        checkName(entity,true);
        entity.setCreatedUser(UserUtil.getUserCode());
        entity.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
        entity.setValid(StmConstans.DEFAULT_VALID);
        return super.save(entity);
    }

    @Override
    public boolean updateById(StProblem entity) {
        checkName(entity,false);
        entity.setUpdatedUser(UserUtil.getUserCode());
        return super.updateById(entity);
    }

    @Override
    public IPage<StProblem> defPage(StProblem stProblem, Integer pageNo, Integer pageSize, Map<String, String[]> parameterMap) {
        QueryWrapper<StProblem> queryWrapper = QueryGenerator.initQueryWrapper(stProblem, parameterMap);
        List<String> caNos = QueryCaManParamsUtlis.getCaNos(parameterMap);
        if(!CollectionUtils.isEmpty(caNos)){
            if(caNos.size()==1 && "notExist".equals(caNos.get(0))){
                return null;
            }
            queryWrapper.in("CA_NO",caNos);
        }
        String [] complianeNoIsNull = parameterMap.get("complianeNoIsNull");
        if(complianeNoIsNull!=null && complianeNoIsNull.length>0){

            queryWrapper.and(wrapper ->
                    wrapper.isNull("COMPLIANE_NO").or().eq("COMPLIANE_NO",""));
        }
        Page<StProblem> page = new Page<StProblem>(pageNo, pageSize);
        IPage<StProblem> pageList = this.page(page, queryWrapper);
        return pageList;
    }

    @Override
    public void deletecompliane(String id) {
        if(StringUtils.hasText(id)) {
            StProblem stProblem = new StProblem();
            stProblem.setId(Integer.valueOf(id));
            stProblem.setComplianeNo("");
            this.updateById(stProblem);
        }else{
            throw new JeecgBootException("问题"+id+"不存在");
        }
    }

    @Override
    public IPage<StProblemVo> queryProblemWithPage(Page<StProblem> page, StProblemVo stProblemVo, Map<String, String[]> parameterMap){
        QueryWrapper<StProblem> queryWrapper = QueryGenerator.initQueryWrapper(stProblemVo, parameterMap);
        System.out.println(parameterMap.toString());;
        IPage<StProblemVo> data = this.baseMapper.queryStProblems(page,stProblemVo);
        return data;
    }

    @Override
    public void insertProblemCa(StProblemCa stProblemCa) {
        this.baseMapper.insertProblemCa(stProblemCa);
    }

    @Override
    public void deleteProblemCa(StProblemCa stProblemCa) {
        this.baseMapper.deleteProblemCa(stProblemCa);
    }

    @Override
    public IPage<StProblem> queryPageListByProjectId(StProblem stProblem,Integer projectCreId, Integer pageNo, Integer pageSize, Map<String, String[]> parameterMap) {

        QueryWrapper<StProblem> queryWrapper = QueryGenerator.initQueryWrapper(stProblem, parameterMap);


        IStProjectActService iStProjectActService = SpringContextUtils.getBean(IStProjectActService.class);
        QueryWrapper<StProjectAct> stProjectActQueryWrapper = new QueryWrapper<>();
        stProjectActQueryWrapper.eq("PROJECT_CRE_ID",projectCreId);
        StProjectAct stProjectAct1 = iStProjectActService.getOne(stProjectActQueryWrapper);
        if(stProjectAct1==null){
            return null;
        }
        QueryWrapper<StCaMainRelCollection> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("CA_NO");
        queryWrapper2.eq("COLLECTION_NO",stProjectAct1.getCollectionNo());
        IStCaMainRelCollectionService stCaMainRelCollectionService = SpringContextUtils.getBean(IStCaMainRelCollectionService.class);
        List<StCaMainRelCollection> caList =  stCaMainRelCollectionService.list(queryWrapper2);
        if(CollectionUtils.isEmpty(caList)){
            return null;
        }
        List<String> caNos = caList.stream().map(StCaMainRelCollection::getCaNo).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(caNos)){
            if(caNos.size()==1 && "notExist".equals(caNos.get(0))){
                return null;
            }
            queryWrapper.in("CA_NO",caNos);
        }

        Page<StProblem> page = new Page<StProblem>(pageNo, pageSize);
        IPage<StProblem> pageList = this.page(page, queryWrapper);
        return pageList;
    }


}
