package com.njcn.gather.device.controller;

import cn.hutool.core.util.StrUtil;
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.param.PqStandardDevParam;
import com.njcn.gather.device.pojo.po.PqStandardDev;
import com.njcn.gather.device.pojo.vo.PqStandardDevVO;
import com.njcn.gather.device.service.IPqStandardDevService;
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.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.util.List;

/**
 * @author caozehui
 * @date 2025-07-02
 */
@Slf4j
@Api(tags = "标准设备管理")
@RestController
@RequestMapping("/pqStandardDev")
@RequiredArgsConstructor
public class PqStandardDevController extends BaseController {
    private final IPqStandardDevService pqStandardDevService;

    @OperateInfo
    @PostMapping("/list")
    @ApiOperation("分页查询标准设备")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<PqStandardDev>> list(@RequestBody PqStandardDevParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<PqStandardDev> result = pqStandardDevService.listPqStandardDevs(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo
    @GetMapping("/getById")
    @ApiOperation("根据id查询标准设备")
    @ApiImplicitParam(name = "id", value = "标准设备id", required = true)
    public HttpResult<PqStandardDevVO> getById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getById");
        LogUtil.njcnDebug(log, "{}，查询ID为：{}", methodDescribe, id);
        PqStandardDevVO result = pqStandardDevService.getPqStandardDevById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增标准设备")
    @ApiImplicitParam(name = "param", value = "标准设备", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated PqStandardDevParam param) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增数据为：{}", methodDescribe, param);
        boolean result = pqStandardDevService.addPqStandardDev(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改标准设备")
    @ApiImplicitParam(name = "param", value = "标准设备", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated PqStandardDevParam.UpdateParam param) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，修改数据为：{}", methodDescribe, param);
        boolean result = pqStandardDevService.updatePqStandardDev(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除标准设备")
    @ApiImplicitParam(name = "ids", value = "标准设备id", required = true)
    public HttpResult<Boolean> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，删除ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = pqStandardDevService.deletePqStandardDevs(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/export")
    @ApiOperation("批量导出标准设备")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public void export(@RequestBody PqStandardDevParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("export");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        pqStandardDevService.export(queryParam);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.DOWNLOAD)
    @PostMapping("/downloadTemplate")
    @ApiOperation("下载标准设备导入文件模板")
    public void downloadTemplate() {
        pqStandardDevService.downloadTemplate();
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPLOAD)
    @PostMapping("/import")
    @ApiOperation("导入标准设备")
    @ApiImplicitParam(name = "file", value = "导入文件", required = true)
    public HttpResult<Object> importData(@RequestParam("file") MultipartFile file, HttpServletResponse response) {
        String methodDescribe = getMethodDescribe("importData");
        LogUtil.njcnDebug(log, "{}，导入文件为：{}", methodDescribe, file.getOriginalFilename());
        boolean fileType = FileUtil.judgeFileIsExcel(file.getOriginalFilename());
        if (!fileType) {
            throw new BusinessException(CommonResponseEnum.FILE_XLSX_ERROR);
        }
        pqStandardDevService.importData(file, response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }

    @OperateInfo
    @GetMapping("/getAll")
    @ApiOperation("获取所有标准设备")
    public HttpResult<List<PqStandardDev>> getAll() {
        String methodDescribe = getMethodDescribe("getAll");
        LogUtil.njcnDebug(log, "{}，查询所有数据", methodDescribe);
        List<PqStandardDev> result = pqStandardDevService.lambdaQuery().eq(PqStandardDev::getState, DataStateEnum.ENABLE.getCode()).list();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

//    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
//    @PostMapping("/listByPlanId")
//    @ApiOperation("查询出指定计划已关联的标准设备")
//    @ApiImplicitParam(name = "planId", value = "计划id", required = true)
//    public HttpResult<List<PqStandardDev>> listByPlanId(@RequestParam("planId") String planId) {
//        String methodDescribe = getMethodDescribe("listByPlanId");
//        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, planId);
//        List<PqStandardDev> pqDevVOList = pqStandardDevService.listByPlanId(planId);
//
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, pqDevVOList, methodDescribe);
//    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/canBindingList")
    @ApiOperation("查询可绑定的标准设备")
    public HttpResult<List<PqStandardDev>> canBindingList() {
        String methodDescribe = getMethodDescribe("canBindingList");
        LogUtil.njcnDebug(log, "{}，查询可绑定的标准设备", methodDescribe);
        List<PqStandardDev> result = pqStandardDevService.canBindingList();

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

