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


import com.alibaba.excel.EasyExcel;
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.param.StatisticsBizBaseParam;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.device.pms.annotation.TerminalOperationLogDesc;
import com.njcn.device.pms.pojo.excel.PowerDistributionAreaExcel;
import com.njcn.device.pms.pojo.param.PowerDistributionareaParam;
import com.njcn.device.pms.pojo.param.TaiZhangParam;
import com.njcn.device.pms.pojo.po.PowerDistributionarea;
import com.njcn.device.pms.pojo.vo.PVCapacity;
import com.njcn.device.pms.service.majornetwork.IPowerDistributionareaService;
import com.njcn.web.pojo.param.BaseParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  台区信息增删改查
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Slf4j
@RestController
@RequestMapping("/pms/powerDistributionarea")
@Api(tags = "台账-台区信息")
@RequiredArgsConstructor
@Validated
public class PmsPowerDistributionareaController extends BaseController {

    private final IPowerDistributionareaService iPowerDistributionareaService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("getPowerDistributionAreaList")
    @ApiOperation("分页查询台区所有信息")
    @ApiImplicitParam(name = "baseParam",value = "查询台区信息",required = true)
    public HttpResult<Page<PowerDistributionarea>> getList(@RequestBody TaiZhangParam baseParam){
        String methodDescribe = getMethodDescribe("getList");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, baseParam);
        Page<PowerDistributionarea> res = iPowerDistributionareaService.getList(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,res,methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("getPowerDistributionList")
    @ApiOperation("查询台区所有信息")
    public HttpResult<List<PowerDistributionarea>> getPowerDistributionList(@RequestBody PowerDistributionareaParam powerDistributionareaParam){
        String methodDescribe = getMethodDescribe("getPowerDistributionList");
        List<PowerDistributionarea> res = iPowerDistributionareaService.getPowerDistributionList(powerDistributionareaParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,res,methodDescribe);
    }


    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getPowerDistributionAreaById")
    @ApiOperation("根据ID查询台区数据")
    @ApiImplicitParam(name = "id",value = "id",required = true)
    public HttpResult<PowerDistributionarea> getDistrictById(@RequestParam("id") String id){
        String methodDescribe = getMethodDescribe("getDistrictById");
        PowerDistributionarea powerDistributionarea = iPowerDistributionareaService.getDistrictById(id);
        if (Objects.nonNull(powerDistributionarea)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, powerDistributionarea, methodDescribe);
        }
        else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
    }

    /**
     * 新增台区信息
     * @author hany
     * @date 2022/10/26
     */
    @PostMapping("/addPowerDistributionArea")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType=OperateType.ADD)
    @ApiOperation("新增台区信息")
    @ApiImplicitParam(name = "newParam", value = "模板实体", required = true)
    @TerminalOperationLogDesc(value = "新增台区信息",module = 3)
    public HttpResult<Object> add(@RequestBody @Validated PowerDistributionareaParam newParam){
        String methodDescribe = getMethodDescribe("add");
        boolean result = iPowerDistributionareaService.add(newParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 修改台区信息
     * @author hany
     * @date 2022/10/26
     */
    @PostMapping("/updatePowerDistributionArea")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON,operateType = OperateType.UPDATE)
    @ApiOperation("修改台区信息")
    @ApiImplicitParam(name = "UpdateParam", value = "更新实体", required = true)
    @TerminalOperationLogDesc(value = "修改台区信息",module = 3)
    public HttpResult<Object> update(@RequestBody @Validated PowerDistributionareaParam UpdateParam){
        String methodDescribe = getMethodDescribe("update");
        boolean result = iPowerDistributionareaService.update(UpdateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    /**
     * 删除台区信息
     * @param ids id
     * @author hany
     * @date 2022/10/26
     */
    @PostMapping("/deletePowerDistributionArea")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @ApiOperation("删除台区信息")
    @ApiImplicitParam(name = "ids", value = "ID索引", required = true)
    @TerminalOperationLogDesc(value = "删除台区信息",module = 3)
    public HttpResult<Object> delete(@RequestBody List<String> ids){
        String methodDescribe = getMethodDescribe("delete");
        boolean result = iPowerDistributionareaService.delete(ids);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPVCapacity")
    @ApiOperation("冀北分布式光伏获取分布式光伏容量")
    public HttpResult<PVCapacity> getPVCapacity(@RequestBody StatisticsBizBaseParam param) {
        String methodDescribe = getMethodDescribe("getPVCapacity");
        PVCapacity pvCapacity = iPowerDistributionareaService.getPVCapacity(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pvCapacity, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPVPermeability")
    @ApiOperation("冀北分布式光伏获取分布式光伏渗透率")
    public HttpResult<PVCapacity.PVCapacityCount> getPVPermeability(@RequestBody StatisticsBizBaseParam param) {
        String methodDescribe = getMethodDescribe("getPVPermeability");
        PVCapacity.PVCapacityCount pvCapacityCount = iPowerDistributionareaService.getPVPermeability(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pvCapacityCount, methodDescribe);
    }
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPVAbsorptiveCapacity")
    @ApiOperation("冀北分布式光伏获取消纳能力")
    public HttpResult<List<PVCapacity.Absorptive>> getPVAbsorptiveCapacity(@RequestBody StatisticsBizBaseParam param) {
        String methodDescribe = getMethodDescribe("getPVAbsorptiveCapacity");
        List<PVCapacity.Absorptive> absorptiveCapacity = iPowerDistributionareaService.getAbsorptiveCapacity(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, absorptiveCapacity, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/export")
    @ApiOperation(value ="导出台区台账数据模板", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public HttpResult<String>  export() {
        String methodDescribe = getMethodDescribe("export");
        iPowerDistributionareaService.export();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @PostMapping(value = "/importDistributionAreaExcel")
    @ApiOperation(value ="批量导入台区台账数据", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    public HttpResult<String> importDistributionAreaExcel(@ApiParam(value = "文件", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response) throws IOException {
        String methodDescribe = getMethodDescribe("importDistributionAreaExcel");
        List<PowerDistributionAreaExcel.ErrMsg> list = EasyExcel.read(file.getInputStream())
                .head(PowerDistributionAreaExcel.ErrMsg.class)
                .headRowNumber(2)
                .doReadAllSync();
        iPowerDistributionareaService.importDistributionAreaExcel(list, response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }


}

