package org.jeecg.modules.stm.act.check.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.stm.act.check.entity.StCaMainCheck;
import org.jeecg.modules.stm.act.check.mapper.StCaMainCheckMapper;
import org.jeecg.modules.stm.act.check.service.IStCaMainCheckService;
import org.jeecg.modules.stm.act.check.vo.StCaMainCheckVo;
import org.jeecg.modules.stm.act.main.entity.StCaMain;
import org.jeecg.modules.stm.act.main.entity.StCaMainLable;
import org.jeecg.modules.stm.act.main.service.IStCaMainLableService;
import org.jeecg.modules.stm.act.main.service.IStCaMainService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;
import org.jeecg.modules.stm.base.lable.entity.StLable;
import org.jeecg.modules.stm.base.lable.service.IStLableService;
import org.jeecg.modules.stm.utils.QueryCaManParamsUtlis;
import org.jeecg.modules.stm.utils.StmConstans;
import org.jeecg.modules.stm.utils.UserUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 检查方法
 * @Author: yzh
 * @Date:   2023-03-06
 * @Version: V1.0
 */
@Service
public class StCaMainCheckServiceImpl extends ServiceImpl<StCaMainCheckMapper, StCaMainCheck> implements IStCaMainCheckService {

    @Autowired
    private IStCaMainLableService stCaMainLableService;

    @Autowired
    private IStLableService iStLableService;

    @Autowired
    private IStDomainService iStDomainService;

    @Autowired
    private IStCaMainService iStCaMainService;


    private void check(StCaMainCheck entity){
        if(entity==null){
            return;
        }
        QueryWrapper<StCaMainCheck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CA_NO",entity.getCaNo());
        long count = super.count(queryWrapper);
        if(count>0){
            throw new JeecgBootException("控制活动名称已存在");
        }
    }

    @Override
    public boolean save(StCaMainCheck entity) {
        //check(entity);
        entity.setCreatedUser(UserUtil.getUserCode());
        entity.setValid(StmConstans.DEFAULT_VALID);
        entity.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
        return super.save(entity);
    }


    @Override
    public boolean updateById(StCaMainCheck entity) {
        entity.setUpdatedUser(UserUtil.getUserCode());
        return super.updateById(entity);
    }

    private List<StCaMainLable> listLable(String caNo){
        QueryWrapper<StCaMainLable> wapper = new QueryWrapper();
        wapper.eq("CA_NO",caNo);
        return stCaMainLableService.list(wapper);
    }

