package com.kiisoo.ic.store.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kiisoo.ic.activity.entity.PmnActivityInstance;
import com.kiisoo.ic.activity.entity.PmnPromotionSrc;
import com.kiisoo.ic.activity.service.IPmnActivityInstanceService;
import com.kiisoo.ic.activity.service.IPmnPromotionSrcService;
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.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.Set;
import java.util.stream.Collectors;

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

    @Autowired
    private PoiStoreStaffDOMapper poiStoreStaffDOMapper;

    @Autowired
    private PoiStoreService poiStoreService;

    @Autowired
    private PrivilageDomainService privilageDomainService;

    @Autowired
    private CustomerViewService customerViewService;
    @Autowired
    private IPmnActivityInstanceService pmnActivityInstanceService;
    @Autowired
    private IPmnPromotionSrcService pmnPromotionSrcService;

    /**
     * 找到店铺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.addAll(customerViewService.getCompanyShop(companyId, userId));
        }
//        else if(customers != null){
//            for(Long cp : customers){
//                //有公司就查公司店铺
//                shopIds.addAll(customerViewService.getCompanyShop(cp,userId));
//            }
//
//        }
        else {
            //什么都没有就全查
            shopIds = privilageDomainService.listUserDatePermission(userId);
        }
        List<PoiStoreStaffDTO> sellers = poiStoreStaffDOMapper.selectSellersByShopIds(shopIds);
        for (PoiStoreStaffDTO seller : sellers) {
            List<PmnPromotionSrc> srcs = pmnPromotionSrcService.list(Wrappers.<PmnPromotionSrc>lambdaQuery().eq(PmnPromotionSrc::getPromoterId, seller.getUserId()).eq(PmnPromotionSrc::getType, 1).eq(PmnPromotionSrc::getStatus, 1).select(PmnPromotionSrc::getInstanceId));
            List<Long> instanceIds = srcs.stream().map(PmnPromotionSrc::getInstanceId).collect(Collectors.toList());
            if(instanceIds.size() > 0) {
                List<PmnActivityInstance> instances = pmnActivityInstanceService.listByIds(instanceIds);
                seller.setInstanceList(instances);
            }
        }
        return sellers;
    }

}
