package cn.qinys.platform.upms.api.controller;


import cn.qinys.platform.base.response.Result;
import cn.qinys.platform.base.security.annotation.HasPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.qinys.platform.entity.upms.UpmsTenant;
import cn.qinys.platform.upms.api.req.*;
import cn.qinys.platform.upms.api.resp.UserAccountInfoResp;
import cn.qinys.platform.upms.api.resp.UserDetailResp;
import cn.qinys.platform.upms.api.resp.UserPageResp;
import cn.qinys.platform.upms.api.service.ITenantService;
import cn.qinys.platform.upms.config.mybatisplus.TenantContextHolder;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * Tenant 的控制器
 *
 * @author tanlie
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/upms/tenant")
@Slf4j
public class TenantController extends DevBaseController{

    @Resource
    private ITenantService tenantService;

    /**
     * 分页查询
     *
     * @param query 查询参数
     * @return 分页数据
     */
    @GetMapping("/page")
    @HasPermission({"dev.tenant"})
    public Result<Page<UpmsTenant>> page(TenantQueryReq query) {
        log.info("查询Tenant的query请求入参: {}", query);
        return new Result<>(tenantService.queryPage(query));
    }

    /**
     * 新增
     *
     * @param req 参数
     */
    @PostMapping("create")
    @HasPermission({"dev.tenant"})
    public Result<String> create(@Validated @RequestBody TenantCreateReq req) {
        log.info("新增Tenant请求入参: {}", req);
        tenantService.createTenant(req);
        return new Result<>("操作成功");
    }

    /**
     * 修改
     *
     * @param req 参数
     */
    @PostMapping("update")
    @HasPermission({"dev.tenant"})
    public Result<String> update(@Validated @RequestBody TenantUpdateReq req) {
        log.info("更新Tenant请求入参: {}", req);
        tenantService.updateTenant(req);
        return new Result<>("操作成功");
    }

    /**
     * 删除
     *
     * @param ids id列表
     */
    @PostMapping("remove")
    @HasPermission({"dev.tenant"})
    public Result<String> remove(@RequestBody List<String> ids) {
        log.info("删除Tenant请求入参: {}", ids);
        tenantService.removeTenant(ids);
        return new Result<>("操作成功");
    }


    /**
     * 更新菜单
     *
     * @return
     */
    @HasPermission({"dev.tenant"})
    @PostMapping("/menu/sync")
    public Result<String> syncMenu(@RequestBody @Valid MenuSyncReq req) {
        tenantService.syncToTenant(req.getTenantId());
        return new Result<>("操作成功");
    }

    /**
     * 用户分页
     *
     * @param req
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @GetMapping("/user/page")
    public Result<Page<UserPageResp>> getUserPage(UserPageReq req) {
        TenantContextHolder.setTenantId(null);
        Page<UserPageResp> resp = tenantService.getUserPage(req);
        TenantContextHolder.clear();
        return new Result<>(resp);
    }

    /**
     * 账号信息
     *
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @GetMapping("/user/account")
    public Result<UserAccountInfoResp> accountInfo(String id) {
        TenantContextHolder.setTenantId(null);
        UserAccountInfoResp resp = tenantService.getAccountInfo(id);
        TenantContextHolder.clear();
        return new Result<>(resp);
    }

    /**
     * 修改锁定状态
     *
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @PostMapping("/user/change-lock")
    public Result<String> changeLock(@RequestBody @Valid UserChangeLockReq req) {
        TenantContextHolder.setTenantId(null);
        tenantService.changeLock(req);
        TenantContextHolder.clear();
        return new Result<>("操作成功");
    }


    /**
     * 修改密码
     *
     * @param req
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @PostMapping("/user/reset-pwd")
    public Result<String> changePassword(@RequestBody @Valid UserResetPasswordReq req) {
        TenantContextHolder.setTenantId(null);
        tenantService.resetPassword(req);
        TenantContextHolder.clear();
        return new Result<>("操作成功");
    }

    /**
     * 用户角色新增
     *
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @PostMapping("/user/role-add")
    public Result<String> userRoleAdd(@RequestBody @Valid UserRoleOperationReq req) {
        tenantService.userRoleAdd(req);
        return new Result<>("操作成功");
    }

    /**
     * 用户角色删除
     *
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @PostMapping("/user/role-delete")
    public Result<String> userRoleDelete(@RequestBody @Valid UserRoleOperationReq req) {
        tenantService.userRoleDelete(req);
        return new Result<>("操作成功");
    }

    /**
     * 新增管理员
     *
     * @param req
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @PostMapping("/user/remove")
    public Result<String> removeUser(@RequestBody @Valid UserRemoveReq req) {
        TenantContextHolder.setTenantId(null);
        tenantService.removeUser(req);
        TenantContextHolder.clear();
        return new Result<>("操作成功");
    }
    /**
     * 用户详情
     *
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @GetMapping("/user/detail/by-id")
    public Result<UserDetailResp> userDetailById(String id) {
        TenantContextHolder.setTenantId(null);
        UserDetailResp resp = tenantService.userDetailById(id);
        TenantContextHolder.clear();
        return new Result<>(resp);
    }
    /**
     * 修改锁定状态
     *
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @PostMapping("/user/detail/update")
    public Result<String> updateUserDetail(@RequestBody @Valid UserUpdateReq req) {
        TenantContextHolder.setTenantId(null);
        tenantService.updateUser(req);
        TenantContextHolder.clear();
        return new Result<>("操作成功");
    }
    /**
     * 新增管理员
     *
     * @param req
     * @return
     */
    @HasPermission({"dev.tenant.user"})
    @PostMapping("/user/create")
    public Result<String> createUser(@RequestBody @Valid UserCreateReq req) {
        TenantContextHolder.setTenantId(null);
        tenantService.createUser(req);
        TenantContextHolder.clear();
        return new Result<>("操作成功");
    }


}