package com.njcn.gather.plan.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.DataStateEnum;
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.LogUtil;
import com.njcn.gather.device.pojo.enums.CommonEnum;
import com.njcn.gather.device.pojo.param.PqDevParam;
import com.njcn.gather.device.pojo.po.PqDev;
import com.njcn.gather.device.pojo.po.PqStandardDev;
import com.njcn.gather.device.pojo.vo.PqDevVO;
import com.njcn.gather.device.service.IPqDevService;
import com.njcn.gather.plan.pojo.param.AdPlanParam;
import com.njcn.gather.plan.pojo.po.AdPlan;
import com.njcn.gather.plan.pojo.vo.AdPlanVO;
import com.njcn.gather.plan.pojo.vo.PlanStatisticsVO;
import com.njcn.gather.plan.service.AsyncPlanHandler;
import com.njcn.gather.plan.service.IAdPlanService;
import com.njcn.gather.type.pojo.po.DevType;
import com.njcn.gather.type.service.IDevTypeService;
import com.njcn.gather.user.user.pojo.po.SysUser;
import com.njcn.gather.user.user.service.ISysUserService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.FileUtil;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.njcn.web.utils.RequestUtil.getUserId;

/**
 * @author caozehui
 * @date 2024-12-09
 */
@Slf4j
@Api(tags = "检测计划管理")
@RestController
@RequestMapping("/adPlan")
@RequiredArgsConstructor
public class AdPlanController extends BaseController {

