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

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.countrygarden.bi.main.core.AbstractService;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbDeskTopFeesDailyMapper;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbDeskTopFeesDaily;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbDeskTopFeesDailyService;
import cn.com.countrygarden.bi.main.util.RedisCacheUtils;

/**
 * 日收费统计
 *
 * @author huangzhengjun
 */
@Service
@Transactional
public class TbDeskTopFeesDailyServiceImpl extends AbstractService<TbDeskTopFeesDaily>
        implements TbDeskTopFeesDailyService {

    @Resource
    private TbDeskTopFeesDailyMapper mapper;

    @Override
    public TbDeskTopFeesDaily getFirstFeesOfLastDayFromMapper() {
        return mapper.getFirstFeesOfLastDay();
    }

    @Override
    public TbDeskTopFeesDaily getMaxFeesOfLastDayFromMapper() {
        return mapper.getMaxFeesOfLastDay();
    }

    @Override
    public TbDeskTopFeesDaily getFirstFeesOfTodayFromMapper() {
        return mapper.getFirstFeesOfToday();
    }

    @Override
    public TbDeskTopFeesDaily getMaxFeesOfTodayFromMapper() {
        return mapper.getMaxFeesOfToday();
    }

    @Override
    public Map<String, Object> getFirstFeesAndMaxFeesOfLastDayFromCache() {
        Map<String, String> param = new HashMap<>();
        Function<Map<String, String>, Map<String, Object>> function = p -> getFirstFeesOfLastDayFromMapperToMap(p);

        Map<String, Object> resource = RedisCacheUtils.rememberMap("biapp:fees:lastdayfirst:daily",
                1200 /* 20min: 60 * 20 */, param, function);

        return resource;
    }

    @Override
    public Map<String, Object> getFirstFeesAndMaxFeesOfTodayFromCache() {
        Map<String, String> param = new HashMap<>();
        Function<Map<String, String>, Map<String, Object>> function = p -> getFirstFeesOfTodayFromMapperToMap(p);

        Map<String, Object> resource = RedisCacheUtils.rememberMap("biapp:fees:todayfirst:daily",
                1200 /* 20min: 60 * 20 */, param, function);

        return resource;
    }

    private Map<String, Object> getFirstFeesOfTodayFromMapperToMap(Map<String, String> p) {
        Map<String, Object> data = new HashMap<>();
        TbDeskTopFeesDaily todayFirstFeesdetail = getFirstFeesOfTodayFromMapper();
        TbDeskTopFeesDaily todayMaxFeesdetail = getMaxFeesOfTodayFromMapper();

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

        data.put("organName", todayFirstFeesdetail.getOrganName());
        data.put("commName", todayFirstFeesdetail.getCommName());
        data.put("custName", todayFirstFeesdetail.getCustName());
        data.put("mobilePhone", todayFirstFeesdetail.getMobilePhone());
        data.put("chargeMode", todayFirstFeesdetail.getChargeMode());
        data.put("dealAmount", todayFirstFeesdetail.getDealAmount());
        data.put("lateAmount", todayFirstFeesdetail.getLateAmount());
        data.put("dealDate", formatDate(todayFirstFeesdetail.getDealDate()));
        data.put("content", "【今日首缴】" + formatDate(todayFirstFeesdetail.getDealDate()) + " " + todayFirstFeesdetail.getOrganName() + " " + todayFirstFeesdetail.getCommName() + " "
                + todayFirstFeesdetail.getCustName().substring(0, 1) + "**缴纳" + todayFirstFeesdetail.getDealAmount()
                + "元        " + "【今日缴纳最高】" + todayMaxFeesdetail.getOrganName() + todayMaxFeesdetail.getCommName() + " "
                + todayMaxFeesdetail.getCustName().substring(0, 1) + "**缴纳" + todayMaxFeesdetail.getDealAmount()
                + "元        ");

        return data;
    }

    /**
     * 解析成 Map 以便于 redis 保存、解析
     *
     * @param param
     * @return
     */
    private Map<String, Object> getFirstFeesOfLastDayFromMapperToMap(Map<String, String> param) {
        Map<String, Object> data = new HashMap<>();
        TbDeskTopFeesDaily lastDayFirstFeesdetail = getFirstFeesOfLastDayFromMapper();
        TbDeskTopFeesDaily lastDayMaxFeesdetail = getMaxFeesOfLastDayFromMapper();

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

        data.put("organName", lastDayFirstFeesdetail.getOrganName());
        data.put("commName", lastDayFirstFeesdetail.getCommName());
        data.put("custName", lastDayFirstFeesdetail.getCustName());
        data.put("mobilePhone", lastDayFirstFeesdetail.getMobilePhone());
        data.put("chargeMode", lastDayFirstFeesdetail.getChargeMode());
        data.put("dealAmount", lastDayFirstFeesdetail.getDealAmount());
        data.put("lateAmount", lastDayFirstFeesdetail.getLateAmount());
        data.put("dealDate", formatDate(lastDayFirstFeesdetail.getDealDate()));
        data.put("content", "【昨日首缴】" + lastDayFirstFeesdetail.getOrganName() + lastDayFirstFeesdetail.getCommName()
                + lastDayFirstFeesdetail.getCustName().substring(0, 1) + "**缴纳" + lastDayFirstFeesdetail.getDealAmount()
                + "元        " + "【昨日缴纳最高】" + lastDayMaxFeesdetail.getOrganName() + lastDayMaxFeesdetail.getCommName()
                + lastDayMaxFeesdetail.getCustName().substring(0, 1) + "**缴纳" + lastDayMaxFeesdetail.getDealAmount()
                + "元        ");

        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 TbDeskTopFeesDaily getMaxFeesOfOrganInLastDayFromMapper() {
        return mapper.getMaxFeesOfOrganInLastDay();
    }

    @Override
    public TbDeskTopFeesDaily getMaxFeesOfCommInLastDayFromMapper() {
        return mapper.getMaxFeesOfCommInLastDay();
    }

    @Override
    public Map<String, Object> getMaxFeesOfOrganAndCommInLastDayFromCache() {
        Map<String, String> param = new HashMap<>();
        Function<Map<String, String>, Map<String, Object>> function = p -> getMaxFeesOfOrganAndCommInLastDayFromMapperToMap(p);

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

        return resource;
    }

    private Map<String, Object> getMaxFeesOfOrganAndCommInLastDayFromMapperToMap(Map<String, String> p) {
        Map<String, Object> data = new HashMap<>();
        TbDeskTopFeesDaily maxFeesOfOrganInLastDay = getMaxFeesOfOrganInLastDayFromMapper();
        TbDeskTopFeesDaily MaxFeesOfCommInLastDay = getMaxFeesOfCommInLastDayFromMapper();

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

        //data.put("organName", maxFeesOfOrganInLastDay.getOrganName());
        //data.put("commName", maxFeesOfOrganInLastDay.getCommName());
        //data.put("custName", maxFeesOfOrganInLastDay.getCustName());
        //data.put("mobilePhone", maxFeesOfOrganInLastDay.getMobilePhone());
        //data.put("chargeMode", maxFeesOfOrganInLastDay.getChargeMode());
        //data.put("dealAmount", maxFeesOfOrganInLastDay.getDealAmount());
        //data.put("lateAmount", maxFeesOfOrganInLastDay.getLateAmount());
        data.put("dealDate", formatDate(maxFeesOfOrganInLastDay.getDealDate()));
        data.put("content", "【昨日缴费最高区域】" + maxFeesOfOrganInLastDay.getOrganName() + " "
                + "收缴 " + maxFeesOfOrganInLastDay.getDealAmount()
                + "元        " + "【昨日缴费最高项目】" + MaxFeesOfCommInLastDay.getCommName() + " "
                + "收缴 " + MaxFeesOfCommInLastDay.getDealAmount()
                + "元        ");

        return data;
    }

    @Transactional(readOnly = false)
    @Override
    public List<Map<String, Object>> thisAndLastMonthChargeArea() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<TbDeskTopFeesDaily> charges = mapper.thisAndLastMonthChargeArea();
        Map<String, Object> tempMap = new HashMap<>();
        if (charges == null || charges.get(0) == null) {
            tempMap.put("lastMonthChargeArea", 0);
            tempMap.put("thisMonthChargeArea", 0);
        } else {
            tempMap.put("lastMonthChargeArea", charges.get(0).getChargeArea());
            tempMap.put("thisMonthChargeArea", charges.get(1).getChargeArea());
        }

        resultList.add(tempMap);
        return resultList;
    }

    /**
     * 季度实际收费面积
     *
     * @return
     */
    @Override
    public Map<String,Object> seasonRealChargeArea() {
        Map<String,Object> resultMap = new HashMap<>();
        List<TbDeskTopFeesDaily> tbDeskTopFeesDailyList = mapper.seasonRealChargeArea();
        for(int i = 0 ; i < tbDeskTopFeesDailyList.size();i++){
            resultMap.put("season"+(i+1),tbDeskTopFeesDailyList.get(i).getChargeArea());
        }
        return resultMap;
    }
}