    @Override
    public IPage<StCaMainCheck> defPage(StCaMain stCaMain, Integer pageNo, Integer pageSize, Map<String, String[]> parameterMap) {

        //1、 查询条件组织数据
        List<String> caNos = QueryCaManParamsUtlis.getCaNos(parameterMap);
        QueryWrapper<StCaMainCheck> wrapper = new QueryWrapper<>();
        if(!CollectionUtils.isEmpty(caNos)){
            if(caNos.size()==1 && "notExist".equals(caNos.get(0))){
               return null;
            }
            wrapper.in("CA_NO",caNos);
        }
        if(stCaMain.getId()!=null){
            wrapper.eq("id",stCaMain.getId());
        }
        wrapper.and(w->{
            w.last("\tUPDATED_TIME in(\n" +
                    "select\n" +
                    "\tmax(scmc.UPDATED_TIME) \n" +
                    "from\n" +
                    "\tst_ca_main_check scmc\n" +
                    "\twhere scmc .CA_NO = CA_NO\n" +
                    "group by\n" +
                    "\tscmc.CA_NO)");
        });
        wrapper.orderByDesc("updated_time");
        // 进行检查信息的查询
        Page<StCaMainCheck> page = new Page<StCaMainCheck>(pageNo, pageSize);
        IPage<StCaMainCheck> pageList = super.page(page, wrapper);
        List<StCaMainCheck> voList = new ArrayList<>();
        List<StCaMainCheck> list = pageList.getRecords();
        // 遍历list转子信息，增强查询信息
        for(StCaMainCheck stCaMainCheck:list){
            StCaMainCheckVo sCaMainCheckVo = new StCaMainCheckVo();
            BeanUtils.copyProperties(stCaMainCheck,sCaMainCheckVo);
            // 增强控制活动名称
            StCaMain stCaMain2 = iStCaMainService.queryByCaNo(stCaMainCheck.getCaNo());
            if(stCaMain2!=null) {
                sCaMainCheckVo.setCaName(stCaMain2.getCaName());
            }else{
                sCaMainCheckVo.setCaName("-");
            }
            // 增强标签信息
            List<StCaMainLable> data = listLable(stCaMainCheck.getCaNo());
            if(!CollectionUtils.isEmpty(data)){
                List<String> lableCodeList = data.stream().map(StCaMainLable::getLableCode).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(lableCodeList)) {
                    List<StLable> lables = iStLableService.listByCods(lableCodeList);
                    List<String> lableNames = lables.stream().map(StLable::getLableName).collect(Collectors.toList());
                    if(!CollectionUtils.isEmpty(lableNames)) {
                        sCaMainCheckVo.setLableNameList(lableNames);
                    }else {
                        sCaMainCheckVo.setLableNameList(Arrays.asList("-"));
                    }
                }else {
                    sCaMainCheckVo.setLableNameList(Arrays.asList("-"));
                }
            }else{
                sCaMainCheckVo.setLableNameList(Arrays.asList("-"));
            }
            List<StCaMainCheck> childList =  getChild(sCaMainCheckVo.getId(),sCaMainCheckVo.getCaNo());
            sCaMainCheckVo.setChildren(childList);
            voList.add(sCaMainCheckVo);
        }
        pageList.setRecords(voList);
        return pageList;
    }

    private List<StCaMainCheck> getChild(Serializable id,String caNo){
        QueryWrapper<StCaMainCheck> wrapper = new QueryWrapper<>();
        wrapper.eq("CA_NO",caNo);
        wrapper.ne("id",id);
        wrapper.orderByDesc("updated_time");
        List<StCaMainCheck> stCaMainCheckList = super.list(wrapper);
        List<StCaMainCheck> voList = new ArrayList<>();
        for(StCaMainCheck stCaMainCheck:stCaMainCheckList){
            StCaMainCheckVo sCaMainCheckVo = new StCaMainCheckVo();
            BeanUtils.copyProperties(stCaMainCheck,sCaMainCheckVo);
            // 增强控制活动名称
            StCaMain stCaMain2 = iStCaMainService.queryByCaNo(stCaMainCheck.getCaNo());
            if(stCaMain2!=null) {
                sCaMainCheckVo.setCaName(stCaMain2.getCaName());
            }else{
                sCaMainCheckVo.setCaName("-");
            }
            // 增强标签信息
            List<StCaMainLable> data = listLable(stCaMainCheck.getCaNo());
            if(!CollectionUtils.isEmpty(data)){
                List<String> lableCodeList = data.stream().map(StCaMainLable::getLableCode).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(lableCodeList)) {
                    List<StLable> lables = iStLableService.listByCods(lableCodeList);
                    List<String> lableNames = lables.stream().map(StLable::getLableName).collect(Collectors.toList());
                    if(!CollectionUtils.isEmpty(lableNames)) {
                        sCaMainCheckVo.setLableNameList(lableNames);
                    }else {
                        sCaMainCheckVo.setLableNameList(Arrays.asList("-"));
                    }
                }else {
                    sCaMainCheckVo.setLableNameList(Arrays.asList("-"));
                }
            }else{
                sCaMainCheckVo.setLableNameList(Arrays.asList("-"));
            }
            voList.add(sCaMainCheckVo);
        }
        return voList;
    }
}
