package com.fzm.mall.server.admin.auth_center.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.auth_center.entity.dto.AddAdminDTO;
import com.fzm.mall.server.admin.auth_center.entity.dto.RepairAdminRoleDTO;
import com.fzm.mall.server.admin.auth_center.entity.dto.ResetAdminPwdDTO;
import com.fzm.mall.server.admin.auth_center.entity.vo.AdminRoleListVO;
import com.fzm.mall.server.admin.auth_center.service.IAdminRoleService;
import com.fzm.mall.server.admin.config.Redis;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.home_page.entity.vo.AdminVO;
import com.fzm.mall.server.admin.home_page.enums.type.AdminTypeEnum;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.util.PhoneFormatCheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangp
 * @date 2021/4/9 13:25
 * @description 账号管理
 * @since JDK 1.8
 */
@Api(tags = "权限中心-账号管理")
@RestController
@RequestMapping("/accountManage")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AccountManageController {

    private final IAdminRoleService adminRoleService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;
    private final Redis redis;

    //WAIT 功能管理

    @ApiOperation(value = "添加")
    @AdminLog(description = "权限中心-账号管理-添加管理员", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.ADD_ADMIN)
    @PostMapping("/add")
    public ResponseVO add(@RequestBody @Valid AddAdminDTO adminDTO, @RequestHeader String adminId) {
        if (!PhoneFormatCheckUtils.isChinaPhoneLegal(adminDTO.getLinkPhone())) {
            return resFac.getResponse(MallResponseError.PHONE_FORMAT__ERROR);
        }
        if (adminService.checkAdminExist(adminDTO.getAccount())) {
            return resFac.getResponse(MallResponseError.ACCOUNT_HAS_EXISTED);
        }
        Admin admin = adminService.getByAdminId(adminId);
        adminService.add(adminDTO, admin.getMerchantId(), admin.getType());
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "角色列表")
    @GetMapping("/roleList")
    public ResponseVO page(@RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        List<AdminRoleListVO> roleList = adminRoleService.getRoleList(admin.getType());
        return resFac.getSuccessResponseWithData(roleList);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public ResponseVO page(
            @ApiParam(name = "uid", value = "管理员id")
            @RequestParam(required = false) String uid,
            @ApiParam(name = "account", value = "账号")
            @RequestParam(required = false) String account,
            @ApiParam(name = "pageNum", value = "当前页数")
            @RequestParam Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页个数")
            @RequestParam Integer pageSize,
            @ApiParam(name = "adminId", value = "管理员id")
            @RequestHeader String adminId) {
        Admin admin = adminService.getByAdminId(adminId);
        IPage<AdminVO> page = adminService.page(uid, account, admin, pageNum, pageSize);
        return resFac.getSuccessResponseWithData(page);
    }

    @ApiOperation("查询管理员登陆密码")
    @GetMapping("/queryLoginPwd")
    public ResponseVO queryLoginPwd(@ApiParam(name = "uid", value = "管理员id") @RequestParam String uid,
                                    @ApiParam(name = "adminId", value = "管理员id") @RequestHeader String adminId) {
        String password = adminService.getByAdminId(uid).getPassword();
        return resFac.getSuccessResponseWithData(password);
    }

    @ApiOperation(value = "禁用")
    @AdminLog(description = "权限中心-账号管理-禁用账号", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.DISABLE_ADMIN)
    @GetMapping("/disable/{uid}")
    public ResponseVO disable(@ApiParam(name = "uid", value = "被禁用管理员id") @PathVariable String uid,
                              @RequestHeader String adminId) {
        adminService.disable(uid);
        redis.del(uid + MALLGlobalConfig.MALL_TOKEN_REDIS_KEY_SUFFIX);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "启用")
//    @AdminLog(description = "权限中心-账号管理-禁用账号", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.DISABLE_ADMIN)
    @GetMapping("/able/{uid}")
    public ResponseVO able(@ApiParam(name = "uid", value = "被禁用管理员id") @PathVariable String uid,
                           @ApiParam(name = "adminId", value = "管理员id") @RequestHeader String adminId) {
        adminService.able(uid);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "重置密码")
    @AdminLog(description = "权限中心-账号管理-重置密码", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.RESET_ADMIN_PWD)
    @PutMapping("/resetPwd")
    public ResponseVO resetPwd(@RequestBody @Valid ResetAdminPwdDTO dto, @RequestHeader String adminId) {
        if (!dto.getNewPwd1().equals(dto.getNewPwd2())) {
            return resFac.getResponse(MallResponseError.PASSWORDS_ENTERED_TWICE_ARE_INCONSISTENT);
        }
        adminService.resetAdminPwd(dto);
        redis.del(dto.getAdminId() + MALLGlobalConfig.MALL_TOKEN_REDIS_KEY_SUFFIX);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "修改角色")
    @AdminLog(description = "权限中心-账号管理-重置密码", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.REPAIR_ADMIN_ROLE)
    @PutMapping("/repairRole")
    public ResponseVO repairRole(@RequestBody @Valid RepairAdminRoleDTO dto, @RequestHeader String adminId) {
        adminService.repairAdminRole(dto);
        redis.del(dto.getAdminId() + MALLGlobalConfig.MALL_TOKEN_REDIS_KEY_SUFFIX);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "删除")
    @AdminLog(description = "权限中心-账号管理-删除管理员", module = OperateModuleEnum.AUTH_CENTER, opType = OperateTypeEnum.DEL_ADMIN)
    @GetMapping("/delete/{uid}")
    public ResponseVO delete(@ApiParam(name = "uid", value = "角色id") @PathVariable String uid, @RequestHeader String adminId) {
        adminService.delete(uid);
        redis.del(uid + MALLGlobalConfig.MALL_TOKEN_REDIS_KEY_SUFFIX);
        return resFac.getSimpleSuccessResponse();
    }
}
