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.AssessDicCableTypeQuery;
import com.njcn.advance.pojo.po.assess.AssessDicCableType;
import com.njcn.advance.service.assess.IAssessDicCableTypeService;
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("/assessDicCableType")
@Api(tags = "线缆类型字典管理")
@RequiredArgsConstructor
public class AssessDicCableTypeController extends BaseController {

    private final IAssessDicCableTypeService assessDicCableTypeService;

    @ApiOperation("获取线缆类型字典列表")
    @PostMapping("/list")
    public HttpResult<List<AssessDicCableType>> list(@RequestBody AssessDicCableTypeQuery query) {
        LambdaQueryWrapper<AssessDicCableType> wrapper = Wrappers.<AssessDicCableType>lambdaQuery()
                .like(query.getLinetypeName() != null, AssessDicCableType::getLinetypeName, query.getLinetypeName())
                .eq(query.getLinetypeType() != null, AssessDicCableType::getLinetypeType, query.getLinetypeType());
        List<AssessDicCableType> list = assessDicCableTypeService.list(wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("分页查询线缆类型字典")
    @PostMapping("/page")
    public HttpResult<Page<AssessDicCableType>> page(@RequestBody AssessDicCableTypeQuery query) {
        Page<AssessDicCableType> pageParam = new Page<>(
                PageFactory.getPageNum(query),
                PageFactory.getPageSize(query)
        );
        LambdaQueryWrapper<AssessDicCableType> wrapper = Wrappers.<AssessDicCableType>lambdaQuery()
                .like(StrUtil.isNotBlank(query.getSearchValue()), AssessDicCableType::getLinetypeName, query.getSearchValue())
                .eq(StrUtil.isNotBlank(query.getLinetypeType()), AssessDicCableType::getLinetypeType, query.getLinetypeType());
        Page<AssessDicCableType> pageData = assessDicCableTypeService.page(pageParam, wrapper);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), pageData,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据类型ID获取线缆类型字典详情")
    @ApiImplicitParam(name = "linetypeId", value = "线路线缆类型ID", required = true, type = "String")
    @GetMapping("/getInfo")
    public HttpResult<AssessDicCableType> getInfo(@RequestParam("linetypeId") String linetypeId) {
        AssessDicCableType param = assessDicCableTypeService.getByLinetypeId(linetypeId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), param,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("新增线缆类型字典")
    @PostMapping("/add")
    public HttpResult<Boolean> add(@RequestBody @Validated AssessDicCableTypeQuery param) {
        boolean result = assessDicCableTypeService.add(param);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("修改线缆类型字典")
    @PostMapping("/edit")
    public HttpResult<Boolean> edit(@RequestBody @Validated AssessDicCableTypeQuery.UpdateAssessDicCableTypeQuery updateQuery) {
        assessDicCableTypeService.update(updateQuery);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), true,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("删除线缆类型字典")
    @ApiImplicitParam(name = "linetypeId", value = "线路线缆类型ID", required = true, type = "String")
    @PostMapping("/delete")
    public HttpResult<Boolean> delete(@RequestBody  List<String> linetypeId) {
        boolean result = assessDicCableTypeService.removeByIds(linetypeId);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), result,
                CommonResponseEnum.SUCCESS.getMessage());
    }

    @ApiOperation("根据线缆类型查询字典")
    @ApiImplicitParam(name = "linetypeType", value = "线路线缆类型（01-架空线，02-电缆）", required = true, type = "String")
    @GetMapping("/listByType")
    public HttpResult<List<AssessDicCableType>> listByType(@RequestParam("linetypeType") String linetypeType) {
        List<AssessDicCableType> list = assessDicCableTypeService.listByType(linetypeType);
        return HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), list,
                CommonResponseEnum.SUCCESS.getMessage());
    }
}