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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Service
@Transactional
public class TbDesktop经营主题停车收入ServiceImpl
        extends AbstractService<TbDesktop经营主题停车收入>
        implements TbDesktop经营主题停车收入Service {

    protected final Logger log = LoggerFactory.getLogger(TbDesktop经营主题停车收入ServiceImpl.class);

    @Autowired
    private TbDesktop经营主题停车收入Mapper mapper;


    /**
     * 获取用户资源数据
     *
     * @param userCode
     * @param organCode
     * @param commID
     * @return
     */
    public List<Map<String, String>> sumByUserCodeFromCache(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>, List<Map<String, String>>> function = p -> sumByUserCodeFromMapperToArrayList(p);

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

        return resource;
    }

    /**
     * 获取用户资源数据
     *
     * @param sysOrgan
     * @param organCode
     * @param commID
     * @return
     */
    public List<Map<String, String>> sumByUserCodeFromCache(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 -> sumByUserCodeFromMapperToArrayList(p);

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

        return resource;
    }

    /**
     * 根据参数获取 resource 汇总
     *
     * @param param
     * @return
     */
    public List<TbDesktop经营主题停车收入> sumByUserCodeFromMapper(Map<String, String> param) {
        return mapper.sumByUserCode(param);
    }

    /**
     * 解析成 ArrayList 以便于 redis 保存、解析
     *
     * @param param
     * @return
     */
    private List<Map<String, String>> sumByUserCodeFromMapperToArrayList(Map<String, String> param) {
        List<Map<String, String>> data = new ArrayList<>();
        List<TbDesktop经营主题停车收入> states = sumByUserCodeFromMapper(param);

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

        Integer count = 1;
        for (TbDesktop经营主题停车收入 state : states) {
            Map<String, String> item = new HashMap<String, String>() {{
                put("total", String.valueOf(state.getTotal()));
                put("service", String.valueOf(state.get车位服务费()));
                put("cost", String.valueOf(state.get车位使用费()));
                put("property", String.valueOf(state.get车位物业费()));
                put("temp", String.valueOf(state.get临时停车费()));
                put("organName", String.valueOf(state.getOrganname()));
            }};

            item.put("sort", String.valueOf(count++));

            data.add(item);
        }

        return data;
    }
}
