

package com.zcloud.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.utils.CamelCaseUtil;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import com.zcloud.common.validator.Assert;
import com.zcloud.modules.sys.entity.BusImgfilesEntity;
import com.zcloud.modules.sys.entity.SysUserEntity;
import com.zcloud.modules.sys.form.PasswordForm;
import com.zcloud.modules.sys.service.BusImgfilesService;
import com.zcloud.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 */
@RestController
@RequestMapping("/sys/user")
@Api(tags = "用户管理")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;

    @Resource
    private BusImgfilesService busImgfilesService;


    /**
     * 用户分页列表
     */
    @PostMapping("/list")
    //@RequiresPermission("sys:user:list")
    @ApiOperation(value = "用户分页列表")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 所有用户列表(不分页)
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "所有用户列表")
    public R listAll(@RequestBody Map<String, Object> params) {
        params.put("isDelete", 0);
        List<SysUserEntity> list = sysUserService.listAll(params);
        return R.ok().put("data", list);
    }

    /**
     * 下拉用户列表(不分页)
     */
    @PostMapping("/listForSelect")
    @ApiOperation(value = "下拉用户列表")
    public R listForSelect(@RequestBody Map<String, Object> params) {
        List<SysUserEntity> list = sysUserService.listForSelect(params);
        return R.ok().put("userList", list);
    }

    /**
     * 获取登录的用户信息
     */
    @PostMapping("/info")
    @ApiOperation(value = "获取登录的用户信息")
    public R info() {
        return R.ok().put("user", getUser());
    }

    /**
     * 修改登录用户密码
     */
    @SysLog("修改登录用户密码")
    @PostMapping("/password")
    @ApiOperation(value = "修改登录用户密码")
    public R password(@RequestBody PasswordForm form) {
        Assert.isBlank(form.getNewPassword(), "新密码不为能空");

        //sha256加密
        String password = new Sha256Hash(form.getPassword(), getUsername()).toString();
        //sha256加密
        String newPassword = new Sha256Hash(form.getNewPassword(), getUsername()).toString();

        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (!flag) {
            return R.error(200, "原密码不正确");
        }

        return R.ok();
    }

    /**
     * 用户信息
     */
    @PostMapping("/getInfo")
    //@RequiresPermission("sys:user:info")
    @ApiOperation(value = "获取用户信息")
    public R info(@RequestBody SysUserEntity user) {
        user = sysUserService.queryById(user.getUserId());
        return R.ok().put("user", user);
    }

    /**
     * 查询用户是否存在
     */
    @PostMapping("/hasUser")
    @ApiOperation(value = "查询用户是否存在")
    public R hasUser(@RequestBody SysUserEntity user) {
        user = sysUserService.queryByUserName(user.getUsername());
        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @PostMapping("/save")
    //@RequiresPermission("sys:user:save")
    @ApiOperation(value = "保存用户")
    public R save(@RequestPart SysUserEntity user,
                  @RequestPart(value = "file", required = false) MultipartFile file) throws IOException {
        user.setCreator(getUserId());
        return sysUserService.saveUser(user, file);
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @PostMapping("/update")
    //@RequiresPermission("sys:user:update")
    @ApiOperation(value = "修改用户")
    public R update(@RequestPart SysUserEntity user,
                    @RequestPart(value = "file", required = false)  MultipartFile file) throws IOException {
        return sysUserService.updateSysUserInfo(user,file);
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @PostMapping("/delete")
    //@RequiresPermission("sys:user:delete")
    @ApiOperation(value = "删除用户")
    public R delete(@RequestBody SysUserEntity user) {
        if (ArrayUtils.contains(user.getUserIds(), 1L)) {
            return R.error(200, "系统管理员不能删除");
        }

        if (ArrayUtils.contains(user.getUserIds(), getUserId())) {
            return R.error(200, "当前用户不能删除");
        }

        sysUserService.deleteBatch(user.getUserIds());

        return R.ok();
    }

    /**
     * 重置用户密码
     */
    @SysLog("重置用户密码")
    @PostMapping("/resetPassword")
    @ApiOperation(value = "重置用户密码")
    public R resetPassword(@RequestBody SysUserEntity user) {
        user = sysUserService.getById(user.getUserId());
        user.setPassword(new Sha256Hash("Aqsc@2024", user.getUsername()).toHex());

        //更新密码
        sysUserService.update(user);

        return R.ok();
    }


}
