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

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.marketing.ContractSummary;
import cn.com.countrygarden.bi.main.service.common.CommService;
import cn.com.countrygarden.bi.main.service.marketing.ContractService;
import cn.com.countrygarden.bi.main.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("marketingBackendContractController")
@RequestMapping("/api/biapp/marketing/backend/contract")
public class ContractController {

    @Autowired
    private ContractService service;
    @Autowired
    private CommService commService;

    /**
     * 数据汇总
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/summary", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result summary() {
        Map<String, Object> response = new HashMap<>();

        List<ContractSummary> contractsByCorpType = service.sumThisYearByCorpType();
        List<ContractSummary> contractsByProjectType = service.sumThisYearByProjectType();

        List<Map<String, Object>> corpTypes = new ArrayList<>();
        for (ContractSummary corpType : contractsByCorpType) {
            Map<String, Object> item = new HashMap<>();
            item.put("area", getTenThousand(corpType.getContractArea()));
            item.put("corpType", corpType.getCorpType());
            corpTypes.add(item);
        }
        response.put("corpType", corpTypes);

        List<Map<String, Object>> projectTypes = new ArrayList<>();
        for (ContractSummary projectType : contractsByProjectType) {
            Map<String, Object> item = new HashMap<>();
            item.put("area", getTenThousand(projectType.getContractArea()));
            item.put("projectType", projectType.getProjectType());
            projectTypes.add(item);
        }
        response.put("projectType", projectTypes);

        return Response.response(response);
    }

    /**
     * 签约趋势
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tendency", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result tendency() {
        List<Map<String, Object>> response = new ArrayList<>();
        List<ContractSummary> contracts = service.tendency();

        for (ContractSummary contract : contracts) {
            Map<String, Object> item = new HashMap<>();
            item.put("y", getTenThousand(contract.getContractArea()));
            item.put("x", contract.getMonth() + "月");
            response.add(item);
        }

        return Response.response(response);
    }


    /**
     * top 5
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ranking", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result ranking(@RequestParam(value = "limit", defaultValue = "10") int limit) {
        List<Map<String, Object>> response = new ArrayList<>();
        List<ContractSummary> contracts = service.ranking(limit);

        Map<String, Object> organs = commService.findOrganMapCache();
        for (ContractSummary contract : contracts) {
            Map<String, Object> item = new HashMap<>();
            item.put("organ", organs.getOrDefault(contract.getOrganId(), "其他"));
            item.put("area", getTenThousand(contract.getContractArea()));
            item.put("quota", getTenThousand(contract.getQuotaContractArea()));
            item.put("rate", Float.parseFloat(NumberUtil.simpleFormat(contract.getRate())));
            response.add(item);
        }

        return Response.response(response);
    }


    /**
     * 总签约面积占总接管面积的比例
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/contractAreaInTotalTakeoverArea", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result contractAreaInTotalTakeoverArea() {
        List<Map<String, Object>> list = service.contractAreaInTotalTakeoverArea();
        return Response.response(list);
    }

    /**
     * 转换为万
     *
     * @param area
     * @return
     */
    private float getTenThousand(BigDecimal area) {
        float tenThousand = 0f;
        if (area == null) {
            return tenThousand;
        }

        tenThousand = Float.parseFloat(NumberUtil.simpleFormat(area.floatValue() / 10000));

        return tenThousand;
    }
}
