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.SgMachineParam;
import com.njcn.advance.pojo.vo.govern.voltage.SgMachineVO;
import com.njcn.advance.service.govern.voltage.ISgMachineService;
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("/sgMachine")
@RequiredArgsConstructor
public class SgMachineController extends BaseController {

    private final ISgMachineService sgMachineService;


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询设备数据")
    @ApiImplicitParam(name = "sgMachineQueryParam", value = "查询参数", required = true)
    public HttpResult<Page<SgMachineVO>> list(@RequestBody SgMachineParam.SgMachineQueryParam sgMachineQueryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询设备数据为：{}", methodDescribe, sgMachineQueryParam);
        Page<SgMachineVO> result = sgMachineService.sgMachineListByProductLineId(sgMachineQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/querySgMachineByProductLineId")
    @ApiOperation("根据生产线id查询所有的设备信息")
    @ApiImplicitParam(name = "productId", value = "生产线Id", required = true)
    public HttpResult<List<SgMachineVO>> querySgMachineByProductLineId(String productId) {
        String methodDescribe = getMethodDescribe("querySgMachineByProductLineId");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, productId);
        List<SgMachineVO> result = sgMachineService.listAllMachineByProductLineId(productId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增设备")
    @ApiImplicitParam(name = "sgMachineParam", value = "设备数据", required = true)
    public HttpResult<String> add(@RequestBody @Validated SgMachineParam sgMachineParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，设备数据：{}", methodDescribe, sgMachineParam);
        String machineId = sgMachineService.addMachine(sgMachineParam);
        if (StrUtil.isNotBlank(machineId)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, machineId, 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 SgMachineParam.SgMachineUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，设备数据：{}", methodDescribe, updateParam);
        boolean result = sgMachineService.updateSgMachine(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 = sgMachineService.deleteSgMachineData(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }



}

