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.TbDesktop本日新增Mapper;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbDesktop本日新增;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbDesktop本日新增Service;
import cn.com.countrygarden.bi.main.util.MD5;
import cn.com.countrygarden.bi.main.util.RedisCacheUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * Created by CodeGenerator on 2017/08/10.
 */
@Service
@Transactional
public class TbDesktop本日新增ServiceImpl extends AbstractService<TbDesktop本日新增> implements TbDesktop本日新增Service {
    @Resource
    private TbDesktop本日新增Mapper mapper;

    protected final Logger logger = LoggerFactory.getLogger(TbDesktop本日新增ServiceImpl.class);


    /**
     * 获取首页收入数据
     *
     * @param userCode
     * @param organCode
     * @param commID
     * @return
     */
    public Map<String, Object> sumHomeMainIncomeFromCache(String userCode, String organCode, String commID) {
        Map<String, String> param = new HashMap<String, String>() {{
            put("userCode", userCode);
            put("organCode", organCode);
            put("commID", commID);
        }};
        Function<Map<String, String>, Map<String, Object>> function = p -> sumHomeMainIncomeFromMapperToMap(p);

        Map<String, Object> resource = RedisCacheUtils.rememberMap(
                "biapp:desktop:increased:today:home:income:",
                1800 /* 15 * 60 */,
                param,
                function
        );

        return resource;
    }

    /**
     * 根据参数获取各种收入数据
     *
     * @param param
     * @return
     */
    public TbDesktop本日新增 sumHomeMainIncomeFromMapper(Map<String, String> param) {
        TbDesktop本日新增 data;
        String userCode = param.getOrDefault("userCode", "");
        String organCode = param.getOrDefault("organCode", "");
        String commID = param.getOrDefault("commID", "");

        if (userCode != null && !userCode.equals("")
                && organCode != null && !organCode.equals("")
                && commID != null && !commID.equals("")) {
            data = mapper.sumHomeMainIncomeByUserCodeAndOrganCodeAndCommID(new HashMap<String, String>() {{
                put("userCode", userCode);
                put("organCode", organCode);
                put("commID", commID);
            }});
        } else if (userCode != null && !userCode.equals("")
                && organCode != null && !organCode.equals("")) {
            data = mapper.sumHomeMainIncomeByUserCodeAndOrganCode(new HashMap<String, String>() {{
                put("userCode", userCode);
                put("organCode", organCode);
            }});
        } else if (userCode != null && !userCode.equals("")) {
            data = mapper.sumHomeMainIncomeByUserCode(userCode);
        } else {
            data = null;
        }
        return data;
    }

    /**
     * 解析成 Map 以便于 redis 保存、解析
     *
     * @param param
     * @return
     */
    private Map<String, Object> sumHomeMainIncomeFromMapperToMap(Map<String, String> param) {
        Map<String, Object> data = new HashMap<>();
        TbDesktop本日新增 rate = sumHomeMainIncomeFromMapper(param);

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

        data.put("receiptsYesterday", rate.getZrss());
        data.put("recoveredYesterday", rate.getZrzj());
        data.put("parkingIncomToday", rate.getBrcwsr());

        return data;
    }
}
