package com.huiju.estateagents.property.controller;

import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.service.*;
import com.google.common.collect.Maps;
import com.huiju.estateagents.service.ITaBuildingDynamicService;
import com.huiju.estateagents.service.ITaPointsExchangeService;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FXF
 * @date 2019-05-28
 */
@RestController
@RequestMapping("/api")
public class IndexCountController extends BaseController {
	
	@Autowired
	private IndexCountServiceI indexCountService;

	@Autowired
	BuildingTreeServiceI buildingTreeService;

	@Autowired
	IBuildingOwnerInfoService iBuildingOwnerInfoService;

	@Autowired
	IBillService iBillService;

	@Autowired
	TpTicketService tpTicketService;

	@Autowired
	ITaUserVerifyService iTaUserVerifyService;

	@Autowired
	ITaPointsExchangeService iTaPointsExchangeService;

	@Autowired
	ITaBuildingDynamicService iTaBuildingDynamicService;

	// @ApiOperation(value = "获取物业端首页所有统计数据", notes = "获取物业端首页所有统计数据")
	@ApiImplicitParams({

	})
	@RequestMapping(value = "/index/count", method = RequestMethod.GET)
	public ResponseBean getIndexCount(HttpServletRequest request){
		TaUser userElement = getTaUser(request);
		ResponseBean  responseBean = new ResponseBean();
		Map<String,Object> indexCountMap = Maps.newHashMap();
		try {
			indexCountMap = indexCountService.getIndexCount(userElement);
		}catch (Exception e){
			responseBean.addError(e.getMessage());
			return responseBean;
		}
		responseBean.addSuccess(indexCountMap);
		return responseBean;
	}

	@GetMapping("/admin/dashboard/statis")
	public ResponseBean getDashboardStatis(@RequestParam("startDate") String startDate,
										   @RequestParam("endDate") String endDate,
										   @RequestParam Integer communityId,
										   HttpServletRequest request) {
		Integer orgId = getOrgId(request);

		// 楼栋
		int buildingNum = buildingTreeService.getRoomNum(orgId, communityId);
		// 业主
		int propNum = iBuildingOwnerInfoService.getAvailableProp(orgId, communityId);
		// 缴费
		int houseNum = iBillService.getBillHouse(orgId, communityId, startDate, endDate);
		// 工单
		int ticketNum = tpTicketService.getTicketNum(orgId, communityId, startDate, endDate);

		Map<String, Object> res = new HashMap<>();
		res.put("buildingNum", buildingNum);
		res.put("propNum", propNum);
		res.put("houseNum", houseNum);
		res.put("ticketNum", ticketNum);

		return ResponseBean.success(res);
	}

	@GetMapping("/admin/dashboard/dynamic")
	public ResponseBean getDashboardDynamic(@RequestParam Integer communityId,
											@RequestParam("startDate") String startDate,
											@RequestParam("endDate") String endDate,
											HttpServletRequest request) {
		Integer orgId = getOrgId(request);

		// 工单
		List<Map<String, Object>> ticketList = tpTicketService.getTicketMapList(orgId, communityId, startDate, endDate);

		// 业主
		List<Map<String, Object>> propList = iTaUserVerifyService.getVerifyMapList(orgId, communityId, startDate, endDate);

		// 缴费
		List<Map<String, Object>> billList = iBillService.getBillMapList(orgId, communityId, startDate, endDate);

		Map<String, Object> res = new HashMap<>();
		res.put("ticketList", ticketList);
		res.put("propList", propList);
		res.put("billList", billList);

		return ResponseBean.success(res);
	}


	@GetMapping("/admin/dashboard/house-verified")
	public ResponseBean getDashboardHouseVerified(@RequestParam Integer communityId,
												  HttpServletRequest request) {
		Integer orgId = getOrgId(request);

		Map<String, Object> houseList = iBuildingOwnerInfoService.getVerifyNum(orgId, communityId);

		return ResponseBean.success(houseList);
	}

	@GetMapping("/admin/dashboard/tips")
	public ResponseBean getDashboardTips(@RequestParam Integer communityId,
										 HttpServletRequest request) {
		Integer orgId = getOrgId(request);

		// 待处理工单
		int ticketNum = tpTicketService.getStartNum(orgId, communityId);
		// 待审核业主
		int verifyNum = iTaUserVerifyService.getNoVerifyNum(orgId, communityId);
		// 兑换列表
		int exchangeNum = iTaPointsExchangeService.getTotalNum(orgId);
		// 近期在进行的活动
		Map<String, Integer> enlistNum = iTaBuildingDynamicService.getLastActEnlistNum(orgId);

		Map<String, Object> res = new HashMap<>();
		res.put("ticketNum", ticketNum);
		res.put("verifyNum", verifyNum);
		res.put("exchangeNum", exchangeNum);
		res.put("enlistNum", enlistNum);

		return ResponseBean.success(res);
	}
}
