package com.huiju.estateagents.property.task;

import com.huiju.estateagents.property.model.TaTemporaryLicenseCount;
import com.huiju.estateagents.property.model.TaUserLicenseCount;
import com.huiju.estateagents.property.model.TpBillInvoiceCount;
import com.huiju.estateagents.property.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * @author FXF
 * @date 2019-05-28
 */
@Component
@Slf4j
public class IndexCountTasks {
	
	
	@Autowired
	private IBillInvoiceService billInvoiceService;
	
	@Autowired
	private ITpBillInvoiceCountService tpBillInvoiceCountService;
	
	@Autowired
	private ITaUserLicenseOrderService userLicenseOrderService;
	
	@Autowired
	private ITaUserLicenseCountService taUserLicenseCountService;
	
	@Autowired
	private ITaTemporaryLicenseCountService taTemporaryLicenseCountService;
	
	/**
	 * 统计定时任务，每天23点执行。
	 */
	@Scheduled(cron = "0 0 23 * * ?")
	public void billInvoice() {
		log.info("定时任务，收费组总缴费统计,月租车总续费统计,临时车总缴费统计。开始。每天23点执行");
		log.info("收费组总缴费统计");
		//收费组总缴费统计,查询所有的金额并插入统计表
		List<Map<String,Object>> priceList =  billInvoiceService.getPriceCount();
		//插入统计表
		List<TpBillInvoiceCount> countList = new ArrayList<>();
		LocalDateTime nowTime = LocalDateTime.now();
		for (Map<String,Object> map : priceList){
			TpBillInvoiceCount tpBillInvoiceCount = new TpBillInvoiceCount();
			tpBillInvoiceCount.setCommunityId(Integer.valueOf(map.get("communityId").toString()));
			String totalPrice = String.valueOf(map.get("totalPrice"));
			int index = totalPrice.indexOf(".");
			if (index > 0) {
				totalPrice = totalPrice.substring(0,index);
			}
			tpBillInvoiceCount.setPriceTotal(Integer.valueOf(totalPrice));
			tpBillInvoiceCount.setCreateDate(nowTime);
			countList.add(tpBillInvoiceCount);
		}
		tpBillInvoiceCountService.saveBatch(countList);
		
		log.info("月租车总续费统计");
		//月租车总续费统计,查询所有的金额并插入统计表
		List<Map<String,Object>> licenseOrderPriceList = userLicenseOrderService.getPriceCount();
		//插入统计表
		List<TaUserLicenseCount> LicenseCountList = new ArrayList<>();
		for (Map<String,Object> map : licenseOrderPriceList){
			TaUserLicenseCount taUserLicenseCount = new TaUserLicenseCount();
			taUserLicenseCount.setCommunityId(Integer.valueOf(map.get("communityId").toString()));
			String totalPrice = String.valueOf(map.get("totalPrice"));
			int index = totalPrice.indexOf(".");
			if (index > 0) {
				totalPrice = totalPrice.substring(0,index);
			}
			taUserLicenseCount.setPriceTotal(Integer.valueOf(totalPrice));
			taUserLicenseCount.setCreateDate(nowTime);
			LicenseCountList.add(taUserLicenseCount);
		}
		taUserLicenseCountService.saveBatch(LicenseCountList);
		
		log.info("临时车总缴费统计");
		//临时车总缴费统计,查询所有的金额并插入统计表
		List<Map<String,Object>> temporarylicenseOrderPriceList = taTemporaryLicenseCountService.getTemporaryLicenseOrderPriceCount();
		//插入统计表
		List<TaTemporaryLicenseCount> temporaryLicenseCountList = new ArrayList<>();
		for (Map<String,Object> map : licenseOrderPriceList){
			TaTemporaryLicenseCount temporaryLicenseCount = new TaTemporaryLicenseCount();
			temporaryLicenseCount.setCommunityId(Integer.valueOf(map.get("communityId").toString()));
			String totalPrice = String.valueOf(map.get("totalPrice"));
			int index = totalPrice.indexOf(".");
			if (index > 0) {
				totalPrice = totalPrice.substring(0,index);
			}
			temporaryLicenseCount.setPriceTotal(Integer.valueOf(totalPrice));
			temporaryLicenseCount.setCreateDate(nowTime);
			temporaryLicenseCountList.add(temporaryLicenseCount);
		}
		taTemporaryLicenseCountService.saveBatch(temporaryLicenseCountList);
	}
}
