package com.ifish.daoImpl;

import com.ifish.dao.DevicePetUserDao;
import com.ifish.dao.DeviceUserDao;
import com.ifish.entity.DevicePetUser;
import com.ifish.entity.DeviceUser;
import com.ifish.entity.DeviceUserId;
import com.ifish.hibernate.HibernateBaseDao;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;

/** 
 * @ClassName: DeviceUserDaoImpl
 * @Description: TODO 
 * @author ggw 
 *  
 */

@Repository
public class DevicePeiUserDaoImpl extends HibernateBaseDao<DevicePetUser, DeviceUserId> implements DevicePetUserDao {

	@Override
	protected Class<DevicePetUser> getEntityClass() {
		return DevicePetUser.class;
	}

	@Override
	public void removeAll(List<DevicePetUser> list) {
		for (DevicePetUser devicePetUser:list) {
			delete(devicePetUser);
		}
	}

	/**
	 * 查询用户的设备数量
	 * @param userId
	 * @return
	 */
	@Override
	public int getDeviceNumById(Integer userId) {
		String sql = "select count(*) from tbl_pet_list where user_id=?";
		int deviceNum = ((BigInteger)this.getSession().createSQLQuery(sql).setInteger(0, userId).uniqueResult()).intValue();
		return deviceNum;
	}

	/**
	 * 查询宠物店
	 * @param userId
	 * @return
	 */
	@Override
	public List<String> getDeviceStoreName(Integer userId) {
		String sql = "select store_name from tbl_pet_list where user_id=?  group by store_name";
		List<String> list = this.getSession().createSQLQuery(sql).setInteger(0, userId).list();
		return list;
	}

	@Override
	public List getDeviceIdByStoreNameAndUserId(Integer userId, String storeName) {
		String sql = "select device_id, device_name from tbl_pet_list where user_id=? and store_name = ?";
		List list = this.getSession().createSQLQuery(sql).setInteger(0, userId).setString(1, storeName).list();
		return list;
	}

	@Override
	public void updateStoreName(int userId, String oldName, String newName, String storeName) {
		String sql = "update tbl_pet_list set device_name = ?,store_name=? where user_id = ? and device_name = ?";
		int i = this.getSession().createSQLQuery(sql).setString(0, newName).setString(1, storeName).setInteger(2, userId).setString(3, oldName).executeUpdate();
		System.out.println("updateStoreName : " + i);
	}

	/**
	 * 根据设备ID查询设备用户对应关系表
	 * @param userId
	 * @return
	 */
	@Override
	public List<DevicePetUser> getUserIdByDeviceId(Integer userId) {
		Criterion criterion = Restrictions.eq("priId.userId",userId);
		List<DevicePetUser> devicePetUsers = this.findByProperty(criterion);
		return devicePetUsers;
	}

	/**
	 *
	 * @param deviceId
	 * @return
	 */
	@Override
	public List<DevicePetUser> findByDeviceId(Integer deviceId) {
		Criterion criterion = Restrictions.eq("priId.deviceId",deviceId);
		List<DevicePetUser> devicePetUsers = this.findByProperty(criterion);
		return devicePetUsers;
	}

	@Override
	public DevicePetUser getDevicePetUserByUseridAndDeviceId(String userId, String deviceId) {

		return null;
	}
}
