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.TsCustomerStatisticMonthly;
import com.huiju.estateagents.statistic.mapper.TsCustomerStatisticMonthlyMapper;
import com.huiju.estateagents.statistic.service.ITsCustomerStatisticMonthlyService;
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 TsCustomerStatisticMonthlyServiceImpl extends ServiceImpl<TsCustomerStatisticMonthlyMapper, TsCustomerStatisticMonthly> implements ITsCustomerStatisticMonthlyService {
	
	@Autowired
	private TsCustomerStatisticMonthlyMapper customerStatisticMonthlyMapper;
	
	/**
	 * 每月新增客户统计，每天都更新
	 *
	 * @param nowDate
	 */
	@Override
	public void newCustomerStatisticMonthly(LocalDateTime nowDate) {
		//获取当月新增客户总数
		List<TsCustomerStatisticMonthly> newCustomerMonthList = customerStatisticMonthlyMapper.getNewCustomerStatisticMonthly(nowDate,CommConstant.STATUS_NORMAL);
		//批量插入
		statisticMonthlySaveBatch(newCustomerMonthList,nowDate, CommConstant.CUSTOMER_TYPE_NEW);
	}
	
	/**
	 * 每月跟进客户统计，每天都更新
	 *
	 * @param nowDate
	 */
	@Override
	public void followUpStatisticMonthly(LocalDateTime nowDate) {
		//获取当月跟进客户总数
		List<TsCustomerStatisticMonthly> followUpMonthList = customerStatisticMonthlyMapper.getFollowUpStatisticMonthly(nowDate);
		statisticMonthlySaveBatch(followUpMonthList,nowDate, CommConstant.CUSTOMER_TYPE_FOLLOW);
	}
	
	/**
	 * 每月访客客户统计，每天都更新
	 *
	 * @param nowDate
	 */
	@Override
	public void visiteStatisticMonthly(LocalDateTime nowDate) {
		//获取当月访问客户总数
		List<TsCustomerStatisticMonthly> visiteMonthList = customerStatisticMonthlyMapper.getVisiteStatisticMonthly(nowDate,CommConstant.STATUS_NORMAL);
		List<TsCustomerStatisticMonthly> newVisiteMonthList = new ArrayList<>();
		visiteMonthList.forEach(e -> {
			if (null != e.getOrgId() && !e.getOrgId().equals("null")){
				newVisiteMonthList.add(e);
			}
		});
		statisticMonthlySaveBatch(newVisiteMonthList,nowDate, CommConstant.CUSTOMER_TYPE_VISITE);
	}
	
	/**
	 * 查询12个月的客户统计分析数据
	 *
	 * @param customerType
	 * @param orgId
	 * @param personId
	 * @param userId
	 * @return
	 */
	@Override
	public List<TsCustomerStatisticMonthly> getCustomerMonthlyList(String customerType, Integer orgId, String personId, Integer userId) {
		//当前日期
		LocalDateTime nowDate = LocalDateTime.now();
		//12个月前的日期
		LocalDateTime beforeDate = nowDate.plusMonths(-12);
		//获取12个月数据
		List<TsCustomerStatisticMonthly> list = customerStatisticMonthlyMapper.getMonthlyList(customerType,orgId,nowDate,beforeDate,personId,userId);
		//构造12个月的数据
		List<TsCustomerStatisticMonthly> newList = new ArrayList<>();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
		for (int i = 0; i < 12;i++){
			String newDate = nowDate.plusMonths(-i).format(formatter);
			TsCustomerStatisticMonthly tsCustomerStatisticMonthly = new TsCustomerStatisticMonthly();
			tsCustomerStatisticMonthly.setCustomerType(customerType);
			tsCustomerStatisticMonthly.setMonth(newDate);
			tsCustomerStatisticMonthly.setCustomerNum(0);
			tsCustomerStatisticMonthly.setOrgId(orgId);
			tsCustomerStatisticMonthly.setRealtyConsultant(personId);
			list.forEach(l -> {
				if (newDate.equals(l.getMonth())){
					tsCustomerStatisticMonthly.setCustomerNum(tsCustomerStatisticMonthly.getCustomerNum() + l.getCustomerNum());
				}
			});
			newList.add(tsCustomerStatisticMonthly);
		}
		
		return newList;
	}
	
	/**
	 * 批量保存当月的统计数据
	 * @param list
	 * @param nowDate
	 * @param customerType
	 */
	private void statisticMonthlySaveBatch(List<TsCustomerStatisticMonthly> list, LocalDateTime nowDate, String customerType) {
		//格式化年月
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
		String todayDate = nowDate.format(formatter);
		
		//先删除在添加
		QueryWrapper<TsCustomerStatisticMonthly> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("customer_type",customerType);
		queryWrapper.eq("month",todayDate);
		customerStatisticMonthlyMapper.delete(queryWrapper);
		
		list.forEach(e -> {
			e.setCreateDate(nowDate);
			e.setCustomerType(customerType);
			e.setMonth(todayDate);
		});
		//批量保存
		this.saveBatch(list);
	}
}
