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.SgUserParam;
import com.njcn.advance.pojo.po.govern.voltage.SgUser;
import com.njcn.advance.pojo.vo.govern.voltage.SgUserVO;
import com.njcn.advance.service.govern.voltage.ISgUserService;
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
@RestController
@Api(tags = "暂降治理业务用户控制器")
@RequestMapping("/sgUser")
@RequiredArgsConstructor
public class SgUserController extends BaseController {

    private final ISgUserService sgUserService;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询用户数据")
    @ApiImplicitParam(name = "sgUserQueryParam", value = "查询参数", required = true)
    public HttpResult<Page<SgUserVO>> list(@RequestBody  SgUserParam.SgUserQueryParam sgUserQueryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, sgUserQueryParam);
        Page<SgUserVO> result = sgUserService.sgUserList(sgUserQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/sgUserList")
    @ApiOperation("查询所有的用户数据")
    public HttpResult<List<SgUser>> sgUserList() {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询所有的用户数据", methodDescribe);
        List<SgUser> result = sgUserService.sgUserAllList();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }



    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增业务用户")
    @ApiImplicitParam(name = "sgUserParam", value = "用户数据", required = true)
    public HttpResult<String> add(@RequestBody @Validated SgUserParam sgUserParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，用户数据为：{}", methodDescribe, sgUserParam);
        String userId = sgUserService.addUserInfo(sgUserParam);
        if (StrUtil.isNotBlank(userId)) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, userId, 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 SgUserParam.SgUserUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，用户数据为：{}", methodDescribe, updateParam);
        boolean result = sgUserService.updateSgUser(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 = sgUserService.deleteSgUserData(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }

}

