package com.ssj.dao.weixin.sales.dao;

import com.ssj.bean.weixin.sales.domain.LibOrderDetails;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


/** 
 * @author  ZhangCaibao 
 * @version 2017年4月28日 下午3:09:31 
 * 类说明 
 */
@Repository
public interface LibOrderDetailsDao extends JpaRepository<LibOrderDetails, String>{

	@Query(nativeQuery = true, value = "SELECT  t.*  from  tb_lib_order_details   t  JOIN   tb_user  u \n" +
			"on  t.user_id  = u.id \n" +
			"where  u.wx_account =?1 ")
	public List<LibOrderDetails> getByUserId(String account);
	
	@Query(nativeQuery = true, value = "SELECT  t.*  from  tb_lib_order_details   t  where  t.order_no =?1 \n" )
	public  LibOrderDetails  getByOrderNo(String orderNo);
	
	@Query(nativeQuery = true, value = "SELECT DISTINCT u.wx_account  from  tb_lib_staffing  t join  tb_user u\n" +
			"on  t.user_id  = u.id \n" +
			"where t.lib_id  =?1  and  t.type = ?2")
	public List<String> getByStaffingOpenId(String libId,String type);
	
	@Query(nativeQuery = true, value = "SELECT \n" +
			"	auditor_open_id\n" +
			"FROM\n" +
			"	tb_lib_order_refunds t \n" +
			"WHERE\n" +
			"	t.order_details_id = ?2 \n" +
			"AND t.lib_id = ?1 and t.stat_void = 1 and t.id != ?3 ")
	public List<String> getOrderRefundsOpenId(String libId,String orderDetailsId,String id);
	
	@Query(nativeQuery = true, value ="SELECT  SUM(t.price) from  tb_lib_order_details  t join   tb_user u \n" +
			" on  t.user_id = u.id \n" +
			" where u.wx_account =?1 ")
	public Integer getSalesPrice(String account);
	
	@Query(nativeQuery = true, value ="SELECT  count(1) from   tb_lib_order_details t \n" +
			"where    t.lib_id =?1 and  t.vip_id =?2 and t.price_species IN ('S001', 'S002')  ")
	public Integer getUserHistory(String libId,String vipId); 
	
	@Query(nativeQuery = true, value ="SELECT TO_DAYS(NOW())-TO_DAYS(t.end_time) AS tian\n" +
			"from tb_lib_vip_service  t WHERE   t.end_time <= NOW() and t.stat='2'\n" +
			"AND t.lib_id =?1 AND t.vip_id =?2  AND t.species IN ('S001', 'S002') ")
	public  Integer getLibVipService(String libId,String vipId); 
	

	@Query(nativeQuery = true, value ="SELECT count(1) FROM tb_lib_vip_service t\n" +
			"WHERE t.vip_id =?2 AND t.lib_id =?1 AND t.stat = '1'  \n" +
			"AND t.species IN ('S001', 'S002')  ")
	public  Integer getLibVipServiceNumber(String libId,String vipId); 
	
	@Query(nativeQuery = true, value ="SELECT  count(1) from   tb_lib_order_details t \n" +
			"where    t.lib_id =?1 and  t.vip_id =?2 and t.price_species = 'S003' ")
	public Integer getUserHistoryVIP(String libId,String vipId); 
	
	@Query(nativeQuery = true, value ="SELECT  count(1) from  tb_lib_sales_price  \n" +
			" where id =?1 and  type ='C004' and species ='S003' and lib_id = ?2 ")
	public Integer isVipDc(String salesPriceId,String libId); 
	
	@Query(nativeQuery = true, value ="SELECT  v.id from  tb_lib_vip  v  join   tb_user u on v.user_id  = u.id \n" +
			"			 where u.wx_account =?1")
	public List<String> getLibVipId(String userId);
	/**
	 * 查询当月的销售业绩
	* @param salesUserId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年5月17日 下午3:36:49 
	* @Integer
	 */
	@Query(nativeQuery = true, value =" SELECT count(1) FROM tb_lib_order_details  WHERE DATE_FORMAT(create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )"
			+ " and sales_user_id = ?1 and lib_id=?2 ")
	public Integer monthlySales(String salesUserId ,String libId);
	
