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

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.enums.RoleTypeEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.AdminRole;
import com.gree.mall.manager.plus.entity.AdminUser;
import com.gree.mall.manager.plus.service.AdminRoleService;
import com.gree.mall.manager.plus.service.AdminUserService;
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 ,String companyWechatName, Integer pageNo, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getCompanyWechatIds();

        final AdminRole role = adminRoleService.getById(adminUser.getRoleId());
        Integer type = role.getType();

        if("admin".equals(adminUser.getUserName())){
            type = RoleTypeEnum.ADMIN.getCode();
        } else if ("企业负责人".equals(role.getName())) {
            type = RoleTypeEnum.ADMIN.getCode();
        }

        Page<AdminRole> rolePage = adminRoleService.lambdaQuery()
                .like(StringUtils.isNotBlank(companyWechatName), AdminRole::getCompanyName, companyWechatName)
                .le(AdminRole::getType, type)
                .in(CollectionUtils.isNotEmpty(companyWechatIds), AdminRole::getCompanyWechatId, companyWechatIds)
                .orderByDesc(AdminRole::getType)
                .page(new Page<>(pageNo, pageSize));

        return rolePage;
    }

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

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

        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        adminRole.setCompanyWechatId(adminUser.getCompanyWechatId());
        adminRole.setCompanyName(adminUser.getCompanyName());
        adminRole.setCreateTime(new Date());
        adminRoleService.save(adminRole);
    }

    /**
     * 修改角色
     */
    @Transactional
    public void update(AdminRole adminRole) {
        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 {
        //不允许删除使用该角色的帐号
        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);
    }


}
