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.MarketingProposal;
import cn.com.countrygarden.bi.main.service.MarketingProposalService;
import cn.com.countrygarden.bi.main.util.DateUtil;
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("marketingAppProposalController")
@RequestMapping("/api/biapp/marketing/proposal")
public class ProposalController {

    @Autowired
    private MarketingProposalService proposalService;

    /**
     * 上会趋势
     *
     * @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<MarketingProposal> contracts = proposalService.tendency();

        for (MarketingProposal contract : contracts) {
            Map<String, Object> item = new HashMap<>();
            item.put("pass", contract.getPassComms());
            item.put("pending", contract.getPendingComms());
            item.put("shelve", contract.getShelveComms());
            item.put("y", contract.getComms());
            item.put("x", Integer.valueOf(contract.getProposalMonth()) + "月");
            response.add(item);
        }

        return Response.response(response);
    }

    /**
     * 上会趋势
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tendency/flatten", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result tendencyFlatten() {
        Map<String, Object> response = new HashMap<>();

        Map<String, ArrayList<Integer>> seriesData = new HashMap<>();
        List<Map<String, Object>> series = new ArrayList<>();

        List<MarketingProposal> contracts = proposalService.tendency();

        for (MarketingProposal contract : contracts) {
            String month = Integer.valueOf(contract.getProposalMonth()) + "月";
            String result = contract.getResult();

            ArrayList<Integer> data = seriesData.get(month);
            if (data == null) {
                data = new ArrayList<Integer>() {{
                    add(0);
                    add(0);
                    add(0);
                }};
            }

            switch (result) {
                case "通过":
                    data.set(0, contract.getComms());
                    break;
                case "停止":
                    data.set(1, contract.getComms());
                    break;
                case "待定":
                    data.set(2, contract.getComms());
                    break;
            }

            seriesData.put(month, data);
        }

        for (String month : seriesData.keySet()) {
            Map<String, Object> item = new HashMap<>();
            item.put("name", month);
            item.put("data", seriesData.get(month));
            series.add(item);
        }

        response.put("legend", new ArrayList<String>() {{
            add("通过");
            add("停止");
            add("待定");
        }});

        Collections.reverse(series);
        response.put("series", series);

        return Response.response(response);
    }

    /**
     * 上会趋势
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/ranking", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result ranking(@RequestParam(value = "search", defaultValue = "") String search,
                          @RequestParam(value = "after", defaultValue = "") String after,
                          @RequestParam(value = "before", defaultValue = "") String before,
                          @RequestParam(value = "status", defaultValue = "") String status,
                          @RequestParam(value = "skip", defaultValue = "0") int skip,
                          @RequestParam(value = "limit", defaultValue = "10") int limit) {
        List<Map<String, Object>> response = new ArrayList<>();
        List<MarketingProposal> proposals = proposalService.search(search, after, before, status, skip, limit);

        for (MarketingProposal proposal : proposals) {
            response.add(getReturnProposal(proposal));
        }

        return Response.response(response);
    }

    /**
     * 上会项目详情
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result detail(@RequestParam(value = "id", defaultValue = "") int id) {
        return Response.response(getReturnProposal(proposalService.findById(id)));
    }

    /**
     * 上会项目详情返回数据
     *
     * @param proposal
     * @return
     */
    private Map<String, Object> getReturnProposal(MarketingProposal proposal) {
        if (proposal == null) {
            return new HashMap<>();
        }

        return new HashMap<String, Object>() {{
            put("id", proposal.getId());
            put("projectName", proposal.getCommName());
            put("takeoverRegion", proposal.getOrganName());
            put("quoteArea", proposal.getProposalOrganName());
            put("projectType", proposal.getProjectType());
            put("projectNature", proposal.getProjectNature());
            put("cooperationType", proposal.getCorpType());
            put("projectArea", proposal.getArea());
            put("proposalDate", DateUtil.simpleFormat(proposal.getProposalDate()));
            put("reviewResult", proposal.getResult());
            put("followerAndPhoneNum", proposal.getContacts());
            put("remark", proposal.getMemo());
        }};
    }
}
