package com.njcn.process.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.minioss.bo.MinIoUploadResDTO;
import com.njcn.poi.util.PoiUtil;
import com.njcn.process.pojo.param.*;
import com.njcn.process.pojo.po.RGeneralSurveyPlanPO;
import com.njcn.process.pojo.po.RSurveyPlanConfigPO;
import com.njcn.process.pojo.vo.*;
import com.njcn.process.service.RGeneralSurveyPlanPOService;
import com.njcn.process.service.impl.RSurveyPlanConfigService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/11/11  14:50【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */

@Slf4j
@RestController
@RequestMapping("/rGeneralSurveyPlan")
@Api(tags = "普测计划管理")
@AllArgsConstructor
public class RGeneralSurveyPlanController extends BaseController {

    private final RGeneralSurveyPlanPOService rGeneralSurveyPlanPOService;


    private final RSurveyPlanConfigService rSurveyPlanConfigService;

    /**
     * @Description: 新增/修改普测计划
     * @Param: [rGeneralSurveyPlanAddParm]
     * @return: com.njcn.common.pojo.response.HttpResult<java.lang.Boolean>
     * @Author: clam
     * @Date: 2022/11/22
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addPlan")
    @ApiOperation("新增/修改普测计划")
    @ApiImplicitParam(name = "rGeneralSurveyPlanAddParm", value = "新增普测计划参数", required = true)
    public HttpResult<Boolean> addPlan(@RequestBody @Validated RGeneralSurveyPlanAddParm rGeneralSurveyPlanAddParm) {
        String methodDescribe = getMethodDescribe("addPlan");
        Boolean addFlag = rGeneralSurveyPlanPOService.addPlan(rGeneralSurveyPlanAddParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, addFlag, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addPlanCycle")
    @ApiOperation("设置普测周期")
    @ApiImplicitParam(name = "cycleNum", value = "普测周期", required = true)
    public HttpResult<RSurveyCycleVO> addPlanCycle(@RequestParam("cycleNum") Integer cycleNum) {
        String methodDescribe = getMethodDescribe("addPlanCycle");
        RSurveyCycleVO rSurveyCycleVO = rGeneralSurveyPlanPOService.addPlanCycle(cycleNum);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rSurveyCycleVO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/initDetpStataionTree")
    @ApiOperation("初始化部门")
    @ApiImplicitParam(name = "orgId", value = "部门号", required = true)
    public HttpResult<List<DeptSubstationVO>> initDetpStataionTree(@RequestParam("orgId") String orgId) {
        String methodDescribe = getMethodDescribe("initDetpStataionTree");
        List<DeptSubstationVO> list = rGeneralSurveyPlanPOService.initDetpStataionTree(orgId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/addPlanConfig")
    @ApiOperation("增加普测计划参数")
    @ApiImplicitParam(name = "rSurveyPlanConfigVOList", value = "", required = true)
    public HttpResult<Boolean> addPlanConfig(@Validated @RequestBody List<RSurveyPlanConfigPO> rSurveyPlanConfigVOList) {
        String methodDescribe = getMethodDescribe("addPlanConfig");
        double sum = rSurveyPlanConfigVOList.stream().mapToDouble(RSurveyPlanConfigPO::getProportion).sum();
        if (sum > 1) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.MATH_ERROR, false, methodDescribe);
        }
        boolean b = rSurveyPlanConfigService.saveOrUpdateBatchByMultiId(rSurveyPlanConfigVOList, 500);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryPlanConfig")
    @ApiOperation("查询普测计划参数")
    public HttpResult<List<RSurveyPlanConfigPO>> queryPlanConfig() {
        String methodDescribe = getMethodDescribe("queryPlanConfig");
        List<RSurveyPlanConfigPO> list = rSurveyPlanConfigService.getOrgIds();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/querySubStatation")
    @ApiOperation("电站勾选接口")
    @ApiImplicitParam(name = "statetionNum", value = "电站数量", required = true)
    public HttpResult<RGeneralSurveyPlanAddParm> querySubStatation(@RequestParam("statetionNum") Integer statetionNum) {
        String methodDescribe = getMethodDescribe("querySubStatation");
        RGeneralSurveyPlanAddParm rGeneralSurveyPlanAddParm = rGeneralSurveyPlanPOService.querySubStatation(statetionNum);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rGeneralSurveyPlanAddParm, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryPlan")
    @ApiOperation("查询普测计划-新建页面")
    @ApiImplicitParam(name = "rGeneralSurveyPlanQueryParm", value = "普测计划查询参数", required = true)
    public HttpResult<IPage<RGeneralSurveyPlanVO>> queryPlan(@Validated @RequestBody RGeneralSurveyPlanQueryParm rGeneralSurveyPlanQueryParm) {
        String methodDescribe = getMethodDescribe("queryPlan");
        IPage<RGeneralSurveyPlanVO> rGeneralSurveyPlanVOS = rGeneralSurveyPlanPOService.query(rGeneralSurveyPlanQueryParm, Stream.of("0", "1", "2", "3","4","5").collect(Collectors.toList()), "1");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rGeneralSurveyPlanVOS, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryPlanOne")
    @ApiOperation("查询普测计划详情")
    @ApiImplicitParam(name = "planId", value = "计划id", required = true)
    public HttpResult<RGeneralSurveyPlanVO> queryPlanOne(@RequestParam("planId")String planId) {
        String methodDescribe = getMethodDescribe("queryPlanOne");
        RGeneralSurveyPlanVO rGeneralSurveyPlanVO = rGeneralSurveyPlanPOService.queryPlanOne( planId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rGeneralSurveyPlanVO, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryPlanAudit")
    @ApiOperation("查询普测计划-审核页面")
    @ApiImplicitParam(name = "rGeneralSurveyPlanQueryParm", value = "普测计划查询参数", required = true)
    public HttpResult<IPage<RGeneralSurveyPlanVO>> queryPlanAudit(@Validated @RequestBody RGeneralSurveyPlanQueryParm rGeneralSurveyPlanQueryParm) {
        String methodDescribe = getMethodDescribe("queryPlanAudit");
        IPage<RGeneralSurveyPlanVO> rGeneralSurveyPlanVOS = rGeneralSurveyPlanPOService.query(rGeneralSurveyPlanQueryParm, Stream.of("1").collect(Collectors.toList()), "2");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rGeneralSurveyPlanVOS, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryPlanResult")
    @ApiOperation("查询普测计划-结果页面")
    @ApiImplicitParam(name = "rGeneralSurveyPlanQueryParm", value = "普测计划查询参数", required = true)
    public HttpResult<IPage<RGeneralSurveyPlanVO>> queryPlanResult(@Validated @RequestBody RGeneralSurveyPlanQueryParm rGeneralSurveyPlanQueryParm) {
        String methodDescribe = getMethodDescribe("queryPlanResult");
        IPage<RGeneralSurveyPlanVO> rGeneralSurveyPlanVOS = rGeneralSurveyPlanPOService.query(rGeneralSurveyPlanQueryParm, Stream.of("3", "4").collect(Collectors.toList()), "3");
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rGeneralSurveyPlanVOS, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/checkPlanAudit")
    @ApiOperation("普测计划-审核")
    @ApiImplicitParam(name = "rGeneralSurveyPlanChcekParm", value = "普测计划审核参数", required = true)
    public HttpResult<Boolean> checkPlanAudit(@Validated @RequestBody RGeneralSurveyPlanChcekParm rGeneralSurveyPlanChcekParm) {
        String methodDescribe = getMethodDescribe("checkPlanAudit");
        Boolean flag = rGeneralSurveyPlanPOService.checkPlanAudit(rGeneralSurveyPlanChcekParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

//    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
//    @PostMapping("/queryPlandetail")
//    @ApiOperation("根据planNO查询普测计划详情")
//    @ApiImplicitParam(name = "rGeneralSurveyPlandetailQueryParm", value = "普测计划详情查询参数", required = true)
//    public HttpResult<IPage<RGeneralSurveyPlanVO.RGeneralSurveyPlanDetailVO>> queryPlandetail(@Validated @RequestBody RGeneralSurveyPlandetailQueryParm rGeneralSurveyPlandetailQueryParm){
//        String methodDescribe = getMethodDescribe("queryPlandetail");
//
//        IPage<RGeneralSurveyPlanVO.RGeneralSurveyPlanDetailVO> rGeneralSurveyPlanDetailVOIPage  = rGeneralSurveyPlanDetailService.queryPlandetail (rGeneralSurveyPlandetailQueryParm);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, rGeneralSurveyPlanDetailVOIPage, methodDescribe);
//    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping("/surveyResultUpload")
    @ApiOperation("上传普测结果报告")
    public HttpResult<Boolean> surveyResultUpload(@Validated SurveyResultUploadParam surveyResultUploadParam) {
        String methodDescribe = getMethodDescribe("surveyResultUpload");
        boolean res = rGeneralSurveyPlanPOService.surveyResultUpload(surveyResultUploadParam);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/surveyResultDownload")
    @ApiOperation("下载普测结果报告")
    @ApiImplicitParam(name = "planNo", value = "计划号", required = true)
    public HttpResult<List<MinIoUploadResDTO>> surveyResultDownload(@RequestParam("planNo") String planNo) {
        String methodDescribe = getMethodDescribe("surveyResultDownload");
        List<MinIoUploadResDTO> downloadUrls = rGeneralSurveyPlanPOService.surveyResultDownload(planNo);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, downloadUrls, methodDescribe);

    }

    @ResponseBody
    @ApiOperation("导出普测计划信息")
    @PostMapping(value = "exportSurveyPlan")
    @ApiImplicitParam(name = "planIdList", value = "普测计划详情查询参数", required = true)
    public HttpResult<String> exportSurveyPlan(HttpServletResponse response, @RequestBody List<String> planIdList) {
        String methodDescribe = getMethodDescribe("exportSurveyPlan");
        String fileName = "普测计划信息.xls";
        List<SurveyPlanExcel> baseExcelList;
        try {
            baseExcelList = rGeneralSurveyPlanPOService.exportSurveyPlan(planIdList);
        } catch (Exception e) {
            e.printStackTrace();
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
        ExportParams exportExcel = new ExportParams("普测计划信息", "普测计划信息");
        Workbook workbook = ExcelExportUtil.exportExcel(exportExcel, SurveyPlanExcel.class, baseExcelList);
        PoiUtil.exportFileByWorkbook(workbook, fileName, response);
        return null;
    }

    @ResponseBody
    @ApiOperation("普测计划信息问题件新增查询")
    @PostMapping(value = "querySurveyPlanOnQuestion")
    @ApiImplicitParam(name = "questionQueryParm", value = "普测计划信息问题件查询参数", required = true)
    public HttpResult<List<RGeneralSurveyPlanDetailOnQuestionVO>> querySurveyPlanOnQuestion(@Validated @RequestBody SurveyPlanQuestionQueryParm questionQueryParm) {
        String methodDescribe = getMethodDescribe("querySurveyPlanOnQuestion");
        List<RGeneralSurveyPlanDetailOnQuestionVO> list = rGeneralSurveyPlanPOService.querySurveyPlanOnQuestion(questionQueryParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);

    }

    @ResponseBody
    @ApiOperation("普测计划名称查询")
    @PostMapping(value = "querySurveyPlanName")
    @ApiImplicitParam(name = "questionQueryParm", value = "普测计划名称查询参数", required = true)
    public HttpResult<List<RGeneralSurveyPlanPO>> querySurveyPlanName(@Validated @RequestBody SurveyPlanQuestionQueryParm questionQueryParm) {
        String methodDescribe = getMethodDescribe("querySurveyPlanName");
        List<RGeneralSurveyPlanPO> list = rGeneralSurveyPlanPOService.querySurveyPlanName(questionQueryParm);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);

    }

    @ResponseBody
    @ApiOperation("普测计划状态更改为待审核")
    @PostMapping(value = "planStatus")
    public HttpResult<Boolean> planStatus(@RequestBody List<String> planNo) {
        String methodDescribe = getMethodDescribe("planStatus");
        int count = rGeneralSurveyPlanPOService.count(new LambdaUpdateWrapper<RGeneralSurveyPlanPO>()
                .in(RGeneralSurveyPlanPO::getPlanNo, planNo)
                .ne(RGeneralSurveyPlanPO::getStatus, Arrays.asList(0, 2))
        );
        if (count > 0) {
            throw new BusinessException("请检查普测计划状态是否正确！");
        }
        Boolean update = rGeneralSurveyPlanPOService.update(new LambdaUpdateWrapper<RGeneralSurveyPlanPO>()
                .in(RGeneralSurveyPlanPO::getPlanNo, planNo)
                .set(RGeneralSurveyPlanPO::getStatus, 1)
        );
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, update, methodDescribe);

    }


    /**
     * 提交审核
     * @author cdf
     * @date 2024/3/29
     */

    @ApiOperation("提交审核")
    @PostMapping(value = "submitAuditUser")
    @ApiImplicitParam(name = "rGeneralSurveyPlanAuditUserParam", value = "实体参数", required = true)
    public HttpResult<Boolean> submitAuditUser(@RequestBody @Validated RGeneralSurveyPlanAuditUserParam rGeneralSurveyPlanAuditUserParam) {
        String methodDescribe = getMethodDescribe("submitAuditUser");
        rGeneralSurveyPlanPOService.submitAuditUser(rGeneralSurveyPlanAuditUserParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
    }



    /**
     * 删除新建驳回的计划
     * @author cdf
     * @date 2024/4/7
     */
    @ApiOperation("删除计划")
    @PostMapping(value = "deletePlan")
    @ApiImplicitParam(name = "planIds", value = "实体参数", required = true)
    public HttpResult<Boolean> deletePlan(@RequestBody List<String> planIds) {
        String methodDescribe = getMethodDescribe("deletePlan");
        rGeneralSurveyPlanPOService.deletePlan(planIds);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
    }
}
