package com.njcn.gather.user.user.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.DataStateEnum;
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.LogUtil;
import com.njcn.gather.user.user.pojo.param.SysUserParam;
import com.njcn.gather.user.user.pojo.po.SysRole;
import com.njcn.gather.user.user.pojo.po.SysUser;
import com.njcn.gather.user.user.service.ISysUserRoleService;
import com.njcn.gather.user.user.service.ISysUserService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
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 java.util.List;
import java.util.stream.Collectors;


/**
 * @author caozehui
 * @since 2024-11-08
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/sysUser")
@RequiredArgsConstructor
public class SysUserController extends BaseController {

    private final ISysUserService sysUserService;
    private final ISysUserRoleService sysUserRoleService;

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation("分页查询用户列表")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<SysUser>> list(@RequestBody @Validated SysUserParam.SysUserQueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<SysUser> result = sysUserService.listUser(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "addUserParam", value = "新增用户", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated SysUserParam.SysUserAddParam addUserParam) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，用户数据为：{}", methodDescribe, addUserParam);
        boolean result = sysUserService.addUser(addUserParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改用户")
    @ApiImplicitParam(name = "updateUserParam", value = "修改用户", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated SysUserParam.SysUserUpdateParam updateUserParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，用户数据为：{}", methodDescribe, updateUserParam);
        boolean result = sysUserService.updateUser(updateUserParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("批量删除用户")
    @ApiImplicitParam(name = "ids", value = "用户id", required = true)
    public HttpResult<Boolean> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，用户id为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = sysUserService.deleteUser(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    @ApiImplicitParam(name = "param", value = "修改密码参数", required = true)
    public HttpResult<Boolean> updatePassword(@RequestBody @Validated SysUserParam.SysUserUpdatePasswordParam param) {
        String methodDescribe = getMethodDescribe("updatePassword");
        LogUtil.njcnDebug(log, "{}，用户id:{},用户旧密码:{},新密码:{}", methodDescribe, param.getId(), param.getOldPassword(), param.getNewPassword());
        boolean result = sysUserService.updatePassword(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getAll")
    @ApiOperation("获取所有用户")
    public HttpResult<List<SysUser>> getAll() {
        String methodDescribe = getMethodDescribe("getAll");
        LogUtil.njcnDebug(log, "{}，查询所有用户", methodDescribe);
        List<SysUser> result = sysUserService.lambdaQuery().eq(SysUser::getState, DataStateEnum.ENABLE.getCode()).list();
        result.forEach(user -> {
            user.setPassword(null);
            List<SysRole> sysRoles = sysUserRoleService.listRoleByUserId(user.getId());
            user.setRoleIds(sysRoles.stream().map(SysRole::getId).collect(Collectors.toList()));
            user.setRoleCodes(sysRoles.stream().map(SysRole::getCode).collect(Collectors.toList()));
            user.setRoleNames(sysRoles.stream().map(SysRole::getName).collect(Collectors.toList()));
        });
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}

