package com.njcn.advance.controller.assess;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.advance.pojo.param.assess.AssessCapacitorParamQuery;
import com.njcn.advance.pojo.po.assess.AssessCapacitorParam;
import com.njcn.advance.service.assess.IAssessCapacitorParamService;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.web.factory.PageFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.njcn.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 评估电容器参数 前端控制器
 * </p>
 *
 * @author cdf
 * @since 2025-06-06
 */
@RestController
@RequestMapping("/assessCapacitorParam")
@Api(tags = "评估电容器参数管理")
@RequiredArgsConstructor
public class AssessCapacitorParamController extends BaseController {

    private final IAssessCapacitorParamService assessCapacitorParamService;

    @ApiOperation("获取评估电容器参数列表")
    @PostMapping("/list")
    public HttpResult<List<AssessCapacitorParam>> list(@RequestBody AssessCapacitorParamQuery query) {
        LambdaQueryWrapper<AssessCapacitorParam> wrapper = Wrappers.<AssessCapacitorParam>lambdaQuery()
                .like(query.getAssessId() != null, AssessCapacitorParam::getAssessId, query.getAssessId())
                .like(query.getCapacitorName() != null, AssessCapacitorParam::getCapacitorName, query.getCapacitorName())
                .eq(query.getCapacitorScale() != null, AssessCapacitorParam::getCapacitorScale, query.getCapacitorScale());
        List<AssessCapacitorParam> list = assessCapacitorParamService.list(wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("分页查询评估电容器参数")
    @PostMapping("/page")
    public HttpResult<Page<AssessCapacitorParam>> page(@RequestBody AssessCapacitorParamQuery query) {
        Page<AssessCapacitorParam> pageParam = new Page<>(
                PageFactory.getPageNum(query),
                PageFactory.getPageSize(query)
        );
        LambdaQueryWrapper<AssessCapacitorParam> wrapper = Wrappers.<AssessCapacitorParam>lambdaQuery()
                .like(query.getAssessId() != null, AssessCapacitorParam::getAssessId, query.getAssessId())
                .like(query.getCapacitorName() != null, AssessCapacitorParam::getCapacitorName, query.getCapacitorName())
                .eq(query.getCapacitorScale() != null, AssessCapacitorParam::getCapacitorScale, query.getCapacitorScale());
        Page<AssessCapacitorParam> pageData = assessCapacitorParamService.page(pageParam, wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), pageData,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据ID获取评估电容器参数详情")
    @ApiImplicitParam(name = "capacitorId", value = "电容器组ID", required = true, type = "String")
    @GetMapping("/getInfo")
    public HttpResult<AssessCapacitorParam> getInfo(@RequestParam("capacitorId") String capacitorId) {
        AssessCapacitorParam param = assessCapacitorParamService.getByCapacitorId(capacitorId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), param,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("新增评估电容器参数")
    @PostMapping("/add")
    public HttpResult<Boolean> add(@RequestBody @Validated AssessCapacitorParamQuery param) {
        boolean result = assessCapacitorParamService.add(param);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("修改评估电容器参数")
    @PostMapping("/edit")
    public HttpResult<Boolean> edit(@RequestBody @Validated AssessCapacitorParamQuery.UpdateAssessCapacitorParamQuery updateQuery) {
        assessCapacitorParamService.update(updateQuery);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), true,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("删除评估电容器参数")
    @ApiImplicitParam(name = "capacitorId", value = "电容器组ID", required = true, type = "String")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@RequestBody List<String> capacitorId) {
        boolean result = assessCapacitorParamService.removeByIds(capacitorId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据评估用户ID查询电容器参数")
    @ApiImplicitParam(name = "assessId", value = "评估用户ID", required = true, type = "String")
    @GetMapping("/listByAssessId")
    public HttpResult<List<AssessCapacitorParam>> listByAssessId(@RequestParam("assessId") String assessId) {
        List<AssessCapacitorParam> list = assessCapacitorParamService.listByAssessId(assessId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据电压等级查询电容器参数")
    @ApiImplicitParam(name = "capacitorScale", value = "电压等级（与PMS3.0编码一致）", type = "String")
    @GetMapping("/listByScale")
    public HttpResult<List<AssessCapacitorParam>> listByScale(@RequestParam(required = false) String capacitorScale) {
        LambdaQueryWrapper<AssessCapacitorParam> wrapper = Wrappers.<AssessCapacitorParam>lambdaQuery()
                .eq(capacitorScale != null, AssessCapacitorParam::getCapacitorScale, capacitorScale);
        List<AssessCapacitorParam> list = assessCapacitorParamService.list(wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }
}