package com.njcn.advance.controller.assess;

import cn.hutool.core.util.StrUtil;
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.AssessDicTransformerQuery;
import com.njcn.advance.pojo.po.assess.AssessDicTransformer;
import com.njcn.advance.service.assess.IAssessDicTransformerService;
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("/assessDicTransformer")
@Api(tags = "变压器参数字典管理")
@RequiredArgsConstructor
public class AssessDicTransformerController extends BaseController {

    private final IAssessDicTransformerService assessDicTransformerService;

    @ApiOperation("获取变压器参数字典列表")
    @PostMapping("/list")
    public HttpResult<List<AssessDicTransformer>> list(@RequestBody AssessDicTransformerQuery query) {
        LambdaQueryWrapper<AssessDicTransformer> wrapper = Wrappers.<AssessDicTransformer>lambdaQuery()
                .like(query.getTranstypeName() != null, AssessDicTransformer::getTranstypeName, query.getTranstypeName())
                .eq(query.getTranshighScale() != null, AssessDicTransformer::getTranshighScale, query.getTranshighScale())
                .eq(query.getTranslowScale() != null, AssessDicTransformer::getTranslowScale, query.getTranslowScale());
        List<AssessDicTransformer> list = assessDicTransformerService.list(wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("分页查询变压器参数字典")
    @PostMapping("/page")
    public HttpResult<Page<AssessDicTransformer>> page(@RequestBody AssessDicTransformerQuery query) {
        Page<AssessDicTransformer> pageParam = new Page<>(
                PageFactory.getPageNum(query),
                PageFactory.getPageSize(query)
        );
        LambdaQueryWrapper<AssessDicTransformer> wrapper = Wrappers.<AssessDicTransformer>lambdaQuery()
                .like(StrUtil.isNotBlank(query.getSearchValue()), AssessDicTransformer::getTranstypeName, query.getSearchValue())
                .orderByDesc(AssessDicTransformer::getCreateTime);
        Page<AssessDicTransformer> pageData = assessDicTransformerService.page(pageParam, wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), pageData,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据类型ID获取变压器参数详情")
    @ApiImplicitParam(name = "transtypeId", value = "变压器类型ID", required = true, type = "String")
    @GetMapping("/getInfo")
    public HttpResult<AssessDicTransformer> getInfo(@RequestParam("transtypeId") String transtypeId) {
        AssessDicTransformer param = assessDicTransformerService.getByTranstypeId(transtypeId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), param,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("新增变压器参数字典")
    @PostMapping("/add")
    public HttpResult<Boolean> add(@RequestBody @Validated AssessDicTransformerQuery param) {
        boolean result = assessDicTransformerService.add(param);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("修改变压器参数字典")
    @PostMapping("/edit")
    public HttpResult<Boolean> edit(@RequestBody @Validated AssessDicTransformerQuery.UpdateAssessDicTransformerQuery updateQuery) {
        assessDicTransformerService.update(updateQuery);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), true,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("删除变压器参数字典")
    @ApiImplicitParam(name = "transtypeId", value = "变压器类型ID", required = true, type = "String")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@RequestBody List<String> transtypeId) {
        boolean result = assessDicTransformerService.removeByIds(transtypeId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据电压等级查询变压器参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "transhighScale", value = "高压侧电压等级", type = "String"),
            @ApiImplicitParam(name = "translowScale", value = "低压侧电压等级", type = "String")
    })
    @GetMapping("/listByVoltage")
    public HttpResult<List<AssessDicTransformer>> listByVoltage(
            @RequestParam(required = false) String transhighScale,
            @RequestParam(required = false) String translowScale) {
        LambdaQueryWrapper<AssessDicTransformer> wrapper = Wrappers.<AssessDicTransformer>lambdaQuery()
                .eq(transhighScale != null, AssessDicTransformer::getTranshighScale, transhighScale)
                .eq(translowScale != null, AssessDicTransformer::getTranslowScale, translowScale);
        List<AssessDicTransformer> list = assessDicTransformerService.list(wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }
}