package com.wechat.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wechat.common.CommonEnum;
import com.wechat.common.utils.StringsUtils;
import com.wechat.dao.WechatMenuDao;
import com.wechat.model.dbEntity.TdtgWechatMenu;


/**
 * 团购服务提供类
 * 
 */
@Service
public class GroupService {

    @Resource
    private WechatMenuDao wechatMenuDao;
    
    /**
     * 获取主菜单
     * 
     * @return
     */
    public List<TdtgWechatMenu> getMainList()
    {
    	return wechatMenuDao.getMainList();
    }
    
    /**
     * 获取子菜单
     * 
     * @param parentMenuId 上级菜单ID
     * @return
     */
    public List<TdtgWechatMenu> getChildList(int parentMenuId)
    {
    	return wechatMenuDao.getChildList(parentMenuId);
    }
    
    /**
     * 获取微信菜单(json)
     * 
     */
    public JSONObject getWechatMenu() throws Exception{
        JSONObject menuObj = new JSONObject();
    	JSONArray menuAry = new JSONArray();
        //取得一级菜单
        List<TdtgWechatMenu> mainList = wechatMenuDao.getMainList();
        if(!StringsUtils.isListEmpty(mainList))
        {
        	for(TdtgWechatMenu mainDto : mainList)
        	{
        		JSONObject mainJson = new JSONObject();
        		mainJson.put("name", mainDto.getMenuName());
        		//获取子菜单
        		List<TdtgWechatMenu> childList = wechatMenuDao.getChildList(mainDto.getMenuId());
        		JSONArray childAry = new JSONArray();
        		for(TdtgWechatMenu childDto : childList)
            	{
        			JSONObject childJson = new JSONObject();
        			//子菜单类型
        			String subBtnType = childDto.getSubBtnType();
        			//json拼装
        			childJson.put("type", subBtnType);
        			childJson.put("name", childDto.getMenuName());
        			childJson.put(CommonEnum.SUB_BTN_TYPE.getEnumLabel(subBtnType), childDto.getMenuContent());
        			//子菜单数组时装
        			childAry.add(childJson);
            	}
        		mainJson.put("sub_button", childAry);
        		//菜单列表时装
        		menuAry.add(mainJson);
        	}
        }
      
        //菜单设置用json串
        menuObj.put("button", menuAry);
        return menuObj;
    }

    
}
