package cn.com.countrygarden.bi.main.service.twerp.hm.impl;

import cn.com.countrygarden.bi.main.core.AbstractService;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbHsprFeesdetailMapper;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbHsprFeesdetail;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbHsprFeesdetailService;
import cn.com.countrygarden.bi.main.util.RedisCacheUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

@Service
@Transactional
public class TbHsprFeesdetailServiceImpl extends AbstractService<TbHsprFeesdetail> implements TbHsprFeesdetailService {
	@Resource
	private TbHsprFeesdetailMapper mapper;

	public TbHsprFeesdetail getFirstFeesOfDay() {
		return mapper.getfirstFeesOfDay();
	}

	public TbHsprFeesdetail getFirstFeesOfDayFromMapper() {
		return mapper.getfirstFeesOfDay();
	}

	/**
	 * 每天第一条缴费数据
	 *
	 * @return
	 */
	public Map<String, Object> getFirstFeesOfDayFromCache() {
		Map<String, String> param = new HashMap<>();
		Function<Map<String, String>, Map<String, Object>> function = p -> getFirstFeesOfDayFromMapperToMap(p);

		Map<String, Object> resource = RedisCacheUtils.rememberMap("biapp:hm:hspr:fees:first:daily",
				43200 /* 12H: 60 * 60 * 12 */, param, function);

		return resource;
	}

	/**
	 * 解析成 Map 以便于 redis 保存、解析
	 *
	 * @param param
	 * @return
	 */
	private Map<String, Object> getFirstFeesOfDayFromMapperToMap(Map<String, String> param) {
		Map<String, Object> data = new HashMap<>();
		TbHsprFeesdetail feesdetail = getFirstFeesOfDayFromMapper();

		if (feesdetail == null) {
			return data;
		}

		data.put("organName", feesdetail.getOrganName());
		data.put("commName", feesdetail.getCommName());
		data.put("custName", feesdetail.getCustName());
		data.put("mobilePhone", feesdetail.getMobilePhone());
		data.put("chargeMode", feesdetail.getChargemode());
		data.put("chargeAmount", feesdetail.getChargeamount());
		data.put("lateFeeAmount", feesdetail.getLatefeeamount());
		data.put("chargeDate", formatDate(feesdetail.getChargedate()));
		data.put("content", "【本日首缴】" + feesdetail.getOrganName() + feesdetail.getCommName()
				+ feesdetail.getCustName().substring(0, 1) + "**缴纳" + feesdetail.getChargeamount() + "元");
		data.put("content", "【本日首缴】" + feesdetail.getOrganName() + feesdetail.getCommName()
				+ feesdetail.getCustName().substring(0, 1) + "**缴纳" + feesdetail.getChargeamount() + "元");

		return data;
	}

	/**
	 * 日期格式化
	 *
	 * @param date
	 * @return
	 */
	private String formatDate(Date date) {
		String dateString;
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateString = dateFormat.format(date);
		return dateString;
	}

	/**
	 * 从缓存中获取昨天第一笔收入和最大一笔收入的记录
	 */
	@Override
	public Map<String, Object> getFirstFeesAndMaxFeesOfLastDayFromCache() {
		Map<String, String> param = new HashMap<>();
		Function<Map<String, String>, Map<String, Object>> function = p -> getFirstFeesAndMaxFeesOfLastDayFromMapperToMap(
				p);

		Map<String, Object> resource = RedisCacheUtils.rememberMap("biapp:hm:hspr:fees:first:daily",
				43200 /* 12H: 60 * 60 * 12 */, param, function);

		return resource;
	}

	private Map<String, Object> getFirstFeesAndMaxFeesOfLastDayFromMapperToMap(Map<String, String> p) {
		Map<String, Object> data = new HashMap<>();
		TbHsprFeesdetail feesdetail = getFirstFeesOfLastDayFromMapper();
		TbHsprFeesdetail lastDateFeesdetail = getMaxFeesOfLastDayFromMapper();

		if (feesdetail == null) {
			return data;
		}

		data.put("organName", feesdetail.getOrganName());
		data.put("commName", feesdetail.getCommName());
		data.put("custName", feesdetail.getCustName());
		data.put("mobilePhone", feesdetail.getMobilePhone());
		data.put("chargeMode", feesdetail.getChargemode());
		data.put("chargeAmount", feesdetail.getChargeamount());
		data.put("lateFeeAmount", feesdetail.getLatefeeamount());
		data.put("chargeDate", formatDate(feesdetail.getChargedate()));
		data.put("content", "【本日首缴】" + feesdetail.getOrganName() + feesdetail.getCommName()
				+ feesdetail.getCustName().substring(0, 1) + "**缴纳" + feesdetail.getChargeamount() + "元");
		data.put("content", "【本日首缴】" + feesdetail.getOrganName() + feesdetail.getCommName()
				+ feesdetail.getCustName().substring(0, 1) + "**缴纳" + feesdetail.getChargeamount() + "元");

		return data;
	}

	/**
	 * 从数据库中获取昨天第一笔收入的记录
	 * 
	 */
	@Override
	public TbHsprFeesdetail getFirstFeesOfLastDayFromMapper() {

		return null;
	}

	/**
	 * 从数据库中获取昨天最大一笔收入的记录
	 * 
	 * @return
	 */
	@Override
	public TbHsprFeesdetail getMaxFeesOfLastDayFromMapper() {

		return null;
	}
}
