package com.huiju.estateagents.statistic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.statistic.entity.GenderStatistic;
import com.huiju.estateagents.statistic.entity.TsCustomerGenderStatistic;
import com.huiju.estateagents.statistic.mapper.TsCustomerGenderStatisticMapper;
import com.huiju.estateagents.statistic.service.ITsCustomerGenderStatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户分析性别统计表 小程序盘客工具客户分析 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-11-08
 */
@Service
public class TsCustomerGenderStatisticServiceImpl extends ServiceImpl<TsCustomerGenderStatisticMapper, TsCustomerGenderStatistic> implements ITsCustomerGenderStatisticService {
	
	@Autowired
	private TsCustomerGenderStatisticMapper customerGenderStatisticMapper;
	
	/**
	 * 每天的新增客户性别统计
	 *
	 * @param nowDate
	 */
	@Override
	public void newCustomerSexStatisticDaily(LocalDateTime nowDate) {
		//获取新增客户的男女性别比和性别数
		List<GenderStatistic> newCustomerSexList = customerGenderStatisticMapper.getNewCustomerSexCount(nowDate);
		//批量保存
		saveBatchCustomerSexData(newCustomerSexList,nowDate,CommConstant.CUSTOMER_TYPE_NEW);
	}
	
	/**
	 * 每天的跟进客户性别统计
	 *
	 * @param nowDate
	 */
	@Override
	public void followUpSexStatisticDaily(LocalDateTime nowDate) {
		//获取跟进客户的男女性别比和性别数
		List<GenderStatistic> followUpSexList = customerGenderStatisticMapper.getFollowUpSexCount();
		//批量保存
		saveBatchCustomerSexData(followUpSexList,nowDate,CommConstant.CUSTOMER_TYPE_FOLLOW);
	}
	
	/**
	 * 每天的到访客户性别统计
	 *
	 * @param nowDate
	 */
	@Override
	public void visiteSexStatisticDaily(LocalDateTime nowDate) {
		//获取到访客户的男女性别比和性别数
		List<GenderStatistic> visiteSexList = customerGenderStatisticMapper.getVisiteSexCount();
		List<GenderStatistic> newVisiteSexList = new ArrayList<>();
		visiteSexList.forEach(e -> {
			if (null != e.getOrgId() && !e.getOrgId().equals("null")){
				newVisiteSexList.add(e);
			}
		});
		//批量保存
		saveBatchCustomerSexData(newVisiteSexList,nowDate,CommConstant.CUSTOMER_TYPE_VISITE);
	}
	
	/**
	 * 批量保存性别数据
	 * @param customerSexList
	 * @param nowDate
	 */
	private void saveBatchCustomerSexData(List<GenderStatistic> customerSexList, LocalDateTime nowDate,String customerType) {
		//先删除在添加
		QueryWrapper<TsCustomerGenderStatistic> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("customer_type",customerType);
		customerGenderStatisticMapper.delete(queryWrapper);
		
		List<TsCustomerGenderStatistic> list = new ArrayList<>();
		//划分男女未知三条数据
		customerSexList.forEach(e -> {
			TsCustomerGenderStatistic maleCustomer = new TsCustomerGenderStatistic();
			maleCustomer.setCreateDate(nowDate);
			maleCustomer.setGenderType(CommConstant.SEX_MALE);
			maleCustomer.setCustomerType(customerType);
			maleCustomer.setCustomerNum(e.getManSum());
			maleCustomer.setPercentage(e.getManPct());
			maleCustomer.setOrgId(e.getOrgId());
			maleCustomer.setRealtyConsultant(e.getRealtyConsultant());
			list.add(maleCustomer);
			
			TsCustomerGenderStatistic femaleCustomer = new TsCustomerGenderStatistic();
			femaleCustomer.setCreateDate(nowDate);
			femaleCustomer.setGenderType(CommConstant.SEX_FEMALE);
			femaleCustomer.setCustomerType(customerType);
			femaleCustomer.setCustomerNum(e.getWomanSum());
			femaleCustomer.setPercentage(e.getWomanPct());
			femaleCustomer.setOrgId(e.getOrgId());
			femaleCustomer.setRealtyConsultant(e.getRealtyConsultant());
			list.add(femaleCustomer);
			
			TsCustomerGenderStatistic unknownCustomer = new TsCustomerGenderStatistic();
			unknownCustomer.setCreateDate(nowDate);
			unknownCustomer.setGenderType(CommConstant.SEX_UNKNOWN);
			unknownCustomer.setCustomerType(customerType);
			unknownCustomer.setCustomerNum(e.getUnknownSum());
			unknownCustomer.setPercentage(e.getUnknownPct());
			unknownCustomer.setOrgId(e.getOrgId());
			unknownCustomer.setRealtyConsultant(e.getRealtyConsultant());
			list.add(unknownCustomer);
		});
		//批量插入
		this.saveBatch(list);
	}
}
