package com.njcn.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.system.mapper.CsStatisticalSetPOMapper;
import com.njcn.system.pojo.param.CsStatisticalSetAddParam;
import com.njcn.system.pojo.po.CsStatisticalSetPO;
import com.njcn.system.pojo.po.EleEpdPqd;
import com.njcn.system.pojo.vo.CsStatisticalSetVO;
import com.njcn.system.pojo.vo.DictTreeVO;
import com.njcn.system.pojo.vo.EleEpdPqdListVO;
import com.njcn.system.service.CsStatisticalSetPOService;
import com.njcn.system.service.IEleEpdPqdService;
import com.njcn.system.service.SysDicTreePOService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Description:
 * Date: 2023/6/9  16:17【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class CsStatisticalSetPOServiceImpl extends MppServiceImpl<CsStatisticalSetPOMapper, CsStatisticalSetPO> implements CsStatisticalSetPOService {

    private final IEleEpdPqdService epdPqdService;

    private final SysDicTreePOService sysDicTreePOService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean saveData(List<CsStatisticalSetAddParam> csStatisticalSetAddParams) {
        csStatisticalSetAddParams.forEach(temp -> {
            List<CsStatisticalSetPO> csStatisticalSetPOList = new ArrayList<>();
            QueryWrapper<CsStatisticalSetPO> queryWrap = new QueryWrapper<>();
            queryWrap.lambda().eq(CsStatisticalSetPO::getStatisicalId, temp.getId());
            this.getBaseMapper().delete(queryWrap);
            temp.getIds().forEach(tempId -> {
                CsStatisticalSetPO csStatisticalSetPO = new CsStatisticalSetPO();
                csStatisticalSetPO.setStatisicalId(temp.getId());
                csStatisticalSetPO.setTargetId(tempId);
                csStatisticalSetPOList.add(csStatisticalSetPO);
            });
            this.saveBatch(csStatisticalSetPOList);
        });

        return true;
    }

    @Override
    public CsStatisticalSetVO queryStatistical(String id) {
        CsStatisticalSetVO csStatisticalSetVO = new CsStatisticalSetVO();

        List<EleEpdPqdListVO> allList = epdPqdService.selectAll();
        List<EleEpdPqdListVO> selectedList = new ArrayList<>();
        List<EleEpdPqdListVO> unSelectedList = new ArrayList<>();
        List<DictTreeVO> dictTreeVOS = sysDicTreePOService.queryByPid(id);
        List<String> collect;
        if (!CollectionUtils.isEmpty(dictTreeVOS)) {
            dictTreeVOS = dictTreeVOS.stream().sorted(Comparator.comparing(DictTreeVO::getSort)).collect(Collectors.toList());
            List<String> collect3 = dictTreeVOS.stream().map(DictTreeVO::getId).collect(Collectors.toList());
            collect3.add(id);
            QueryWrapper<CsStatisticalSetPO> queryWrap = new QueryWrapper<>();
            queryWrap.lambda().in(CsStatisticalSetPO::getStatisicalId, collect3);
            List<CsStatisticalSetPO> result = this.baseMapper.selectList(queryWrap);
            collect = result.stream().map(CsStatisticalSetPO::getTargetId).collect(Collectors.toList());
            dictTreeVOS.forEach(temp -> {
                EleEpdPqdListVO vo = new EleEpdPqdListVO();
                vo.setDataType(temp.getId());
                vo.setSort(temp.getSort());
                List<CsStatisticalSetPO> collect2 = result.stream().filter(csStatisticalSetPO -> Objects.equals(csStatisticalSetPO.getStatisicalId(), temp.getId())).collect(Collectors.toList());
                List<EleEpdPqd> eleEpdPqds = new ArrayList<>();
                if (!CollectionUtil.isEmpty(collect2)) {
                    List<String> collect4 = collect2.stream().map(CsStatisticalSetPO::getTargetId).collect(Collectors.toList());
                    eleEpdPqds = epdPqdService.getBaseMapper().selectBatchIds(collect4);
                }

                vo.setEleEpdPqdVOS(eleEpdPqds);
                vo.setDataTypeName(temp.getName());
                selectedList.add(vo);
            });
        } else {
            collect = new ArrayList<>();
        }
        for (EleEpdPqdListVO temp : allList) {
            EleEpdPqdListVO unSelected = new EleEpdPqdListVO();
            List<EleEpdPqd> all = temp.getEleEpdPqdVOS();
            List<EleEpdPqd> unSelectedELe = all.stream().filter(tempEleEpdPqd -> !collect.contains(tempEleEpdPqd.getId())).collect(Collectors.toList());
            unSelected.setDataTypeName(temp.getDataTypeName());
            unSelected.setDataType(temp.getDataType());
            unSelected.setEleEpdPqdVOS(unSelectedELe);
            unSelectedList.add(unSelected);
        }
        csStatisticalSetVO.setUnSelectedList(unSelectedList);
        csStatisticalSetVO.setSelectedList(selectedList);


        return csStatisticalSetVO;
    }

    @Override
    public List<EleEpdPqd> queryStatisticalSelect(List<String> list) {
        QueryWrapper<CsStatisticalSetPO> queryWrap = new QueryWrapper<>();
        queryWrap.lambda().in(CsStatisticalSetPO::getStatisicalId, list);
        List<CsStatisticalSetPO> result = this.baseMapper.selectList(queryWrap);
        List<String> collect = result.stream().map(CsStatisticalSetPO::getTargetId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return new ArrayList<>();
        }
        return epdPqdService.listByIds(collect);
    }

    @Override
    public List<CsStatisticalSetPO> queryStatisticalById(List<String> list) {
        QueryWrapper<CsStatisticalSetPO> queryWrap = new QueryWrapper<>();
        queryWrap.lambda().in(CsStatisticalSetPO::getStatisicalId, list);
        return this.baseMapper.selectList(queryWrap);
    }
}


