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


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
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.PmsTemProcessTrackMapper;
import com.njcn.harmonic.mapper.upload.PmsTemUserMapper;
import com.njcn.harmonic.pojo.param.upload.PmsTemProcessTrackParam;
import com.njcn.harmonic.pojo.po.upload.PmsTemProcessTrackPO;
import com.njcn.harmonic.pojo.po.upload.PmsTemUserPO;
import com.njcn.harmonic.service.upload.IPmsTemProcessTrackService;
import com.njcn.harmonic.service.upload.IPmsTemUserService;
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.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-10-09
 */
@Service
@RequiredArgsConstructor
public class PmsTemProcessTrackServiceImpl extends ServiceImpl<PmsTemProcessTrackMapper, PmsTemProcessTrackPO> implements IPmsTemProcessTrackService {

    private final IPmsTemUserService iPmsTemUserService;

    private final DeptFeignClient deptFeignClient;

    private final PmsTemUserMapper pmsTemUserMapper;


    @Override
    public Boolean addTemProcessTrack(PmsTemProcessTrackParam param) {
        PmsTemProcessTrackPO pmsTemProcessTrackPO = new PmsTemProcessTrackPO();
        BeanUtil.copyProperties(param, pmsTemProcessTrackPO);
        pmsTemProcessTrackPO.setObjId(IdUtil.simpleUUID());
        PmsTemUserPO pmsTemUserPO = iPmsTemUserService.getById(param.getTempUserDossierId());
        pmsTemProcessTrackPO.setTempUserDossierName(pmsTemUserPO.getConsName());
        pmsTemUserPO.setCreateTime(new Timestamp(new Date().getTime()));
        pmsTemUserPO.setUpdateTime(new Timestamp(new Date().getTime()));
        pmsTemProcessTrackPO.setUploadStatus(Integer.valueOf(DicDataEnum.No_Upload.getCode()));
        return this.save(pmsTemProcessTrackPO);
    }

    @Override
    public Boolean updateTemProcessTrack(PmsTemProcessTrackParam.UpdatePmsTemProcessTrackParam updatePmsTemProcessTrackParam) {
        //PmsTemProcessTrackPO result = this.getById(updatePmsTemProcessTrackParam.getObjId());
        PmsTemProcessTrackPO pmsTemProcessTrackPO = new PmsTemProcessTrackPO();
        BeanUtil.copyProperties(updatePmsTemProcessTrackParam, pmsTemProcessTrackPO);
        PmsTemUserPO pmsTemUserPO = iPmsTemUserService.getById(updatePmsTemProcessTrackParam.getTempUserDossierId());
        pmsTemProcessTrackPO.setTempUserDossierName(pmsTemUserPO.getConsName());

        PmsTemProcessTrackPO tem = this.getById(updatePmsTemProcessTrackParam.getTempUserDossierId());
        if(Integer.valueOf(DicDataEnum.Has_Upload.getCode()).equals(tem.getUploadStatus())){
            pmsTemProcessTrackPO.setUploadStatus(Integer.valueOf(DicDataEnum.Return_Upload.getCode()));
        }
        pmsTemProcessTrackPO.setUpdateTime(new Timestamp(new Date().getTime()));
        return this.updateById(pmsTemProcessTrackPO);
    }

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

    @Override
    public Page<PmsTemProcessTrackPO> pageList(PmsTemProcessTrackParam.QueryPmsTemProcessTrackParam parkQuery) {
        List<String> deptCodes = deptFeignClient.getDepSonSelfCodetByDeptId(parkQuery.getMaintOrg()).getData();
        List<PmsTemUserPO> pmsTemUserPOList = pmsTemUserMapper.selectList(new LambdaQueryWrapper<PmsTemUserPO>().select(PmsTemUserPO::getObjId,PmsTemUserPO::getCityOrgName,PmsTemUserPO::getMaintOrgName).in(PmsTemUserPO::getMaintOrg,deptCodes));
        Map<String,PmsTemUserPO> pmsTemUserPOMap = pmsTemUserPOList.stream().collect(Collectors.toMap(PmsTemUserPO::getObjId, Function.identity()));
        List<String> objIds = pmsTemUserPOList.stream().map(PmsTemUserPO::getObjId).collect(Collectors.toList());
        if(CollUtil.isEmpty(objIds)){
            return new Page<>(parkQuery.getPageNum(),parkQuery.getPageSize());
        }
        LambdaQueryWrapper<PmsTemProcessTrackPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(PmsTemProcessTrackPO::getTempUserDossierId,objIds);

        Page<PmsTemProcessTrackPO> page = this.page(new Page<>(PageFactory.getPageNum(parkQuery), PageFactory.getPageSize(parkQuery)), lambdaQueryWrapper);
        page.getRecords().forEach(item->{
            PmsTemUserPO tem = pmsTemUserPOMap.get(item.getTempUserDossierId());
            item.setCityOrgName(tem.getCityOrgName());
            item.setMaintOrgName(tem.getMaintOrgName());
        });
        return page;
    }

    @Override
    public PmsTemProcessTrackPO getById(PmsTemProcessTrackParam.QueryPmsTemProcessTrackParam param) {
        LambdaQueryWrapper<PmsTemProcessTrackPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsTemProcessTrackPO::getTempUserDossierId,param.getTempUserDossierId());
        PmsTemProcessTrackPO pmsTemProcessTrackPO = this.getOne(lambdaQueryWrapper);
        return pmsTemProcessTrackPO;
    }

    @Override
    public Boolean upGw(List<String> ids) {
        LambdaQueryWrapper<PmsTemProcessTrackPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(PmsTemProcessTrackPO::getObjId,ids);
        List<PmsTemProcessTrackPO> TemProcessTrackPOList = this.list(lambdaQueryWrapper);
        if(CollUtil.isEmpty(TemProcessTrackPOList)){
            throw new BusinessException("查询用户临时档案数据为空");
        }

        List<List<PmsTemProcessTrackPO>> list = CollUtil.split(TemProcessTrackPOList,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.TEMP_PROCESS_TRACK_CREATE);
            List<String> TemProcessTrackPOIds = list.get(i).stream().map(PmsTemProcessTrackPO::getObjId).collect(Collectors.toList());
            int count = GwSendUtil.returnInfoMsg(TemProcessTrackPOIds,sendRes);
            System.out.println("上送成功,上送成功返回"+count+"条");
            if(count == list.get(i).size()){
                LambdaUpdateWrapper<PmsTemProcessTrackPO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
                lambdaUpdateWrapper.set(PmsTemProcessTrackPO::getUploadStatus,Integer.valueOf(DicDataEnum.Has_Upload.getCode())).in(PmsTemProcessTrackPO::getObjId,ids);
                this.update(lambdaUpdateWrapper);
            }
        }
        return true;
    }
    
}
