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

import com.ssj.dao.weixin.sales.dao.qdSalesQueryDao;
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 QdSalesQueryDaoImpl implements qdSalesQueryDao {

	@Autowired
	private PagingHibernateJdbcDao dao;
	
	@Override
	public Page<Map<String, Object>> findForPage(Map<String, Object> params, Pageable initPage) {
		 StringBuilder selSQL = new StringBuilder();
	        List<Object> queryParams = new ArrayList<Object>();
	        selSQL.append("select t13.league_name,t12.name,t11.details,t.sales_fee,\n");
	        selSQL.append("case when t.stat=1 then '打款成功' else '打款失败' END stat,t.give_desc,\n");
	        selSQL.append("ifnull(t14.child_name,t15.wx_name) as child_name,t11.order_no,t.create_time\n");
	        selSQL.append("from zyb_qd_salse_his t \n");
	        selSQL.append("left join tb_lib_order_details t11 on t.order_detail_id = t11.id\n");
	        selSQL.append("left join tb_lib_staffing t12 on t11.lib_id = t12.lib_id and t11.sales_user_id=t12.user_id\n");
	        selSQL.append("left join tb_lib_vip t14 on t11.vip_id = t14.id\n");
	        selSQL.append("left join tb_user t15 on t11.user_id = t15.id\n");
	        selSQL.append("left join tb_lib_join t13 on t12.lib_id = t13.id\n");
	        if(params!=null&&params.size()>0){
	        	selSQL.append("  where  1=1 ");
	        }
	        if (params.get("orgCode") != null && !params.get("orgCode").equals("")) {
	        	selSQL.append(" and   t13.org_code in ( ");
	        	selSQL.append(params.get("orgCode"));
	        	selSQL.append(" ) ");
	        }
	        
	        if (params.get("startTime") != null && !params.get("startTime").equals("")) {
	        	selSQL.append(" and  TO_DAYS(t.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(params.get("startTime"));
	        }
	        if (params.get("endTime") != null && !params.get("endTime").equals("")) {
	        	selSQL.append(" and  TO_DAYS(t.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(params.get("endTime"));
	        }
	        if (params.get("libId") != null && !params.get("libId").equals("")) {
	        	selSQL.append(" and t13.id = ? ");
	        	queryParams.add(params.get("libId"));
	        }
	        if (params.get("stat") != null && !params.get("stat").equals("")) {
	        	selSQL.append(" and t.stat = ? ");
	        	queryParams.add(params.get("stat"));
	        }
	        selSQL.append("order by t.create_time desc");
	        return dao.findPage(selSQL.toString(), queryParams.toArray(), initPage);
	}

	@Override
	public List<Map<String, Object>> getForList(Map<String, Object> searchParams) {
		 StringBuilder selSQL = new StringBuilder();
	        List<Object> queryParams = new ArrayList<Object>();
	        selSQL.append("select t13.league_name,t12.name,t11.details,t.sales_fee,\n");
	        selSQL.append("case when t.stat=1 then '打款成功' else '打款失败' END stat,t.give_desc,\n");
	        selSQL.append("ifnull(t14.child_name,t15.wx_name) as child_name,t11.order_no,DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%i:%s')as sj\n");
	        selSQL.append("from zyb_qd_salse_his t \n");
	        selSQL.append("left join tb_lib_order_details t11 on t.order_detail_id = t11.id\n");
	        selSQL.append("left join tb_lib_staffing t12 on t11.lib_id = t12.lib_id and t11.sales_user_id=t12.user_id\n");
	        selSQL.append("left join tb_lib_vip t14 on t11.vip_id = t14.id\n");
	        selSQL.append("left join tb_user t15 on t11.user_id = t15.id\n");
	        selSQL.append("left join tb_lib_join t13 on t12.lib_id = t13.id\n");
	        selSQL.append("  where   1=1  ");
	        if (searchParams.get("orgCode") != null && !searchParams.get("orgCode").equals("")) {
	        	selSQL.append(" and  t13.org_code in ( ");
	        	selSQL.append(searchParams.get("orgCode"));
	        	selSQL.append(" ) ");
	        }
	        if (searchParams.get("startTime") != null && !searchParams.get("startTime").equals("")) {
	        	selSQL.append(" and  TO_DAYS(t.create_time) >=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("startTime"));
	        }
	        if (searchParams.get("endTime") != null && !searchParams.get("endTime").equals("")) {
	        	selSQL.append(" and  TO_DAYS(t.create_time) <=TO_DAYS(?) ");
	        	queryParams.add(searchParams.get("endTime"));
	        }
	        if (searchParams.get("libId") != null && !searchParams.get("libId").equals("")) {
	        	selSQL.append(" and t13.id = ? ");
	        	queryParams.add(searchParams.get("libId"));
	        }
	        if (searchParams.get("stat") != null && !searchParams.get("stat").equals("")) {
	        	selSQL.append(" and t.stat = ? ");
	        	queryParams.add(searchParams.get("stat"));
	        }
	        selSQL.append("order by t.create_time desc");
	        return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

}
