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.pojo.po.EleEpdPqd;
import com.njcn.system.pojo.vo.CsStatisticalSetVO;
import com.njcn.system.pojo.vo.EleEpdPqdListVO;
import com.njcn.system.service.IEleEpdPqdService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.system.mapper.CsStatisticalSetPOMapper;
import com.njcn.system.pojo.po.CsStatisticalSetPO;
import com.njcn.system.service.CsStatisticalSetPOService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * 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;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean saveData(String id, List<String> ids) {
        List<CsStatisticalSetPO> csStatisticalSetPOList = new ArrayList<>();
        QueryWrapper<CsStatisticalSetPO> queryWrap = new QueryWrapper<>();
        queryWrap.lambda().eq(CsStatisticalSetPO::getStatisicalId, id);
        this.getBaseMapper().delete(queryWrap);
        ids.forEach(tempId -> {
            CsStatisticalSetPO csStatisticalSetPO = new CsStatisticalSetPO();
            csStatisticalSetPO.setStatisicalId(id);
            csStatisticalSetPO.setTargetId(tempId);
            csStatisticalSetPOList.add(csStatisticalSetPO);
        });
        boolean b = this.saveBatch(csStatisticalSetPOList);
        return b;
    }

    @Override
    public CsStatisticalSetVO queryStatistical(String id) {
        CsStatisticalSetVO csStatisticalSetVO = new CsStatisticalSetVO();
        QueryWrapper<CsStatisticalSetPO> queryWrap = new QueryWrapper<>();
        queryWrap.lambda().eq(CsStatisticalSetPO::getStatisicalId, id);
        List<CsStatisticalSetPO> result = this.baseMapper.selectList(queryWrap);
        List<String> collect = result.stream().map(CsStatisticalSetPO::getTargetId).collect(Collectors.toList());
        List<EleEpdPqdListVO> allList = epdPqdService.selectAll();
        List<EleEpdPqdListVO> selectedList = new ArrayList<>();
        List<EleEpdPqdListVO> unSelectedList = new ArrayList<>();



            for (EleEpdPqdListVO temp :allList){
                EleEpdPqdListVO selected = new EleEpdPqdListVO();
                EleEpdPqdListVO unSelected = new EleEpdPqdListVO();
                List<EleEpdPqd> all = temp.getEleEpdPqdVOS();
                List<EleEpdPqd> selectedELe = all.stream().filter(tempEleEpdPqd -> collect.contains(tempEleEpdPqd.getId())).collect(Collectors.toList());
                List<EleEpdPqd> unSelectedELe = all.stream().filter(tempEleEpdPqd -> !collect.contains(tempEleEpdPqd.getId())).collect(Collectors.toList());

                selected.setDataTypeName(temp.getDataTypeName());
                selected.setDataType(temp.getDataTypeName());
                selected.setEleEpdPqdVOS(selectedELe);
                unSelected.setDataTypeName(temp.getDataTypeName());
                unSelected.setDataType(temp.getDataTypeName());
                unSelected.setEleEpdPqdVOS(unSelectedELe);
                selectedList.add(selected);
                unSelectedList.add(unSelected);
            }
            csStatisticalSetVO.setUnSelectedList(unSelectedList);
            csStatisticalSetVO.setSelectedList(selectedList);



        return csStatisticalSetVO;
    }

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


