package cn.com.countrygarden.bi.main.controller.v2;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysOrgan;
import cn.com.countrygarden.bi.main.service.twerp.hm.*;
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("operationDataControllerV2")
@RequestMapping("/api/biapp/data/v2/opetation")
public class OperationDataController {

    @Autowired
    private TbSysUserService userService;
    @Autowired
    private TbDesktop经营主题Service service;
    @Autowired
    private TbDesktop经营主题收款方式Service paymentService;
    @Autowired
    private TbDesktop本日新增Service addedService;
    @Autowired
    private TbDesktopResourceService resourceService;


    /**
     * 查询本年累计追缴率,本年综合收费率,本年欠费追缴率
     *
     * @return
     */
    @RequestMapping(value = "/chargeRate", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public Object chargeRate() {
        AccessToken accessToken = userService.getCurrentAccessToken();
        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }

        return Response.response(service.findByUserSysOrganFromCache(
                accessToken.getUserCode(),
                sysOrgan.getOrganCode(),
                sysOrgan.getCommID()
        ));
    }

    /**
     * 基础数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/overview", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Object overview(@RequestParam(value = "CommID", defaultValue = "") String commID,
                           @RequestParam(value = "OrganCode", defaultValue = "") String organCode) {
        Map<String, Object> dataMap = new HashMap<>();

        AccessToken accessToken = userService.getCurrentAccessToken();
        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }

        Map<String, Object> overview = service.sumHomeMainIncomeFromCache(
                accessToken.getUserCode(),
                organCode,
                commID
        );
        overview.putAll(addedService.sumHomeMainIncomeFromCache(
                accessToken.getUserCode(),
                organCode,
                commID
        ));

        dataMap.put("overview", overview);
        dataMap.put("rate", service.findByUserSysOrganFromCache(
                accessToken.getUserCode(),
                sysOrgan.getOrganCode(),
                sysOrgan.getCommID()
        ));

        return Response.response(dataMap);
    }

    /**
     * 物业业态饼图信息
     *
     * @return
     */
    @RequestMapping(value = "/property", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public Object property(@RequestParam(value = "CommID", defaultValue = "") String commID,
                           @RequestParam(value = "OrganCode", defaultValue = "") String organCode) {
        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }
        return Response.response(resourceService.sumPropertyFormatFromCache(
                sysOrgan,
                organCode,
                commID
        ));
    }

    /**
     * 收款方式饼图信息
     *
     * @return
     */
    @RequestMapping(value = "/payment", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public Object payment(@RequestParam(value = "CommID", defaultValue = "") String commID,
                          @RequestParam(value = "OrganCode", defaultValue = "") String organCode) {
        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }

        return Response.response(paymentService.sumFromCache(
                sysOrgan,
                organCode,
                commID
        ));
    }

    /**
     * 开发主体饼图信息
     *
     * @return
     */
    @RequestMapping(value = "/developer", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public Object developer(@RequestParam(value = "CommID", defaultValue = "") String commID,
                            @RequestParam(value = "OrganCode", defaultValue = "") String organCode) {

        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }
        return Response.response(resourceService.sumDevelopmentSubjectFromCache(
                sysOrgan,
                organCode,
                commID
        ));
    }
}