    private final IAdPlanService adPlanService;
    private final IPqDevService pqDevService;
    private final IDevTypeService devTypeService;
    private final ISysUserService sysUserService;
    private final AsyncPlanHandler asyncPlanHandler;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("分页查询检测计划")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<List<AdPlanVO>> list(@RequestBody @Validated AdPlanParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        List<AdPlanVO> result = adPlanService.listAdPlan(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增检测计划")
    @ApiImplicitParam(name = "param", value = "检测计划", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated AdPlanParam param) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增数据为：{}", methodDescribe, param);
        boolean result = adPlanService.addAdPlan(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改检测计划")
    @ApiImplicitParam(name = "updateParam", value = "检测计划", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated AdPlanParam.UpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，修改数据为：{}", methodDescribe, updateParam);
        boolean result = adPlanService.updateAdPlan(updateParam);

        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除检测计划")
    @ApiImplicitParam(name = "ids", value = "计划id列表", required = true)
    public HttpResult<Boolean> delete(@RequestBody List<String> ids, @RequestParam("pattern") String pattern) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，删除ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids), pattern);
        boolean result = adPlanService.deleteAdPlan(ids, pattern);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/listByPattern")
    @ApiOperation("按照模式查询检测计划")
    @ApiImplicitParam(name = "pattern", value = "模式Id", required = true)
    public HttpResult<List<Map<String, Object>>> listByPattern(@RequestParam("pattern") String pattern) {
        String methodDescribe = getMethodDescribe("listByPattern");
        LogUtil.njcnDebug(log, "{}，模式Id为：{}", methodDescribe, pattern);
        List<Map<String, Object>> result = adPlanService.listByPattern(pattern);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.DOWNLOAD)
    @PostMapping("/export")
    @ApiOperation("导出检测计划")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public void exportPlan(@RequestBody @Validated AdPlanParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("exportPlan");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);

        adPlanService.exportPlan(queryParam);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/downloadTemplate")
    @ApiOperation("下载检测计划导出模板")
    public void downloadTemplate(@RequestBody AdPlanParam param) {
        adPlanService.downloadTemplate(param.getPattern());
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping(value = "/import")
    @ApiOperation("导入检测计划数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "检测计划数据文件", required = true),
            @ApiImplicitParam(name = "pattern", value = "模式Id", required = true)
    })
    public HttpResult<Boolean> importPlan(@RequestParam("file") MultipartFile file, @RequestParam("patternId") String patternId, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("importCNPlan");
        LogUtil.njcnDebug(log, "{}，上传文件为：{}", methodDescribe, file.getOriginalFilename());

        boolean fileType = FileUtil.judgeFileIsExcel(file.getOriginalFilename());
        if (!fileType) {
            throw new BusinessException(CommonResponseEnum.FILE_XLSX_ERROR);
        }
        boolean result = adPlanService.importPlan(file, patternId, response);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getBigTestItem")
    @ApiOperation("获取检测大项数据")
    @ApiImplicitParam(name = "checkParam", value = "检测计划id", required = true)
    public HttpResult<List<Map<String, String>>> getBigTestItem(@RequestBody AdPlanParam.CheckParam checkParam) {
        String methodDescribe = getMethodDescribe("getBigTestItem");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, checkParam);
        List<Map<String, String>> result = adPlanService.getBigTestItem(checkParam.getReCheckType(), checkParam.getPlanId(), checkParam.getDevIds(), checkParam.getPatternId(), checkParam.getScriptType());
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/analyse")
    @ApiOperation("检测数据分析")
    @ApiImplicitParam(name = "ids", value = "检测计划id", required = true)
    public void analyse(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("analyse");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        adPlanService.analyse(ids);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/statistics")
    @ApiOperation("检测计划统计")
    @ApiImplicitParam(name = "param", value = "检测计划统计参数", required = true)
    public HttpResult<PlanStatisticsVO> statistics(@RequestBody @Validated AdPlanParam.StatisticsParam param) {
        String methodDescribe = getMethodDescribe("statistics");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);
        PlanStatisticsVO result = adPlanService.statistics(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/listByPlanId")
    @ApiOperation("查询出所有已绑定的设备")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<List<PqDevVO>> listByPlanId(@RequestBody @Validated PqDevParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("listByPlanId");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);

        AdPlan plan = adPlanService.getById(param.getPlanIdList().get(0));
        if (CommonEnum.FATHER_ID.getValue().equals(plan.getFatherPlanId())) {
            List<AdPlan> planList = adPlanService.lambdaQuery().eq(AdPlan::getFatherPlanId, param.getPlanIdList().get(0)).list();
            List<String> planIdList = planList.stream().map(AdPlan::getId).collect(Collectors.toList());
            param.getPlanIdList().addAll(planIdList);
        }
        List<PqDevVO> pqDevVOList = pqDevService.listByPlanId(param);
        List<AdPlan> planList = adPlanService.listByIds(param.getPlanIdList());

        pqDevVOList.forEach(pqDevVO -> {
            DevType devType = devTypeService.getById(pqDevVO.getDevType());
            if (ObjectUtil.isNotNull(devType)) {
                pqDevVO.setDevType(devType.getName());
                pqDevVO.setDevVolt(devType.getDevVolt());
                pqDevVO.setDevCurr(devType.getDevCurr());
                pqDevVO.setDevChns(devType.getDevChns());
            }
            if (StrUtil.isNotBlank(pqDevVO.getPlanId())) {
                AdPlan plan1 = planList.stream().filter(p -> p.getId().equals(pqDevVO.getPlanId())).findFirst().get();
                if (ObjectUtil.isNotNull(plan1) && plan.getId().equals(plan1.getFatherPlanId())) {
                    pqDevVO.setBoundPlanName(plan1.getName());
                    pqDevVO.setAssign(DataStateEnum.ENABLE.getCode());
                } else {
                    pqDevVO.setAssign(DataStateEnum.DELETED.getCode());
                }
            } else {
                pqDevVO.setAssign(DataStateEnum.DELETED.getCode());
            }
        });
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqDevVOList, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/listDevByPlanId")
    @ApiOperation("根据计划id分页查询被检设备")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<Page<PqDevVO>> listDevByPlanId(@RequestBody @Validated PqDevParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("listDevByPlanId");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);

        AdPlan plan = adPlanService.getById(param.getPlanIdList().get(0));
        if (CommonEnum.FATHER_ID.getValue().equals(plan.getFatherPlanId())) {
            List<AdPlan> planList = adPlanService.lambdaQuery().eq(AdPlan::getFatherPlanId, param.getPlanIdList().get(0)).list();
            List<String> planIdList = planList.stream().map(AdPlan::getId).collect(Collectors.toList());
            param.getPlanIdList().addAll(planIdList);
        }
        param.setPlanId(plan.getId());
        Page<PqDevVO> pqDevVOPage = pqDevService.listPqDevs(param);

        List<AdPlan> planList = adPlanService.listByIds(param.getPlanIdList());
        for (PqDevVO pqDevVO : pqDevVOPage.getRecords()) {
            if (StrUtil.isNotBlank(pqDevVO.getPlanId())) {
                AdPlan plan1 = planList.stream().filter(p -> p.getId().equals(pqDevVO.getPlanId())).findFirst().get();
                if (ObjectUtil.isNotNull(plan1) && plan.getId().equals(plan1.getFatherPlanId())) {
                    pqDevVO.setBoundPlanName(plan1.getName());
                    pqDevVO.setAssign(DataStateEnum.ENABLE.getCode());
                } else {
                    pqDevVO.setAssign(DataStateEnum.DELETED.getCode());
                }
            } else {
                pqDevVO.setAssign(DataStateEnum.DELETED.getCode());
            }
        }

        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqDevVOPage, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @GetMapping("/updateSubPlanName")
    @ApiOperation("修改子计划名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "计划id", required = true),
            @ApiImplicitParam(name = "name", value = "计划名称", required = true)
    })
    public HttpResult<Boolean> updateSubPlanName(@RequestParam("planId") String planId, @RequestParam("name") String name) {
        String methodDescribe = getMethodDescribe("updateSubPlanName");
        LogUtil.njcnDebug(log, "{}，修改数据为：{}", methodDescribe, planId, name);
        boolean result = adPlanService.updateSubPlanName(planId, name);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getUnBoundStandardDev")
    @ApiOperation("根据父计划ID获取未被子计划绑定的标准设备")
    @ApiImplicitParam(name = "fatherPlanId", value = "父计划ID", required = true)
    public HttpResult<List<PqStandardDev>> getUnBoundStandardDev(@RequestParam("fatherPlanId") String fatherPlanId) {
        String methodDescribe = getMethodDescribe("getUnBoundStandardDev");
        LogUtil.njcnDebug(log, "{}，父计划ID为：{}", methodDescribe, fatherPlanId);
        List<PqStandardDev> result = adPlanService.getUnBoundStandardDev(fatherPlanId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getBoundStandardDev")
    @ApiOperation("根据计划ID获取已绑定的标准设备")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "计划ID", required = true),
            @ApiImplicitParam(name = "all", value = "是否获取所有")
    })
    public HttpResult<List<PqStandardDev>> getBoundStandardDev(@RequestParam("planId") String planId, @RequestParam(name = "all", required = false) Integer all) {
        String methodDescribe = getMethodDescribe("getUnBoundStandardDev");
        LogUtil.njcnDebug(log, "{}，计划ID为：{}，获取所有：{}", methodDescribe, planId, all);
        List<PqStandardDev> result = adPlanService.getBoundStandardDev(planId, all);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateBindStandardDev")
    @ApiOperation("子计划绑定/解绑标准设备")
    @ApiImplicitParam(name = "param", value = "绑定/解绑参数", required = true)
    public HttpResult<Boolean> updateBindStandardDev(@RequestBody @Validated PqDevParam.BindPlanParam param) {
        String methodDescribe = getMethodDescribe("updateBindStandardDev");
        LogUtil.njcnDebug(log, "{}，绑定/解绑参数为：{}", methodDescribe, param);

        boolean result = adPlanService.updateBindStandardDev(param.getPlanId(), param.getDevIds());
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateBindDev")
    @ApiOperation("子计划绑定/解绑被检设备")
    @ApiImplicitParam(name = "param", value = "绑定/解绑参数", required = true)
    public HttpResult<Boolean> updateBindDev(@RequestBody @Validated PqDevParam.BindPlanParam param) {
        String methodDescribe = getMethodDescribe("updateBindDev");
        LogUtil.njcnDebug(log, "{}，绑定/解绑参数为：{}", methodDescribe, param);

        boolean result = adPlanService.updateBindDev(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

//    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DELETE)
//    @PostMapping("/deleteSubPlan")
//    @ApiOperation("删除子计划")
//    @ApiImplicitParam(name = "ids", value = "子计划id列表", required = true)
//    public HttpResult<Boolean> deleteSubPlan(@RequestBody List<String> ids) {
//        String methodDescribe = getMethodDescribe("deleteSubPlan");
//        LogUtil.njcnDebug(log, "{}，删除ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
//        boolean result = adPlanService.deleteAdPlan(ids);
//        if (result) {
//            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
//        } else {
//            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
//        }
//    }

    @OperateInfo(operateType = OperateType.DOWNLOAD)
    @PostMapping("/exportSubPlan")
    @ApiOperation("导出子计划元信息")
    @ApiImplicitParam(name = "planId", value = "子计划id", required = true)
    public void exportSubPlan(@RequestParam("planId") String planId, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("exportSubPlan");
        LogUtil.njcnDebug(log, "{}，导出ID数据为：{}", methodDescribe, planId);
        adPlanService.exportSubPlanDataZip(planId, response);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping(value = "/importSubPlan")
    @ApiOperation("导入子计划元信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "检测计划数据文件", required = true),
            @ApiImplicitParam(name = "patternId", value = "模式Id", required = true)
    })
    public HttpResult<Boolean> importSubPlan(@RequestParam("file") MultipartFile file, @RequestParam("patternId") String patternId, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("importSubPlan");
        LogUtil.njcnDebug(log, "{}，上传文件为：{}", methodDescribe, file.getOriginalFilename());
        boolean fileType = FileUtil.judgeFileIsZip(file.getOriginalFilename());
        if (!fileType) {
            CommonResponseEnum fileTypeError = CommonResponseEnum.FILE_XLSX_ERROR;
            fileTypeError.setMessage("请上传zip文件");
            throw new BusinessException(fileTypeError);
        }
        adPlanService.importSubPlanDataZip(file, patternId, response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);

    }


    @OperateInfo(operateType = OperateType.DOWNLOAD)
    @PostMapping("/exportPlanCheckData")
    @ApiOperation("导出计划检测结果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planId", value = "计划id", required = true),
            @ApiImplicitParam(name = "devIds", value = "被检设备ids", required = true),
            @ApiImplicitParam(name = "report", value = "是否导出报告, 0 否，1 是", required = true)
    })
    public HttpResult<Boolean> exportPlanCheckData(@RequestParam("planId") String planId, @RequestParam("devIds") String devIds, @RequestParam("report") Integer report) {
        String methodDescribe = getMethodDescribe("exportPlanCheckData");
        LogUtil.njcnDebug(log, "{}，导出计划ID数据为：{} {} {}", methodDescribe, planId, devIds, report);
        // 获取检测计划绑定的被检设备数据
        List<PqDev> devList = pqDevService.list(new LambdaQueryWrapper<PqDev>().eq(PqDev::getPlanId, planId).in(PqDev::getId, StrUtil.split(devIds, StrUtil.COMMA)));
        if (CollUtil.isEmpty(devList)) {
            throw new BusinessException(CommonResponseEnum.FAIL, "选择的被检设备不存在");
        }
        asyncPlanHandler.exportPlanCheckDataZip(getUserId(), planId, devList, report);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);

    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/getMemberList")
    @ApiOperation("根据计划ID获取项目成员")
    @ApiImplicitParam(name = "planId", value = "计划ID", required = true)
    public HttpResult<List<SysUser>> getMemberList(@RequestParam("planId") String planId) {
        String methodDescribe = getMethodDescribe("getMemberList");
        LogUtil.njcnDebug(log, "{}，计划ID为：{}，获取项目成员", methodDescribe, planId);
        AdPlan plan = adPlanService.getById(planId);
        List<SysUser> result = new ArrayList<>();
        if (plan != null) {
            String members = plan.getMembers();
            if (StrUtil.isNotBlank(members)) {
                List<String> memberIds = StrUtil.split(members, StrUtil.COMMA);
                result = sysUserService.listByIds(memberIds);
                result.forEach(member -> member.setPassword(null));
            }
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping(value = "/importAndMergePlanCheckData")
    @ApiOperation("合并计划检测结果数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "检测计划检测结果数据文件", required = true),
            @ApiImplicitParam(name = "planId", value = "计划Id", required = true)
    })
    public HttpResult<Boolean> importAndMergePlanCheckData(@RequestParam("file") MultipartFile file, @RequestParam("planId") String planId) {
        String methodDescribe = getMethodDescribe("importAndMergePlanCheckData");
        LogUtil.njcnDebug(log, "{}，合并计划ID检测数据为：{}", methodDescribe, planId);
        boolean fileType = FileUtil.judgeFileIsZip(file.getOriginalFilename());
        if (!fileType) {
            CommonResponseEnum fileTypeError = CommonResponseEnum.FILE_XLSX_ERROR;
            fileTypeError.setMessage("请上传zip文件");
            throw new BusinessException(fileTypeError);
        }
        // 创建临时文件
        File tempFile = cn.hutool.core.io.FileUtil.createTempFile();
        // 将MultipartFile内容写入临时文件
        try {
            file.transferTo(tempFile);
        } catch (IOException e) {
            throw new BusinessException(CommonResponseEnum.FAIL, "文件保存失败");
        }
        // 获取文件路径
        String filePath = tempFile.getAbsolutePath();
        asyncPlanHandler.importAndMergePlanCheckData(filePath, getUserId(), planId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);

    }
}