	@Query(nativeQuery = true, value = "update tb_lib_order_details set order_status = 2  where order_id = ?1 ")
	@Modifying
	public void updateOrderStatus(String orderId);
	
	
	@Query(nativeQuery = true, value =" SELECT id  FROM tb_lib_order_details  WHERE user_id = ? and vip_id is null ORDER BY end_time asc ")
	public List<String> findByUserIdAndVipIsNull(String userId);
	/**
	 * 查询该销售当月该馆的销售金额，
	* @param salesUserId
	* @return
	* 创建人：ZhangCaibao 
	* 2017年10月9日 上午10:15:59 
	* @Integer
	 */
	@Query(nativeQuery = true, value ="SELECT IFNULL(SUM(a.price), 0) AS price_total,count(a.user_id)  user_total from  (\n" +
			"SELECT\n" +
			"	\n" +
			"  price,\n" +
			"	user_id\n" +
			"FROM\n" +
			"	tb_lib_order_details\n" +
			"WHERE\n" +
			"	lib_id =?2\n" +
			"and DATE_FORMAT(create_time, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')\n" +
			"and sales_user_id =?1\n" +
			"GROUP BY order_no \n" +
			") a ")
	public List<Object[]> monthlySalesUser(String salesUserId,String libId);
	
   @Query(nativeQuery = true,value="SELECT IFNULL(SUM(a.price), 0) AS price_total,count(a.user_id)  user_total from  ( \n" +
		   "SELECT \n" +
		   "	\n" +
		   "  price,\n" +
		   "	user_id \n" +
		   "FROM \n" +
		   "	tb_lib_order_details \n" +
		   "WHERE\n" +
		   "	DATE_FORMAT(create_time, '%Y%m') = DATE_FORMAT(CURDATE() , '%Y%m') \n" +
		   "and lib_id =?1 \n" +
		   "GROUP BY order_no \n" +
		   ") a ")
   public List<Object[]> getlibPriceTotal(String libId);
   
   /**
    * 自动处理过期的服务，改状态为2
   * 
   * 创建人：ZhangCaibao 
   * 2017年8月29日 下午4:09:27 
   * @void
    */
	@Query(nativeQuery = true, value = " UPDATE  tb_lib_vip_service  set  stat=2  where  TO_DAYS(end_time) < TO_DAYS(now()) and  stat =1 ")
	@Modifying
	public void updateVipServiceStat();


	/**
	 * 查询一段时间（当月）购买作业吧的金额
	 * @param vipId
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	@Query(nativeQuery = true, value ="select sum(price) price from tb_lib_order_details where price_species='S003' and vip_id=?1 and create_time between ?2 and ?3")
	public Float findPriceByVipId (String vipId, String beginTime, String endTime);

	/**
	 * 查询某段时间最后一次购买的作业吧记录
	 * @param vipId
	 * @return
	 */
	@Query(nativeQuery = true, value ="select * from tb_lib_order_details where price_species='S003' and vip_id= ?1 order by create_time desc limit 1")
	public LibOrderDetails findFirstByVipId(String vipId);

	@Query(nativeQuery = true, value ="select * from tb_lib_order_details where vip_id=?1 and price_species='S003'")
	public List<LibOrderDetails> findOrderDetailByVipId (String vipId);

	/**
	 * 查看指定会员的服务购买记录
	 * @param vipId
	 * @param priceSpecies
	 * @return
	 */
	List<LibOrderDetails> findByVipIdAndPriceSpeciesAndOrderStatusOrderByCreateTimeDesc(String vipId, String priceSpecies, int orderStatus);

	/**
	 * 查看指定会员的服务购买记录
	 * @param vipId
	 * @param priceSpecies
	 * @param orderStatus
	 * @param appType
	 * @return
	 */
	List<LibOrderDetails> findByVipIdAndPriceSpeciesAndOrderStatusAndAppTypeOrderByCreateTimeDesc(String vipId, String priceSpecies, int orderStatus, int appType);
}
