package com.community.huiju.controller;

import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.model.TaSysMenu;
import com.community.huiju.model.ToBanner;
import com.community.huiju.service.BannerServiceI;
import com.community.huiju.service.MenuServiceI;
import com.community.huiju.service.MessageServiceI;
import com.community.huiju.service.TpHotelServicel;
import com.community.huiju.service.TpShopServicel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author FXF
 * @date 2018-11-05
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "首页聚合API", description = "首页聚合API")
public class AssembleController extends BaseController {
	
	@Autowired
	private MenuServiceI menuService;
	
	@Autowired
	private BannerServiceI bannerService;
	
	@Autowired
	private MessageServiceI messageService;
	
	@Autowired
	private TpHotelServicel tpHotelServicel;
	
	@Autowired
	private TpShopServicel tpShopServicel;
	
	@ApiOperation(value = "首页数据获取接口", notes = "首页数据获取接口")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", dataType = "String", name = "communityId", value = "小区Id"),
			@ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token") })
	@RequestMapping(value = "/index/{communityId}",method = RequestMethod.GET)
	public ResponseBean getIndexData(@PathVariable("communityId") Integer communityId, HttpSession session){
		UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
		Integer userId = userElement.getId();
		ResponseBean responseBean = new ResponseBean();

		if (!check(communityId)) {
			responseBean.addError("小区不存在");
			return responseBean;
		}

		Map<String,Object> indexMap = new HashMap<>();
		//获取菜单数据
		List<TaSysMenu> menuList = menuService.getMenuList(communityId);
		//获取首页banner数据
		List<ToBanner> toBannerList = bannerService.viewAllBannerImg(1);
		//获取消息总数接口
		Map<String,Object> totalMap = messageService.getMessageTotal(communityId, userElement.getUserVerifyId());
		//获取当前小区的酒店信息
		ResponseBean hotelRes = tpHotelServicel.getHotelCommunity(userElement);
		//获取当前小区的商铺信息
		ResponseBean shopRes = tpShopServicel.getShopCommunity(userElement);
		
		//组装数据
		indexMap.put("menuList",menuList);
		indexMap.put("bannerList",toBannerList);
		indexMap.put("messageTotal",totalMap.get("messageTotal"));
		indexMap.put("tpHotel",hotelRes.getData());
		indexMap.put("tpShopType",((Map<Object, Object>)shopRes.getData()).get("tpShopType"));
		indexMap.put("tpShopList",((Map<Object, Object>)shopRes.getData()).get("tpShopList"));
		indexMap.put("toDoTotal",totalMap.get("toDoTotal"));
		responseBean.addSuccess(indexMap);
		return responseBean;
	}
}
