package com.palmnest.dao.mealCard.impl;

import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.mealCard.MealCardDao;
import com.palmnest.model.mealcard.MealCardUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wangxz
 * <p>
 * 2019年6月15日
 */
@Slf4j
@Repository(value = "mealCardDao")
public class MealCardDaoImpl extends GenericDaoHibernate<MealCardUser, Long> implements MealCardDao {


	public MealCardDaoImpl() {
		super(MealCardUser.class);
	}


	@Override
	public MealCardUser getMealCardUser(String openId, String developId, String mobile) {

		String sql = "select * from tb_meal_card_user where isEnable=1 AND developId='" + developId + "'";

		if (StringUtils.isNotBlank(mobile)) {
			sql += " AND mobile='" + mobile + "'";
		}

		if (StringUtils.isNotBlank(openId)) {
			sql += " AND openId='" + openId + "'";
		}

		List<MealCardUser> list = getSession().createSQLQuery(sql).addEntity(MealCardUser.class).list();

		if (list != null && list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	@Override
	public MealCardUser getMealCardUser(String userMobile) {

		String sql = "select * from tb_meal_card_user where isEnable=1 AND mobile=?";

		List<MealCardUser> list = getSession().createSQLQuery(sql).addEntity(MealCardUser.class).setString(0, userMobile).list();

		if (list != null && list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

}
