package com.ifish.daoImpl;

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

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

import com.ifish.dao.ShopsInfoDao;
import com.ifish.dto.ShopsListDto;
import com.ifish.entity.ShopsInfo;
import com.ifish.enums.BooleanEnum;
import com.ifish.enums.ShopsStatusEnum;
import com.ifish.hibernate.HibernateBaseDao;
import com.ifish.hibernate.Pagination;

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

@Repository
public class ShopsInfoDaoImpl extends HibernateBaseDao<ShopsInfo, Integer>  implements ShopsInfoDao {

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

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ShopsListDto> findByDistance(Double longitude,Double latitude,Integer firstResult, Integer pageSize) {
		//总条数
		String countSql = "select count(*) from tbl_shops_info where status=?";
		int totalCount = ((BigInteger) this.getSession().createSQLQuery(countSql)
		.setParameter(0, ShopsStatusEnum.status1.getKey())
		.uniqueResult()).intValue();
		String sql ="select s.shops_id shopsId,s.is_ping_bi as isPingBi,s.user_id AS userId,u.user_img AS userImg,u.nick_name AS userName,s.shops_name AS shopsName,s.shops_phone AS shopsPhone,s.shops_province AS shopsProvince,s.shops_city AS shopsCity,s.shops_area AS shopsArea,s.shops_address AS shopsAddress,s.picture4 AS picture4,s.remark AS remark,s.weixin_code AS weixinCode,s.shop_link AS shopLink,ifnull(round(6378.138*2*asin(sqrt(pow(sin((?*pi()/180-u.latitude*pi()/180)/2),2)+cos(?*pi()/180)*cos(u.latitude*pi()/180)*pow(sin((?*pi()/180-u.longitude*pi()/180)/2),2)))*1000),99999999999) AS distance from tbl_shops_info s LEFT JOIN tbl_user u ON s.user_id=u.user_id where s.status=? and app_show=? ORDER BY distance ASC LIMIT ?,?";
		//分页数据
		List<ShopsListDto> list =  this.getSession().createSQLQuery(sql)
				.setParameter(0, latitude)
				.setParameter(1, latitude)
				.setParameter(2, longitude)
				.setParameter(3, ShopsStatusEnum.status1.getKey())
				.setParameter(4, BooleanEnum.YES.getKey())
				.setParameter(5, firstResult)
				.setParameter(6, pageSize)
				.setResultTransformer(Transformers.aliasToBean(ShopsListDto.class))
				.list();
		String pingbiMsg = "请通过下方咨询联系我";
		for (ShopsListDto shopsList : list) {
			if(shopsList.getIsPingBi().equals(BooleanEnum.YES.getKey())){
				shopsList.setShopsAddress("");
				shopsList.setShopsPhone(pingbiMsg);
				shopsList.setWeixinCode(pingbiMsg);
				shopsList.setShopLink(pingbiMsg);
			}
		}
		//分页封装
		Pagination<ShopsListDto> page = new Pagination<ShopsListDto>();
		page.setTotalCount(totalCount);
		page.setList(list);
		return page;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Integer> getShopsUserIdByRand(Integer userId) {
		String sql = "SELECT user_id FROM tbl_shops_info WHERE status='1' AND user_id!=? AND user_id!=12 AND app_show='1' ORDER BY RAND() LIMIT 9";
		List<Integer> list = this.getSession().createSQLQuery(sql).setParameter(0, userId).list();
		//固定发送给小陈
		list.add(12);
		return list;
	}

}
