package cn.com.countrygarden.bi.main.controller.marketing.app;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.MarketingCharge;
import cn.com.countrygarden.bi.main.model.MarketingQuota;
import cn.com.countrygarden.bi.main.service.MarketingChargeService;
import cn.com.countrygarden.bi.main.service.MarketingQuotaService;
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("marketingAppChargeController")
@RequestMapping("/api/biapp/marketing/charge")
public class ChargeController {

    @Autowired
    private MarketingChargeService chargeService;
    @Autowired
    private MarketingQuotaService quotaService;

    /**
     * 首页收费面积汇总
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/summary", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result summary() {
        Map<String, Object> response = new HashMap<>();
        List<Map<String, Object>> seasons = new ArrayList<>();
        List<MarketingCharge> charges = chargeService.sumThisYearBySeason();
        MarketingQuota quota = quotaService.sumThisYear();

        for (MarketingCharge charge : charges) {
            Map<String, Object> item = new HashMap<>();
            int season = charge.getSeason();
            String seasonText = "第";
            float seasonQuota = 0f;
            switch (season) {
                case 1:
                    seasonText = seasonText.concat("一");
                    seasonQuota = quota.getChargeArea1st().floatValue();
                    break;
                case 2:
                    seasonText = seasonText.concat("二");
                    seasonQuota = quota.getChargeArea2nd().floatValue();
                    break;
                case 3:
                    seasonText = seasonText.concat("三");
                    seasonQuota = quota.getChargeArea3rd().floatValue();
                    break;
                case 4:
                    seasonText = seasonText.concat("四");
                    seasonQuota = quota.getChargeArea4th().floatValue();
                    break;
            }
            seasonText = seasonText.concat("季度");
            item.put("area", charge.getChargeArea()); //实际收费面积
            item.put("quota", seasonQuota); //收费指标
            item.put("x", seasonText);
            seasons.add(item);
        }

        MarketingCharge charge = chargeService.sumThisYear();
        response.put("area", charge.getChargeArea());
        response.put("quota", quota.getChargeArea());
        response.put("seasons", seasons);

        return Response.response(response);
    }

    /**
     * 签约Top5
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ranking", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result ranking(@RequestParam(value = "season", defaultValue = "0") int season,
                          @RequestParam(value = "skip", defaultValue = "0") int offset,
                          @RequestParam(value = "limit", defaultValue = "10") int limit) {
        Map<String, Object> response = new HashMap<>();

        //今年内目标收费面积
        MarketingQuota quotaSum = quotaService.sumThisYear();
        response.put("targetChargeAreaInThisYear", quotaSum.getChargeArea());

        //今年内实际收费面积
        MarketingCharge chargeSum = chargeService.sumThisYear();
        response.put("actualChargeAreaInThisYear", chargeSum.getChargeArea());

        int sort = Math.max(0, offset) + 1;
        List<Map<String, Object>> chargeRankingList = new ArrayList<>();
        List<MarketingCharge> charges = chargeService.rankingByOrgan(season, offset, limit);
        for (MarketingCharge charge : charges) {
            Map<String, Object> item = new HashMap<String, Object>() {{
                put("id", charge.getOrganId());//区域名
                put("regionName", charge.getOrganName());//区域名
                put("targetChargeArea", charge.getQuotaChargeArea().floatValue());//目标收费面积
                put("actualChargeArea", charge.getChargeArea().floatValue());//实际收费面积
                put("chargeRate", charge.getChargeRate());//完成率
            }};
            item.put("sort", sort++);
            chargeRankingList.add(item);
        }
        response.put("chargeRankingList", chargeRankingList);

        return Response.response(response);
    }
}
