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.MarketingQuota;
import cn.com.countrygarden.bi.main.service.MarketingQuotaService;
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.*;

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

@Controller("marketingBackendQuotaController")
@RequestMapping("/api/biapp/marketing/backend")
public class QuotaController {

    @Autowired
    private TbSysUserService userService;

    @Autowired
    private MarketingQuotaService quotaService;

    /**
     * 指标列表
     *
     * @param search 搜索关键词。模糊搜索区域、项目
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/quota", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result search(@RequestParam(value = "search", defaultValue = "") String search,
                         @RequestParam(value = "skip", defaultValue = "0") int offset,
                         @RequestParam(value = "limit", defaultValue = "10") int limit) {
        List<Map<String, Object>> response = new ArrayList<>();
        List<MarketingQuota> items = quotaService.search(
                search,
                offset,
                limit
        );

        for (MarketingQuota item : items) {
            response.add(new HashMap<String, Object>() {{
                put("id", item.getId());
                put("organName", item.getOrganName());
                put("contractArea", item.getContractArea());
                put("contractArea1st", item.getContractArea1st());
                put("contractArea2nd", item.getContractArea2nd());
                put("contractArea3rd", item.getContractArea3rd());
                put("contractArea4th", item.getContractArea4th());
                put("chargeArea", item.getChargeArea());
                put("chargeAreaComplete", item.getChargeAreaComplete());
                put("chargeArea1st", item.getChargeArea1st());
                put("chargeAreaComplete1st", item.getChargeAreaComplete1st());
                put("chargeArea2nd", item.getChargeArea2nd());
                put("chargeAreaComplete2nd", item.getChargeAreaComplete2nd());
                put("chargeArea3rd", item.getChargeArea3rd());
                put("chargeAreaComplete3rd", item.getChargeAreaComplete3rd());
                put("chargeArea4th", item.getChargeArea4th());
                put("chargeAreaComplete4th", item.getChargeAreaComplete4th());
            }});
        }

        return Response.response(response);
    }

    /**
     * 删除指标
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/quota/{id}", produces = "application/json;charset=utf-8", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") int id) {
        MarketingQuota contract = quotaService.findById(id);
        if (contract == null) {
            throw new ServiceException("记录不存在");
        }
        quotaService.softDelete(id, userService.getCurrentSysUser());
        return Response.response();
    }
}
