package com.ifish.daoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.ifish.dao.UserDao;
import com.ifish.dto.ShopsLookUserDto;
import com.ifish.entity.User;
import com.ifish.enums.BooleanEnum;
import com.ifish.hibernate.HibernateBaseDao;
import com.ifish.hibernate.Pagination;

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

@Repository
public class UserDaoImpl extends HibernateBaseDao<User, Integer> implements UserDao {

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


	@Override
	public User update(User user) {
		getSession().update(user);
		return user;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ShopsLookUserDto> findByCriteria(Integer shopsUserId,Integer firstResult, Integer pageSize) {
		String sql = "select u.user_id as userId,u.nick_name as nickName,c.camera_id as cameraId,u.user_img as userImg from tbl_user u LEFT JOIN tbl_camera_user c ON u.user_id=c.user_id where c.is_look=1 and shops_user_id="+shopsUserId;
		List<ShopsLookUserDto> list = this.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(ShopsLookUserDto.class)).list();
		Pagination<ShopsLookUserDto> page = new Pagination<ShopsLookUserDto>();
		page.setTotalCount(list.size());
		page.setList(list);
		return page;
	}


	@Override
	public int executeLoginUpdate(Integer userId,String loginType) {
		String sql = "update tbl_user set login_count=if(login_count is null,1,login_count+1),login_type=?,login_time=current_timestamp() where user_id=?";
		Query query = this.getSession().createSQLQuery(sql).setString(0, loginType).setInteger(1, userId);
		return query.executeUpdate();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Integer> getUserIds(Integer userId,Integer firstResult, int maxResults) {
		String sql = "select user_id from tbl_user where user_id!=? and is_register_netease=? limit ?,?";
		List<Integer> list = this.getSession().createSQLQuery(sql).setInteger(0, userId).setString(1, BooleanEnum.YES.getKey()).setInteger(2, firstResult).setInteger(3,maxResults).list();
		return list;
	}

}
