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

import com.ssj.bean.weixin.sales.domain.LibStaffing;
import com.ssj.dao.weixin.sales.dao.LibStaffingQueryDao;
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年4月24日 上午11:59:29 
 * 类说明 
 */
@Repository
public class LibStaffingQueryDaoImpl  implements  LibStaffingQueryDao {
	@Autowired
	private PagingHibernateJdbcDao dao;
	@Override
	public Page<Map<String, Object>> getUserList(
			Map<String, Object> searchParams, Pageable pageable) {
		
		 StringBuilder selSQL = new StringBuilder();
	        List<Object> queryParams = new ArrayList<Object>();
	        selSQL.append("  SELECT   u.id,u.wx_name ,u.photo ,u.subscribe,u.country  ");
	        selSQL.append("  from  tb_user  u  where not exists (select * from tb_lib_staffing t where t.user_id = u.id and t.stat =1  )   ");
	        if (searchParams.get("name") != null && !searchParams.get("name").equals("")) {
	        	selSQL.append(" and   u.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
	        	queryParams.add(searchParams.get("name"));
	        }
	        selSQL.append(" order by    u.create_time desc ");
	        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
	@Override
	public void insertTbLibManager(List<LibStaffing> list) {
		dao.batchInsert(list);
	}
	@Override
	public Page<Map<String, Object>> getUserListZB(
			Map<String, Object> searchParams, Pageable pageable) {

		   StringBuilder selSQL = new StringBuilder();
	        List<Object> queryParams = new ArrayList<Object>();
	        selSQL.append("  SELECT   u.id,u.wx_name ,u.photo ,u.subscribe,u.country ,t.id as t_id , t.name , t.telephone ,t.sex ");
	        selSQL.append("  from   tb_lib_staffing t join 	tb_user u on 	t.user_id = u.id ");
	        selSQL.append(" WHERE    not exists ( select * from tb_lib_staffing g where g.user_id = t.user_id  AND g.type = ? and g.stat =1 )   ");
	        selSQL.append("  and   t.lib_id = ? ");
	        queryParams.add(searchParams.get("type"));
	        queryParams.add(searchParams.get("orglibId"));
	        selSQL.append("  and   t.stat = 1  ");
	        if (searchParams.get("name") != null && !searchParams.get("name").equals("")) {
	        	selSQL.append(" and   u.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
	        	queryParams.add(searchParams.get("name"));
	        }
	     return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
	@Override
	public List<Map<String, Object>> getOrderDetail(String userId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append(" SELECT  t.id , t.details, u.species,u.type,u.`name`,u.aliases   from  tb_lib_order_details   t  JOIN   tb_lib_sales_price  u ");
		selSql.append(" on  t.sales_price_id  = u.id  ");
		selSql.append(" LEFT JOIN   tb_orders  s  on   t.order_id =  s.id    ");
		selSql.append(" where  s.order_status  =1  and  t.user_id = ? ");
		queryParams.add(userId);
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	@Override
	public Page<Map<String, Object>> findForVipSerivcePage(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT  s.id ,s.vip_id, s.species,s.type,s.stat,v.child_name,v.parent_name ,v.level ,v.qrcode_url,v.phone ");
        selSQL.append(" ,v.vipno,v.flow_id ,j.league_name,s.start_time,s.end_time,s.order_details_id  ");
        selSQL.append(" from  tb_lib_vip_service s  JOIN  tb_lib_join  j ");
        selSQL.append(" on s.lib_id =j.id  ");
        selSQL.append("  JOIN  tb_lib_vip  v  on  s.vip_id  = v.id  ");
        selSQL.append(" where   1=1  ");
        
        if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
        	selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("child_name"));
        }
        if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
        	//selSQL.append(" and  j.org_code LIKE  CONCAT(?,'%') ");
        	selSQL.append(" and  j.org_code in ( ");
        	selSQL.append(searchParams.get("orgCode"));
        	selSQL.append(" ) ");
        }
        if (searchParams.get("level") != null && !searchParams.get("level").equals("")) {
            selSQL.append(" and v.level = ?");
            queryParams.add(searchParams.get("level"));
        }
        if (searchParams.get("flow_id") != null && !searchParams.get("flow_id").equals("")) {
            selSQL.append(" and v.flow_id = ?");
            queryParams.add(searchParams.get("flow_id"));
        }
        if (searchParams.get("type") != null && !searchParams.get("type").equals("")) {
            selSQL.append(" and s.type = ?");
            queryParams.add(searchParams.get("type"));
        }
        if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
        	selSQL.append(" and j.id = ? ");
        	queryParams.add(searchParams.get("libId"));
        }
        selSQL.append("   ORDER BY s.start_time desc  ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
	@Override
	public Page<Map<String, Object>> getUserListSR(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		 selSQL.append("  SELECT   u.id,u.wx_name ,u.photo ,u.subscribe,u.country ,s.id as s_id , s.name , s.telephone ,s.sex  ");
		 selSQL.append(" from tb_lib_staffing  s join  tb_user u   on s.user_id  = u.id and s.type ='SSJ-SSJ-SR'   ");
	        selSQL.append("   where not exists (select * from tb_lib_manager t where t.user_id = u.id and t.state=1)   and  s.type ='SSJ-SSJ-SR' ");
	        if (searchParams.get("name") != null && !searchParams.get("name").equals("")) {
	        	selSQL.append(" and   u.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
	        	queryParams.add(searchParams.get("name"));
	        }
	        if (searchParams.get("subscribe") != null && !searchParams.get("subscribe").equals("")) {
					selSQL.append(" and u.subscribe = ?  ");
				queryParams.add(searchParams.get("subscribe"));
			}  
	        selSQL.append("   order by  s.create_time desc   ");
	     return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
	/**
	 * 需要更改
	 */
	@Override
	public List<Map<String, Object>> getLibAllService(String vipId,
			String species) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  SELECT a.lib_id ,a.species,a.type ,s.details ,p.price,a.end_time,a.stat,a.order_details_id,s.sales_user_id ");
		selSql.append("   FROM tb_lib_vip_service a  ");
		selSql.append("  JOIN   tb_lib_sales_price  p  on  a.lib_id = p.lib_id and p.species =  a.species and p.type = a.type     ");
		selSql.append("     join tb_lib_order_details s on a.order_details_id = s.id     ");
		selSql.append(" where  a.vip_id=?   and a.stat=2     ");
		queryParams.add(vipId);
		if(species!=null){
			selSql.append(" and  a.species=?   ");
			queryParams.add(species);
		}
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	@Override
	public List<Map<String, Object>> findUserServiceList(String vipid, String libId) {
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  SELECT ifnull(t11.details,case when t.species='S001' then '阅读卡' when t.species='S004' then '特色课卡' else '作业卡' end)  as details,t.start_time,t.end_time,t12.`name` from tb_lib_vip_service t  ");
		selSql.append("   left join tb_lib_order_details t11 on t.order_details_id = t11.id  ");
		selSql.append("   left join tb_lib_staffing t12 on t11.sales_user_id = t12.user_id  and t.lib_id = t12.lib_id and t11.staffing_type=t12.type  ");
		selSql.append(" where  t.vip_id=?   and t.stat=1     ");
		queryParams.add(vipid);
		if(libId !=null){
			selSql.append(" and t.lib_id = ?     ");
			queryParams.add(libId);
		}
		return dao.findMap(selSql.toString(), queryParams.toArray());
	}
	@Override
	public Map<String, Object> findUserCost(String vipId, String libId) {
		//select  price,SUM(price) as total from tb_lib_order_details where vip_id=? and lib_id = ? order by create_time DESC
		StringBuilder selSql = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSql.append("  select  ifnull(price,0) as price,ifnull(SUM(price),0) as total from tb_lib_order_details  ");
		selSql.append("   where vip_id=? and lib_id = ? and order_status=1 order by create_time DESC  ");
		queryParams.add(vipId);
		queryParams.add(libId);
		List<Map<String, Object>> mapList = dao.findMap(selSql.toString(), queryParams.toArray());
		return mapList!=null?mapList.get(0):null;
	}
	@Override
	public Page<Map<String, Object>> findTeacherList(
			Map<String, Object> searchParams, Pageable pageable) {
		 StringBuilder selSQL = new StringBuilder();
	        List<Object> queryParams = new ArrayList<Object>();
	        selSQL.append("  select t.id, t.vip_id,v.school ,v.id as v_id, v.child_name, v.img_url   ");
	        selSQL.append("  from  tb_lib_vip_service t   join   tb_lib_vip v ");
	        selSQL.append("  on t.vip_id =  v.id     ");
	        selSQL.append("  WHERE  t.lib_id = ?  and  t.course_hour =?  and t.species='S003' and  t.stat =1 ");
	        queryParams.add(searchParams.get("libId"));
	        queryParams.add(searchParams.get("courseHour"));
	        selSQL.append("  ORDER BY  t.create_time  desc   ");
	        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public List<Map<String, Object>> findLibMasterByLibIdAndType(String libId, String type) {
		StringBuilder sb = new StringBuilder();
		sb.append("select u.wx_account, m.name, u.wx_name from tb_lib_staffing m left join tb_user u on m.user_id=u.id where m.lib_id=? and m.type=? and m.stat=1");
		List<Object> params = new ArrayList<>();
		params.add(libId);
		params.add(type);
		return dao.findMap(sb.toString(), params.toArray());
	}

	@Override
	public Page<Map<String, Object>> findBylist(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append("  SELECT ifnull(t.name,t13.wx_name) as name,t.sex,t.user_id,t.telephone,count(DISTINCT t11.id) as salecount,  ");
		selSQL.append(" count(DISTINCT t12.id) as devicecount FROM tb_lib_staffing t  ");
		selSQL.append(" left join tb_user t13 on t.user_id = t13.id ");
		selSQL.append(" left join fx_merchant t11 on t.admin_name = t11.sale_login_name   ");
		selSQL.append(" left join fx_device t12 on t11.pos_code = t12.pos_code  ");
		selSQL.append(" where t.type='SSJ-ZB-XS'  ");
		selSQL.append(" group by t.admin_name  ");
		return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public Page<Map<String, Object>> findForVipSerivcePage2(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
		List<Object> queryParams = new ArrayList<Object>();
		selSQL.append(" SELECT  s.id ,s.vip_id, s.species,s.type,s.stat,v.child_name,v.parent_name ,v.level ,v.qrcode_url,v.phone ");
		selSQL.append(" ,v.vipno,v.flow_id ,j.league_name,s.start_time,s.end_time,s.order_details_id  ");
		selSQL.append(" from  tb_lib_vip_service s  JOIN  tb_lib_join  j ");
		selSQL.append(" on s.lib_id =j.id  ");
		selSQL.append("  JOIN  tb_lib_read_vip  v  on  s.vip_id  = v.id  ");
		selSQL.append(" where   1=1  ");

		if (searchParams.get("child_name") != null && !searchParams.get("child_name").equals("")) {
			selSQL.append(" and  v.child_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
			queryParams.add(searchParams.get("child_name"));
		}
		if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
			selSQL.append(" and  j.org_code in ( ");
			selSQL.append(searchParams.get("orgCode"));
			selSQL.append(" ) ");
		}
		if (searchParams.get("level") != null && !searchParams.get("level").equals("")) {
			selSQL.append(" and v.level = ?");
			queryParams.add(searchParams.get("level"));
		}
		if (searchParams.get("flow_id") != null && !searchParams.get("flow_id").equals("")) {
			selSQL.append(" and v.flow_id = ?");
			queryParams.add(searchParams.get("flow_id"));
		}
		if (searchParams.get("type") != null && !searchParams.get("type").equals("")) {
			selSQL.append(" and s.type = ?");
			queryParams.add(searchParams.get("type"));
		}
		if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
			selSQL.append(" and j.id = ? ");
			queryParams.add(searchParams.get("libId"));
		}
		selSQL.append("   ORDER BY s.start_time desc  ");
		return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
}
