package com.kym.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.kym.common.R;
import com.kym.common.annotation.SysLog;
import com.kym.common.controller.IController;
import com.kym.entity.admin.queryParams.CommonQueryParam;
import com.kym.entity.admin.vo.AdminUserRoleVo;
import com.kym.entity.admin.vo.AdminUserVo;
import com.kym.service.admin.AdminUserService;
import com.kym.service.admin.RoleService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * admin用户表 前端控制器
 * </p>
 *
 * @author skyline
 * @since 2023-07-11
 */
@RestController
@RequestMapping("/admin-user")
public class AdminUserController extends IController {

    private final AdminUserService adminUserService;

    private final RoleService roleService;

    public AdminUserController(AdminUserService adminUserService, RoleService roleService) {
        this.adminUserService = adminUserService;
        this.roleService = roleService;
    }

    @SysLog("登录")
    @PostMapping("/login")
    R<?> adminLogin(@RequestBody JSONObject json) {
        return adminUserService.login(json.getString("mobilePhone"), json.getString("password"));
    }

    @SysLog("登出")
    @PostMapping("/logout")
    R<?> adminLogout() {
        StpUtil.logout();
        return R.success();
    }

    @SysLog("账号信息读取")
    @GetMapping("/profile")
    R<?> profile() {
        return resp(adminUserService::profile);
    }

    /**
     * 操作员列表
     *
     * @param params
     * @return
     */
    @GetMapping("listAdminUser")
    R<?> listAdminUser(@ModelAttribute CommonQueryParam params) {
        return R.success(adminUserService.listAdminUser(params));
    }


    @GetMapping("list")
    R<?> listUser(@ModelAttribute CommonQueryParam params) {
        return R.success(adminUserService.listUser(params));
    }

    /**
     * 新增操作员
     *
     * @param adminUserVo
     * @return
     */
    @SaCheckPermission("user.add")
    @PostMapping("add")
    R<?> createAdminUser(@RequestBody AdminUserVo adminUserVo) {
        return resp(()->adminUserService.createAdminUser(adminUserVo));
    }


    /**
     * 新增操作员
     *
     * @param adminUserVo
     * @return
     */
    @SaCheckPermission("user.modify")
    @PostMapping("modify")
    R<?> updateAdminUser(@RequestBody AdminUserVo adminUserVo) {
        adminUserService.updateAdminUser(adminUserVo);
        return R.success();
    }


    /**
     * 角色列表
     *
     * @return
     */
    @GetMapping("listRole")
    R<?> listRole() {
        return R.success(roleService.listRole(null));
    }

    @GetMapping("detail/{id}")
    public R<?> detail(@PathVariable(name = "id") long id) {
        return resp(() -> adminUserService.detail(id));
    }

    @SaCheckPermission("user.modify")
    @PostMapping("updateRole")
    R<?> updateRole(@RequestBody AdminUserRoleVo vo) {
        adminUserService.updateRole(vo.getUserId(), vo.getRoleIdList());
        return R.success();
    }


}
