package com.kiisoo.ic.domain.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kiisoo.ic.common.DataConstants;
import com.kiisoo.ic.domain.entity.PrivilageDomainDO;
import com.kiisoo.ic.domain.entity.PrivilageDomainEntityDO;
import com.kiisoo.ic.domain.entity.PrivilageUserDomainDO;
import com.kiisoo.ic.domain.enums.DomainEnum;
import com.kiisoo.ic.domain.mapper.PrivilageDomainDOMapper;
import com.kiisoo.ic.domain.mapper.PrivilageDomainEntityDOMapper;
import com.kiisoo.ic.domain.mapper.PrivilageUserDomainDOMapper;
import com.kiisoo.ic.domain.service.PrivilageDomainService;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import com.kiisoo.ic.system.entity.PrivilageRoleDO;
import com.kiisoo.ic.system.enums.RoleEnum;
import com.kiisoo.ic.system.mapper.PrivilageRoleDOMapper;
import com.kiisoo.ic.system.service.PrivilageUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 数据权限域表  服务实现类
 * @author jinchaofan
 * @since 2020-02-26
 */
@Service
public class PrivilageDomainServiceImpl extends ServiceImpl<PrivilageDomainDOMapper, PrivilageDomainDO> implements PrivilageDomainService {

    /**
     * 店铺 mapper
     */
    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;
    
    /**
     * 域 mapper
     */
    @Autowired
    private PrivilageDomainDOMapper privilageDomainDOMapper;

    /**
     * 用户域 mapper
     */
    @Autowired
    private PrivilageUserDomainDOMapper privilageUserDomainDOMapper;

    /**
     * 域实体 mapper
     */
    @Autowired
    private PrivilageDomainEntityDOMapper privilageDomainEntityDOMapper;

    /**
     * 用户 service
     */
    @Autowired
    private PrivilageUserService privilageUserService;

    /**
     * 角色 mapper
     */
    @Autowired
    private PrivilageRoleDOMapper privilageRoleDOMapper;

    @Override
    public void saveUserDomain(long userId, long domainId) {
        PrivilageUserDomainDO privilageUserDomainDO = new PrivilageUserDomainDO();
        privilageUserDomainDO.setUserId(userId);
        privilageUserDomainDO.setDomainId(domainId);
        privilageUserDomainDO.setCreateTime(new Date());
        //保存用户域
        privilageUserDomainDOMapper.insert(privilageUserDomainDO);

    }

    @Override
    public void removeUserDomain(long userId) {
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("user_id", userId);
        privilageUserDomainDOMapper.deleteByMap(deleteMap);
    }

    @Override
    public void removeUserEntity(Long userId) {
        privilageUserService.removeUserShopRelation(userId);
        removeUserDomain(userId);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void saveAllDate(Long userId, long roleId) {
        PrivilageDomainDO privilageDomainDO = privilageDomainDOMapper.selectDomain(roleId, DataConstants.DOMAIN_STATUS_NORMAL, DataConstants.DOMAIN_ENTITY_TYPE_ROLE);
        //新建域
        if(privilageDomainDO == null) {
            PrivilageDomainDO insertDomainBean = new PrivilageDomainDO();
            insertDomainBean.setName(DomainEnum.DOMAIN_ALL_SHOP.getDomainName());
            insertDomainBean.setStatus(DataConstants.DOMAIN_STATUS_NORMAL);
            insertDomainBean.setUpdateTime(new Date());
            privilageDomainDOMapper.insert(insertDomainBean);

            //新建角色域关系
            PrivilageDomainEntityDO privilageDomainEntityDO = new PrivilageDomainEntityDO();
            privilageDomainEntityDO.setDomainId(insertDomainBean.getId());
            privilageDomainEntityDO.setEntityId(roleId);
            privilageDomainEntityDO.setType(DataConstants.DOMAIN_ENTITY_TYPE_ROLE);
            privilageDomainEntityDO.setCreateTime(new Date());
            privilageDomainEntityDOMapper.insert(privilageDomainEntityDO);
            //回调
            saveAllDate(userId, roleId);
            return;
        }
        Long domainId = privilageDomainDO.getId();
        //维护店铺域关系
        List<Long> shopIdList = poiStoreStaffDOMapper.selectAllShopId(null, null);
        //实体域关系
        List<Long> entityIdList = privilageDomainDOMapper.selectEntityByDomainId(domainId, DataConstants.DOMAIN_ENTITY_TYPE_SHOP);
        //需要删除的关系
        List<Long> removeList = new ArrayList<>(entityIdList);
        //需要新增的关系
        List<Long> addList = new ArrayList<>(shopIdList);

        removeList.removeAll(shopIdList);
        addList.removeAll(entityIdList);
        //删除
        if(removeList.size() > 0) {
            privilageDomainDOMapper.removeDomainEntity(removeList, domainId, DataConstants.DOMAIN_ENTITY_TYPE_SHOP);
        }
        //新增
        List<PrivilageDomainEntityDO> addPrivilageDomainEntityList = new ArrayList<>();
        addList.forEach(entityId -> {
            PrivilageDomainEntityDO bean = new PrivilageDomainEntityDO();
            bean.setDomainId(domainId);
            bean.setEntityId(entityId);
            bean.setType(DataConstants.DOMAIN_ENTITY_TYPE_SHOP);
            bean.setCreateTime(new Date());
            addPrivilageDomainEntityList.add(bean);
        });
        if(addPrivilageDomainEntityList.size() > 0) {
            privilageDomainDOMapper.addDomainEntity(addPrivilageDomainEntityList);
        }
        //新增用户域
        PrivilageUserDomainDO privilageUserDomainDO = new PrivilageUserDomainDO();
        privilageUserDomainDO.setUserId(userId);
        privilageUserDomainDO.setDomainId(domainId);
        privilageUserDomainDO.setCreateTime(new Date());
        privilageUserDomainDOMapper.insert(privilageUserDomainDO);
    }

    @Override
    public void saveOneShop(long userId,String staffCode, long shopId,Long type) {
        privilageUserService.saveUserShopRelation(userId,staffCode,shopId,type);
    }

    @Override
    public List<Long> listUserDatePermission(Long userId) {
        List<Long> shopIdList = new ArrayList<>();
        PrivilageRoleDO privilageRoleDO = privilageRoleDOMapper.selectRoleByUserId(userId);
        if(privilageRoleDO == null) return shopIdList;
        //店长
        if(privilageRoleDO.getCode().equals(RoleEnum.ROLE_CODE_DZ.getRoleCode())) {
            shopIdList = poiStoreStaffDOMapper.selectByUserId(userId);
        }
        //运营人员
        else if (privilageRoleDO.getCode().equals(RoleEnum.ROLE_CODE_YYRY.getRoleCode())) {
            shopIdList = privilageDomainEntityDOMapper.selectEntityByUserId(userId, DataConstants.DOMAIN_ENTITY_TYPE_SHOP);
        }
        //管理员
        else if (privilageRoleDO.getCode().equals(RoleEnum.ROLE_CODE_GLY.getRoleCode())) {
            shopIdList = poiStoreStaffDOMapper.selectAllShopId(null,null);
        }
        return shopIdList;
    }


}
