package com.kiisoo.ic.region.service;

import com.kiisoo.ic.common.DataConstants;
import com.kiisoo.ic.domain.service.PrivilageDomainService;
import com.kiisoo.ic.region.entity.RegionDO;
import com.kiisoo.ic.region.mapper.RegionDOMapper;
import com.kiisoo.ic.store.bean.PoiStoreStaffDTO;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域
 */
@Service
public class RegionService {

    @Autowired
    private RegionDOMapper regionDOMapper;

    @Autowired
    private PrivilageRoleDOMapper privilageRoleDOMapper;

    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    @Autowired
    private PoiStoreDOMapper poiStoreDOMapper;

    @Autowired
    private PrivilageDomainService privilageDomainService;
    /**
     * 找到用户的店铺和区域
     * @param userId 用户id
     * @return map key：shop,region value: regiondos, shopIds
     */
    public Map<String, Object> getRegionAndShop(long userId){

        //找到区域集合
        List<RegionDO> regionDOS = getUserRegion(userId);
        //找到店铺集合
        List<Long> shopsIds = privilageDomainService.listUserDatePermission(userId);
        List<PoiStore> shops = poiStoreDOMapper.selectBatchIds(shopsIds);
        //找到店铺对应的导购
        List<PoiStoreStaffDTO> sellers = poiStoreStaffDOMapper.selectSellersByShopIds(shopsIds);

        Map<String, Object> regionAndShop = new HashMap<>();
        regionAndShop.put("region", regionDOS);
        regionAndShop.put("shop", shops);
        regionAndShop.put("seller", sellers);
        return regionAndShop;
    }

    /**
     * 根据用户id找到对应的区域
     */
    public List<RegionDO> getUserRegion(long userId){

        List<RegionDO> userRegions = new ArrayList<>();
        //找到用户所属角色
        PrivilageRoleDO privilageRoleDO = privilageRoleDOMapper.selectRoleByUserId(userId);

        //店长
        if(privilageRoleDO.getCode().equals(RoleEnum.ROLE_CODE_DZ.getRoleCode())) {
            //找到店铺所属区域
            List<Long> shopIdList = poiStoreStaffDOMapper.selectByUserId(userId);
            userRegions = getShopRegion(shopIdList);
        }
        //运营人员
        else if (privilageRoleDO.getCode().equals(RoleEnum.ROLE_CODE_YYRY.getRoleCode())) {
            //找到所有区域
            userRegions = getAllRegion();
        }

        return userRegions;
    }

    /**
     * 根据店铺找到所属区域
     * @param shopIds 店铺集合
     * @return
     */
    public List<RegionDO> getShopRegion(List<Long> shopIds){
        List<RegionDO> userRegions = regionDOMapper.selectRegionsByShopId(shopIds);
        return userRegions;
    }

    /**
     * 根据用户找到所属区域
     * @return
     */
    public List<RegionDO> getAllRegion(){
        List<RegionDO> userRegions = regionDOMapper.selectList(null);
        return userRegions;
    }

}
