package org.springblade.common.constant;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.ldt.agent.entity.AgentRecord;
import org.springblade.ldt.agent.service.IAgentRecordService;
import org.springblade.ldt.mall.entity.Mall;
import org.springblade.ldt.mall.service.IMallService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: Silent
 * @Description
 * @Date: Created in 14:59 2021/10/12
 * @Modified By:
 */
public interface Salesman {
	//角色名称
	String roleName = "salesman";

	//租户ID
	String tenantId = "000000";

	//二维码推广页面键值
	String EXTEND_QRCODE_PAGE = "extend.qrcode.page";

	IShopService shopService = SpringUtil.getBean(IShopService.class);

	IMallService mallService = SpringUtil.getBean(IMallService.class);

	IAgentRecordService agentRecordService = SpringUtil.getBean(IAgentRecordService.class);

	/**
	 * 判断是否是业务员
	 * @param bladeUser
	 * @return
	 */
	static boolean isSalesman(BladeUser bladeUser) {
		return StringUtils.equals(bladeUser.getTenantId(), tenantId) && StringUtils.equals(bladeUser.getRoleName(), roleName);
	}

	/**
	 * 获取业务员推广商户、商场、代理ID
	 * @return
	 */
	static Map<Class, List<Long>> getSalesmanPromoteIdToMap(BladeUser bladeUser){
		HashMap<Class, List<Long>> classListHashMap = new HashMap<>();
		classListHashMap.put(Shop.class,getSalesmanPromoteIdToListByShop(bladeUser));
		classListHashMap.put(Mall.class,getSalesmanPromoteIdToListByMall(bladeUser));
		classListHashMap.put(AgentRecord.class,getSalesmanPromoteIdToListByAgentRecord(bladeUser));
		return classListHashMap;
	}

	/**
	 * 获取业务员推广ID
	 * @param bladeUser
	 * @return
	 */
	static List<Long> getSalesmanPromoteIdToList(BladeUser bladeUser){
		Map<Class, List<Long>> salesmanPromoteId = getSalesmanPromoteIdToMap(bladeUser);
		LinkedList<Long> longs = new LinkedList<>();
		for (Map.Entry<Class, List<Long>> classListEntry : salesmanPromoteId.entrySet()) {
			longs.addAll(classListEntry.getValue());
		}
		return longs;
	}

	/**
	 * 获取业务员推广ID
	 * @param bladeUser
	 * @return
	 */
	static List<Long> getSalesmanPromoteIdToListByShop(BladeUser bladeUser){
		return shopService.list(Wrappers.<Shop>lambdaQuery().eq(Shop::getSalesmanId, bladeUser.getUserId())).stream().map(Shop::getId).collect(Collectors.toList());
	}

	/**
	 * 获取业务员推广ID
	 * @param bladeUser
	 * @return
	 */
	static List<Long> getSalesmanPromoteIdToListByMall(BladeUser bladeUser){
		return mallService.list(Wrappers.<Mall>lambdaQuery().eq(Mall::getSalesmanId, bladeUser.getUserId())).stream().map(Mall::getId).collect(Collectors.toList());
	}

	/**
	 * 获取业务员推广ID
	 * @param bladeUser
	 * @return
	 */
	static List<Long> getSalesmanPromoteIdToListByAgentRecord(BladeUser bladeUser){
		return agentRecordService.list(Wrappers.<AgentRecord>lambdaQuery().eq(AgentRecord::getSalesmanId, bladeUser.getUserId())).stream().map(AgentRecord::getAccountId).collect(Collectors.toList());
	}
}
