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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.statistic.entity.TsCustomerStatisticDaily;
import com.huiju.estateagents.statistic.mapper.TsCustomerStatisticDailyMapper;
import com.huiju.estateagents.statistic.service.ITsCustomerStatisticDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 客户分析日统计表 小程序盘客工具客户分析 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-11-08
 */
@Service
public class TsCustomerStatisticDailyServiceImpl extends ServiceImpl<TsCustomerStatisticDailyMapper, TsCustomerStatisticDaily> implements ITsCustomerStatisticDailyService {
	
	@Autowired
	private TsCustomerStatisticDailyMapper customerStatisticDailyMapper;
	
	/**
	 * 每天的跟进客户统计
	 *
	 * @param nowDate
	 */
	@Override
	public void followUpStatisticDaily(LocalDateTime nowDate) {
		//按当前时间获取当天的跟进客户的数量，多一个客户进行多次跟进行为，只记录一次
		List<TsCustomerStatisticDaily> list = customerStatisticDailyMapper.getFollowUpData(nowDate);
		//批量保存
		statisticDailySaveBatch(list,nowDate,CommConstant.CUSTOMER_TYPE_FOLLOW);
	}
	
	/**
	 * 每天的新增客户统计
	 *
	 * @param nowDate
	 */
	@Override
	public void newCustomerStatisticDaily(LocalDateTime nowDate) {
		//按当前时间获取当天的新增客户的数量，ta_recommend_customer 表的status 为1 的就是新增客户
		List<TsCustomerStatisticDaily> list = customerStatisticDailyMapper.getNewCustomerData(nowDate,CommConstant.STATUS_NORMAL);
		//批量保存
		statisticDailySaveBatch(list,nowDate,CommConstant.CUSTOMER_TYPE_NEW);
	}
	
	/**
	 * 每天的到访客户统计
	 *
	 * @param nowDate
	 */
	@Override
	public void visiteStatisticDaily(LocalDateTime nowDate) {
		//按当前时间获取当天的到访客户的数量，同一天同一个项目只记录一次
		List<TsCustomerStatisticDaily> list = customerStatisticDailyMapper.getVisiteCustomerData(nowDate,CommConstant.STATUS_NORMAL);
		List<TsCustomerStatisticDaily> saveList = new ArrayList<>();
		list.forEach(e -> {
			if (null != e.getOrgId() && !e.getOrgId().equals("null")){
				saveList.add(e);
			}
		});
		//批量保存
		statisticDailySaveBatch(saveList,nowDate,CommConstant.CUSTOMER_TYPE_VISITE);
	}
	
	/**
	 * 获取日统计数据
	 *
	 * @param customerType
	 * @param orgId
	 * @param personId
	 * @param userId
	 * @return
	 */
	@Override
	public List<TsCustomerStatisticDaily> getDailyList(String customerType, Integer orgId, String personId, Integer userId) {
		//当前日期
		LocalDateTime nowDate = LocalDateTime.now();
		//31天前的日期
		LocalDateTime beforeDate = nowDate.plusDays(-31);
		//获取31天的日期数据
		List<TsCustomerStatisticDaily> list = customerStatisticDailyMapper.getDailyList(customerType,orgId,nowDate,beforeDate,personId,userId);
		//构造31天的数据
		List<TsCustomerStatisticDaily> newList = new ArrayList<>();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		for (int i = 0; i < 31;i++){
			String newDate = nowDate.plusDays(-i).format(formatter);
			TsCustomerStatisticDaily tsCustomerStatisticDaily = new TsCustomerStatisticDaily();
			tsCustomerStatisticDaily.setOrgId(orgId);
			tsCustomerStatisticDaily.setCustomerNum(0);
			tsCustomerStatisticDaily.setDay(newDate);
			tsCustomerStatisticDaily.setCustomerType(customerType);
			tsCustomerStatisticDaily.setRealtyConsultant(personId);
			
			//合并数据
			list.forEach(l -> {
				if (newDate.equals(l.getDay())){
					tsCustomerStatisticDaily.setCustomerNum(tsCustomerStatisticDaily.getCustomerNum() + l.getCustomerNum());
				}
			});
			newList.add(tsCustomerStatisticDaily);
		}
		
		return newList;
	}
	
	/**
	 * 批量保存当天的统计数据
	 * @param list
	 * @param nowDate
	 * @param customerType
	 */
	private void statisticDailySaveBatch(List<TsCustomerStatisticDaily> list, LocalDateTime nowDate, String customerType) {
		//格式化年月日
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		String todayDate = nowDate.format(formatter);
		
		//先删除当天数据在添加防止有脏数据
		customerStatisticDailyMapper.deleteNowDate(nowDate,customerType);
		
		list.forEach(e -> {
			e.setCreateDate(nowDate);
			e.setCustomerType(customerType);
			e.setDay(todayDate);
		});
		//批量保存
		this.saveBatch(list);
	}
}
