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

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysOrgan;

import cn.com.countrygarden.bi.main.service.twerp.hm.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller("resourceDataControllerV2")
@RequestMapping("/api/biapp/data/v2/resource")
public class ResourceDataController {

    protected final Logger logger = LoggerFactory.getLogger(ResourceDataController.class);

    @Autowired
    private TbDesktopResourceService resourceService;
    @Autowired
    private TbSysUserService userService;
    @Autowired
    private TbHsprCommunityService communityService;
    @Autowired
    private TbDesktop经营主题Service service;
    @Autowired
    private TbDesktop本日新增Service addedService;


    /**
     * 基础资源数据
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/basic", produces = "application/json;charset=utf-8")
    public Object summary(@RequestParam(value = "CommID", defaultValue = "") String commID,
                          @RequestParam(value = "OrganCode", defaultValue = "") String organCode) {
        TbSysOrgan sysOrgan = userService.getSysOrgan();
        if (sysOrgan == null) {
            return Response.response();
        }
        Map<String, Object> resources = resourceService.sumByUserCodeFromCache(
                sysOrgan.getUserCode(),
                organCode,
                commID
        );
        Map<String, Map<String, Object>> basic = summaryFlattenResponse(resources);

        // 项目统计
        Map<String, Object> comms = new HashMap<>();
        Map<String, Object> commsThisMonth = communityService.ccountThisMonthByUserCodeFromCache(sysOrgan.getUserCode());
        Map<String, Object> commsAll = communityService.countAllByUserCodeFromCache(sysOrgan.getUserCode());
        comms.put("thisMonth", new HashMap<String, Object>() {{
            put("CNName", "本月新增项目数");
            put("data", commsThisMonth.getOrDefault("comms", "0"));
        }});
        comms.put("comms", new HashMap<String, Object>() {{
            put("CNName", "项目数");
            put("data", commsAll.getOrDefault("comms", "0"));
        }});
        basic.put("comms", comms);

        // 收入统计
        Map<String, Object> income = service.sumHomeMainIncomeFromCache(
                sysOrgan.getUserCode(),
                organCode,
                commID
        );
        income.putAll(addedService.sumHomeMainIncomeFromCache(
                sysOrgan.getUserCode(),
                organCode,
                commID
        ));

        Map<String, Object> increasedThisMonth = basic.get("increasedThisMonth");
        if (sysOrgan.isCommLimited()) {
            increasedThisMonth.put("commsOrBuilding", new HashMap<String, Object>() {{
                put("CNName", "楼宇数(栋)");
                put("data", String.valueOf(resources.getOrDefault("building", "0")));
            }});
        } else {
            increasedThisMonth.put("commsOrBuilding", new HashMap<String, Object>() {{
                put("CNName", "项目数(个)");
                put("data", String.valueOf(commsThisMonth.getOrDefault("comms", "0")));
            }});
        }

        basic.put("income", income);

        return Response.response(basic);
    }

    /**
     * 基础资源数据 wrapper，适配后向兼容输出
     *
     * @param resource
     * @return
     */
    private Map<String, Map<String, Object>> summaryFlattenResponse(Map<String, Object> resource) {
        Map<String, Map<String, Object>> data = new HashMap<>();

        Map<String, Object> basic = new HashMap<>();
        basic.put("area", new HashMap<String, Object>() {{
            put("CNName", "面积");
            put("data", resource.getOrDefault("area", "0.00"));
        }});
        basic.put("owner", new HashMap<String, Object>() {{
            put("CNName", "业主数");
            put("data", resource.getOrDefault("owner", "0.00"));
        }});
        basic.put("house", new HashMap<String, Object>() {{
            put("CNName", "住宅数");
            put("data", resource.getOrDefault("house", "0.00"));
        }});
        basic.put("parking", new HashMap<String, Object>() {{
            put("CNName", "车位");
            put("data", resource.getOrDefault("parking", "0.00"));
        }});
        basic.put("business", new HashMap<String, Object>() {{
            put("CNName", "商铺");
            put("data", resource.getOrDefault("business", "0.00"));
        }});
        basic.put("building", new HashMap<String, Object>() {{
            put("CNName", "楼宇数");
            put("data", resource.getOrDefault("building", "0.00"));
        }});
        basic.put("room", new HashMap<String, Object>() {{
            put("CNName", "房屋数");
            put("data", resource.getOrDefault("room", "0.00"));
        }});
        basic.put("customer", new HashMap<String, Object>() {{
            put("CNName", "客户数");
            put("data", resource.getOrDefault("customer", "0.00"));
        }});


        Map<String, Object> area = new HashMap<>();
        area.put("area", new HashMap<String, Object>() {{
            put("CNName", "面积");
            put("data", resource.getOrDefault("area", "0.00"));
        }});
        area.put("inner", new HashMap<String, Object>() {{
            put("CNName", "套内面积");
            put("data", resource.getOrDefault("innerArea", "0.00"));
        }});
        area.put("share", new HashMap<String, Object>() {{
            put("CNName", "公摊面积");
            put("data", resource.getOrDefault("shareArea", "0.00"));
        }});
        area.put("garden", new HashMap<String, Object>() {{
            put("CNName", "花园面积");
            put("data", resource.getOrDefault("gardenArea", "0.00"));
        }});
        area.put("business", new HashMap<String, Object>() {{
            put("CNName", "商铺面积");
            put("data", resource.getOrDefault("businessArea", "0.00"));
        }});
        area.put("basement", new HashMap<String, Object>() {{
            put("CNName", "地下室面积");
            put("data", resource.getOrDefault("basementArea", "0.00"));
        }});
        area.put("house", new HashMap<String, Object>() {{
            put("CNName", "住宅面积");
            put("data", resource.getOrDefault("houseArea", "0.00"));
        }});

        Map<String, Object> increasedThisMonth = new HashMap<>();
        increasedThisMonth.put("area", new HashMap<String, Object>() {{
            put("CNName", "本月增加面积");
            put("data", resource.getOrDefault("monthNewArea", "0.00"));
        }});
        increasedThisMonth.put("owner", new HashMap<String, Object>() {{
            put("CNName", "本月增加业主数");
            put("data", resource.getOrDefault("monthNewOwner", "0.00"));
        }});
        increasedThisMonth.put("house", new HashMap<String, Object>() {{
            put("CNName", "本月增加住宅数");
            put("data", resource.getOrDefault("monthNewHouse", "0.00"));
        }});
        increasedThisMonth.put("parking", new HashMap<String, Object>() {{
            put("CNName", "本月增加车位");
            put("data", resource.getOrDefault("monthNewParking", "0.00"));
        }});
        increasedThisMonth.put("business", new HashMap<String, Object>() {{
            put("CNName", "本月增加商铺");
            put("data", resource.getOrDefault("monthNewBusiness", "0.00"));
        }});
        increasedThisMonth.put("building", new HashMap<String, Object>() {{
            put("CNName", "本月增加楼宇数");
            put("data", resource.getOrDefault("monthNewBuilding", "0.00"));
        }});
        increasedThisMonth.put("room", new HashMap<String, Object>() {{
            put("CNName", "本月增加房屋数");
            put("data", resource.getOrDefault("monthNewRoom", "0.00"));
        }});
        increasedThisMonth.put("customer", new HashMap<String, Object>() {{
            put("CNName", "本月增加客户数");
            put("data", resource.getOrDefault("monthNewCustomer", "0.00"));
        }});

        data.put("basic", basic);
        data.put("area", area);
        data.put("increasedThisMonth", increasedThisMonth);

        return data;
    }

    /**
     * 基础资源数据按省份汇总
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/province", produces = "application/json;charset=utf-8")
    public Object province(@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.sumByUserCodeByProvinceFromCache(
                sysOrgan.getUserCode(),
                organCode,
                commID
        ));
    }
}
