package com.czg.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.user.SysUserAddDTO;
import com.czg.account.dto.user.SysUserEditDTO;
import com.czg.account.dto.user.SysUserEditPwdDTO;
import com.czg.account.service.SysUserService;
import com.czg.account.vo.SysUserDetailVO;
import com.czg.annotation.SaAdminCheckRole;
import com.czg.exception.CzgException;
import com.czg.resp.CzgResult;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 系统用户管理
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/sysUser")
public class SysController {
    @Resource
    private SysUserService sysUserService;

    /**
     * 系统用户列表
     *
     * @param key       名称或邮箱搜索
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param status    状态：1启用、0禁用
     * @return 分页数据
     */
//    @SaAdminCheckPermission("sysUser:list")
    @SaAdminCheckRole("admin")
    @GetMapping
    public CzgResult<Page<SysUserDetailVO>> list(String key, String startTime, String endTime, Integer status) {
        return CzgResult.success(sysUserService.getPage(key, startTime, endTime, status));
    }

    /**
     * 员工相信信息
     *
     * @param id 用户id
     * @return 员工信息
     */
    @SaAdminCheckRole("admin")
    @GetMapping("/detail")
    public CzgResult<SysUserDetailVO> detail(@RequestParam Integer id) {
        return CzgResult.success(sysUserService.detail(id));
    }

    /**
     * 系统账号修改
     *
     * @param sysUserEditDTO 修改信息
     * @return 是否成功
     */
//    @SaAdminCheckPermission("sysUser:edit")
    @SaAdminCheckRole("admin")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated SysUserEditDTO sysUserEditDTO) {
        return CzgResult.success(sysUserService.edit(sysUserEditDTO));
    }

    /**
     * 登录账号密码修改
     *
     * @param sysUserEditPwdDTO 修改密码
     *                          原密码与验证码 二选一
     * @return 是否成功
     */
    @PutMapping("/pwd")
    public CzgResult<Boolean> editPwd(@RequestBody @Validated SysUserEditPwdDTO sysUserEditPwdDTO) {
        if (StrUtil.isBlank(sysUserEditPwdDTO.getOriginalPassword()) || StrUtil.isBlank(sysUserEditPwdDTO.getCode())) {
            throw new CzgException("原密码或验证码不能同时为空");
        }
        return CzgResult.success(sysUserService.editPwd(sysUserEditPwdDTO));
    }

    /**
     * 系统用户删除
     *
     * @param id 用户id
     * @return 是否成功
     */
//    @SaAdminCheckPermission("sysUser:del")
    @SaAdminCheckRole("admin")
    @DeleteMapping
    public CzgResult<Boolean> del(@RequestParam Integer id) {
        return CzgResult.success(sysUserService.delete(id));
    }

    /**
     * 系统用户添加
     *
     * @param sysUserAddDTO 添加信息
     * @return 是否成功
     */
    @SaAdminCheckRole("admin")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated SysUserAddDTO sysUserAddDTO) {
        return CzgResult.success(sysUserService.add(sysUserAddDTO));
    }

    /**
     * 系统用户导出
     *
     * @param key       名称或邮箱搜索
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param status    状态：1启用、0禁用
     * @throws IOException IO异常
     */
    @SaAdminCheckRole("admin")
    @GetMapping("/download")
    public void download(String key, String startTime, String endTime, Integer status, HttpServletResponse response) throws IOException {
        sysUserService.download(key, startTime, endTime, status, response);
    }
}
