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

import com.ssj.dao.weixin.sales.dao.LibOrderDetailsQueryDao;
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;


/** 
 * @author  ZhangCaibao 
 * @version 2017年5月8日 上午11:17:53 
 * 类说明 
 */
@Repository
public class LibOrderDetailsQueryDaoImpl  implements LibOrderDetailsQueryDao {
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public Page<Map<String, Object>> findForPageList(
			Map<String, Object> searchParams, Pageable pageable) {
		
			 StringBuilder selSQL = new StringBuilder();
		        List<Object> queryParams = new ArrayList<Object>();
		        selSQL.append(" SELECT  d.* , t.league_name ,s.type as s_type ,s.`name` ,s.telephone  ,v.parent_name ,v.child_name,v.flow_id    ");
		        selSQL.append("  from   tb_lib_order_details d  join  tb_lib_join  t   ");
		        selSQL.append("  on d.lib_id = t.id  ");
		        selSQL.append(" left join  tb_lib_staffing  s  on s.lib_id  =  d.lib_id and  s.user_id =  d.sales_user_id   and  d.staffing_type = s.type and s.stat =1 ");
		        selSQL.append(" left  join  tb_lib_vip v  on v.id  =  d.vip_id   ");
		        selSQL.append("  where   1=1  ");
		        
		        if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
		        	selSQL.append(" and  t.org_code in ( ");
		        	selSQL.append(searchParams.get("orgCode"));
		        	selSQL.append(" ) ");
		        }
		        if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
		        	selSQL.append(" and TO_DAYS(d.create_time) >=TO_DAYS(?) ");
		        	queryParams.add(searchParams.get("beginDate"));
		        }
		        if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
		        	selSQL.append(" and TO_DAYS(d.create_time) <=TO_DAYS(?) ");
		        	queryParams.add(searchParams.get("endDate"));
		        }
		        if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
		        	selSQL.append(" and t.id = ? ");
		        	queryParams.add(searchParams.get("libId"));
		        }
		        if (searchParams.get("libtype") != null && !searchParams.get("libtype").equals("")) {
		        	selSQL.append(" and d.libtype = ? ");
		        	queryParams.add(searchParams.get("libtype"));
		        }
		        if (searchParams.get("order_no") != null && !searchParams.get("order_no").equals("")) {
	        		selSQL.append(" and d.order_no = ?  ");
	        	queryParams.add(searchParams.get("order_no"));
	        }
		        
		        selSQL.append(" order by d.create_time  desc  ");
		    return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public List<Map<String, Object>> getOrderDetailByOrgCode(Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT    d.price ,d.book_price ,d.order_no, d.know ,date_format(d.create_time, '%Y-%m-%d %H:%i:%s')as create_time  ,d.details , t.league_name ,    ");
		 selSQL.append(" s.type as s_type ,s.`name` ,s.telephone  ,v.parent_name ,v.child_name ,v.phone  ,v.school ,v.grade ,  date_format(d.end_time, '%Y-%m-%d %H:%i:%s')as end_time  , ");
		 selSQL.append(" case d.type  WHEN '1' THEN 	'新签' ELSE '续签' END  as d_type ,   ");
		 selSQL.append("   CASE d.price_species WHEN 'S001' THEN (CASE d.vip_status WHEN '4' THEN '联名卡' WHEN '5' THEN '特色课程'  ELSE '外借书及阅读指导' END)  ");
		 selSQL.append("  WHEN 'S002' THEN	'荣誉馆长' WHEN 'S003' THEN	'同伴作业督导' WHEN 'S004' THEN	'特色课程' WHEN 'S005' THEN	'注册教师' WHEN 'S005' THEN	'明星教师' ELSE '其他' END  as species ,  ");
		 selSQL.append("  CASE d.price_type WHEN 'C001' THEN 	'会员卡标准卡年卡' WHEN 'C002' THEN	'会员卡标准卡半年卡'     ");
		 selSQL.append("   WHEN 'C003' THEN	'会员卡标准卡月卡' WHEN 'C004' THEN	'作业吧月卡'     ");
		 selSQL.append("  WHEN 'C005' THEN	'VIP专席卡半年卡' WHEN 'C006' THEN	'作业吧年卡'     ");
		 selSQL.append("  WHEN 'C007' THEN '会员卡周末卡' WHEN 'C008' THEN '长假卡寒假卡'     ");
		 selSQL.append("  WHEN 'C009' THEN '自然拼读课程L1' WHEN 'C010' THEN '励登幼升小拼音识字班'      ");
		 selSQL.append("  WHEN 'C011' THEN '励登幼升小数学思维班' WHEN 'C012' THEN '励登幼升小课程全天班'     ");
		 selSQL.append("  WHEN 'C015' THEN '作业吧季度卡'     ");
		 selSQL.append("  WHEN 'C016' THEN '自然拼读课程L2'     ");
		 selSQL.append("  WHEN 'C017' THEN '自然拼读课程L3'     ");
		 selSQL.append("  WHEN 'C020' THEN '阅读产品收费'     ");
		 selSQL.append("  ELSE '其他' END  as type  ,       ");
		 selSQL.append(" case d.libtype  WHEN '1' THEN 	'家庭图书馆' WHEN '2' THEN 	'共享图书馆A' ELSE '共享图书馆B' END  as d_libtype       ");
	        selSQL.append("  from   tb_lib_order_details d left join  tb_lib_join  t   ");
	        selSQL.append("  on d.lib_id = t.id  ");
	        selSQL.append("  left join  tb_lib_staffing  s  on s.lib_id  =  d.lib_id    AND  s.user_id =  d.sales_user_id  and  d.staffing_type = s.type  ");
	        selSQL.append("  left join  tb_lib_vip v  on v.id  =  d.vip_id   ");
	        selSQL.append("  where    d.order_status = 1 ");
	        
	        if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
	        	selSQL.append(" and  t.org_code in ( ");
	        	selSQL.append(searchParams.get("orgCode"));
	        	selSQL.append(" ) ");
	        }
	        if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("beginDate"));
	       }
	      if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("endDate"));
	      }
	      if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
	        	selSQL.append(" and t.id = ? ");
	        	queryParams.add(searchParams.get("libId"));
	        }
	      if (searchParams.get("order_no") != null && !searchParams.get("order_no").equals("")) {
      		selSQL.append(" and d.order_no = ?  ");
      	queryParams.add(searchParams.get("order_no"));
      }
	      selSQL.append(" ORDER BY  t.league_code desc , d.create_time desc 	 ");
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getOrderDetailMonthByOrgCode(Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();                                               
		selSQL.append("  SELECT                                                                            ");
	    selSQL.append("  t.league_city , t.league_name  ,             ");
	    selSQL.append("  DATE_FORMAT(d.create_time,'%Y年%m月') months ,                                    ");
	    
	    selSQL.append(" sum(CASE  d.vip_status WHEN '4' THEN                                       ");
	    selSQL.append("                       (  CASE d.price_species WHEN 'S001' THEN             ");
	    selSQL.append("                       ( case d.price_type WHEN 'C001' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )        ");
	    selSQL.append("   else 0 END  )else 0 END  ) as  lx_xin_count,         ");
	    selSQL.append(" sum(CASE  d.vip_status WHEN '4' THEN                                       ");
	    selSQL.append("                       (  CASE d.price_species WHEN 'S001' THEN             ");
	    selSQL.append("                        ( case d.price_type WHEN 'C001' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )        ");
	    selSQL.append("   else 0 END  )else 0 END  ) as  lx_xin_price,          ");//联名
	    
	    selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN   	                                   ");
	    selSQL.append("	   ( case d.price_type WHEN 'C007' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )   ");
	    selSQL.append("	 else 0 END ) as  zm_xin_count , 	                                   ");//周末
	    selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN   	                                   ");
	    selSQL.append("	   ( case d.price_type WHEN 'C007' THEN  ( CASE  d.type WHEN '1' THEN d.price else 0 END )    ELSE 0 end  )   ");
	    selSQL.append("	 else 0 END ) as  zm_xin_price , 	                                   ");//周末
	    
	    selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN   	                                   ");
	    selSQL.append("	   ( case d.price_type WHEN 'C008' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )   ");
	    selSQL.append("	 else 0 END ) as  hj_xin_count , 	                                   ");//寒假
	    selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN   	                                   ");
	    selSQL.append("	   ( case d.price_type WHEN 'C008' THEN  ( CASE  d.type WHEN '1' THEN d.price else 0 END )    ELSE 0 end  )   ");
	    selSQL.append("	 else 0 END ) as  hj_xin_price , 	                                   ");//寒假
	    
	  //特色
	    selSQL.append("	  sum(CASE d.price_species WHEN 'S004' THEN 	                                   ");
		selSQL.append("	       ( case d.price_type WHEN 'C009' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )                   ");
		selSQL.append("	  else 0 END ) as xin_tese_count ,                                                  ");
		selSQL.append("	  sum(CASE d.price_species WHEN 'S004' THEN 	                                   ");
		selSQL.append("	      ( case d.price_type WHEN 'C009' THEN  ( CASE  d.type WHEN '1' THEN d.price else 0 END )    ELSE 0 end    )       ");
		selSQL.append("	  else 0 END ) as xin_tese_price ,                                                  ");
		
		//幼升小
		selSQL.append("	  sum(CASE d.price_species WHEN 'S004' THEN 	                                   ");
		selSQL.append("	       ( case d.price_type WHEN 'C009' THEN  0   ELSE ( CASE  d.type WHEN '1' THEN 1 else 0 END )  end  )                   ");
		selSQL.append("	  else 0 END ) as xin_ysx_count ,                                                  ");
		selSQL.append("	  sum(CASE d.price_species WHEN 'S004' THEN 	                                   ");
		selSQL.append("	      ( case d.price_type WHEN 'C009' THEN  0    ELSE ( CASE  d.type WHEN '1' THEN d.price else 0 END ) end    )       ");
		selSQL.append("	  else 0 END ) as xin_ysx_price ,                                                  ");
		
	    
		    
		
		
		
		selSQL.append(" sum(CASE  d.vip_status WHEN '1' THEN                                       ");
		selSQL.append("                       (  CASE d.price_species WHEN 'S001' THEN             ");
		selSQL.append("                       ( case d.price_type WHEN 'C001' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )         ");
		selSQL.append("                        WHEN 'C003' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )        ");
		selSQL.append("   else 0 END  )else 0 END  ) as  xin_huiyuan_count,         ");
		selSQL.append(" sum(CASE  d.vip_status WHEN '1' THEN                                       ");
		selSQL.append("                       (  CASE d.price_species WHEN 'S001' THEN             ");
		selSQL.append("                        ( case d.price_type WHEN 'C001' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)          ");
		selSQL.append("                         WHEN 'C003' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )        ");
		selSQL.append("   else 0 END  )else 0 END  ) as  xin_huiyuan_price,          ");// 会员卡
		    
		
		
		selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN 	                                   ");
		selSQL.append("	             ( case d.price_type WHEN 'C001' THEN  (CASE  d.type WHEN '2' THEN 1 else 0 END)    ");
		selSQL.append("	               WHEN 'C003' THEN  (CASE  d.type WHEN '2' THEN 1 else 0 END)    ELSE 0 end ) ");
		selSQL.append("	  else 0 END ) as xu_huiyuan_count ,                                              ");
		
		selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN 	                                   ");
		selSQL.append("	           ( case d.price_type WHEN 'C001' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)	           ");
		selSQL.append("	           WHEN 'C003' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	           ");
		selSQL.append("	  else 0 END ) as xu_huiyuan_price ,                                              ");
		
		selSQL.append("	  sum(CASE d.price_species WHEN 'S003' THEN 	                                   ");
		selSQL.append("	                        (CASE  d.type WHEN '1' THEN 1 else 0 END)                  ");
		selSQL.append("	  else 0 END ) as xin_vip_count ,                                                  ");
		selSQL.append("	  sum(CASE d.price_species WHEN 'S003' THEN 	                                   ");
		selSQL.append("	                        (CASE  d.type WHEN '1' THEN d.price else 0 END)            ");
		selSQL.append("	  else 0 END ) as xin_vip_price ,                                                  ");
		selSQL.append("	  sum(CASE d.price_species WHEN 'S003' THEN 	                                   ");
		selSQL.append("	                        (CASE  d.type  WHEN '2' THEN 1 else 0  END)                ");
		selSQL.append("	  else 0 END ) as xu_vip_count ,                                                   ");
		selSQL.append("	  sum(CASE d.price_species WHEN 'S003' THEN 	                                   ");
		selSQL.append("	                        (CASE  d.type  WHEN '2' THEN d.price else 0 END)           ");
		selSQL.append("	  else 0 END ) as xu_vip_price,                                                    ");
        selSQL.append("                                                                                    ");
		selSQL.append("	  count(d.type) as total_count,                                                    ");
		selSQL.append("	  sum(IFNULL(d.price,0)) as total_price ,                                          ");
        
        /**       支出部分          **/
		
	    selSQL.append("   SUM(CASE d.staffing_type WHEN 's_sd' THEN (   CASE  d.vip_status WHEN '1' THEN (    ");
	    selSQL.append("         case d.price_type   WHEN 'C001' THEN   (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)else 0 END)     ");
	    selSQL.append("   else 0 END )as  sd_hy_nk_xin_count,                                              ");
	    
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C001' THEN   ");
	    selSQL.append("       (CASE  d.type WHEN '2' THEN 1  else 0 END)else 0 END)                        ");
	    selSQL.append("  else 0 END )as  sd_hy_nk_xu_count,                                                ");
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C003' THEN   ");
	    selSQL.append("       (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)                        ");
	    selSQL.append("  else 0 END )as  sd_hy_yk_xin_count,                                               ");
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C003' THEN   ");
	    selSQL.append("       (CASE  d.type WHEN '2' THEN 1  else 0 END)else 0 END)                        ");
	    selSQL.append("  else 0 END )as  sd_hy_yk_xu_count,                                                ");
        selSQL.append("                                                                                    ");
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C004' THEN   ");
	    selSQL.append("       (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)                        ");
	    selSQL.append("  else 0 END )as  sd_vip_yk_xin_count,                                              ");
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C004' THEN   ");
	    selSQL.append("       (CASE  d.type WHEN '2' THEN 1  else 0 END)else 0 END)                        ");
	    selSQL.append("  else 0 END )as  sd_vip_yk_xu_count,                                               ");
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C006' THEN   ");
	    selSQL.append("       (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)                        ");
	    selSQL.append("  else 0 END )as  sd_vip_nk_xin_count,                                              ");
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C006' THEN   ");
	    selSQL.append("       (CASE  d.type WHEN '2' THEN 1  else 0 END)else 0 END)                        ");
	    selSQL.append("  else 0 END )as  sd_vip_nk_xu_count  ,                                             ");
	    
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C007' THEN      ");
	    selSQL.append("       (CASE  d.type WHEN '1' THEN (CASE   WHEN d.price > 300 THEN 1  else 0 END)  else 0 END)else 0 END)                       ");
	    selSQL.append("  else 0 END )as  sd_zm_xin_count,                                              ");//周末
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C008' THEN      ");
	    selSQL.append("       (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)                       ");
	    selSQL.append("  else 0 END )as  sd_hj_xin_count  ,                                            ");//寒假
	    
	    
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C009' THEN       ");
	    selSQL.append("         (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)                         ");
	    selSQL.append("   else 0 END )as  sd_c009_count   ,                                         ");//特色
	    
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C010' THEN       ");
	    selSQL.append("         (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)                         ");
	    selSQL.append("   else 0 END )as  sd_c010_count   ,                                         ");//拼音识字
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C011' THEN       ");
	    selSQL.append("         (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)                         ");
	    selSQL.append("   else 0 END )as  sd_c011_count   ,                                         ");//数学思维
	    selSQL.append("  SUM(CASE d.staffing_type WHEN 's_sd' THEN (case d.price_type   WHEN 'C012' THEN       ");
	    selSQL.append("         (CASE  d.type WHEN '1' THEN 1  else 0 END)else 0 END)                         ");
	    selSQL.append("   else 0 END )as  sd_c012_count   ,                                         ");//全天班
	    
	    selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN       ");
	    selSQL.append("         ( case d.price_type WHEN 'C009' THEN  ( CASE   WHEN (d.price >864) THEN d.price else 864 END)    ");
	    selSQL.append("    ELSE 0 end )                     ");
	    selSQL.append("    else 0 END ) as c009_price ,                 ");//自然拼读--咨询服务费
	    selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN       ");
	    selSQL.append("         ( case d.price_type WHEN 'C010' THEN  ( CASE   WHEN (d.price >1620) THEN d.price else 1620 END)    ");
	    selSQL.append("    ELSE 0 end )                     ");
	    selSQL.append("    else 0 END ) as c010_price ,                 ");//拼音识字--咨询服务费
	    selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN       ");
	    selSQL.append("         ( case d.price_type WHEN 'C011' THEN  ( CASE   WHEN (d.price >1620) THEN d.price else 1620 END)    ");
	    selSQL.append("    ELSE 0 end )                     ");
	    selSQL.append("    else 0 END ) as c011_price ,                 ");//数学思维--咨询服务费
	    selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN       ");
	    selSQL.append("         ( case d.price_type WHEN 'C012' THEN  ( CASE   WHEN (d.price >3240) THEN d.price else 3240 END)    ");
	    selSQL.append("    ELSE 0 end )                     ");
	    selSQL.append("    else 0 END ) as c012_price                  ");//全天班--咨询服务费
	    
	    
		selSQL.append("	  from                                                                             ");
		selSQL.append("	  tb_lib_order_details d                                                           ");
	    selSQL.append(" left join  tb_lib_join  t on d.lib_id = t.id                                       ");
		selSQL.append(" where    d.order_status = 1  and d.libtype=1  ");                                                  
		 
		 if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
	        	selSQL.append(" and  t.org_code in ( ");
	        	selSQL.append(searchParams.get("orgCode"));
	        	selSQL.append(" ) ");
	        }
	        if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("beginDate"));
	       }
	      if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("endDate"));
	      }
	      if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
	        	selSQL.append(" and t.id = ? ");
	        	queryParams.add(searchParams.get("libId"));
	        }
	      selSQL.append(" GROUP BY  d.lib_id ,months  ");
	      selSQL.append(" ORDER BY  d.lib_id desc   ,	  months asc 	 ");
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getOrderDetailZongBuRenZheng(Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT  t.league_city ,t.league_name , s.`name` , s.user_id, s.type , t.org_code, DATE_FORMAT(d.create_time,'%Y年%m月') months ,   ");
		
		selSQL.append("  CASE d.staffing_type WHEN 's_cc'  THEN 'CC'  WHEN 's_cd' THEN 'CD'      ");
		selSQL.append("  WHEN 's_cda' THEN 'CDA' WHEN 's_sd' THEN '总部SD'    ");
		selSQL.append("  WHEN 's_zb' THEN '总部认证组' WHEN 's_dsf' THEN '第三方'   WHEN 's_qd' THEN '销售人员'  ");
		selSQL.append(" else  'null'  END  as c_type,    ");
		selSQL.append("   '0' as ty_count,'0' as ty_price,  ");
		
		selSQL.append("  sum(CASE  d.vip_status WHEN '4' THEN  ");
		selSQL.append("   ( CASE d.price_species WHEN 'S001' THEN ");
		selSQL.append("  ( case d.price_type    WHEN 'C001' THEN  1    ELSE 0 end  )  ");
		selSQL.append("   else 0 END )else 0 END  ) as  lx_xin_count,  ");
		
		selSQL.append("  sum(CASE  d.vip_status WHEN '4' THEN  ");
		selSQL.append("  (CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("   ( case d.price_type WHEN 'C001' THEN  d.price    ELSE 0 end )	  ");
		selSQL.append("  else 0 END ) else 0 END ) as  lx_xin_price ,  ");
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C007' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  zm_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C007' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  zm_xin_price ,  ");//周末
		selSQL.append("   ");
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C008' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  hj_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C008' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  hj_xin_price ,  ");//寒假
		selSQL.append("   ");
		
		
		
		selSQL.append("   sum(CASE  d.vip_status WHEN '1' THEN  ");
		selSQL.append("    ( CASE d.price_species WHEN 'S001' THEN       ");
		selSQL.append("  ( case d.price_type    WHEN 'C001' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  ) ");
		selSQL.append("  else 0 END )else 0 END  ) as  hy_nk_xin_count, ");
		
		selSQL.append("   sum(CASE  d.vip_status WHEN '1' THEN ");
		selSQL.append("        (CASE d.price_species WHEN 'S001' THEN                 ");
		selSQL.append("   ( case d.price_type WHEN 'C001' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end ) ");
		selSQL.append("  else 0 END ) else 0 END ) as  hy_nk_xin_price , ");
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C001' THEN  ( CASE  d.type WHEN '2' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  hy_nk_xu_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C001' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  hy_nk_xu_price ,  ");
		selSQL.append("  ");
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C003' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  hy_yk_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C003' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  hy_yk_xin_price ,  ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C003' THEN  ( CASE  d.type WHEN '2' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  hy_yk_xu_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C003' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  hy_yk_xu_price ,  ");
		selSQL.append("  ");
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN 1 else 0 END) ");
		selSQL.append("  else 0 END ) as xin_huiyuan_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xin_huiyuan_price , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN 	 ");
		selSQL.append("                        (CASE  d.type  WHEN '2' THEN 1 else 0  END) ");
		selSQL.append("  else 0 END ) as xu_huiyuan_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN 	 ");
		selSQL.append("                        (CASE  d.type  WHEN '2' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xu_huiyuan_price , ");
		
		
		selSQL.append("   '0' as vip_ty_count,'0' as vip_ty_price,  ");
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C004' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  vip_yk_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C004' THEN  ( CASE  d.type WHEN '1' THEN d.price else 0 END )    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  vip_yk_xin_price ,  ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C004' THEN  ( CASE  d.type WHEN '2' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  vip_yk_xu_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C004' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  vip_yk_xu_price ,  ");
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C006' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  vip_nk_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C006' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  vip_nk_xin_price , ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C006' THEN  ( CASE  d.type WHEN '2' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  vip_nk_xu_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C006' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  vip_nk_xu_price,  ");
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN 1 else 0 END) ");
		selSQL.append("  else 0 END ) as xin_vip_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xin_vip_price , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN 	 ");
		selSQL.append("                        (CASE  d.type  WHEN '2' THEN 1 else 0  END) ");
		selSQL.append("  else 0 END ) as xu_vip_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN 	 ");
		selSQL.append("                        (CASE  d.type  WHEN '2' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xu_vip_price , ");
		
		
	/*	selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN 1 else 0 END) ");
		selSQL.append("  else 0 END ) as xin_tese_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xin_tese_price  ");*/
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C009' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  c009_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C009' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  c009_xin_price , ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C010' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  c010_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C010' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  c010_xin_price , ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C011' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  c011_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C011' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  c011_xin_price , ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C012' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  c012_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C012' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  c012_xin_price  ");
		selSQL.append("   ");
		selSQL.append(" from      ");
		selSQL.append(" tb_lib_order_details d left join  tb_lib_join  t on d.lib_id = t.id ");
		selSQL.append("left join  tb_lib_staffing  s  on s.lib_id  =  d.lib_id  and  s.user_id =  d.sales_user_id    and  d.staffing_type = s.type");
		selSQL.append(" where   d.order_status = 1   ");
		 
		 if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
	        	selSQL.append(" and  t.org_code in ( ");
	        	selSQL.append(searchParams.get("orgCode"));
	        	selSQL.append(" ) ");
	        }
		 
		if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("beginDate"));
	    }
	    if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("endDate"));
	    }
	    if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
        	selSQL.append(" and t.id = ? ");
        	queryParams.add(searchParams.get("libId"));
        }
		selSQL.append(" GROUP BY    d.sales_user_id ,d.lib_id ,months  ");
		selSQL.append(" ORDER BY   t.league_code desc ,months desc  	 ");
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getOrderDetailQuDao(
			Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT  t.league_city ,t.league_name , t.org_code,  d.staffing_type ,  DATE_FORMAT(d.create_time,'%Y年%m月') months ,  ");
		selSQL.append("  CASE d.staffing_type WHEN 's_cc'  THEN 'CC'  WHEN 's_cd' THEN 'CD'      ");
		selSQL.append("  WHEN 's_cda' THEN 'CDA' WHEN 's_sd' THEN '总部SD'    ");
		selSQL.append("  WHEN 's_zb' THEN '总部认证组' WHEN 's_dsf' THEN '第三方'   WHEN 's_qd' THEN '销售人员'  ");
		selSQL.append(" else  'null'  END  as c_type,    ");
		selSQL.append(" CASE d.staffing_type WHEN 's_cc'  THEN '自营'  WHEN 's_cd' THEN '自营'   ");
		selSQL.append("  WHEN 's_cda' THEN '自营' WHEN 's_sd' THEN '外包'   ");
		selSQL.append("   WHEN 's_zb' THEN '外包'   WHEN 's_dsf' THEN '外包'   WHEN 's_qd' THEN '外包'     ");
		selSQL.append("  else  'null'  END as n_type ,   '0' as ty_count,'0' as ty_price,  ");
		
		selSQL.append("  sum(CASE  d.vip_status WHEN '4' THEN  ");
		selSQL.append("   ( CASE d.price_species WHEN 'S001' THEN ");
		selSQL.append("  ( case d.price_type    WHEN 'C001' THEN  1    ELSE 0 end  )  ");
		selSQL.append("   else 0 END )else 0 END  ) as  lx_xin_count,  ");
		
		selSQL.append("  sum(CASE  d.vip_status WHEN '4' THEN  ");
		selSQL.append("  (CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("   ( case d.price_type WHEN 'C001' THEN  d.price    ELSE 0 end )	  ");
		selSQL.append("  else 0 END ) else 0 END ) as  lx_xin_price ,  ");//联名卡
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C007' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  zm_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C007' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  zm_xin_price ,  ");//周末
		selSQL.append("   ");
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C008' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  hj_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C008' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  hj_xin_price ,  ");//寒假
		selSQL.append("   ");
		
		
		selSQL.append("   sum(CASE  d.vip_status WHEN '1' THEN  ");
		selSQL.append("    ( CASE d.price_species WHEN 'S001' THEN       ");
		selSQL.append("  ( case d.price_type    WHEN 'C001' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  ) ");
		selSQL.append("  else 0 END )else 0 END  ) as  hy_nk_xin_count, ");
		
		selSQL.append("   sum(CASE  d.vip_status WHEN '1' THEN ");
		selSQL.append("        (CASE d.price_species WHEN 'S001' THEN                 ");
		selSQL.append("   ( case d.price_type WHEN 'C001' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end ) ");
		selSQL.append("  else 0 END ) else 0 END ) as  hy_nk_xin_price , ");
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C001' THEN  ( CASE  d.type WHEN '2' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  hy_nk_xu_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C001' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  hy_nk_xu_price ,  ");
		selSQL.append("  ");
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C003' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  hy_yk_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C003' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  hy_yk_xin_price ,  ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C003' THEN  ( CASE  d.type WHEN '2' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  hy_yk_xu_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C003' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  hy_yk_xu_price ,  ");
		selSQL.append("  ");
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN 1 else 0 END) ");
		selSQL.append("  else 0 END ) as xin_huiyuan_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xin_huiyuan_price , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN 	 ");
		selSQL.append("                        (CASE  d.type  WHEN '2' THEN 1 else 0  END) ");
		selSQL.append("  else 0 END ) as xu_huiyuan_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S001' THEN 	 ");
		selSQL.append("                        (CASE  d.type  WHEN '2' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xu_huiyuan_price ,");
		
		
		
		selSQL.append("   '0' as vip_ty_count,'0' as vip_ty_price,  ");
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C004' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  vip_yk_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C004' THEN  ( CASE  d.type WHEN '1' THEN d.price else 0 END )    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  vip_yk_xin_price ,  ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C004' THEN  ( CASE  d.type WHEN '2' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  vip_yk_xu_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C004' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  vip_yk_xu_price ,  ");
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C006' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  vip_nk_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C006' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  vip_nk_xin_price , ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C006' THEN  ( CASE  d.type WHEN '2' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  vip_nk_xu_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C006' THEN  (CASE  d.type WHEN '2' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  vip_nk_xu_price,  ");
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN 1 else 0 END) ");
		selSQL.append("  else 0 END ) as xin_vip_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xin_vip_price , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN 	 ");
		selSQL.append("                        (CASE  d.type  WHEN '2' THEN 1 else 0  END) ");
		selSQL.append("  else 0 END ) as xu_vip_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S003' THEN 	 ");
		selSQL.append("                        (CASE  d.type  WHEN '2' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xu_vip_price ,");
		
		
		//特色课，太极。。。。
		/*selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN 1 else 0 END) ");
		selSQL.append("  else 0 END ) as xin_tese_count , ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN 	 ");
		selSQL.append("                        (CASE  d.type WHEN '1' THEN d.price else 0 END) ");
		selSQL.append("  else 0 END ) as xin_tese_price  ");*/
		
		
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C009' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  c009_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C009' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  c009_xin_price , ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C010' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  c010_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C010' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  c010_xin_price , ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C011' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  c011_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C011' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  c011_xin_price , ");
		selSQL.append("   ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C012' THEN  ( CASE  d.type WHEN '1' THEN 1 else 0 END )    ELSE 0 end  )	                       ");
		selSQL.append("  else 0 END ) as  c012_xin_count ,  ");
		selSQL.append("  ");
		selSQL.append("  sum(CASE d.price_species WHEN 'S004' THEN  ");
		selSQL.append("          ( case d.price_type WHEN 'C012' THEN  (CASE  d.type WHEN '1' THEN d.price else 0 END)    ELSE 0 end )	                       ");
		selSQL.append("  else 0 END ) as  c012_xin_price  ");
		selSQL.append("   ");
		
		
		
		selSQL.append(" from    tb_lib_order_details d   ");
		selSQL.append("   left  join   tb_lib_join  t  on d.lib_id = t.id  ");
		 selSQL.append(" where    d.order_status = 1   ");
	     
	     if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
	        	selSQL.append(" and  t.org_code in ( ");
	        	selSQL.append(searchParams.get("orgCode"));
	        	selSQL.append(" ) ");
	        }
	     
		if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("beginDate"));
	    }
	    if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("endDate"));
	    }
	    if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
        	selSQL.append(" and t.id = ? ");
        	queryParams.add(searchParams.get("libId"));
        }
		selSQL.append(" GROUP BY  d.staffing_type ,months ,t.id   ");
		selSQL.append(" ORDER BY  t.league_code desc , 	 months desc  	 ");
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getExpiredTemplate() {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  SELECT 	t.id,t.details,t.price,s.end_time,t.lib_id ");
		selSql.append(" , u.wx_account , v.parent_name ,v.child_name ,v.flow_id, us.wx_account as xs_openid ,  j.league_name");
		selSql.append("  FROM tb_lib_vip_service  s  join  tb_lib_order_details t on s.order_details_id = t.id     ");
		selSql.append("  left join tb_lib_staffing ls on t.lib_id=ls.lib_id and ls.user_id=t.sales_user_id  and ls.stat=1     ");
		selSql.append("   LEFT  JOIN tb_user us ON ls.user_id = us.id     ");
		selSql.append("  LEFT  join  tb_lib_vip  v  on s.vip_id = v.id      ");
		selSql.append("   LEFT  JOIN tb_user u ON v.user_id = u.id  ");
		selSql.append(" LEFT   join  tb_lib_join  j on j.id = s.lib_id      ");
		selSql.append("   where  s.species !='S004' AND  s.stat =1 and v.user_id is not null and ((TO_DAYS(s.end_time)) - TO_DAYS(now()) = 3) and u.wx_account is not null  ");
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> getLibVipCodeList(
			Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT  t.vip_code ,j.league_name ,u.wx_name, ");
		 selSQL.append(" CASE t.type WHEN 1 THEN 	'未使用'  ELSE '已使用' END  as type,   ");
		 selSQL.append(" date_format(t.create_time,'%Y-%m-%d %H:%i:%s') AS create_time,     ");
		 selSQL.append("  t.discount   from  tb_lib_vip_code  t ");
		 selSQL.append(" join  tb_lib_join  j  on t.lib_id  =  j.id     ");
		 selSQL.append(" LEFT join  tb_user  u  on  u.id = t.user_id ");
	        selSQL.append(" where  t.lib_id  = ? ");
	        queryParams.add(searchParams.get("EQ_libId"));
	        if (searchParams.get("EQ_type") != null && !searchParams.get("EQ_type").equals("")) {
	            selSQL.append(" and t.type = ?");
	            queryParams.add(searchParams.get("EQ_type"));
	        }
	      selSQL.append(" ORDER BY  t.create_time desc  ");
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public Page<Map<String, Object>> findForsellPageList(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT t.*, j.league_name  FROM tb_lib_sell t  ");
        selSQL.append(" LEFT JOIN tb_lib_join j ON t.lib_id = j.id  ");
        selSQL.append("  where   1=1  ");
        if (searchParams.get("EQ_libId") != null && !searchParams.get("EQ_libId").equals("")) {
        	selSQL.append(" and  t.lib_id =  ? ");
        	queryParams.add(searchParams.get("EQ_libId"));
        }
        	if (searchParams.get("baby_name") != null && !searchParams.get("baby_name").equals("")) {
            	selSQL.append(" and t.baby_name = ? ");
        	queryParams.add(searchParams.get("baby_name"));
        }
        	if (searchParams.get("name") != null && !searchParams.get("name").equals("")) {
            	selSQL.append(" and t.name = ? ");
        	queryParams.add(searchParams.get("name"));
        }
        
        selSQL.append(" order by t.sell_date  desc  ");
    return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
}

	@Override
	public List<Map<String, Object>> getOrderDetailMonthA(
			Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();                                               
		selSQL.append("  SELECT                                                                            ");
	    selSQL.append("  t.libtype,	t.league_city , t.league_name  ,             ");
	    selSQL.append("  DATE_FORMAT(d.create_time,'%Y年%m月') months ,  DATE_FORMAT(t.create_time,'%Y年%m月%d日') qy_months_day ,                                  ");
	    selSQL.append("   DATE_FORMAT(t.create_time,'%Y年%m月') qy_months ,                                  ");
	    
	    
	    selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN   	                                   ");
	    selSQL.append("	   ( case d.price_type WHEN 'C007' THEN  1    ELSE 0 end  )   ");
	    selSQL.append("	 else 0 END ) as  zm_count , 	                                   ");//周末
	    selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN   	                                   ");
	    selSQL.append("	   ( case d.price_type WHEN 'C007' THEN  d.price   ELSE 0 end  )   ");
	    selSQL.append("	 else 0 END ) as  zm_price , 	                                   ");//周末
	    
		
		selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN 	                                   ");
		selSQL.append("	             ( case d.price_type WHEN 'C002' THEN  1    ");
		selSQL.append("	                   ELSE 0 end ) ");
		selSQL.append("	  else 0 END ) as bn_count ,                                              ");//会员卡半年卡
		
		selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN 	                                   ");
		selSQL.append("	           ( case d.price_type WHEN 'C002' THEN  d.price	           ");
		selSQL.append("	            ELSE 0 end )	           ");
		selSQL.append("	  else 0 END ) as bn_price ,                                              ");//会员卡半年卡价格总和
		
		selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN 	                                   ");
		selSQL.append("	             ( case d.price_type WHEN 'C001' THEN  1    ");
		selSQL.append("	                   ELSE 0 end ) ");
		selSQL.append("	  else 0 END ) as huiyuan_count ,                                              ");//会员卡年卡
		
		selSQL.append("	  sum(CASE d.price_species WHEN 'S001' THEN 	                                   ");
		selSQL.append("	           ( case d.price_type WHEN 'C001' THEN  d.price	           ");
		selSQL.append("	            ELSE 0 end )	           ");
		selSQL.append("	  else 0 END ) as huiyuan_price ,                                              ");//会员卡年卡价格总和
		
		selSQL.append("	  count(d.type) as total_count,                                                    ");
		selSQL.append("	  sum(IFNULL(d.price,0)) as total_price ,                                         ");
		
		
		selSQL.append(" (SELECT COUNT(1) from tb_lib_order_details o where o.lib_id=d.lib_id AND d.libtype=o.libtype AND o.create_time >=t.create_time  ");
		selSQL.append(" AND o.create_time <DATE_FORMAT(DATE_ADD(date_add(t.create_time,interval 2 month),interval -day(date_add(t.create_time,interval 2 month))+1 day),'%Y-%m-%d') ");
		selSQL.append(" AND o.order_status = 1  ");
		selSQL.append(" ) as cycle_count ");//第一周期的总卡数
		
		if (searchParams.get("libtype") != null && !searchParams.get("libtype").equals("")) {
			 selSQL.append(" , (SELECT  DATE_FORMAT(o.create_time,'%Y-%m') months  from tb_lib_order_details o  ");
			 selSQL.append(" where o.lib_id=d.lib_id AND d.libtype=o.libtype AND o.order_status = 1  ");
			 selSQL.append(" ORDER BY o.create_time asc ");
			 if (searchParams.get("num") != null && !searchParams.get("num").equals("")) {
				 selSQL.append(" LIMIT ?,1 ");
				 queryParams.add(searchParams.get("num"));
			 }else{
				 selSQL.append(" LIMIT 0 ");
			 }
			 selSQL.append(" ) as arrive_time ");//libtype模式下的达标的时间
		}
		selSQL.append("	  from                                                                             ");
		selSQL.append("	  tb_lib_order_details d                                                           ");
	    selSQL.append(" left join  tb_lib_join  t on d.lib_id = t.id                                       ");
		selSQL.append(" where    d.order_status = 1    ");                                                  
		 
		  if (searchParams.get("libtype") != null && !searchParams.get("libtype").equals("")) {
	        	selSQL.append(" and  d.libtype =? ");
	        	queryParams.add(searchParams.get("libtype"));
	        }
		  if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
			  selSQL.append(" and  t.org_code in ( ");
			  selSQL.append(searchParams.get("orgCode"));
			  selSQL.append(" ) ");
		  }
	        if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("beginDate"));
	       }
	      if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
	        	selSQL.append(" and TO_DAYS(d.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("endDate"));
	      }
	      if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
	        	selSQL.append(" and t.id = ? ");
	        	queryParams.add(searchParams.get("libId"));
	        }
	      selSQL.append(" GROUP BY  d.lib_id ,months  ");
	      selSQL.append(" ORDER BY  d.lib_id desc   ,	  months asc 	 ");
		return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

}
