package com.kiisoo.ic.store.service;

import com.kiisoo.ic.domain.service.PrivilageDomainService;
import com.kiisoo.ic.generalize.service.RetailCompanyService;
import com.kiisoo.ic.store.bean.PoiStoreStaffDTO;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 导购
 */
@Service
public class PoiSellerService {

    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    @Autowired
    private PoiStoreService poiStoreService;

    @Autowired
    private PrivilageDomainService privilageDomainService;

    @Autowired
    private RetailCompanyService retailCompanyService;
    /**
     * 找到店铺id中所有的导购
     * @param shopId 店铺id
     */
    public List<PoiStoreStaffDTO> getShopSeller(List<Long> shopId){
        List<PoiStoreStaffDTO> sellers = poiStoreStaffDOMapper.selectSellersByShopIds(shopId);
        return sellers;
    }

    /**
     * 找到区域或者店铺中所有的导购
     */
    public List<PoiStoreStaffDTO> getAllSeller(Long regionId, Long shopId, long userId, Long companyId){
        List<Long> shopIds = new ArrayList<>();
        //先找到店铺
        if(null != shopId){
            //有店铺就用店铺
            shopIds.add(shopId);
        }else if(regionId != null){
            //有区域就查区域店铺
            shopIds = privilageDomainService.listUserDatePermission(userId);
            List<PoiStore> stores = poiStoreService.getRegionShop(regionId);
            List<Long> regionShopIds = stores.stream().map(i -> i.getId()).collect(Collectors.toList());
            shopIds.retainAll(regionShopIds);
        }else if(companyId != null){
            //有公司就查公司店铺
            shopIds = privilageDomainService.listUserDatePermission(userId);
            List<PoiStore> stores = retailCompanyService.selectShopByCompany(companyId);
            List<Long> regionShopIds = stores.stream().map(i -> i.getId()).collect(Collectors.toList());
            shopIds.retainAll(regionShopIds);
        }else{
            //什么都没有就全查
            shopIds = privilageDomainService.listUserDatePermission(userId);
        }
        List<PoiStoreStaffDTO> sellers = poiStoreStaffDOMapper.selectSellersByShopIds(shopIds);
        return sellers;
    }

}
