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.bean.user.RoleTypeBean;
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 com.gree.mall.manager.plus.service.AdminWebsitService;
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.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class AdminRoleLogic {

    @Autowired
    AdminRoleService adminRoleService;
    @Autowired
    AdminUserService adminUserService;
    @Autowired
    AdminWebsitService adminWebsitService;
    @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();

        Integer type = adminUser.getType();

        Integer level = null;
        if (adminUser.getType() == 0 && adminUser.getIsMaster()) {
            final AdminRole adminRole = adminRoleService.getById(adminUser.getRoleId());
            level = adminRole.getLevel();
        }

        Page<AdminRole> rolePage = adminRoleService.lambdaQuery()
                .like(StringUtils.isNotBlank(companyWechatName), AdminRole::getCompanyName, companyWechatName)
                .le(AdminRole::getType, type)
                .in(CollectionUtils.isNotEmpty(companyWechatIds), AdminRole::getCompanyWechatId, companyWechatIds)
                .eq(Objects.nonNull(level), AdminRole::getLevel, level)
                .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);

        if (adminUser.getType() == 1
                && Objects.nonNull(adminRole.getLevel())
                && adminRole.getLevel() != 0) {
            final Integer count = adminRoleService.lambdaQuery()
                    .eq(AdminRole::getCompanyWechatId, adminUser.getCompanyWechatId())
                    .eq(AdminRole::getType, 1)
                    .eq(AdminRole::getLevel, adminRole.getLevel())
                    .count();
            if (count > 0) {
                throw new RemoteServiceException("已存在同级别的角色不能再添加");
            }
        }


        adminRole.setCompanyWechatId(adminUser.getCompanyWechatId());
        adminRole.setCompanyName(adminUser.getCompanyName());
        adminRoleService.save(adminRole);
    }

    /**
     * 修改角色
     */
    @Transactional
    public void update(AdminRole adminRole) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (adminUser.getType() == 1
                && Objects.nonNull(adminRole.getLevel())
                && adminRole.getLevel() != 0) {
            final Integer count = adminRoleService.lambdaQuery()
                    .eq(AdminRole::getCompanyWechatId, adminUser.getCompanyWechatId())
                    .eq(AdminRole::getType, 1)
                    .eq(AdminRole::getLevel, adminRole.getLevel())
                    .ne(AdminRole::getAdminRoleId, adminRole.getAdminRoleId())
                    .count();
            if (count > 0) {
                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 {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (adminUser.getType() == 0) {
            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 List<RoleTypeBean> getRoleType() {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<RoleTypeBean> list = new ArrayList<>();
        if (adminUser.getType() == 0) {
            RoleTypeBean bean = new RoleTypeBean();
            bean.setCode(2);
            bean.setName("平台");
            list.add(bean);
        }
        RoleTypeBean bean2 = new RoleTypeBean();
        bean2.setCode(1);
        bean2.setName("平台");
        list.add(bean2);

        RoleTypeBean bean3 = new RoleTypeBean();
        bean3.setCode(0);
        bean3.setName("网点");
        list.add(bean3);

        return list;
    }
}
