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.AssessDicHarmonicCharacterQuery;
import com.njcn.advance.pojo.po.assess.AssessDicHarmonicCharacter;
import com.njcn.advance.service.assess.IAssessDicHarmonicCharacterService;
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("/assessDicHarmonicCharacter")
@Api(tags = "谐波源特性字典管理")
@RequiredArgsConstructor
public class AssessDicHarmonicCharacterController extends BaseController {

    private final IAssessDicHarmonicCharacterService assessDicHarmonicCharacterService;

    @ApiOperation("获取谐波源特性字典列表")
    @PostMapping("/list")
    public HttpResult<List<AssessDicHarmonicCharacter>> list(@RequestBody AssessDicHarmonicCharacterQuery query) {
        LambdaQueryWrapper<AssessDicHarmonicCharacter> wrapper = Wrappers.<AssessDicHarmonicCharacter>lambdaQuery()
                .like(query.getHarmName() != null, AssessDicHarmonicCharacter::getHarmName, query.getHarmName());
        List<AssessDicHarmonicCharacter> list = assessDicHarmonicCharacterService.list(wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("分页查询谐波源特性字典")
    @PostMapping("/page")
    public HttpResult<Page<AssessDicHarmonicCharacter>> page(@RequestBody AssessDicHarmonicCharacterQuery query) {
        Page<AssessDicHarmonicCharacter> pageParam = new Page<>(
                PageFactory.getPageNum(query),
                PageFactory.getPageSize(query)
        );
        LambdaQueryWrapper<AssessDicHarmonicCharacter> wrapper = Wrappers.<AssessDicHarmonicCharacter>lambdaQuery()
                .like(StrUtil.isNotBlank(query.getSearchValue()), AssessDicHarmonicCharacter::getHarmName, query.getSearchValue());
        Page<AssessDicHarmonicCharacter> pageData = assessDicHarmonicCharacterService.page(pageParam, wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), pageData,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据ID获取谐波源特性字典详情")
    @ApiImplicitParam(name = "harmId", value = "谐波源特性字典ID", required = true, type = "String")
    @GetMapping("/getInfo")
    public HttpResult<AssessDicHarmonicCharacter> getInfo(@RequestParam("harmId") String harmId) {
        AssessDicHarmonicCharacter param = assessDicHarmonicCharacterService.getByHarmId(harmId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), param,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("新增谐波源特性字典")
    @PostMapping("/add")
    public HttpResult<Boolean> add(@RequestBody @Validated AssessDicHarmonicCharacterQuery param) {
        boolean result = assessDicHarmonicCharacterService.add(param);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("修改谐波源特性字典")
    @PostMapping("/edit")
    public HttpResult<Boolean> edit(@RequestBody @Validated AssessDicHarmonicCharacterQuery.UpdateAssessDicHarmonicCharacterQuery param) {
        assessDicHarmonicCharacterService.update(param);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), true,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("删除谐波源特性字典")
    @ApiImplicitParam(name = "harmId", value = "谐波源特性字典ID", required = true, type = "String")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@RequestBody List<String> harmId) {
        boolean result = assessDicHarmonicCharacterService.removeByIds(harmId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据谐波源特性名称查询")
    @ApiImplicitParam(name = "harmName", value = "谐波源特性名称", type = "String")
    @GetMapping("/listByName")
    public HttpResult<List<AssessDicHarmonicCharacter>> listByName(@RequestParam(required = false) String harmName) {
        LambdaQueryWrapper<AssessDicHarmonicCharacter> wrapper = Wrappers.<AssessDicHarmonicCharacter>lambdaQuery()
                .like(harmName != null, AssessDicHarmonicCharacter::getHarmName, harmName);
        List<AssessDicHarmonicCharacter> list = assessDicHarmonicCharacterService.list(wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }
}