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


import cn.hutool.core.util.StrUtil;
import com.njcn.advance.pojo.param.govern.voltage.IncomingLineParam;
import com.njcn.advance.pojo.vo.govern.voltage.IncomingLineVO;
import com.njcn.advance.pojo.vo.govern.voltage.SgOption;
import com.njcn.advance.service.govern.voltage.ISgIncomingLineService;
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-02-29
 */
@Slf4j
@Validated
@Api(tags = "暂降治理业务进线控制器")
@RestController
@RequestMapping("/sgIncomingLine")
@RequiredArgsConstructor
public class SgIncomingLineController extends BaseController {

    private final ISgIncomingLineService sgIncomingLineService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/list")
    @ApiOperation("查询用户的进线数据")
    public HttpResult<List<IncomingLineVO>> list(String userId) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询进线数据为：{}", methodDescribe, userId);
        List<IncomingLineVO> result = sgIncomingLineService.incomingLineList(userId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/lineList")
    @ApiOperation("查询监测点数据")
    public HttpResult<List<SgOption>> lineList( ) {
        String methodDescribe = getMethodDescribe("lineList");
        LogUtil.njcnDebug(log, "{}", methodDescribe);
        List<SgOption> result = sgIncomingLineService.lineList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增业务用户的进线")
    @ApiImplicitParam(name = "incomingLineParam", value = "进线数据", required = true)
    public HttpResult<String> add(@RequestBody @Validated IncomingLineParam incomingLineParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，进线数据为：{}", methodDescribe, incomingLineParam);
        String lineId = sgIncomingLineService.addIncomingLine(incomingLineParam);
        if (StrUtil.isNotBlank(lineId)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, lineId, 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 IncomingLineParam.IncomingLineUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，进线数据为：{}", methodDescribe, updateParam);
        boolean result = sgIncomingLineService.updateIncomingLine(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 = sgIncomingLineService.deleteIncomingLineData(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

}

