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.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.MarketingProposal;
import cn.com.countrygarden.bi.main.model.marketing.ProposalMonthData;
import cn.com.countrygarden.bi.main.service.MarketingProposalService;
import cn.com.countrygarden.bi.main.service.marketing.ProposalService;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbSysUserService;
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.*;

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

@Controller("marketingBackendProposalController")
@RequestMapping("/api/biapp/marketing/backend")
public class ProposalController {

    @Autowired
    private TbSysUserService userService;

    @Autowired
    private MarketingProposalService proposalService;

    @Autowired
    private ProposalService proService;

    /**
     * 签约列表
     *
     * @param search 搜索关键词。模糊搜索区域、项目
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/proposal", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result search(@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 = "/proposal/{id}", produces = "application/json;charset=utf-8", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") int id) {
        MarketingProposal proposal = proposalService.findById(id);
        if (proposal == null) {
            throw new ServiceException("记录不存在");
        }
        proposalService.softDelete(id, userService.getCurrentSysUser());
        return Response.response();
    }


    /**
     * 上会项目详情返回数据
     *
     * @param proposal
     * @return
     */
    private Map<String, Object> getReturnProposal(MarketingProposal proposal) {
        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());
        }};
    }

    /**
     * 上会趋势
     *
     * @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<ProposalMonthData> monthDatas = proService.tendency();

        for (ProposalMonthData monthData : monthDatas) {
            Map<String, Object> item = new HashMap<>();
            item.put("pass", monthData.getPassed());
            item.put("pending", monthData.getCandidate());
            item.put("shelve", monthData.getSuspend());
            item.put("y", monthData.getTotal());
            item.put("x", Integer.valueOf(monthData.getX()) + "月");
            response.add(item);
        }

        return Response.response(response);
    }
}
