package com.kym.admin.controller;

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.AdminUserVo;
import com.kym.service.admin.AdminUserService;
import com.kym.service.admin.RoleService;
import org.springframework.web.bind.annotation.*;

/**
 * <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));
    }

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


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