package com.njcn.harmonic.service.upload.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.harmonic.mapper.upload.PmsTemProcessTestRunMapper;
import com.njcn.harmonic.mapper.upload.PmsTemUserMapper;
import com.njcn.harmonic.pojo.param.upload.PmsTemProcessApprovalParam;
import com.njcn.harmonic.pojo.param.upload.PmsTemProcessTestRunParam;
import com.njcn.harmonic.pojo.po.upload.PmsTemProcessApproval;
import com.njcn.harmonic.pojo.po.upload.PmsTemProcessTestRun;
import com.njcn.harmonic.pojo.po.upload.PmsTemUserPO;
import com.njcn.harmonic.service.upload.IPmsTemProcessTestRunService;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.web.enums.GWSendEnum;
import com.njcn.web.factory.PageFactory;
import com.njcn.web.pojo.param.SendParam;
import com.njcn.web.utils.GwSendUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cdf
 * @since 2024-11-04
 */
@Service
@RequiredArgsConstructor
public class PmsTemProcessTestRunServiceImpl extends ServiceImpl<PmsTemProcessTestRunMapper, PmsTemProcessTestRun> implements IPmsTemProcessTestRunService {

    private final DeptFeignClient deptFeignClient;

    private final PmsTemUserMapper pmsTemUserMapper;

    @Override
    public Boolean create(PmsTemProcessTestRunParam param) {
        PmsTemProcessTestRun pmsTemProcessTestRun = new PmsTemProcessTestRun();
        BeanUtil.copyProperties(param,pmsTemProcessTestRun);
        pmsTemProcessTestRun.setUploadStatus(Integer.valueOf(DicDataEnum.No_Upload.getCode()));
        pmsTemProcessTestRun.setCreateTime(new Timestamp(new Date().getTime()));
        pmsTemProcessTestRun.setUpdateTime(new Timestamp(new Date().getTime()));
        this.save(pmsTemProcessTestRun);
        return true;
    }

    @Override
    public Boolean update(PmsTemProcessTestRunParam.UpdatePmsTemProcessTestRunParamParam param) {
        PmsTemProcessTestRun tem = this.getById(param.getObjId());
        PmsTemProcessTestRun pmsTemProcessTestRun = new PmsTemProcessTestRun();
        BeanUtil.copyProperties(param,pmsTemProcessTestRun);
        pmsTemProcessTestRun.setUpdateTime(new Timestamp(new Date().getTime()));
        if(Integer.valueOf(DicDataEnum.Has_Upload.getCode()).equals(tem.getUploadStatus())){
            pmsTemProcessTestRun.setUploadStatus(Integer.valueOf(DicDataEnum.Return_Upload.getCode()));
        }
        this.updateById(pmsTemProcessTestRun);
        return true;
    }

    @Override
    public Boolean del(List<String> ids) {
        return this.removeByIds(ids);
    }

    @Override
    public Page<PmsTemProcessTestRun> pageList(PmsTemProcessTestRunParam.QueryPmsTemProcessTestRunParam param) {
        List<String> deptCodes = deptFeignClient.getDepSonSelfCodetByDeptId(param.getMaintOrg()).getData();
        List<PmsTemUserPO> pmsTemProcessTestRunList = pmsTemUserMapper.selectList(new LambdaQueryWrapper<PmsTemUserPO>().select(PmsTemUserPO::getObjId,PmsTemUserPO::getConsName).in(PmsTemUserPO::getMaintOrg,deptCodes)).stream().collect(Collectors.toList());
        if(CollUtil.isEmpty(pmsTemProcessTestRunList)){
            return new Page<>(param.getPageNum(),param.getPageSize());
        }
        List<String> objIds = pmsTemProcessTestRunList.stream().map(PmsTemUserPO::getObjId).distinct().collect(Collectors.toList());
        Map<String,PmsTemUserPO> map = pmsTemProcessTestRunList.stream().collect(Collectors.toMap(PmsTemUserPO::getObjId, Function.identity()));
        LambdaQueryWrapper<PmsTemProcessTestRun> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(PmsTemProcessTestRun::getTempUserDossierId,objIds);

        Page<PmsTemProcessTestRun> page = this.page(new Page<>(PageFactory.getPageNum(param), PageFactory.getPageSize(param)), lambdaQueryWrapper);

        if(CollUtil.isNotEmpty(page.getRecords())){
            page.getRecords().forEach(item->{
                PmsTemUserPO tem = map.get(item.getTempUserDossierId());
                item.setTempUserDossierName(tem.getConsName());
                item.setCityOrgName(tem.getCityOrgName());
                item.setMaintOrgName(tem.getMaintOrgName());
            });
        }

        return page;
    }

    @Override
    public PmsTemProcessTestRun queryById(String id) {
        PmsTemProcessTestRun pmsTemProcessTestRun = this.getById(id);


        return pmsTemProcessTestRun;
    }

    @Override
    public Boolean upGw(List<String> ids) {
        LambdaQueryWrapper<PmsTemProcessTestRun> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(PmsTemProcessTestRun::getObjId,ids);
        List<PmsTemProcessTestRun> pmsTemProcessApprovalList = this.list(lambdaQueryWrapper);
        if(CollUtil.isEmpty(pmsTemProcessApprovalList)){
            throw new BusinessException("查询试运行为空");
        }

        List<List<PmsTemProcessTestRun>> list = CollUtil.split(pmsTemProcessApprovalList,100);
        for(int i=0;i<list.size();i++){
            SendParam sendParam = new SendParam();
            if(i==0){
                sendParam.setIsAppend("0");
            }else if(i==list.size()-1){
                sendParam.setIsAppend("2");
            }else {
                sendParam.setIsAppend("1");
            }
            sendParam.setStats(list.get(i));
            Map<String, String> sendRes = GwSendUtil.newSend(sendParam, GWSendEnum.PROCESS_TEST_RUN_CREATE);
            List<String> TemProcessTrackPOIds = list.get(i).stream().map(PmsTemProcessTestRun::getObjId).collect(Collectors.toList());
            int count = GwSendUtil.returnInfoMsg(TemProcessTrackPOIds,sendRes);
            System.out.println("上送成功,上送成功返回"+count+"条");
            if(count == list.get(i).size()){
                LambdaUpdateWrapper<PmsTemProcessTestRun> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.set(PmsTemProcessTestRun::getUploadStatus,Integer.valueOf(DicDataEnum.Has_Upload.getCode())).in(PmsTemProcessTestRun::getObjId,ids);
                this.update(lambdaUpdateWrapper);
            }
        }
        return true;
    }
}
