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.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.pms.annotation.TerminalOperationLogDesc;
import com.njcn.device.pms.pojo.excel.PowerGenerationUserExcel;
import com.njcn.device.pms.pojo.param.PowerDistributionareaParam;
import com.njcn.device.pms.pojo.param.PowerGenerationUserParam;
import com.njcn.device.pms.pojo.param.PowerUserParam;
import com.njcn.device.pms.pojo.po.PowerGenerationUser;
import com.njcn.device.pms.pojo.vo.PowerGenerationUserVO;
import com.njcn.device.pms.service.majornetwork.IPowerGenerationUserService;
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 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 javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@RestController
@RequestMapping("/pms/powerGenerationUser")
@Api(tags = "台账-发电用户")
@RequiredArgsConstructor
public class PmsPowerGenerationUserController extends BaseController {

    private final IPowerGenerationUserService iPowerGenerationUserService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.ADD)
    @PostMapping("/addPowerGenerationUser")
    @ApiOperation("新增发电用户")
    @ApiImplicitParam(name = "powerGenerationUserParam", value = "发电用户实体", required = true)
    @TerminalOperationLogDesc(value = "新增发电用户",module = 4)
    public HttpResult<Boolean> addPowerGenerationUser(@RequestBody @Validated PowerGenerationUserParam powerGenerationUserParam) {
        String methodDescribe = getMethodDescribe("addPowerGenerationUser");
        boolean result = iPowerGenerationUserService.addPowerGenerationUser(powerGenerationUserParam);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.UPDATE)
    @PostMapping("/updatePowerGenerationUser")
    @ApiOperation("修改发电用户")
    @ApiImplicitParam(name = "powerGenerationUserParam", value = "发电用户实体", required = true)
    @TerminalOperationLogDesc(value = "修改发电用户",module = 4)
    public HttpResult<Boolean> updatePowerGenerationUser(@RequestBody @Validated PowerGenerationUserParam powerGenerationUserParam) {
        String methodDescribe = getMethodDescribe("updatePowerGenerationUser");
        boolean result = iPowerGenerationUserService.updatePowerGenerationUser(powerGenerationUserParam);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON,operateType = OperateType.DELETE)
    @DeleteMapping("/delPowerGenerationUser")
    @ApiOperation("删除发电用户")
    @ApiImplicitParam(name = "powerGenerationUserIds", value = "发电用户ids", required = true)
    @TerminalOperationLogDesc(value = "删除发电用户",module = 4)
    public HttpResult<Boolean> delPowerGenerationUser(@RequestBody @NotEmpty(message = "发电用户id不可为空") List<String> powerGenerationUserIds) {
        String methodDescribe = getMethodDescribe("delPowerGenerationUser");
        boolean result = iPowerGenerationUserService.delPowerGenerationUser(powerGenerationUserIds);
        if(result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getPowerGenerationUserById")
    @ApiOperation("根据发电用户id获取发电用户")
    @ApiImplicitParam(name = "powerGenerationUserId", value = "发电用户id", required = true)
    public HttpResult<PowerGenerationUser> getPowerGenerationUserById(@RequestParam("powerGenerationUserId") String powerGenerationUserId) {
        String methodDescribe = getMethodDescribe("getPowerGenerationUserById");
        PowerGenerationUser result = iPowerGenerationUserService.getPowerGenerationUserById(powerGenerationUserId);
        if(Objects.nonNull(result)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);

    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPowerGenerationUserList")
    @ApiOperation("获取发电用户列表")
    public HttpResult<List<PowerGenerationUserVO>> getPowerGenerationUserList(@RequestBody PowerDistributionareaParam powerDistributionareaParam) {
        String methodDescribe = getMethodDescribe("getPowerGenerationUserList");
        List<PowerGenerationUserVO> result = iPowerGenerationUserService.getPowerGenerationUserList(powerDistributionareaParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getPowerGenerationUserPageList")
    @ApiOperation("分页获取发电用户列表")
    @ApiImplicitParam(name = "baseParam", value = "基本查询体", required = true)
    public HttpResult<Page<PowerGenerationUser>> getPowerGenerationUserPageList(@RequestBody BaseParam baseParam) {
        String methodDescribe = getMethodDescribe("getPowerGenerationUserPageList");
        Page<PowerGenerationUser> result = iPowerGenerationUserService.getPowerGenerationUserPageList(baseParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getGenerateElectricityUserList")
    @ApiOperation("获取用户列表")
    @ApiImplicitParam(name = "powerUserParam", value = "参数", required = true)
    public HttpResult<List<PowerGenerationUser>> getGenerateElectricityUserList(@RequestBody PowerUserParam powerUserParam) {
        String methodDescribe = getMethodDescribe("getGenerateElectricityUserList");
        List<PowerGenerationUser> result = iPowerGenerationUserService.getGenerateElectricityUserList(powerUserParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

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

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

