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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
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.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.harmonic.mapper.upload.PmsTemUserMapper;
import com.njcn.harmonic.pojo.param.upload.PmsTemProcessApprovalParam;
import com.njcn.harmonic.pojo.po.upload.PmsTemProcessTrackPO;
import com.njcn.harmonic.pojo.po.upload.PmsTemUserPO;
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.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

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;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.harmonic.mapper.upload.PmsTemProcessApprovalMapper;
import com.njcn.harmonic.pojo.po.upload.PmsTemProcessApproval;
import com.njcn.harmonic.service.upload.PmsTemProcessApprovalService;
/**
 * 
 * Description:
 * Date: 2024/10/25  10:23【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class PmsTemProcessApprovalServiceImpl extends ServiceImpl<PmsTemProcessApprovalMapper, PmsTemProcessApproval> implements PmsTemProcessApprovalService{

    private final DeptFeignClient deptFeignClient;

    private final PmsTemUserMapper pmsTemUserMapper;

    @Override
    public Boolean createOrUpdate(PmsTemProcessApprovalParam param) {
        PmsTemProcessApproval pmsTemProcessApproval = new PmsTemProcessApproval();
        BeanUtils.copyProperties(param,pmsTemProcessApproval);
        if(StrUtil.isBlank(pmsTemProcessApproval.getObjId())){
            pmsTemProcessApproval.setCreateTime(new Timestamp(new Date().getTime()));
            pmsTemProcessApproval.setUpdateTime(new Timestamp(new Date().getTime()));
            pmsTemProcessApproval.setUploadStatus(Integer.valueOf(DicDataEnum.No_Upload.getCode()));
        }else {
            PmsTemProcessApproval tem = this.getById(param.getObjId());
            if(Integer.valueOf(DicDataEnum.Has_Upload.getCode()).equals(tem.getUploadStatus())){
                pmsTemProcessApproval.setUploadStatus(Integer.valueOf(DicDataEnum.Return_Upload.getCode()));
            }
            pmsTemProcessApproval.setUpdateTime(new Timestamp(new Date().getTime()));
            pmsTemProcessApproval.setUploadStatus(Integer.valueOf(DicDataEnum.Return_Upload.getCode()));
        }
        boolean b = this.saveOrUpdate(pmsTemProcessApproval);
        return b;
    }

    @Override
    public Page<PmsTemProcessApproval> pageList(PmsTemProcessApprovalParam.QueryPmsTemProcessApprovalParam param) {
        List<String> deptCodes = deptFeignClient.getDepSonSelfCodetByDeptId(param.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<>(param.getPageNum(),param.getPageSize());
        }
        LambdaQueryWrapper<PmsTemProcessApproval> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .between(StringUtils.isNotBlank(param.getSearchBeginTime())&&StringUtils.isNotBlank(param.getSearchEndTime()),PmsTemProcessApproval::getCreateTime,param.getSearchBeginTime(),param.getSearchEndTime()).in(PmsTemProcessApproval::getTempUserDossierId,objIds);

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

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

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