package com.palmnest.service.dinner.manager.impl;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.dao.dinner.MealCarDao;
import com.palmnest.dao.dinner.MealDao;
import com.palmnest.model.dinner.Meal;
import com.palmnest.model.dinner.MealCar;
import com.palmnest.service.dinner.manager.MealCarManager;
import com.palmnest.service.impl.GenericManagerImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service("mealCarManager")
public class MealCarManagerImpl extends GenericManagerImpl<MealCar, Long> implements MealCarManager {


	private MealCarDao mealCarDao;

	@Autowired
	public void setMealCarDao(MealCarDao mealCarDao) {
		this.mealCarDao = mealCarDao;
		this.dao = mealCarDao;
	}

	@Autowired
	private MealDao mealDao;

	/**
	 * 清空购物车
	 *
	 * @param consId 登录用户
	 * @return
	 */
	@Override
	public int removeMealCar(String consId) {
		return mealCarDao.removeMealCar(consId);
	}

	/**
	 * 获取当前用户购物车中的餐品
	 *
	 * @param consId
	 * @return
	 */
	@Override
	public List<MealCar> findMealCarListByConsId(String consId, String isEnabled) {
		return mealCarDao.findMealCarListByConsId(consId, isEnabled);
	}

	@Override
	public int getMealCarByUserIdAndTime(String userId, String time,
										 String operator) {
		return mealCarDao.getMealCarByUserIdAndTime(userId, time, operator);
	}

	@Override
	public List<MealCar> findMealCarListByUserId(String userId, String time) {
		return mealCarDao.findMealCarListByUserId(userId, time);
	}

	@Override
	public List<MealCar> findMealCarListByUserId(String userId) {
		return mealCarDao.findMealCarListByUserId(userId);
	}

	@Override
	public int findMealCarListByUserId(String userId, String time,
									   String mealId) {
		return mealCarDao.findMealCarListByUserId(userId, time, mealId);
	}

	@Override
	public List<MealCar> getMealCarByParemens(Map<String, String> parements) {
		return mealCarDao.getMealCarByParemens(parements);
	}

	@Override
	public List<Map<String, Object>> getMealCarGroupFood(
			Map<String, String> parements) {
//		List<Map<String, Object>> resList=new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> list = mealCarDao.getMealCarGroupFood(parements);
//		if(list!=null&&list.size()>0){
//			for(int i=0;i<list.size();i++){
//				Map<String, Object> map=list.get(i);
//				if(map!=null){
//					String usersStr=map.get("users")==null?"":map.get("users").toString();
//					if(StringUtils.isNotEmpty(usersStr)){
//						String users="";
//						String [] str=usersStr.split(",");
//						if(str!=null&&str.length>0){
//							for(int k=0;k<str.length;k++){
//								if(StringUtils.isNotEmpty(str[k])){
//									users=users+str[k]+",";
//								}
//							}
//							if(StringUtils.isNotEmpty(users)){
//								users=users.substring(0,users.length()-1);
//								parements.put("users", users);
//								List<Map<String, Object>> mapList=mealCarDao.getMealCarByStapleFood(parements);
//								if(mapList!=null&&mapList.size()>0){
//									Map<String, Object> usersMap=mapList.get(0);
//									if(usersMap!=null){
//										map.put("grayUsers", usersMap.get("grayUsers"));
//									}
//								}
//							}
//						}
//					}
//					resList.add(map);
//				}
//			}
//		}
		return list;
	}

	@Override
	public int removeMealCarList(String userId, int type) {
		return mealCarDao.removeMealCarList(userId, type);
	}

	@Override
	public int findByUserId(String userId, String time) {
		return mealCarDao.findByUserId(userId, time);
	}

	@Override
	public Map<String, Object> getMealUser(Map<String, String> parements) {
		Map<String, Object> map    = new HashMap<String, Object>();
		String              mealId = parements.get("mealId");
		if (mealDao.exists(Long.parseLong(mealId))) {
			Meal meal = mealDao.get(Long.parseLong(mealId));
			if (meal != null) {
				String stapleFood = meal.getStapleFood();
				if (StringUtils.isNotEmpty(stapleFood)) {//组合
					String[] stapleFoodArr = stapleFood.split(";");
					if (stapleFoodArr != null && stapleFoodArr.length > 0) {
						for (int i = 0; i < stapleFoodArr.length; i++) {
							parements.put("stapleFood", stapleFoodArr[i]);
							Map<String, Object>       usersMap = new HashMap<String, Object>();
							List<Map<String, Object>> mapList  = mealCarDao.getMealCarGroupFood(parements);
							String                    users    = "";
							if (mapList != null && mapList.size() > 0) {
								if (mapList.get(0) != null) {
									if (mapList.get(0).get("users") != null) {
										users = mapList.get(0).get("users").toString();
									}
								}
							}
							usersMap.put("users", users);
							parements.put("users", users);
							List<Map<String, Object>> grayUsersList = mealCarDao.getMealCarByStapleFood(parements);
							String                    grayUsers     = "";
							if (grayUsersList != null && grayUsersList.size() > 0) {
								if (grayUsersList.get(0) != null) {
									if (grayUsersList.get(0).get("grayUsers") != null) {
										grayUsers = grayUsersList.get(0).get("grayUsers").toString();
									}
								}
							}
							usersMap.put("grayUsers", grayUsers);
							map.put(stapleFoodArr[i], usersMap);
						}
					}
				} else {//单品
					Map<String, Object>       usersMap = new HashMap<String, Object>();
					List<Map<String, Object>> mapList  = mealCarDao.getMealCarGroupFood(parements);
					String                    users    = "";
					if (mapList != null && mapList.size() > 0) {
						if (mapList.get(0) != null) {
							if (mapList.get(0).get("users") != null) {
								users = mapList.get(0).get("users").toString();
							}
						}
					}
					usersMap.put("users", users);
					parements.put("users", users);
					List<Map<String, Object>> grayUsersList = mealCarDao.getMealCarByStapleFood(parements);
					String                    grayUsers     = "";
					if (grayUsersList != null && grayUsersList.size() > 0) {
						if (grayUsersList.get(0) != null) {
							if (grayUsersList.get(0).get("grayUsers") != null) {
								grayUsers = grayUsersList.get(0).get("grayUsers").toString();
							}
						}
					}
					usersMap.put("grayUsers", grayUsers);
					map.put(parements.get("mealId"), usersMap);
				}
			}
		}
		return map;
	}

	@Override
	public int getMealCarList(String userId, String orgId) {
		return mealCarDao.getMealCarList(userId, orgId);
	}

	@Override
	public int getMealCarCountByMealId(String userId,
									   String time, String mealId) {
		return mealCarDao.getMealCarCountByMealId(userId, time, mealId);
	}

}
