package com.zfire.mall.manager.logic.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.constant.Constant;
import com.zfire.mall.manager.enums.RoleTypeEnum;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.AdminRole;
import com.zfire.mall.manager.plus.entity.AdminUser;
import com.zfire.mall.manager.plus.service.AdminRoleService;
import com.zfire.mall.manager.plus.service.AdminUserService;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.plus.entity.AdminRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class AdminRoleLogic {

    @Autowired
    AdminRoleService adminRoleService;
    @Autowired
    AdminUserService adminUserService;
    @Autowired
    CommonLogic commonLogic;


    /**
     * 角色列表
     */
    public IPage<AdminRole> listPage(HttpServletRequest request, Integer pageNo, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getAdminCompanyIds();

        Integer type = adminRoleService.getById(adminUser.getRoleId()).getType();
        if("admin".equals(adminUser.getUserName())){
            type = RoleTypeEnum.ADMIN.getCode();
        }

        return adminRoleService.lambdaQuery()
                .le(AdminRole::getType, type)
                .in(CollectionUtils.isNotEmpty(companyWechatIds), AdminRole::getAdminCompanyId, companyWechatIds)
                .orderByDesc(AdminRole::getType)
                .page(new Page<>(pageNo, pageSize));
    }

    /**
     * 新增角色
     */
    public void add(HttpServletRequest request, AdminRole adminRole) {

        Integer count = adminRoleService.lambdaQuery().eq(AdminRole::getName, adminRole.getName()).count();
        if(count > 0){
            throw new RemoteServiceException("当前角色名称已存在。");
        }

        //默认部门角色
        adminRole.setType(RoleTypeEnum.WEBSIT.getCode());

        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        adminRole.setAdminCompanyId(adminUser.getLoginCompanyId());
        adminRole.setAdminCompanyName(adminUser.getLoginCompanyName());
        adminRole.setCreateTime(new Date());
        adminRoleService.save(adminRole);
    }

    /**
     * 修改角色
     */
    @Transactional
    public void update(AdminRole adminRole) {
//        if(adminRole.getAdminRoleId().length() < 10){
//            throw new RemoteServiceException("系统角色不可编辑");
//        }
//
//        if(adminRole.getName() != null && adminRole.getName().equals(Constant.NOT_DEL_ROLE_NAME)){
//            throw new RemoteServiceException("不可操作经销商角色");
//        }
        adminRoleService.updateById(adminRole);

        if (adminRole.getName() != null) {
            adminUserService.lambdaUpdate().set(AdminUser::getRoleName, adminRole.getName())
                    .eq(AdminUser::getRoleId, adminRole.getAdminRoleId()).update();
        }
    }

    /**
     * 删除角色
     */
    public void delete(String adminRoleId) throws RemoteServiceException {
        if(adminRoleId.length() < 10){
            throw new RemoteServiceException("系统角色不可编辑");
        }
        AdminRole adminRole = adminRoleService.getById(adminRoleId);
        if(adminRole.getName().equals(Constant.NOT_DEL_ROLE_NAME)){
            throw new RemoteServiceException("不可操作经销商角色");
        }
        //不允许删除使用该角色的帐号
        Integer count = adminUserService.lambdaQuery().eq(AdminUser::getRoleId, adminRoleId).count();
        if (count > 0) {
            throw new RemoteServiceException("共有" + count + "个帐号使用该角色,不可删除");
        }
        adminRoleService.removeById(adminRoleId);
    }

    /**
     * 详情
     */
    public AdminRole detail(String adminRoleId) {
        return adminRoleService.getById(adminRoleId);
    }


    /**
     * 获取企业角色
     */
    public AdminRole getCompanyRole(){
        AdminRole adminRole = adminRoleService.lambdaQuery().eq(AdminRole::getType, RoleTypeEnum.COMPANY.getCode()).one();
        if(adminRole == null){
            throw new RemoteServiceException("请先初始化企业角色");
        }
        return adminRole;
    }
}
