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.MarketingCharge;
import cn.com.countrygarden.bi.main.service.MarketingChargeService;
import cn.com.countrygarden.bi.main.service.marketing.ChargeService;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbDeskTopFeesDailyService;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbHsprCommunityService;
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("marketingBackendChargeController")
@RequestMapping("/api/biapp/marketing/backend")
public class ChargeController {

    @Autowired
    private TbSysUserService userService;

    @Autowired
    private MarketingChargeService chargeService;

    @Autowired
    private TbDeskTopFeesDailyService tbDeskTopFeesDailyService;

    @Autowired
    private ChargeService service;
    @Autowired
    private TbHsprCommunityService tbHsprCommunityService;


    /**
     * 收费列表
     *
     * @param search 搜索关键词。模糊搜索区域、项目
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/charge", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result search(@RequestParam(value = "month", defaultValue = "") int month,
                         @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<MarketingCharge> charges = chargeService.search(
                month,
                search,
                offset,
                limit
        );

        for (MarketingCharge charge : charges) {
            response.add(new HashMap<String, Object>() {{
                put("id", charge.getId());
                put("commName", charge.getCommName());
                put("organName", charge.getOrganName());
                put("developSubject", charge.getDevelopSubject());
                put("ncAccount", charge.getNcAccount());
                put("ncNumber", charge.getNcNumber());
                put("area", charge.getChargeArea());
                put("memo", charge.getMemo());
            }});
        }

        return Response.response(response);
    }

    /**
     * 删除收费
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/charge/{id}", produces = "application/json;charset=utf-8", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") int id) {
        MarketingCharge item = chargeService.findById(id);
        if (item == null) {
            throw new ServiceException("签约记录不存在");
        }
        chargeService.softDelete(id, userService.getCurrentSysUser());
        return Response.response();
    }


    /**
     * 当前收费面积排行
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/charge/ranking", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result ranking() {
        List<Map<String, Object>> rankings = service.rankingThisYear();
        return Response.response(rankings);
    }

    /**
     * 本月和上月收费面积
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/charge/thisAndLastMonthChargeArea", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result thisAndLastMonthChargeArea() {
        List<Map<String, Object>> thisAndLastMonthChargeAreaList = tbDeskTopFeesDailyService.thisAndLastMonthChargeArea();
        return Response.response(thisAndLastMonthChargeAreaList);
    }

    /**
     * 本月收费面积区域排名
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/charge/chargeAreaRanking", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result chargeAreaRanking() {
        List<Map<String, Object>> chargeAreaRankingList = tbHsprCommunityService.chargeAreaRanking(null);
        return Response.response(chargeAreaRankingList);
    }

    /**
     * 本年每月收费面积趋势
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/charge/monthOfYearArea", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result monthOfYearArea() {
        List<Map<String, Object>> monthOfYearAreaList = tbHsprCommunityService.monthOfYearArea();
        return Response.response(monthOfYearAreaList);
    }

    /**
     * 总收费面积占总接管面积的百分比
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/charge/chargeAreaInTotalTakeoverArea", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result chargeAreaInTotalTakeoverArea() {
        List<Map<String, Object>> chargeAreaInTotalTakeoverAreaList = service.chargeAreaInTotalTakeoverArea();
        return Response.response(chargeAreaInTotalTakeoverAreaList);
    }
}
