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.model.twerp.hm.TbSysOrgan;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
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 sysOrgan
     * @param organCode
     * @param commID
     * @return
     */
    public Map<String, Object> sumFromCache(TbSysOrgan sysOrgan, String organCode, String commID) {
        Map<String, String> param = new HashMap<String, String>() {{
            put("userCode", sysOrgan.getUserCode());
            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:operation:subject:payment:",
                1800 /* 15 * 60 */,
                param,
                function
        );

        return resource;
    }

    /**
     * 根据参数获取各种收入数据
     *
     * @param param
     * @return
     */
    public TbDesktop经营主题收款方式 sumFromMapper(Map<String, String> param) {
        return mapper.sumByUserCode(param);
    }

    /**
     * 解析成 Map 以便于 redis 保存、解析
     *
     * @param param
     * @return
     */
    private Map<String, Object> sumHomeMainIncomeFromMapperToMap(Map<String, String> param) {
        Map<String, Object> data = new HashMap<>();
        TbDesktop经营主题收款方式 rate = sumFromMapper(param);

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

        data.put("cash", rate.get现金());
        data.put("payForAnother", rate.get房产代付());
        data.put("bankWithholding", rate.get银行代扣());
        data.put("pos", rate.getPos刷卡());
        data.put("vacancy", rate.get房产支付空置房());
        data.put("other", rate.get其他());

        return data;
    }

    /**
     * 区域收费排行
     *
     * @param sysOrgan
     * @param organCode
     * @param commID
     * @return
     */
    public List<Map<String, String>> sumByOrganFromCache(TbSysOrgan sysOrgan, String organCode, String commID) {
        Map<String, String> param = new HashMap<String, String>() {{
            put("userCode", sysOrgan.getUserCode());
            put("organCode", organCode);
            put("commID", commID);
            put("isCommLimited", sysOrgan.isCommLimited() ? "1" : "0");
            put("isHeadquarters", sysOrgan.isHeadquarters() ? "1" : "0");
        }};
        Function<Map<String, String>, List<Map<String, String>>> function = p -> sumByOrganFromMapperToArrayList(p);

        List<Map<String, String>> resource = RedisCacheUtils.rememberList(
                "biapp:desktop:operation:subject:payment:organ:",
                1800 /* 15 * 60 */,
                param,
                function
        );

        return resource;
    }

    /**
     * 解析成 ArrayList 以便于 redis 保存、解析
     *
     * @param param
     * @return
     */
    private List<Map<String, String>> sumByOrganFromMapperToArrayList(Map<String, String> param) {
        List<Map<String, String>> data = new ArrayList<>();
        List<TbDesktop经营主题收款方式> states = mapper.sumByOrganByUserCode(param);

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

        for (TbDesktop经营主题收款方式 state : states) {
            data.add(new HashMap<String, String>() {{
                put("organName", state.getOrganname());
                put("total", String.valueOf(state.getTotal()));
                put("cash", String.valueOf(state.get现金()));
                put("payForAnother", String.valueOf(state.get房产代付()));
                put("bankWithholding", String.valueOf(state.get银行代扣()));
                put("pos", String.valueOf(state.getPos刷卡()));
                put("vacancy", String.valueOf(state.get房产支付空置房()));
                put("other", String.valueOf(state.get其他()));
            }});
        }

        return data;
    }
}
