package com.njcn.process.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.utils.PubUtils;
import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.process.enums.ProcessResponseEnum;
import com.njcn.process.mapper.SupvFileMapper;
import com.njcn.process.mapper.SupvProblemMapper;
import com.njcn.process.pojo.param.SupvProblemParam;
import com.njcn.process.pojo.po.SupvFile;
import com.njcn.process.pojo.po.SupvPlan;
import com.njcn.process.pojo.po.SupvProblem;
import com.njcn.process.pojo.po.SupvProblem;
import com.njcn.process.service.ISupvProblemService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.vo.PvTerminalTreeVO;
import com.njcn.web.factory.PageFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2023-06-21
 */
@Service
@RequiredArgsConstructor
public class SupvProblemServiceImpl extends ServiceImpl<SupvProblemMapper, SupvProblem> implements ISupvProblemService {

    private final DicDataFeignClient dicDataFeignClient;

    private final DeptFeignClient deptFeignClient;

    private final SupvFileMapper supvFileMapper;

    private final FileStorageUtil fileStorageUtil;

    @Override
    public boolean addProblem(SupvProblemParam supvProblemParam) {
        SupvProblem supvProblem = new SupvProblem();
        BeanUtil.copyProperties(supvProblemParam, supvProblem);
        supvProblem.setPlanRectificationTime(PubUtils.beginTimeToLocalDateTime(supvProblemParam.getPlanRectificationTime()));
        if(StrUtil.isNotBlank(supvProblemParam.getRectificationTime())) {
            supvProblem.setRectificationTime(PubUtils.beginTimeToLocalDateTime(supvProblemParam.getRectificationTime()));
        }
        supvProblem.setIsUploadHead(0);
        this.save(supvProblem);
        return true;
    }

    @Override
    public boolean updateProblem(SupvProblemParam supvProblemParam) {
        SupvProblem supvProblem = new SupvProblem();
        BeanUtil.copyProperties(supvProblemParam, supvProblem);
        supvProblem.setPlanRectificationTime(PubUtils.beginTimeToLocalDateTime(supvProblemParam.getPlanRectificationTime()));
        if(StrUtil.isNotBlank(supvProblemParam.getRectificationTime())) {
            supvProblem.setRectificationTime(PubUtils.beginTimeToLocalDateTime(supvProblemParam.getRectificationTime()));
        }
        this.updateById(supvProblem);
        return true;
    }

    @Override
    public boolean delProblem(List<String> problemIds) {
        LambdaQueryWrapper<SupvProblem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SupvProblem::getIsUploadHead, 1).eq(SupvProblem::getProblemId, problemIds);
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException("请选择未上送国网的删除");
        }
        this.removeByIds(problemIds);
        if (CollUtil.isNotEmpty(problemIds)) {
            LambdaQueryWrapper<SupvFile> fileLambdaQueryWrapper = new LambdaQueryWrapper<>();
            fileLambdaQueryWrapper.in(SupvFile::getBusiId, problemIds);
            List<SupvFile> supvFileList = supvFileMapper.selectList(fileLambdaQueryWrapper);
            supvFileList.forEach(item-> fileStorageUtil.downloadStream(item.getFileUrl()));
            supvFileMapper.delete(fileLambdaQueryWrapper);
        }
        return true;
    }

    @Override
    public Page<SupvProblem> pageProblem(SupvProblemParam supvProblemParam) {
        LambdaQueryWrapper<SupvProblem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SupvProblem::getPlanId, supvProblemParam.getPlanId());
        Page<SupvProblem> page = this.page(new Page<>(PageFactory.getPageNum(supvProblemParam), PageFactory.getPageSize(supvProblemParam)), lambdaQueryWrapper);

        List<PvTerminalTreeVO> deptList = deptFeignClient.allDeptList().getData();
        Map<String, PvTerminalTreeVO> mapCode = deptList.stream().collect(Collectors.toMap(PvTerminalTreeVO::getCode, Function.identity()));

        for (SupvProblem item : page.getRecords()) {
            if (mapCode.containsKey(item.getDutyOrgId())) {
                item.setDutyOrgName(mapCode.get(item.getDutyOrgId()).getName());
            }

            List<SupvFile> supvFileList = supvFileMapper.selectList(new LambdaQueryWrapper<SupvFile>().eq(SupvFile::getBusiId, item.getProblemId()).eq(SupvFile::getType, 1));
            if (CollUtil.isNotEmpty(supvFileList)) {
                for(SupvFile supvFile:supvFileList){
                    if(supvFile.getAttachmentType().equals("01")){
                        item.setAttachmentName(supvFile.getAttachmentName());
                    }else {
                        item.setAttachmentNameTwo(supvFile.getAttachmentName());
                    }

                }
            }

        }
        return page;
    }


}
