package com.kiisoo.ic.store.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

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

    @Autowired
    private PoiStoreDOMapper poiStoreDOMapper;

    @Autowired
    private PrivilageDomainService privilageDomainService;

    @Autowired
    private RetailCompanyService retailCompanyService;
    /**
     * 根据区域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,Long regionId, Long companyId){
        List<PoiStore> poiStores = new ArrayList<>();
        //找到店铺集合
        List<Long> shopsIds = privilageDomainService.listUserDatePermission(userId);
        if(null != regionId){
            List<PoiStore> regionPoiStores = poiStoreDOMapper.selectRegionShop(regionId);
            for(PoiStore item : regionPoiStores){
                if(shopsIds.contains(item.getId())){
                    poiStores.add(item);
                }
            }
        }else if(companyId != null){
            List<PoiStore> regionPoiStores = retailCompanyService.selectShopByCompany(companyId);
            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;
    }
}
