package com.njcn.process.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
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.exception.BusinessException;
import com.njcn.process.mapper.SupvPlanHisMapper;
import com.njcn.process.pojo.param.SupvPlanHisParam;
import com.njcn.process.pojo.po.SupvPlan;
import com.njcn.process.pojo.po.SupvPlanHis;
import com.njcn.process.pojo.po.SupvProblem;
import com.njcn.process.service.ISupvPlanHisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.process.service.ISupvPlanService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 监督工作计划变更历史数据表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-09-01
 */
@Service
@RequiredArgsConstructor
public class SupvPlanHisServiceImpl extends ServiceImpl<SupvPlanHisMapper, SupvPlanHis> implements ISupvPlanHisService {

    private final DeptFeignClient deptFeignClient;
    @Override
    public boolean addPlanHis(SupvPlanHisParam.insert supvPlanHis) {

        return true;
    }

    @Override
    public boolean updatePlanHis(SupvPlanHisParam.update supvPlanHis) {
        SupvPlanHis planHis = BeanUtil.copyProperties(supvPlanHis, SupvPlanHis.class);
        SupvPlanHis byId = this.getById(supvPlanHis.getId());
        if(1==byId.getIsUploadHead()){
            planHis.setIsUploadHead(3);
        }
        return this.updateById(planHis);
    }

    @Override
    public boolean delPlanHis(List<String> planIds) {
        LambdaQueryWrapper<SupvPlanHis> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(SupvPlanHis::getIsUploadHead, 0).in(SupvPlanHis::getId, planIds);
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException("请选择未上送国网的删除");
        }
        return this.removeByIds(planIds);
    }

    @Override
    public Page<SupvPlanHis> pagePlanHis(SupvPlanHisParam supvPlanParam) {
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByCode(supvPlanParam.getOrgId()).getData();
        Page<SupvPlanHis> page = this.page(new Page<>(supvPlanParam.getPageNum(), supvPlanParam.getPageSize()),
                new LambdaQueryWrapper<SupvPlanHis>().in(SupvPlanHis::getSupvOrgId, deptIds)
                        .between(SupvPlanHis::getAdjustTime,
                                DateUtil.beginOfDay(DateUtil.parse(supvPlanParam.getSearchBeginTime())),
                                DateUtil.endOfDay(DateUtil.parse(supvPlanParam.getSearchEndTime())))

        );
        return page;
    }
}
