package com.njcn.advance.controller.govern.voltage;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.advance.pojo.param.govern.voltage.SgSensitiveUnitParam;
import com.njcn.advance.pojo.vo.govern.voltage.SgSensitiveUnitVO;
import com.njcn.advance.service.govern.voltage.ISgSensitiveUnitService;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
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 com.njcn.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hongawen
 * @since 2024-03-14
 */
@Slf4j
@Validated
@Api(tags = "暂降治理元器件控制器")
@RestController
@RequestMapping("/sgSensitiveUnit")
@RequiredArgsConstructor
public class SgSensitiveUnitController extends BaseController {

    private final ISgSensitiveUnitService sgSensitiveUnitService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询元器件数据")
    @ApiImplicitParam(name = "sgSensitiveUnitQueryParam", value = "查询参数", required = true)
    public HttpResult<Page<SgSensitiveUnitVO>> list(@RequestBody SgSensitiveUnitParam.SgSensitiveUnitQueryParam sgSensitiveUnitQueryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询元器件数据为：{}", methodDescribe, sgSensitiveUnitQueryParam);
        Page<SgSensitiveUnitVO> result = sgSensitiveUnitService.sgSensitiveListByMachineId(sgSensitiveUnitQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/queryUnitByMachineId")
    @ApiOperation("根据设备id查询所有的元器件信息")
    @ApiImplicitParam(name = "machineId", value = "设备id", required = true)
    public HttpResult<List<SgSensitiveUnitVO>> queryUnitByMachineId(String machineId) {
        String methodDescribe = getMethodDescribe("queryUnitByMachineId");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, machineId);
        List<SgSensitiveUnitVO> result = sgSensitiveUnitService.listAllUnitByMachineId(machineId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增元器件")
    @ApiImplicitParam(name = "sgSensitiveUnitParam", value = "元器件数据", required = true)
    public HttpResult<String> add(@RequestBody @Validated SgSensitiveUnitParam sgSensitiveUnitParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，元器件数据：{}", methodDescribe, sgSensitiveUnitParam);
        String sensitiveUnitId = sgSensitiveUnitService.addSensitiveUnit(sgSensitiveUnitParam);
        if (StrUtil.isNotBlank(sensitiveUnitId)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, sensitiveUnitId, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }




    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/update")
    @ApiOperation("更新元器件")
    @ApiImplicitParam(name = "updateParam", value = "元器件数据", required = true)
    public HttpResult<Object> update(@RequestBody @Validated SgSensitiveUnitParam.SgSensitiveUnitUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，元器件数据：{}", methodDescribe, updateParam);
        boolean result = sgSensitiveUnitService.updateSgSensitiveUnit(updateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/delete")
    @ApiOperation("删除元器件")
    @ApiImplicitParam(name = "ids", value = "元器件索引", required = true, dataTypeClass = List.class)
    public HttpResult<Object> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，元器件ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = sgSensitiveUnitService.deleteSgSensitiveUnitData(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }




}

