package cn.com.countrygarden.bi.main.controller.v2;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
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.service.twerp.hm.TbDesktop经营主题收款方式Service;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller("rankingDataControllerV2")
@RequestMapping("/api/biapp/data/v2/ranking")
public class RankingDataController {

    @Autowired
    private TbSysUserService userService;
    @Autowired
    private TbDesktop经营主题收款方式Service service;
    @Autowired
    private TbDesktop经营主题Service operationService;


    /**
     * 查询各区域收缴率排行
     *
     * @return
     */
    @RequestMapping(value = "/charge", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public Result chargeRate() {
        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }
        List<Map<String, String>> chargeRages = operationService.rankingChargeRateFromCache(
                sysOrgan,
                false
        );
        Map<String, Map<String, String>> yesterdayChargeRage = parseRateListToMap(
                operationService.rankingChargeRateFromCache(
                        sysOrgan,
                        true
                )
        );

        for (Map<String, String> item : chargeRages) {
            String organName = item.getOrDefault("organName", "");
            Map<String, String> yesterdayIitem = yesterdayChargeRage.getOrDefault(
                    organName,
                    new HashMap<>()
            );

            int sort = Integer.parseInt(item.getOrDefault("sort", "0"));
            float general = Float.parseFloat(item.getOrDefault("general", "0"));

            int yesterdaySort = Integer.parseInt(yesterdayIitem.getOrDefault("sort", "0"));
            float yesterdayGeneral = Float.parseFloat(yesterdayIitem.getOrDefault("general", "0"));

            item.put("up", String.valueOf(sort - yesterdaySort));
            item.put("added", String.valueOf(general - yesterdayGeneral));
        }

        return Response.response(chargeRages);
    }

    /**
     * 查询各区域追缴率排行
     *
     * @return
     */
    @RequestMapping(value = "/recovered", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public Result recovered() {
        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }

        List<Map<String, String>> chargeRages = operationService.rankingChaseRateFromCache(
                sysOrgan,
                false
        );
        Map<String, Map<String, String>> yesterdayChargeRage = parseRateListToMap(
                operationService.rankingChaseRateFromCache(
                        sysOrgan,
                        true
                )
        );

        for (Map<String, String> item : chargeRages) {
            String organName = item.getOrDefault("organName", "");
            Map<String, String> yesterdayIitem = yesterdayChargeRage.getOrDefault(
                    organName,
                    new HashMap<>()
            );

            int sort = Integer.parseInt(item.getOrDefault("sort", "0"));
            float recovered = Float.parseFloat(item.getOrDefault("recovered", "0"));

            int yesterdaySort = Integer.parseInt(yesterdayIitem.getOrDefault("sort", "0"));
            float yesterdayRecovered = Float.parseFloat(yesterdayIitem.getOrDefault("recovered", "0"));

            item.put("up", String.valueOf(sort - yesterdaySort));
            item.put("added", String.valueOf(recovered - yesterdayRecovered));
        }

        return Response.response(chargeRages);
    }

    /**
     * 各区域累计实收排行
     *
     * @return
     */
    @RequestMapping(value = "/totalCharge", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public Result totalCharge(@RequestParam(value = "CommID", defaultValue = "") String commID,
                              @RequestParam(value = "OrganCode", defaultValue = "") String organCode) {
        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }

        return Response.response(service.sumByOrganFromCache(
                sysOrgan,
                organCode,
                commID
        ));
    }

    private Map<String, Map<String, String>> parseRateListToMap(List<Map<String, String>> items) {
        Map<String, Map<String, String>> data = new HashMap();
        if (items.isEmpty()) {
            return data;
        }
        for (Map<String, String> item : items) {
            data.put(item.get("organName"), item);
        }
        return data;
    }
}
