package com.kiisoo.ic.store.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kiisoo.ic.customer.service.CustomerViewService;
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.entity.PoiStoreStaff;
import com.kiisoo.ic.store.mapper.PoiStoreDOMapper;
import com.kiisoo.ic.store.mapper.PoiStoreStaffDOMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 店铺
 */
@Service
public class PoiStoreService {

    @Autowired
    private PoiStoreDOMapper poiStoreDOMapper;

    @Autowired
    private PrivilageDomainService privilageDomainService;

    @Autowired
    private RetailCompanyService retailCompanyService;

    @Autowired
    private CustomerViewService customerViewService;
    /**
     * 根据区域id找到店铺集合
     * @param regionId
     */
    public List<PoiStore> getRegionShop(long regionId){
        List<PoiStore> poiStores = poiStoreDOMapper.selectRegionShop(regionId);
        return poiStores;
    }

    /**
     * 根据区域id找到店铺集合，若没有，则查用户所有店铺
     * @param regionId
     */
    public List<PoiStore> getRegionShop(long userId,List<Long> customerIds){
        List<PoiStore> poiStores = new ArrayList<>();
        //找到店铺集合
        List<Long> shopsIds = privilageDomainService.listUserDatePermission(userId);
        if(null != customerIds && customerIds.size()>0){
            Set<Long> shopIds = new HashSet<>();
            customerIds.forEach(customerId -> {
                Set<Long> shops = customerViewService.getCompanyShop(customerId,userId);
                if (CollectionUtils.isNotEmpty(shops)){
                    shopIds.addAll(shops);
                }
            });
            if(shopIds.size() > 0){
                List<PoiStore> regionPoiStores = poiStoreDOMapper.selectBatchIds(shopIds);
                for(PoiStore item : regionPoiStores){
                    if(shopsIds.contains(item.getId())){
                        poiStores.add(item);
                    }
                }
            }
        }else{
            poiStores = poiStoreDOMapper.selectBatchIds(shopsIds);
        }
        return poiStores;
    }

    /**
     * 查询所有店铺
     *
     * @return
     */
    public List<PoiStore> listAllShop() {
        List<PoiStore> baseShopDOS = poiStoreDOMapper.selectList(null);
        return baseShopDOS;
    }
}
