package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.dto.admin.query.ResetPasswordReq;
import com.ruoyi.cai.dto.admin.query.UpdateMobileAdminReq;
import com.ruoyi.cai.dto.admin.query.UserForbidReq;
import com.ruoyi.cai.dto.admin.query.UserUpdateAdminReq;
import com.ruoyi.cai.dto.admin.vo.UserAdminVo;
import com.ruoyi.cai.dto.admin.vo.UserFullAdminVo;
import com.ruoyi.cai.manager.UserAdminManager;
import com.ruoyi.cai.manager.UserForbidManager;
import com.ruoyi.cai.service.UserForbidService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户
 *
 * @author 77
 * @date 2023-12-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/user")
public class UserController extends BaseController {

    private final UserService userService;
    private final UserAdminManager userAdminManager;

    /**
     * 查询用户列表
     */
    @SaCheckPermission("cai:user:list")
    @GetMapping("/list")
    public TableDataInfo<UserAdminVo> list(User query, PageQuery pageQuery) {
        Page<UserAdminVo> result = userService.pageAdmin(pageQuery, query);
        return TableDataInfo.build(result);
    }

    @GetMapping("/getByUsercode")
    public R<User> getByUsercode(String usercode){
        return R.ok(userService.getByUserCode(usercode));
    }

    @GetMapping("/listByUserCode")
    public R<List<String>> listByUserCode(String usercode,int limit){
        if(limit == 0){
            limit = 40;
        }
        List<User> users = userService.list(Wrappers.lambdaQuery(User.class)
                .select(User::getUsercode)
                .like(StringUtils.isNotEmpty(usercode), User::getUsercode, usercode)
                .last("limit " + limit));
        return R.ok(users.stream().map(User::getUsercode).collect(Collectors.toList()));
    }

    @Autowired
    private UserForbidManager userForbidManager;


    @PostMapping("/userForbid")
    public R<Void> userForbid(@RequestBody UserForbidReq userForbidReq){
        userForbidManager.batchForbid(userForbidReq);
        return R.ok();
    }


    /**
     * 获取用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:user:query")
    @GetMapping("/{id}")
    public R<User> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(userService.getById(id));
    }

    @SaCheckPermission("cai:user:query")
    @GetMapping("/full/{id}")
    public R<UserFullAdminVo> getFullUser(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(userAdminManager.getUser(id));
    }

    /**
     * 新增用户
     */
    @SaCheckPermission("cai:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody User bo) {
        return toAjax(userService.save(bo));
    }

    /**
     * 修改用户
     */
    @SaCheckPermission("cai:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserUpdateAdminReq bo) {
        return toAjax(userAdminManager.updateUser(bo));
    }

    @SaCheckPermission("cai:user:edit")
    @Log(title = "修改用户手机", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/updateMobile")
    public R<Void> updateMobile(@RequestBody UpdateMobileAdminReq bo) {
        return toAjax(userAdminManager.updateMobileUser(bo));
    }

    @PostMapping("/resetPwd")
    public R<Void> resetPwd(@RequestBody ResetPasswordReq req) {
        User user = userService.getById(req.getUserId());
        userService.resetPassword(req.getUserId(),user.getPassword());
        return R.ok();
    }

    @SaCheckPermission("cai:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return toAjax(userService.removeUser(id));
    }
}
