package com.ssj.dao.weixin.adminsale.dao.impl;

import com.ssj.dao.weixin.adminsale.dao.AdminSaleQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class AdminSaleQueryDaoImpl implements AdminSaleQueryDao {

	@Autowired
	private PagingHibernateJdbcDao dao;
	
	
	@Override
	public Page<Map<String, Object>> findVipBySaleAndLibIdPage(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  SELECT v.child_name,tmp1.ryke_time,tmp.price,u.photo,v.id,tmp.end_time from ( ");
        selSQL.append("  	select 0 as price,a.end_time,a.vip_id from tb_lib_vip_service as a INNER JOIN ");
        selSQL.append(" (select vip_id, max(create_time) as create_time from tb_lib_vip_service where  stat = 1 ");
         if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
        	selSQL.append(" and lib_id =? ");
        	queryParams.add(searchParams.get("libId"));
        }
        selSQL.append(" group by vip_id) as b on a.vip_id = b.vip_id and a.create_time = b.create_time   ");
        selSQL.append(" )as tmp  ");
        selSQL.append("  LEFT JOIN ( ");
        selSQL.append("  		select s.* from tb_lib_subscribe as s INNER JOIN ");
        selSQL.append("  (select vip_id, max(ryke_time) as ryke_time from tb_lib_subscribe  group by vip_id) as ss on s.vip_id = ss.vip_id and s.ryke_time = ss.ryke_time ");
        selSQL.append("  	) as tmp1 ON tmp.vip_id=tmp1.vip_id");
        selSQL.append("  LEFT JOIN tb_lib_vip v ON v.id=tmp.vip_id ");
        selSQL.append("  LEFT JOIN tb_user u ON u.id=v.user_id ");
       
        if (searchParams.get("orderBy") != null && searchParams.get("orderBy").equals("price")) {
        	 selSQL.append("  where v.user_id is not NULL ");
        	 selSQL.append(" ORDER BY tmp.price desc ");
        }else if (searchParams.get("orderBy") != null && searchParams.get("orderBy").equals("rykeTime")) {
        	 selSQL.append("  where v.user_id is not NULL ");
        	 selSQL.append(" ORDER BY tmp1.ryke_time  desc ");
        }else{
        	 selSQL.append(" where TO_DAYS(tmp.end_time)>=TO_DAYS(NOW()) AND v.user_id IS NOT NULL ");
        	 selSQL.append(" ORDER BY tmp.end_time  ASC ");
        }
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}


	@Override
	public List<Map<String, Object>> findLibOrderDetailsByVipId(String vipId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.create_time,so.`name` AS species,sr.`name` as type ,s.`name` from tb_lib_order_details t ");
        selSql.append(" LEFT JOIN tb_lib_staffing s on t.sales_user_id=s.user_id and t.lib_id = s.lib_id ");
        selSql.append(" LEFT JOIN sys_sort so ON t.price_species=so.`code` ");
        selSql.append(" LEFT JOIN sys_sort sr ON t.price_type=sr.`code` ");
        selSql.append(" where t.order_status = 1  AND t.vip_id=? ORDER BY t.create_time desc  ");
        queryParams.add(vipId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public List<Map<String, Object>> findBirthdayBySale(String saleUserId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT  t.child_name,t.birthday,u.photo from tb_lib_vip t  ");
        selSql.append(" LEFT JOIN tb_lib_order_details o ON t.id=o.vip_id  ");
        selSql.append(" LEFT JOIN tb_user u ON t.user_id=u.id ");
        selSql.append(" WHERE o.sales_user_id=? and o.order_status = 1  AND TO_DAYS(NOW())=TO_DAYS(t.birthday) GROUP BY t.id ");
        queryParams.add(saleUserId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public List<Map<String, Object>> findServiceEndBySale(String saleUserId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT (TO_DAYS(end_time)-TO_DAYS(NOW( ))+1) as day,s.*,u.photo ,v.child_name   from (  ");
        selSql.append(" SELECT DISTINCT(vip_id) as vip_id from tb_lib_order_details WHERE sales_user_id=? and order_status = 1  ");
        selSql.append(" ) as o LEFT JOIN tb_lib_vip_service s ON o.vip_id=s.vip_id  ");
        selSql.append(" LEFT JOIN tb_lib_vip v ON s.vip_id=v.id LEFT JOIN tb_user u ON v.user_id=u.id ");
        selSql.append(" where s.stat=1  and TO_DAYS(s.end_time)-TO_DAYS(NOW( ))+1 BETWEEN 1 AND 3 ");
        queryParams.add(saleUserId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public List<Map<String, Object>> findLibOrderDetailsServiceBySale(String saleUserId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT  t.*,u.photo,v.child_name from tb_lib_order_details t LEFT JOIN tb_lib_vip v ON t.vip_id=v.id ");
        selSql.append(" LEFT JOIN tb_user u ON v.user_id=u.id ");
        selSql.append(" WHERE t.sales_user_id=? AND TO_DAYS(NOW())=TO_DAYS(t.create_time) and  t.order_status = 1   ");
        selSql.append(" ORDER BY t.create_time DESC ");
        queryParams.add(saleUserId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public List<Map<String, Object>> findSilentUserBySale(String saleUserId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT v.child_name,v.id,u.photo from ( ");
        selSql.append("	SELECT * from ( ");
        selSql.append(" 	SELECT DISTINCT(t.vip_id) as vip_id from tb_lib_order_details t    ");
        selSql.append(" 	LEFT JOIN tb_lib_vip_service s ON t.vip_id=s.vip_id ");
        selSql.append(" WHERE s.stat=1 AND t.sales_user_id=?   AND s.start_time <=date_sub(now(), interval 60 day) ");
        selSql.append(" ) as tmp LEFT JOIN (  ");
        selSql.append(" SELECT  DISTINCT(su.vip_id) as vip_id1  from tb_lib_subscribe su where su.ryke_time is not null AND su.ryke_time >=date_sub(now(), interval 60 day) ");
        selSql.append(" 	) as tmp2 ON tmp.vip_id=tmp2.vip_id1  ");
        selSql.append(" ) as tmp3  ");
        selSql.append(" LEFT JOIN tb_lib_vip v ON tmp3.vip_id=v.id ");
        selSql.append(" LEFT JOIN tb_user u ON u.id=v.user_id ");
        selSql.append(" where tmp3.vip_id1 IS NULL ");
        queryParams.add(saleUserId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public List<Map<String, Object>> findMonthDetailBySale(String saleUserId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT SUM(t.price) as countPrice,COUNT(1) as num,t.price_species,t.price_type,ss.`name` as pricespecies,sss.`name` as pricetype from tb_lib_order_details t  ");
        selSql.append(" LEFT JOIN sys_sort ss ON t.price_species=ss.`code` ");
        selSql.append(" LEFT JOIN sys_sort sss ON t.price_type=sss.`code`   ");
        selSql.append(" where  t.order_status=1 AND t.lib_id=?  AND t.create_time >=date_sub(now(), interval 30 day) GROUP BY t.price_species,t.price_type");
        queryParams.add(saleUserId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public List<Map<String, Object>>  findCountOrderBySale(String saleUserId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("  SELECT IFNULL(SUM(t.price),0) as countPrice,IFNULL(COUNT(1),0) as num,t.price_species,t.price_type,t.type from tb_lib_order_details t     ");
        selSql.append("      where  t.order_status=1  AND t.sales_user_id=?   GROUP BY t.price_species,t.price_type,t.type");
        queryParams.add(saleUserId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}



	@Override
	public Page<Map<String, Object>> findOrderRankByLibId(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  SELECT * from ( ");
        selSQL.append("  	SELECT tmp.*, @curRank1 \\:= @curRank1 + 1 AS rank from ( ");
        selSQL.append(" 	SELECT SUM(t.price) as countPrice,s.*,u.photo from tb_lib_order_details  t ");
        selSQL.append(" 		LEFT JOIN tb_lib_staffing s ON t.sales_user_id=s.user_id  LEFT JOIN tb_user u ON u.id=t.sales_user_id  ");
        selSQL.append(" where t.order_status=1 AND  s.user_id= ?  ");
        selSQL.append(" GROUP BY t.sales_user_id ");
        selSQL.append("  		ORDER BY countPrice DESC)as tmp,(SELECT @curRank1 \\:= 0) r1 ");
        selSQL.append("  ) as tmp1  ");
        selSQL.append("  	UNION ");
        selSQL.append("  SELECT tmp.*, @curRank \\:= @curRank + 1 AS rank from ( ");
        selSQL.append(" SELECT SUM(t.price) as countPrice,s.*,u.photo from tb_lib_order_details  t  ");
        selSQL.append(" 	LEFT JOIN tb_lib_staffing s ON t.sales_user_id=s.user_id 	LEFT JOIN tb_user u ON u.id=t.sales_user_id  ");
        selSQL.append(" where t.order_status=1 and t.lib_id = ? ");
        selSQL.append(" GROUP BY t.sales_user_id  ");
        selSQL.append(" ORDER BY countPrice DESC)as tmp,(SELECT @curRank \\:= 0) r  ");
        queryParams.add(searchParams.get("saleUserId"));
        queryParams.add(searchParams.get("libId"));
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}


	@Override
	public List<Map<String, Object>>  findExperienceBySale(String saleUserId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,u.photo FROM `tb_experience` t LEFT JOIN tb_user u ON u.id=t.user_id where t.type=3 AND t.sale_user_id=? AND TO_DAYS(t.date_time) = TO_DAYS(NOW())  ORDER BY t.date_time desc ");
        queryParams.add(saleUserId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public List<Map<String, Object>> findServiceByVipId(String vipId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,sf.`name` as sfname,ss.`name` as pricespecies,sss.`name` as pricetype from tb_lib_vip_service t  ");
        selSql.append(" LEFT JOIN tb_lib_order_details d ON t.order_details_id=d.id ");
        selSql.append(" 	LEFT JOIN tb_lib_staffing sf ON d.sales_user_id=sf.user_id and d.lib_id = sf.lib_id ");
        selSql.append("  LEFT JOIN sys_sort ss ON t.species=ss.`code`  ");
        selSql.append("   LEFT JOIN sys_sort sss ON t.type=sss.`code`    ");
        selSql.append(" where t.vip_id=?  ");
        selSql.append(" ORDER BY t.start_time desc ");
        queryParams.add(vipId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	
	@Override
	public List<Map<String, Object>> findSaleByUserId(String saleUserId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" select  * from  tb_lib_staffing  t where user_id = ? AND t.stat=1 AND t.lib_id not  LIKE 'SSJ%' ");
        queryParams.add(saleUserId);
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public Map<String, Object> findCountOrderByLib(String libId) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("  SELECT floor(IFNULL(SUM(t.price),0)) as countPrice from (    ");
        selSql.append("  select DISTINCT a.order_no,a.price from tb_lib_order_details  a ");
        selSql.append("      where  a.order_status=1  AND a.lib_id=?   ");
        selSql.append("    )t  ");
        queryParams.add(libId);
        List<Map<String, Object>> list = dao.findMap(selSql.toString(), queryParams.toArray());
		return list ==null ?null:list.get(0);
	}


	@Override
	public List<Map<String, Object>> findCdLibList(String userId,String searchContent) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t11.id,t11.league_name,GROUP_CONCAT(DISTINCT ifnull(t12.`name`,t13.wx_name)) as cdaname,GROUP_CONCAT(DISTINCT ifnull(t14.`name`,t16.wx_name)) as cdname ,GROUP_CONCAT( DISTINCT ifnull(t15.`name`,t17.wx_name)) as glname   \n");
        selSql.append(" from tb_lib_manager t \n");
        selSql.append(" inner join tb_lib_join t11 on t.lib_id=t11.id \n");
        selSql.append(" left join tb_lib_manager t12 on t.lib_id = t12.lib_id and t12.type=1 and t12.libtype=2 \n");
        selSql.append(" left join tb_lib_manager t14 on t.lib_id = t14.lib_id and t14.type=4 and t14.libtype=2  \n");
        selSql.append(" left join tb_lib_manager t15 on t.lib_id = t15.lib_id and t15.type=5 and t15.libtype=2 \n");
        selSql.append(" left join tb_user t13 on t12.user_id = t13.id \n");
        selSql.append(" left join tb_user t16 on t14.user_id = t16.id  \n");
        selSql.append(" left join tb_user t17 on t15.user_id = t17.id  \n");
        selSql.append("  where t.type BETWEEN 4 and 5 and t.user_id=? and t.state=1 \n");
        queryParams.add(userId);
        if(searchContent != null && !"".equals(searchContent)){
        	selSql.append(" and t11.league_name like concat('%',?,'%') \n");
        	queryParams.add(searchContent);
        }
        selSql.append(" group by t11.id  \n");
        return dao.findMap(selSql.toString(), queryParams.toArray());
	}


	@Override
	public Map<String, Object> findCdComeIn(String userId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("SELECT\n");
        selSQL.append("	floor(IFNULL(sum(IF (\n");
        selSQL.append("				t.create_time BETWEEN concat(date_format(LAST_DAY(now()),'%Y-%m-%'),'01')\n");
        selSQL.append("				and LAST_DAY(now()),\n");
        selSQL.append("				t.price,\n");
        selSQL.append("				0\n");
        selSQL.append("			)), 0)) \n");
        selSQL.append("     AS countPrice,\n");
        selSQL.append("		floor(ifnull(sum(IF (\n");
        selSQL.append("				t.create_time BETWEEN date_format(\n");
        selSQL.append("					date_add(NOW(), INTERVAL - 1 DAY),'%Y-%m-%d')\n");
        selSQL.append("				AND date_format(NOW(), '%Y-%m-%d'),\n");
        selSQL.append("				t.price,\n");
        selSQL.append("				0\n");
        selSQL.append("			)),0)) AS yesCount\n");
        selSQL.append("FROM\n");
        selSQL.append("	tb_lib_order_details t\n");
        selSQL.append("INNER  JOIN tb_lib_manager t11 ON t.lib_id = t11.lib_id and t11.state=1 \n");
        selSQL.append("AND t11.type = 5\n");
        selSQL.append("WHERE\n");
        selSQL.append("	t.order_status = 1 and t11.user_id = ? \n");
        selSQL.append("AND t.create_time > concat(date_format(LAST_DAY(now()),'%Y-%m-%'),'01')");
        queryParams.add(userId);
        List<Map<String, Object>> list = dao.findMap(selSQL.toString(), queryParams.toArray());
		return list ==null ?null:list.get(0);
	}


	@Override
	public Map<String, Object> findOrderById(String detailId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("SELECT t11.child_name,t11.parent_name,t.details,t.price,t.create_time,t12.`name`,\n");
        selSQL.append("t.order_status,t.type,t.libtype,t11.flow_id,t.lib_id \n");
        selSQL.append("from tb_lib_order_details t\n");
        selSQL.append("left join tb_lib_vip t11 on t.vip_id = t11.id\n");
        selSQL.append("left join tb_lib_staffing t12 on t.sales_user_id=t12.user_id and t.lib_id= t12.lib_id\n");
        selSQL.append("where t.id = ?");
        queryParams.add(detailId);
        List<Map<String, Object>> list = dao.findMap(selSQL.toString(), queryParams.toArray());
		return list ==null ?null:list.get(0);
	}
	

}
