package com.njcn.device.pms.controller.ledgerManger;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.pms.mapper.majornetwork.StatationStatMapper;
import com.njcn.device.pms.pojo.po.*;
import com.njcn.device.pms.service.majornetwork.*;
import com.njcn.user.pojo.po.Dept;
import com.njcn.user.pojo.vo.DeptTreeVO;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.stream.Collectors;

/**
 * pqs
 * 主要用于单位更新时候同步台账表里的相关单位信息
 * @author cdf
 * @date 2023/5/17
 */
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("pmsLedger")
public class PmsLedgerDeptController extends BaseController {

    private final IMonitorService iMonitorService;

    private final IPowerDistributionareaService iPowerDistributionareaService;

    private final IPowerGenerationUserService iPowerGenerationUserService;

    private final IPowerClientService iPowerClientService;

    private final IStatationStatService iStatationStatService;

    private final ITerminalService iTerminalService;


    @PostMapping("update")
    @ApiIgnore
    public HttpResult<Object> updateLedger(@RequestBody Dept dept){
        String methodDescribe = getMethodDescribe("updateLedger");

        //主网监测点
        LambdaQueryWrapper<Monitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Monitor::getStatus, DataStateEnum.ENABLE.getCode())
                .and(item->item.eq(Monitor::getOrgId,dept.getCode()).or().eq(Monitor::getOperationId,dept.getCode()));
        List<Monitor> monitorList = iMonitorService.list(lambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(monitorList)){
           List<Monitor> monitorListP =  monitorList.stream().map(it->{
                Monitor monitor = new Monitor();
                monitor.setOrgName(dept.getName());
                monitor.setOperationName(dept.getName());
                monitor.setId(it.getId());
                return monitor;
            }).collect(Collectors.toList());
            iMonitorService.updateBatchById(monitorListP);
        }

        //台区
        LambdaQueryWrapper<PowerDistributionarea> powerDistributionareaLambdaQueryWrapper = new LambdaQueryWrapper<>();
        powerDistributionareaLambdaQueryWrapper.eq(PowerDistributionarea::getStatus, DataStateEnum.ENABLE.getCode())
                .and(item->item.eq(PowerDistributionarea::getOrgId,dept.getCode()).or().eq(PowerDistributionarea::getOperationId,dept.getCode()));
        List<PowerDistributionarea> powerDistributionareaList = iPowerDistributionareaService.list(powerDistributionareaLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(powerDistributionareaList)){
            List<PowerDistributionarea> powerDistributionareaListP =  powerDistributionareaList.stream().map(it->{
                PowerDistributionarea powerDistributionarea = new PowerDistributionarea();
                powerDistributionarea.setId(it.getId());
                powerDistributionarea.setOrgName(dept.getName());
                powerDistributionarea.setOperationName(dept.getName());
                return powerDistributionarea;
            }).collect(Collectors.toList());
            iPowerDistributionareaService.updateBatchById(powerDistributionareaListP);
        }

        //发电用户
        LambdaQueryWrapper<PowerClient> powerClientLambdaQueryWrapper = new LambdaQueryWrapper<>();
        powerClientLambdaQueryWrapper.eq(PowerClient::getStatus, DataStateEnum.ENABLE.getCode())
                .and(item->item.eq(PowerClient::getOrgId,dept.getCode()).or().eq(PowerClient::getOperationId,dept.getCode()));
        List<PowerClient> powerClientList = iPowerClientService.list(powerClientLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(powerClientList)){
            List<PowerClient> powerClientListP = powerClientList.stream().map(it->{
                PowerClient powerClient = new PowerClient();
                powerClient.setId(it.getId());
                powerClient.setOrgName(dept.getName());
                powerClient.setOperationName(dept.getName());
                return powerClient;
            }).collect(Collectors.toList());
            iPowerClientService.updateBatchById(powerClientListP);
        }

        //用电用户
        LambdaQueryWrapper<PowerGenerationUser> powerGenerationUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        powerGenerationUserLambdaQueryWrapper.eq(PowerGenerationUser::getStatus, DataStateEnum.ENABLE.getCode())
                .and(item->item.eq(PowerGenerationUser::getOrgId,dept.getCode()).or().eq(PowerGenerationUser::getOperationId,dept.getCode()));
        List<PowerGenerationUser> powerGenerationUserList = iPowerGenerationUserService.list(powerGenerationUserLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(powerGenerationUserList)){
            List<PowerGenerationUser> powerGenerationUserListP = powerGenerationUserList.stream().map(it->{
                PowerGenerationUser powerGenerationUser = new PowerGenerationUser();
                powerGenerationUser.setId(it.getId());
                powerGenerationUser.setOrgName(dept.getName());
                powerGenerationUser.setOperationName(dept.getName());
                return powerGenerationUser;
            }).collect(Collectors.toList());
            iPowerGenerationUserService.updateBatchById(powerGenerationUserListP);
        }
        //电站
        LambdaQueryWrapper<StatationStat> statationStatLambdaQueryWrapper = new LambdaQueryWrapper<>();
        statationStatLambdaQueryWrapper.eq(StatationStat::getStatus, DataStateEnum.ENABLE.getCode())
                .eq(StatationStat::getOrgId,dept.getCode());
        List<StatationStat> statationStatList = iStatationStatService.list(statationStatLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(statationStatList)){
            List<StatationStat> statationStatListP = statationStatList.stream().map(it->{
                StatationStat statationStat = new StatationStat();
                statationStat.setPowerId(it.getPowerId());
                statationStat.setOrgName(dept.getName());
                return statationStat;
            }).collect(Collectors.toList());
            iStatationStatService.updateBatchById(statationStatListP);
        }

        //装置
        LambdaQueryWrapper<PmsTerminal> terminalLambdaQueryWrapper = new LambdaQueryWrapper<>();
        terminalLambdaQueryWrapper.eq(PmsTerminal::getStatus, DataStateEnum.ENABLE.getCode())
                .and(item->item.eq(PmsTerminal::getOrgId,dept.getCode()).or().eq(PmsTerminal::getOperationId,dept.getCode()));
        List<PmsTerminal> pmsTerminalList = iTerminalService.list(terminalLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(pmsTerminalList)){
            List<PmsTerminal> pmsTerminalListP = pmsTerminalList.stream().map(it->{
                PmsTerminal pmsTerminal = new PmsTerminal();
                pmsTerminal.setId(it.getId());
                pmsTerminal.setOrgName(dept.getName());
                pmsTerminal.setOperationName(dept.getName());
                return pmsTerminal;
            }).collect(Collectors.toList());
            iTerminalService.updateBatchById(pmsTerminalListP);
        }

        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);


    }


    /**
     * 获取部门树（存在监测点的）
     * @author cdf
     * @date 2024/2/26
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/existMonitorDeptTree")
    @ApiOperation("获取部门树（存在监测点的）")
    public HttpResult<List<Dept>> existMonitorDeptTree() {
        String methodDescribe = getMethodDescribe("existMonitorDeptTree");
        List<Dept> result = iMonitorService.existMonitorDeptTree();
        //删除返回失败，查不到数据返回空数组，兼容治理项目没有部门直接报错的bug
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);

    }


}

