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

import cn.com.countrygarden.bi.main.core.AbstractService;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dao.twerp.hm.TbHsprCommunityMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.twerp.ErpCommunity;
import cn.com.countrygarden.bi.main.model.twerp.ErpNcData;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbHsprCommunity;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbHsprCommunityService;
import cn.com.countrygarden.bi.main.util.MD5;
import cn.com.countrygarden.bi.main.util.RedisCacheUtils;
import com.github.pagehelper.PageRowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;


@Service
@Transactional
public class TbHsprCommunityServiceImpl extends AbstractService<TbHsprCommunity> implements TbHsprCommunityService {
    @Resource
    private TbHsprCommunityMapper mapper;

    public Map<String, Object> ccountThisMonthByUserCodeFromCache(String userCode) {
        Map<String, String> param = new HashMap<>();
        param.put("userCode", userCode);
        Function<Map<String, String>, Map<String, Object>> function = p -> countThisMonthByUserCodeToMap(p);

        return RedisCacheUtils.rememberMap(
                "biapp:hm:hspr:community:comms:thismonth:",
                1800 /*30m: 30 * 60 */,
                param,
                function
        );
    }

    /**
     * 解析成 Map 以便于 redis 保存、解析
     *
     * @param param
     * @return
     */
    private Map<String, Object> countThisMonthByUserCodeToMap(Map<String, String> param) {
        Map<String, Object> data = new HashMap<>();
        TbHsprCommunity community = countThisMonthByUserCode(param.getOrDefault("userCode", ""));

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

        data.put("comms", community.getComms());

        return data;
    }

    public Map<String, Object> countAllByUserCodeFromCache(String userCode) {
        Map<String, String> param = new HashMap<>();
        param.put("userCode", userCode);
        Function<Map<String, String>, Map<String, Object>> function = p -> countAllByUserCodeToMap(p);

        return RedisCacheUtils.rememberMap(
                "biapp:hm:hspr:community:comms:all:",
                1800 /*30m: 30 * 60 */,
                param,
                function
        );
    }

    /**
     * 解析成 Map 以便于 redis 保存、解析
     *
     * @param param
     * @return
     */
    private Map<String, Object> countAllByUserCodeToMap(Map<String, String> param) {
        Map<String, Object> data = new HashMap<>();
        TbHsprCommunity community = countAllByUserCode(param.getOrDefault("userCode", ""));

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

        data.put("comms", community.getComms());

        return data;
    }

    public TbHsprCommunity countThisMonthByUserCode(String userCode) {
        return mapper.countThisMonthByUserCode(userCode);
    }

    public TbHsprCommunity countAllByUserCode(String userCode) {
        return mapper.countAllByUserCode(userCode);
    }

    /**
     * 本月收费面积区域排行
     *
     * @param lastMonth 如果lastMonth不为空则查询上个月的收费面积区域排行，因为可能月初几天有些区域收费面积为空，所以才出此策略
     * @return
     */
    @Override
    public List<Map<String, Object>> chargeAreaRanking(String lastMonth) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        lastMonth = null;
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        long keke = new Date().getTime() - c.getTime().getTime();
        if ((keke / 1000 * 60 * 60 * 24) - 5 < 0) {
            lastMonth = "true";
        }
        List<TbHsprCommunity> tbHsprCommunities = mapper.chargeAreaRanking(lastMonth);
        for (int i = 0; i < tbHsprCommunities.size(); i++) {
            Map<String, Object> tempMap = new HashMap<>();
            tempMap.put("organName", tbHsprCommunities.get(i).getOrganName());
            tempMap.put("area", tbHsprCommunities.get(i).getArea());
            resultList.add(tempMap);
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> monthOfYearArea() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<TbHsprCommunity> tbHsprCommunities = mapper.monthOfYearArea();
        for (int i = 0; i < tbHsprCommunities.size(); i++) {
            Map<String, Object> tempMap = new HashMap<>();
            tempMap.put("month", tbHsprCommunities.get(i).getMonth());
            tempMap.put("area", tbHsprCommunities.get(i).getArea());
            resultList.add(tempMap);
        }

        return resultList;
    }

    @Override
    public List<Map<String, Object>> findCaseFieldCommByOrganCode(String organCode) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        List<TbHsprCommunity> tbHsprCommunityList = mapper.findCaseFieldCommByOrganCode(organCode);
        if (tbHsprCommunityList != null && tbHsprCommunityList.size() > 0) {
            for (TbHsprCommunity tbHsprCommunity : tbHsprCommunityList) {
                Map<String, Object> tempMap = new HashMap<>();
                tempMap.put("commId", tbHsprCommunity.getCommid() + "");
                tempMap.put("commName", tbHsprCommunity.getCommname());
                resultList.add(tempMap);
            }
        }

        return resultList;
    }

    @Override
    public ErpCommunity findBaseCommByCommId(String commId) {
        List<ErpCommunity> baseCommByCommId = mapper.findBaseCommByCommId(commId);
        if (baseCommByCommId != null && baseCommByCommId.size() > 0) {
            ErpCommunity erpCommunity = baseCommByCommId.get(0);
            return erpCommunity;
        }
        return null;
    }

    @Override
    public List<ErpNcData> selectProjectByUserCode(String userCode) {
        return mapper.selectProjectByUserCode(userCode);
    }

    @Override
    public List<ErpNcData> selectRegionByUserCode(String userCode) {
        return mapper.selectRegionByUserCode(userCode);
    }

    @Override
    public List<ErpNcData> selectRegionByOrganCode(String organCode) {
        return mapper.selectRegionByOrganCode(organCode);
    }

    @Override
    public List<String> selectAllOrganCode() {
        return mapper.selectAllOrganCode();
    }
}
