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


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.pms.pojo.param.PmsParkParam;
import com.njcn.device.pms.pojo.po.PmsMidLedger;
import com.njcn.device.pms.pojo.po.PmsParkPO;
import com.njcn.device.pms.pojo.po.PmsParkStationAss;
import com.njcn.device.pms.service.ledgerManger.IPmsParkService;
import com.njcn.device.pms.service.ledgerManger.IPmsParkStationAssService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.njcn.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 园区表 前端控制器
 * </p>
 *
 * @author cdf
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/pmsPark")
@RequiredArgsConstructor
public class PmsParkController extends BaseController {

    private final IPmsParkService iPmsParkService;

    private final IPmsParkStationAssService iPmsParkStationAssService;


    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.ADD)
    @PostMapping("/addPark")
    @ApiOperation("新增园区")
    public HttpResult<Object> addPark(@RequestBody PmsParkParam param) {
        String methodDescribe = getMethodDescribe("addPark");
        Boolean result = iPmsParkService.addPark(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    @PostMapping("/updatePark")
    @ApiOperation("修改园区台账")
    public HttpResult<Object> updatePark(@RequestBody PmsParkParam.UpdatePmsParkParam param) {
        String methodDescribe = getMethodDescribe("updatePark");
        Boolean result = iPmsParkService.updatePark(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.DELETE)
    @PostMapping("/delPark")
    @ApiOperation("修改园区台账")
    public HttpResult<Object> delPark(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delPark");
        Boolean result = iPmsParkService.removeByIds(ids);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/pageList")
    @ApiOperation("分页查询园区台账")
    public HttpResult<Page<PmsParkPO>> pageList(@RequestBody PmsParkParam.PmsParkQuery param) {
        String methodDescribe = getMethodDescribe("pageList");
        Page<PmsParkPO> result = iPmsParkService.pageList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/uploadParkGw")
    @ApiOperation("上送园区到国网")
    public HttpResult<Boolean> uploadParkGw(@RequestBody List<String> objIds) {
        String methodDescribe = getMethodDescribe("uploadParkGw");
        Boolean result = iPmsParkService.uploadParkGw(objIds);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/pageStationList")
    @ApiOperation("分页查询园区下面的电站")
    public HttpResult<Page<PmsMidLedger>> pageStationList(@RequestBody PmsParkParam.PmsParkQuery param) {
        String methodDescribe = getMethodDescribe("pageStationList");
        Page<PmsMidLedger> result = iPmsParkService.pageStationList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/stationList")
    @ApiOperation("根据运行单位获取下面的电站")
    public HttpResult<List<PmsMidLedger>> stationList(@RequestBody PmsParkParam.PmsParkQuery param) {
        String methodDescribe = getMethodDescribe("stationList");
        List<PmsMidLedger> result = iPmsParkService.stationList(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.ADD)
    @PostMapping("/addParkStationAss")
    @ApiOperation("新增园区电站")
    public HttpResult<Object> addParkStationAss(@RequestBody PmsParkStationAss param) {
        String methodDescribe = getMethodDescribe("addParkStationAss");

        LambdaQueryWrapper<PmsParkStationAss> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsParkStationAss::getParkId,param.getParkId()).eq(PmsParkStationAss::getMidStationId,param.getMidStationId());
        long count = iPmsParkStationAssService.count(lambdaQueryWrapper);
        if(count>0){
            throw new BusinessException(CommonResponseEnum.FAIL,"园区已经存在该电站");
        }
        PmsParkStationAss pmsParkStationAss = new PmsParkStationAss();
        BeanUtil.copyProperties(param,pmsParkStationAss);
        Boolean result = iPmsParkStationAssService.save(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.DELETE)
    @PostMapping("/delParkStationAss")
    @ApiOperation("删除园区电站")
    public HttpResult<Object> delParkStationAss(@RequestBody @Validated PmsParkStationAss param) {
        String methodDescribe = getMethodDescribe("delParkStationAss");
        LambdaQueryWrapper<PmsParkStationAss> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsParkStationAss::getParkId,param.getParkId()).eq(PmsParkStationAss::getMidStationId,param.getMidStationId());
        Boolean result = iPmsParkStationAssService.remove(lambdaQueryWrapper);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

}

