package com.gree.mall.manager.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ApiNotAuth;
import com.gree.mall.manager.bean.SVerification;
import com.gree.mall.manager.bean.admin.*;
import com.gree.mall.manager.bean.admin.reqDto.AdminUserAddReqBean;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.admin.AdminUserLogic;
import com.gree.mall.manager.plus.entity.AdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@Api(value = "系统用户管理", tags = {"后台用户相关API"})
@RequestMapping(value = "/admin/user", produces = "application/json; charset=utf-8")
public class AdminUserController {

    @Autowired
    AdminUserLogic adminUserLogic;

    @ApiNotAuth
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public ResponseHelper<AdminUserCom> login(
            @ApiParam(value = "帐号", required = true) @RequestParam(required = true) String userName,
            @ApiParam(value = "密码", required = true) @RequestParam(required = true) String password,
            @ApiParam(value = "验证码code", required = true) @RequestParam(required = true) String code,
            @ApiParam(value = "验证码值", required = true) @RequestParam(required = true) String codeValue

    ) throws RemoteServiceException {
        AdminUserCom adminUserBean = adminUserLogic.login(userName, password, code, codeValue);
        return ResponseHelper.success(adminUserBean);
    }

    @PostMapping("/password/update")
    @ApiOperation(value = "修改密码")
    public ResponseHelper<AdminUserBean> login(
            @ApiParam(value = "帐号", required = true) @RequestParam(required = true) String userName,
            @ApiParam(value = "密码", required = true) @RequestParam(required = true) String password,
            @ApiParam(value = "新密码", required = true) @RequestParam(required = true) String newPassword
    ) throws RemoteServiceException {
        adminUserLogic.updatePassword(userName, password, newPassword);
        return ResponseHelper.success();
    }

    @PostMapping("/status/update")
    @ApiOperation("修改帐号状态(冻结/正常)")
    public ResponseHelper updateStatus(
            @ApiParam(value = "帐号名称", required = true) @RequestParam(required = true) String adminUserId,
            @ApiParam(value = "true:正常  false:冻结", required = true) @RequestParam(required = true) Boolean status
    ) throws RemoteServiceException {
        adminUserLogic.updateStatus(adminUserId, status);
        return ResponseHelper.success();
    }

    @PostMapping("/save")
    @ApiOperation("保存帐号")
    public ResponseHelper add(
            @ApiParam(value = "帐号", required = true) @RequestBody AdminUserAddReqBean adminUserBean,
            HttpServletRequest request
    ) throws RemoteServiceException {
        adminUserLogic.save(adminUserBean, request);
        return ResponseHelper.success();
    }

    @PostMapping("/password/reset")
    @ApiOperation("重置密码")
    public ResponseHelper resetPwd(
            @ApiParam(value = "用户id", required = true) @RequestParam String adminUserId,
            @ApiParam(value = "密码", required = true) @RequestParam String password,
            HttpServletRequest request
    ) throws RemoteServiceException {
        adminUserLogic.resetPassword(adminUserId, password, request);
        return ResponseHelper.success();
    }


    @GetMapping("/list")
    @ApiOperation(value = "帐号列表")
    public ResponseHelper<IPage<AdminUser>> list(
            @ApiParam(value = "角色id", required = false) @RequestParam(required = false) String roleId,
            @ApiParam(value = "状态 true:正常 false:冻结", required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "用户名", required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "商户名称", required = false) @RequestParam(required = false) String companyName,
            @ApiParam(value = "页号", required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<AdminUser> page = adminUserLogic.list(companyName,roleId, status, userName, pageNum, pageSize, request);
        return ResponseHelper.success(page);
    }

    @GetMapping("/module/list")
    @ApiOperation(value = "功能菜单权限列表")
    public ResponseHelper<List<AdminModuleTree>> moduleList(
            @ApiParam(value = "用户id", required = true) @RequestParam(required = true) String adminUserId
    ) throws RemoteServiceException {
        List<AdminModuleTree> adminModuleTrees = adminUserLogic.queryAdminModule(adminUserId);
        return ResponseHelper.success(adminModuleTrees);
    }

    @GetMapping("/module/id/checked")
    @ApiOperation("查询选中的功能模块ids")
    public ResponseHelper<List<String>> queryModuleIdChecked(
            @ApiParam(value = "角色id", required = true) @RequestParam String adminRoleId
    ) throws RemoteServiceException {
        List<String> moduleIds = adminUserLogic.queryModuleIdChecked(adminRoleId);
        return ResponseHelper.success(moduleIds);
    }

    @GetMapping("/module/all")
    @ApiOperation(value = "全部功能菜单权限列表")
    public ResponseHelper<List<AdminModuleTree>> allModuleList(
            @ApiParam(value = "角色", required = true) @RequestParam String adminRoleId,
            HttpServletRequest request
    ) throws RemoteServiceException {
        List<AdminModuleTree> adminModuleTrees = adminUserLogic.queryAllAdminModuleTree(adminRoleId, request);
        return ResponseHelper.success(adminModuleTrees);
    }

    @PostMapping("/module/grant")
    @ApiOperation(value = "授权动能菜单权限")
    public ResponseHelper grant(
            @RequestBody AdminWebsitGrantBean adminWebsitGrantBean
    ) throws RemoteServiceException {
        adminUserLogic.grantModules(adminWebsitGrantBean);
        return ResponseHelper.success();
    }

    @GetMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<AdminUserCom> detail(
            @ApiParam(value = "帐号id", required = true) @RequestParam(required = true) String adminUserId
    ) throws RemoteServiceException {
        AdminUserCom detail = adminUserLogic.detail(adminUserId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public ResponseHelper logout(HttpServletRequest request) {
        adminUserLogic.logout(request);
        return ResponseHelper.success();
    }

    @ApiNotAuth
    @GetMapping("/imageVerification")
    @ApiOperation(value = "获取图片验证码")
    public ResponseHelper<SVerification> defaultKaptcha()
            throws Exception {
        SVerification sVerification = adminUserLogic.defaultKaptcha();
        return ResponseHelper.success(sVerification);
    }

    @ApiNotAuth
    @PostMapping("/check-or-login/account")
    @ApiOperation(value = "检查账号")
    public ResponseHelper<ExternalMapBean> checkOrLoginAccount(
            @ApiParam(value = "账号", required = true) @RequestParam String account,
            @ApiParam(value = "密码", required = true) @RequestParam String password,
            @ApiParam(value = "是否检查", required = true) @RequestParam Boolean isCheck,
            @ApiParam(value = "时间戳", required = true) @RequestParam String timestamp,
            @ApiParam(value = "签名", required = true) @RequestParam String sign
    ) throws Exception {
        ExternalMapBean bean = adminUserLogic.checkOrLoginAccount(account, password, isCheck, timestamp, sign);
        return ResponseHelper.success(bean);
    }


}
