package com.ssj.dao.sys.fx.dao.impl;

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

import org.apache.commons.lang3.StringUtils;
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 com.ssj.dao.sys.fx.dao.FxQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.service.conch.parents.v1.dto.PositionSchoolV1Dto;

@Repository

public class FxQueryDaoImpl implements FxQueryDao {

    @Autowired
    private PagingHibernateJdbcDao dao;

    @Override
    public Page<Map<String, Object>> findSaleCountList(Map<String, Object> params, Pageable pageable) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("select a.* from (");
        selSql.append("SELECT ifnull(t.name,t11.wx_name) as sale_name,t.sex,t.telephone,count(DISTINCT t12.id) as salecount,count(DISTINCT t13.id) as mcount  ");
        selSql.append("from tb_lib_staffing t  ");
        selSql.append("left join tb_user t11 on t.user_id= t11.id ");
        selSql.append(" left join fx_merchant t12 on t.admin_name = t12.sale_login_name ");
        selSql.append(" left join fx_device t13 on t12.pos_code = t13.pos_code ");
        selSql.append(" where t.type='SSJ-ZB-XS' ");
        selSql.append(" ) a ");
        if(!"".equals(params.get("saleName"))){
            selSql.append(" where   a.sale_name like CONCAT('%',?,'%') ");
            queryParams.add(params.get("saleName"));
        }
        return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
    }

    @Override
    public Page<Map<String, Object>> salseMouthtList(Map<String, Object> params, Pageable pageable) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" select a.*,a.tc+a.fl as total from ( ");
        selSql.append(" SELECT sum(a.price) as price,a.unit_name,a.mdate,sum(a.tc) as tc,sum(a.divide_rebate) as fl,a.plevel from ( ");
        selSql.append(" SELECT a.order_no,a.location,a.unit_name,a.price,b.divide_price as tc,b.divide_rebate,a.mdate,a.pos_code,a.parent_pos_code,a.details,a.plevel from (\n");
        selSql.append(" SELECT t15.unit_name,t15.location,t.order_no,");
        if("1".equals(params.get("showType"))){
            selSql.append("  case when t14.is_active=1 then t.price else 0 end price," );
        }else {
            selSql.append("  t.price," );
        }
        selSql.append("  t.details,DATE_FORMAT(t.create_time,'%Y-%m') as mdate, case when t.price_type='C006' then  TIMESTAMPDIFF(YEAR,t14.active_time,NOW())+1 else 0 end active_year,t14.active_level,t14.`level`,t14.is_active,t14.pos_code,t15.parent_pos_code,t15.level as plevel \n");
        selSql.append("        from tb_lib_order_details t   \n");
        selSql.append("        INNER join tb_lib_vip t11 on t.vip_id = t11.id  \n");
        selSql.append("        INNER join conch_derver t12 on t11.derver = t12.derver \n");
        selSql.append("        INNER JOIN fx_device t13 on t12.imei = t13.imei \n");
        selSql.append("				INNER JOIN fx_device_active t14 on t13.imei = t14.imei\n");
        selSql.append("        inner join fx_merchant t15 on t14.pos_code = t15.pos_code \n");
        selSql.append("        where t.price_species = 'S003' and (t.is_close is null or t.is_close=1) \n");
        selSql.append("				and t.create_time >'2019-09'\n");
        if(!"".equals(params.get("name"))){
            selSql.append(" and    t15.unit_name =? ");
            queryParams.add(params.get("name"));
        }
        if(!"".equals(params.get("startDate"))){
            selSql.append(" and    DATE_FORMAT(t.create_time,'%Y-%m') =? ");
            queryParams.add(params.get("startDate"));
        }
        if(!"".equals(params.get("LIKE_orgCode"))){
            if("1".equals(params.get("showType"))){
                selSql.append("and t13.org_code like concat(?,'%')  ");
                queryParams.add(params.get("LIKE_orgCode"));
            }else{
                selSql.append("and t16.parent_pos_code = ?  ");
                queryParams.add(params.get("LIKE_orgCode"));
            }
        }
        selSql.append("				) a left join fx_device_divide b on a.active_level = b.divide_lever and a.`level` = b.divide_lever and a.active_year = b.divide_year and a.is_active = b.is_active");

        selSql.append(" )a GROUP BY a.pos_code,a.mdate   ");
        selSql.append("order by a.pos_code,a.mdate  ");
        selSql.append(") a  ");

        return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
    }

    @Override
    public Page<Map<String, Object>> salseMouthMoneytList(Map<String, Object> params, Pageable pageable) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("SELECT sum(a.price) as price,a.location,a.mdate,count(DISTINCT a.derver) as dcount from ( ");
        selSql.append("SELECT t.price,t.details,DATE_FORMAT(t.create_time,'%Y-%m') as mdate,   ");
        selSql.append("t14.location,t14.`level`,t12.derver,t14.pos_code  ");
        selSql.append("from tb_lib_order_details t  ");
        selSql.append("INNER join tb_lib_vip t11 on t.vip_id = t11.id  ");
        selSql.append("INNER join conch_derver t12 on t11.derver = t12.derver  ");
        selSql.append("INNER JOIN fx_device t13 on t12.imei = t13.imei  ");
        selSql.append("left join fx_merchant t14 on t13.pos_code = t14.pos_code  ");
        selSql.append("where (t.price_species = 'S003' or t.price_species ='S007')  ");
        if(!"".equals(params.get("area"))){
            selSql.append(" and    t14.location =? ");
            queryParams.add(params.get("area"));
        }
        if(!"".equals(params.get("startDate"))){
            selSql.append(" and    DATE_FORMAT(t.create_time,'%Y-%m') =? ");
            queryParams.add(params.get("startDate"));
        }
        selSql.append(") a GROUP BY a.pos_code,a.mdate   ");
        selSql.append("order by a.pos_code,a.mdate  ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
    }

    @Override
    public Page<Map<String, Object>> salsetList(Map<String, Object> params, Pageable pageable) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" select a.* from (");
        selSql.append(" SELECT t.id,t16.unit_name,t.order_no,t.price,t.details,DATE_FORMAT(t.create_time,'%Y-%m-%d %H:%m:%s') as create_time,DATE_FORMAT(t.end_time,'%Y-%m-%d %H:%m:%s') as end_time,t12.imei,case when t.is_close=2 then '已结算' else '未结算' end is_close, \n");
        selSql.append(" t12.derver,t12.simcard,t11.child_name,t14.wx_name ,t15.wx_name as pay_user  \n");
        selSql.append("        from tb_lib_order_details t   \n");
        selSql.append("        INNER join tb_lib_vip t11 on t.vip_id = t11.id  \n");
        selSql.append("        INNER join conch_derver t12 on t11.derver = t12.derver \n");
        selSql.append("        left join fx_merchant t16 on t11.pos_code = t16.pos_code  ");
        selSql.append("        INNER JOIN tb_user t14 on t11.user_id = t14.id \n");
        selSql.append("        INNER JOIN tb_user t15 on t.user_id = t15.id \n");
        selSql.append("        where t.price_species = 'S003' and t.libtype=100 \n");
        if(!"".equals(params.get("orderNo"))){
            selSql.append("and t.order_no = ?   ");
            queryParams.add(params.get("orderNo"));
        }
        if(!"".equals(params.get("startDate")) && !"".equals(params.get("endDate"))){
            selSql.append("and t.create_time BETWEEN  ?  and  ?  ");
            queryParams.add(params.get("startDate"));
            queryParams.add(params.get("endDate"));
        }
        if(!"".equals(params.get("LIKE_orgCode"))){
            if("1".equals(params.get("showType"))){
                selSql.append("and t16.org_code like concat(?,'%')  ");
                queryParams.add(params.get("LIKE_orgCode"));
            }else{
                selSql.append("and (t16.parent_pos_code = ? or t16.pos_code=?) ");
                queryParams.add(params.get("LIKE_orgCode"));
                queryParams.add(params.get("LIKE_orgCode"));
            }

        }
        if(!"".equals(params.get("unit_name"))){
            selSql.append("and t16.pos_code =? ");
            queryParams.add(params.get("unit_name"));
        }

        if(!"".equals(params.get("seachDerver"))){
            selSql.append("and t11.derver =? ");
            queryParams.add(params.get("seachDerver"));
        }

        selSql.append("	) a ");
        if(!"".equals(params.get("order_type"))){
            selSql.append("where  a.is_close =? ");
            queryParams.add(params.get("order_type"));
        }
        return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
    }

    @Override
    public Page<Map<String, Object>> findByPageMap(Map<String, Object> params, Pageable pageable) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("select *,(in_count-userd_count) as leaf_count from tb_materiel ");
        if(!"".equals(params.get("materielName"))){
            selSql.append("where  materiel_name like CONCAT('%',?,'%')   ");
            queryParams.add(params.get("materielName"));
        }
        return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
    }

    @Override
    public Page<Map<String, Object>> findTemplateByPageMap(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("select * from tb_materiel_template ");
        if(!"".equals(params.get("templateName"))){
            selSql.append("where  template_name like CONCAT('%',?,'%')   ");
            queryParams.add(params.get("templateName"));
        }
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
    }

    @Override
    public Page<Map<String, Object>> findMaterielLeafByPageMap(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("select t.materiel_name,t.materie_code,sum(t.userd_count) as sum_count,t.leaf_price from tb_materiel t where t.userd_count>0  ");
        if(!"".equals(params.get("materielName"))){
            selSql.append("and  t.materiel_name like CONCAT('%',?,'%')   ");
            queryParams.add(params.get("materielName"));
        }
        selSql.append(" GROUP BY t.materie_code  ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
    }

    @Override
    public Page<Map<String, Object>> findMaterielOffLeafByPageMap(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("select t.off_flow,DATE_FORMAT(t.off_time,'%Y-%m-%d %H:%i:%s') as off_time,t.off_num,t.loss_num,t11.materie_code,t11.materiel_name,t11.cost_num,t11.price\n");
        selSql.append("       from fx_materiel_off t \n" );
        selSql.append("       left join fx_materiel_off_detail t11 on t.id = t11.off_id  and t11.type=2 ");
        if(!"".equals(params.get("startDate")) ){
            selSql.append("where  t.off_time BETWEEN  >=?  ");
            queryParams.add(params.get("startDate"));
        }
        if(!"".equals(params.get("endDate"))){
            selSql.append("where  t.off_time BETWEEN  <=?  ");
            queryParams.add(params.get("endDate"));
        }

        selSql.append(" order by t.off_flow desc  ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
    }

    @Override
    public List<Map<String, Object>> findMapList(Map<String, Object> params) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("select * from fx_materiel_off_detail where off_id=?  and type=? ");
        queryParams.add(params.get("EQ_offId"));
        queryParams.add(params.get("EQ_type"));
        return dao.findMap(selSql.toString(),queryParams.toArray());
    }

    @Override
    public Page<Map<String, Object>> vipDerverList(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("SELECT t11.derver,t.child_name,t.school,t.grade,t.phone,t.flow_id,t11.imei,t11.simcard from tb_lib_vip t join conch_derver t11 on t.id = t11.vip_id\n");
        selSql.append("where LENGTH(t.user_id)>0 \n");
        if(!"".equals(params.get("childName"))){
            selSql.append(" and t.child_name like CONCAT('%',?,'%')   \n");
            queryParams.add(params.get("childName"));
        }
        if(!"".equals(params.get("vipFlow"))){
            selSql.append(" and t.flow_id = ?\n");
            queryParams.add(params.get("vipFlow"));
        }
        if(!"".equals(params.get("derver"))){
            selSql.append(" and t11.derver = ?");
            queryParams.add(params.get("derver"));
        }
        return dao.findPage(selSql.toString(),queryParams.toArray(),initPage);
    }

    @Override
    public Page<Map<String, Object>> MonthInAndOutList(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("SELECT a.unit_name,a.mdate,a.ruku,a.chuku,a.tuiku,(a.ruku-a.chuku) as jiecun,a.chuku*a.out_price as chuku_total,a.ruku*a.price as ruku_total,(a.ruku-a.chuku)*a.price as jiecun_total from (\n");
        selSql.append("SELECT t.unit_name,t11.mdate,sum(t11.ruku) as ruku,sum(t11.chuku) as chuku,sum(t11.tuiku) as tuiku,t.price,t.out_price from fx_merchant t \n");
        selSql.append("INNER  join (\n");
        selSql.append("SELECT a.to_merchant_id,a.mdate,count(imei) as ruku,0 as tuiku,0 as chuku from ( \n");
        selSql.append("	SELECT to_merchant_id,DATE_FORMAT(create_time,'%Y-%m') as mdate,imei from fx_device_act where type=1 \n");
        if(!"".equals(params.get("queryDate"))){
            selSql.append(" and create_time like CONCAT(?,'%')   \n");
            queryParams.add(params.get("queryDate"));
        }

        selSql.append(") a GROUP BY a.to_merchant_id,a.mdate\n");
        selSql.append("union all\n");
        selSql.append("SELECT a.from_merchant_id,a.mdate,0,count(imei) as tuiku,0 from ( \n");
        selSql.append("	SELECT from_merchant_id,DATE_FORMAT(create_time,'%Y-%m') as mdate,imei from fx_device_act where type=2\n");
        if(!"".equals(params.get("queryDate"))){
            selSql.append(" and create_time like CONCAT(?,'%')   \n");
            queryParams.add(params.get("queryDate"));
        }
        selSql.append(") a GROUP BY a.from_merchant_id,a.mdate\n");
        selSql.append("union all\n");
        selSql.append("SELECT a.from_merchant_id,a.mdate,0 as ruku,0 as tuiku,count(imei) as chuku from ( \n");
        selSql.append("	SELECT from_merchant_id,DATE_FORMAT(create_time,'%Y-%m') as mdate,imei from fx_device_act where type=1 \n");
        if(!"".equals(params.get("queryDate"))){
            selSql.append(" and create_time like CONCAT(?,'%')   \n");
            queryParams.add(params.get("queryDate"));
        }
        selSql.append(") a GROUP BY a.from_merchant_id,a.mdate\n");
        selSql.append(")t11 on t.id = t11.to_merchant_id\n");
        selSql.append("GROUP BY t.id,t11.mdate");
        selSql.append(")a");
        return dao.findPage(selSql.toString(),queryParams.toArray(),initPage);
    }

    @Override
    public List<Map<String, Object>> monthInAndOutDetailList(Map<String, Object> params) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("SELECT  ifnull(t11.unit_name,'工厂') as p_unit,t12.unit_name as c_unit,t.imei,t.remarks,t.create_time\n");
        selSql.append("from fx_device_act  t \n");
        selSql.append("left join fx_merchant t11 on t.from_merchant_id = t11.id\n");
        selSql.append("left join fx_merchant t12 on t.to_merchant_id = t12.id\n");
        if(!"".equals(params.get("queryDate"))){
            selSql.append("where t.create_time like CONCAT(?,'%')  ");
            queryParams.add(params.get("queryDate"));
        }

        return dao.findMap(selSql.toString(),queryParams.toArray());
    }
    
    
    @Override
    public Page<Map<String, Object>> findDeviceActivationList(Map<String, Object> params, Pageable pageable) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.derver,t.updated_date,t.user_id,t.vip_id,t.simcard,d.*,v.child_name,v.parent_name,v.grade,v.school,m.unit_name,s.end_time  FROM `conch_derver` t  ");
        selSql.append(" LEFT JOIN fx_device d ON t.imei=d.imei  ");
        selSql.append(" LEFT JOIN fx_merchant m ON d.pos_code=m.pos_code ");
        selSql.append(" LEFT JOIN tb_lib_vip v ON v.id=t.vip_id ");
        selSql.append(" LEFT JOIN  tb_lib_vip_service s ON v.id=s.vip_id and s.species='S003' and s.stat=1 ");
        selSql.append(" where t.imei is NOT NULL AND t.vip_id is NOT NULL AND t.updated_date is NOT NULL  ");
        selSql.append(" AND d.id is NOT null  ");
        if(StringUtils.isNotEmpty(params.get("GTE_imei1")!=null ? params.get("GTE_imei1").toString() : "")){
        	selSql.append(" AND d.imei >= ?");
        	queryParams.add(params.get("GTE_imei1"));
        }
        if(StringUtils.isNotEmpty(params.get("LTE_imei2")!=null ? params.get("LTE_imei2").toString() : "")){
        	selSql.append(" AND d.imei <= ?");
        	queryParams.add(params.get("LTE_imei2"));
        }
        if(StringUtils.isNotEmpty(params.get("EQ_isSpan")!=null ? params.get("EQ_isSpan").toString() : "")){
        	selSql.append(" AND d.is_span = ?");
        	queryParams.add(params.get("EQ_isSpan"));
        }
        if(StringUtils.isNotEmpty(params.get("EQ_status")!=null ? params.get("EQ_status").toString() : "")){
        	selSql.append(" AND d.`status`=?");
        	queryParams.add(params.get("EQ_status"));
        }
        if(StringUtils.isNotEmpty(params.get("EQ_batchCode")!=null ? params.get("EQ_batchCode").toString() : "")){
        	selSql.append(" AND d.batch_code=?");
        	queryParams.add(params.get("EQ_batchCode"));
        }
        if(StringUtils.isNotEmpty(params.get("EQ_emsNumber")!=null ? params.get("EQ_emsNumber").toString() : "")){
        	selSql.append(" AND d.ems_number=?");
        	queryParams.add(params.get("EQ_emsNumber"));
        }
        if(StringUtils.isNotEmpty(params.get("EQ_type")!=null ? params.get("EQ_type").toString() : "")){
        	selSql.append(" AND d.type=?");
        	queryParams.add(params.get("EQ_type"));
        }
        if(StringUtils.isNotEmpty(params.get("LIKE_remarks")!=null ? params.get("LIKE_remarks").toString() : "")){
        	selSql.append(" AND d.remarks LIKE CONCAT('',?,'%') ");
        	queryParams.add(params.get("LIKE_remarks"));
        }
        if(StringUtils.isNotEmpty(params.get("LIKE_orgCode")!=null ? params.get("LIKE_orgCode").toString() : "")){
	        selSql.append(" AND d.org_code LIKE CONCAT('',?,'%') ");
	        queryParams.add(params.get("LIKE_orgCode"));
        }
        if(StringUtils.isNotEmpty(params.get("EQ_orgCode")!=null ? params.get("EQ_orgCode").toString() : "")){
	        selSql.append(" AND d.org_code =? ");
	        queryParams.add(params.get("EQ_orgCode"));
        }
        if(StringUtils.isNotEmpty(params.get("seachDerver")!=null ? params.get("seachDerver").toString() : "")){
            selSql.append(" AND d.device_number =? ");
            queryParams.add(params.get("seachDerver"));
        }
        selSql.append(" ORDER BY d.update_time DESC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
    }

    @Override
    public Page<Map<String, Object>> findForPageList(Map<String, Object> searchParams, Pageable initPage) {
            StringBuilder selSQL = new StringBuilder();
            List<Object> queryParams = new ArrayList<Object>();
            selSQL.append(" SELECT   d.id, case when d.is_close=2 then '已结算' else '未结算' end is_close, case when d.price_type='C006' then '直接零售' else '辅助零售' end price_type,o.order_no,o.create_time ,o.order_money,o.pay_time,u.wx_name ,r.create_time as refund_time from tb_lib_order_detail d join   tb_orders o on d.order_no=o.order_no join  tb_user  u  ");
            selSQL.append("  on o.user_id  = u.id  ");
            selSQL.append("  LEFT JOIN  tb_order_refund  r  on  o.order_no = r.order_no  ");
            selSQL.append("  where d.price_species='S003' and d.create_time>'2019-09' ");
            if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
                selSQL.append(" and TO_DAYS(o.create_time) >=TO_DAYS(?) ");
                queryParams.add(searchParams.get("beginDate"));
            }
            if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
                selSQL.append(" and TO_DAYS(o.create_time) <=TO_DAYS(?) ");
                queryParams.add(searchParams.get("endDate"));
            }
            if (searchParams.get("order_no") != null && !searchParams.get("order_no").equals("")) {
                selSQL.append(" and o.order_no = ?  ");
                queryParams.add(searchParams.get("order_no"));
            }
            if (searchParams.get("order_type") != null && !searchParams.get("order_type").equals("")) {
                selSQL.append(" and d.price_type = ?  ");
                queryParams.add(searchParams.get("order_no"));
            }
            selSQL.append(" order by o.create_time  desc  ");
            return dao.findPage(selSQL.toString(), queryParams.toArray(), initPage);
    }

    
    

    @Override
    public Page<Map<String, Object>> findDeviceActCountListPage(Map<String, Object> params, Pageable pageable) {
    	 StringBuilder selSql = new StringBuilder();
         List<Object> queryParams = new ArrayList<Object>();
         if((params.get("startTime")!=null && StringUtils.isNotEmpty(params.get("startTime").toString()))
        		 || (params.get("endTime")!=null && StringUtils.isNotEmpty(params.get("endTime").toString()))){
         	selSql.append(" SELECT * from ( ");
         }
         selSql.append(" SELECT a.type,a.batch_code,COUNT(DISTINCT(a.device_id)) as count,a.ems_username,a.ems_telephone,a.ems_number,a.ems_address,a.ems_img,a.remarks,MAX(a.create_time) as create_time,m.pos_code,m.unit_name,fm.unit_name as funit_name,fm.pos_code as fpos_code   ");
         selSql.append(" FROM `fx_device_act` a  ");
         selSql.append(" LEFT JOIN fx_merchant m ON a.to_merchant_id=m.id ");
         selSql.append(" LEFT JOIN fx_merchant fm ON a.from_merchant_id=fm.id ");
         selSql.append(" where  a.type=? ");
         queryParams.add(params.get("type"));
         if(params.get("from_merchant_id")!=null && StringUtils.isNotEmpty(params.get("from_merchant_id").toString())){
             selSql.append(" AND a.from_merchant_id=?  ");
         	queryParams.add(params.get("from_merchant_id"));
         }
         if(params.get("to_merchant_id")!=null && StringUtils.isNotEmpty(params.get("to_merchant_id").toString())){
             selSql.append(" AND a.to_merchant_id=?  "); 
         	queryParams.add(params.get("to_merchant_id"));
         }
         if(StringUtils.isNotEmpty(params.get("batchCode").toString())){
         	 selSql.append(" AND a.batch_code=? ");
         	 queryParams.add(params.get("batchCode"));
         }
         selSql.append(" GROUP BY a.from_merchant_id,a.type,a.batch_code ORDER BY a.create_time DESC ");

         if((params.get("startTime")!=null && StringUtils.isNotEmpty(params.get("startTime").toString()))
        		 || (params.get("endTime")!=null && StringUtils.isNotEmpty(params.get("endTime").toString()))){
         	selSql.append(" )as tmp WHERE  1=1 ");
         	if(params.get("startTime")!=null && StringUtils.isNotEmpty(params.get("startTime").toString())){
         		selSql.append(" AND TO_DAYS(tmp.create_time) >=TO_DAYS(?) ");
         		queryParams.add(params.get("startTime"));
         	}
         	if(params.get("endTime")!=null  && StringUtils.isNotEmpty(params.get("endTime").toString())){
         		selSql.append(" AND TO_DAYS(tmp.create_time) <=TO_DAYS(?) ");
         		queryParams.add(params.get("endTime"));
         	}
         }
         return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
    }

	@Override
	public Page<Map<String, Object>> findDeviceActDetailsPage(Map<String, Object> params, Pageable pageable) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT  a.id,a.imei,d.device_name,d.device_color,d.device_number,d.device_model,d.device_sn, a.type,a.batch_code,a.ems_username,a.ems_telephone,a.ems_number,a.ems_address,a.ems_img,a.remarks,MAX(a.create_time) as create_time,m.pos_code,m.unit_name,d.`status`,d.type as dtype   ");
        selSql.append(" FROM `fx_device_act` a LEFT JOIN fx_device d ON d.imei=a.imei ");
        selSql.append(" LEFT JOIN fx_merchant m ON a.to_merchant_id=m.id");
        selSql.append(" where a.type=? ");
        queryParams.add(params.get("type"));
        if(StringUtils.isNotEmpty(params.get("from_merchant_id").toString())){
            selSql.append(" AND a.from_merchant_id=?  ");
        	queryParams.add(params.get("from_merchant_id"));
        }
        if(StringUtils.isNotEmpty(params.get("batchCode").toString())){
        	 selSql.append(" AND a.batch_code=? ");
        	 queryParams.add(params.get("batchCode"));
        }
        selSql.append(" GROUP BY a.id ORDER BY a.create_time DESC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), pageable);
	}
    
    
	 @Override
	    public Page<Map<String, Object>> deviceUnloadingList(Map<String, Object> params, Pageable pageable) {
	        StringBuilder selSql = new StringBuilder();
	        List<Object> queryParams = new ArrayList<Object>();
	        selSql.append(" SELECT time,merchant_id,SUM(counta) as counta,SUM(countb) as countb,m.unit_name,m.price,m.out_price from ( ");
	        selSql.append("  	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.to_merchant_id as merchant_id,COUNT(1) as counta,0 as countb,'1' as type  from fx_device_act t ");
	        selSql.append("  	where  t.from_merchant_id is NOT NULL AND DATE_FORMAT(t.create_time,'%Y-%m')>=? AND DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND (t.type=3 OR t.type=4) ");
	        queryParams.add(params.get("startMonth"));
	        queryParams.add(params.get("endMonth"));
	        if(params.containsKey("status") && "2".equals(params.get("status"))){
	        	selSql.append(" AND t.is_used=0 ");
	        }
	        if(params.containsKey("status") && "3".equals(params.get("status"))){
	        	selSql.append(" AND t.is_used=1 ");
	        }
	        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
	        	selSql.append(" AND to_merchant_id=? ");
	        	queryParams.add(params.get("merchantId"));
	        }
	         
	        selSql.append("  	GROUP BY time,merchant_id ");
	        selSql.append("  	UNION ");
	        selSql.append("  	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.from_merchant_id as merchant_id,0 as counta,COUNT(1) as countb,'2' as type  from fx_device_act t ");
	        selSql.append("  	where  t.from_merchant_id is NOT NULL AND DATE_FORMAT(t.create_time,'%Y-%m')>=? AND DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND (t.type=1 OR t.type=2) ");
	        queryParams.add(params.get("startMonth"));
	        queryParams.add(params.get("endMonth"));
	        if(params.containsKey("status") && "2".equals(params.get("status"))){
	        	selSql.append(" AND t.is_used=0 ");
	        }
	        if(params.containsKey("status") && "3".equals(params.get("status"))){
	        	selSql.append(" AND t.is_used=1 ");
	        }
	        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
	        	selSql.append(" AND from_merchant_id=? ");
	        	queryParams.add(params.get("merchantId"));
	        }
	        
	        selSql.append("  	GROUP BY time,merchant_id ");
	        selSql.append(" 	)as tmp LEFT JOIN fx_merchant m ON tmp.merchant_id=m.id GROUP BY tmp.time,tmp.merchant_id ORDER BY tmp.time asc ");
	       
	        return dao.findPage(selSql.toString(),queryParams.toArray(),pageable);
	    }

    @Override
    public List<PositionSchoolV1Dto> findSchoolList(String libId) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("SELECT t12.id,t12.name as school_name from fx_merchant t \n" );
        selSql.append("JOIN fx_merchant_area t11 on t.id = t11.merchant_id\n" );
        selSql.append("join sys_cn_area t12 on t11.cn_area_id= t12.id\n" );
        selSql.append("where t.lib_id=?");
        queryParams.add(libId);
        return dao.findList(selSql.toString(),queryParams.toArray(),PositionSchoolV1Dto.class);
    }

    @Override
    public Page<Map<String, Object>> deviceInAndOutList(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();

        selSql.append("SELECT temp2.year_month as time,temp2.merchant_id,'小塾硬件' as content,\n" );
        selSql.append("         case when temp2.year_month = tmp.time then  tmp.counta else 0 end counta,\n" );
        selSql.append("        case when temp2.year_month = tmp.time then  tmp.countb else 0 end countb,\n" );
        selSql.append("        (case when temp2.year_month = tmp.time then  tmp.counta else 0 end) - (case when temp2.year_month = tmp.time then  tmp.countb else 0 end) as lefts,\n" );
        selSql.append("        temp2.total_left,\n" );
        selSql.append("        m.unit_name,t.unit_name as unit_name2\n" );
        selSql.append("          from ( \n" );
        selSql.append("				      select t.time,sum(t.counta) as counta,sum(t.countb) as countb,t.merchant_id from (\n" );
        selSql.append("	          	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.to_merchant_id as merchant_id,count(1) as counta,0 as countb from fx_device_act t \n" );
        selSql.append("	          	where  t.to_merchant_id is NOT NULL AND DATE_FORMAT(t.create_time,'%Y-%m')>=? AND DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND  t.type =3\n" );
        queryParams.add(params.get("startMonth"));
        queryParams.add(params.get("endMonth"));
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.to_merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append("							GROUP BY time,merchant_id\n" );
        selSql.append("	        	   \n" );
        selSql.append("	          	UNION all\n" );
        selSql.append("	          	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.from_merchant_id as merchant_id,0 as counta,count(1) as countb\n" );
        selSql.append("							from fx_device_act t \n" );
        selSql.append("	          	where  t.from_merchant_id is NOT NULL AND DATE_FORMAT(t.create_time,'%Y-%m')>=? AND DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND t.type=2\n" );
        queryParams.add(params.get("startMonth"));
        queryParams.add(params.get("endMonth"));
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.from_merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append("							GROUP BY time,merchant_id\n" );
        selSql.append("							) t GROUP BY t.time,t.merchant_id\n" );
        selSql.append("	        	\n" );
        selSql.append("	         	)as tmp \n" );
        selSql.append("						right join (\n" );
        selSql.append("	SELECT a.`year_month`,sum(if(a.`year_month`>=b.time,b.counta,null))-sum(if(a.`year_month`>=b.time,b.countb,null)) as total_left,b.time,b.merchant_id \n" );
        selSql.append("	from (").append(showMonth()).append(") a\n" );
        selSql.append("						left join (\n" );
        selSql.append("	          	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.to_merchant_id as merchant_id,count(1) as counta,0 as countb from fx_device_act t \n" );
        selSql.append("	          	where  t.to_merchant_id is NOT NULL AND  DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND  t.type =3\n" );
        queryParams.add(params.get("endMonth"));
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.to_merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append("							GROUP BY time,merchant_id\n" );
        selSql.append("	          	UNION all\n" );
        selSql.append("	          	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.from_merchant_id as merchant_id,0 as counta,count(1) as countb\n" );
        selSql.append("							from fx_device_act t \n" );
        selSql.append("	          	where  t.from_merchant_id is NOT NULL  AND DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND t.type=2\n" );
        //selSql.append("							   AND from_merchant_id=?  \n" );
        queryParams.add(params.get("endMonth"));
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.from_merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append("							GROUP BY time,merchant_id\n" );
        selSql.append("	)b on 1=1 and a.year_month>=b.time \n" );
        selSql.append("	where a.`year_month`>=? and a.`year_month`<=? \n" );
        queryParams.add(params.get("startMonth"));
        queryParams.add(params.get("endMonth"));
        selSql.append("	GROUP BY b.merchant_id,a.`year_month`\n" );
        selSql.append("						)temp2 on tmp.merchant_id = temp2.merchant_id and  temp2.year_month=tmp.time \n" );
        selSql.append("						LEFT JOIN fx_merchant m ON temp2.merchant_id=m.id \n" );
        selSql.append("						LEFT JOIN fx_merchant t ON m.parent_pos_code = t.pos_code \n" );
        selSql.append("	 where temp2.merchant_id is not null ");
        if(params.containsKey("fromMerchantId") && StringUtils.isNotEmpty(params.get("fromMerchantId").toString())){
            selSql.append(" and t.id =? ");
            queryParams.add(params.get("fromMerchantId"));
        }
        selSql.append("	 ORDER BY m.id,temp2.year_month asc ");

        return dao.findPage(selSql.toString(),queryParams.toArray(),initPage);
    }

    @Override
    public Page<Map<String, Object>> deviceOutList(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT temp2.`year_month` as time,temp2.merchant_id,'小塾硬件' as content,\n" );
        selSql.append("        case when tmp.time = temp2.`year_month` then tmp.countb else 0 end  as  countab,\n" );
        selSql.append("        case when tmp.time = temp2.`year_month` then tmp.counta else 0 end as countb,\n" );
        selSql.append("        case when tmp.time = temp2.`year_month` then tmp.countb else 0 end - case when tmp.time = temp2.`year_month` then tmp.counta else 0 end as lefts,\n" );
        selSql.append("        temp2.total_left,\n" );
        selSql.append("         m.unit_name,\n" );
        selSql.append("         m.hardware_price  from ( \n" );
        selSql.append("				      select t.time,sum(t.counta) as counta,sum(t.countb) as countb,t.merchant_id from (\n" );
        selSql.append("	          	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.to_merchant_id as merchant_id,count(1) as counta,0 as countb from fx_device_act t \n" );
        selSql.append("	          	where  t.to_merchant_id is NOT NULL AND DATE_FORMAT(t.create_time,'%Y-%m')>=? AND DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND  t.type=4\n" );
        queryParams.add(params.get("startMonth"));
        queryParams.add(params.get("endMonth"));
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.to_merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append("							GROUP BY time,merchant_id\n" );
        selSql.append("	        	   \n" );
        selSql.append("	          	UNION all\n" );
        selSql.append("	          	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.from_merchant_id as merchant_id,0 as counta,count(1) as countb\n" );
        selSql.append("							from fx_device_act t \n" );
        selSql.append("	          	where  t.from_merchant_id is NOT NULL AND DATE_FORMAT(t.create_time,'%Y-%m')>=? AND DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND t.type=1\n" );
        queryParams.add(params.get("startMonth"));
        queryParams.add(params.get("endMonth"));
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.from_merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append("							GROUP BY time,merchant_id\n" );
        selSql.append("							) t GROUP BY t.time,t.merchant_id\n" );
        selSql.append("	        	\n" );
        selSql.append("	         	)as tmp \n" );
        selSql.append("						right join (\n" );
        selSql.append("	SELECT a.`year_month`,sum(if(a.`year_month`>=b.time,b.countb,null))-sum(if(a.`year_month`>=b.time,b.counta,null)) as total_left,b.time,b.merchant_id \n" );
        selSql.append("	from (").append(showMonth()).append(") a		left join (\n" );
        selSql.append("	          	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.to_merchant_id as merchant_id,count(1) as counta,0 as countb from fx_device_act t \n" );
        selSql.append("	          	where  t.to_merchant_id is NOT NULL AND  DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND  t.type=4\n" );

        queryParams.add(params.get("endMonth"));
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.to_merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append("							GROUP BY time,merchant_id\n" );
        selSql.append("	          	UNION all\n" );
        selSql.append("	          	SELECT DATE_FORMAT(t.create_time,'%Y-%m') as time,t.from_merchant_id as merchant_id,0 as counta,count(1) as countb\n" );
        selSql.append("							from fx_device_act t \n" );
        selSql.append("	          	where  t.from_merchant_id is NOT NULL  AND DATE_FORMAT(t.create_time,'%Y-%m')<=?  AND t.type=1\n" );
        queryParams.add(params.get("endMonth"));
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.from_merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append("							GROUP BY time,merchant_id\n" );
        selSql.append("	)b on 1=1 and a.year_month>=b.time \n" );
        selSql.append("	where a.`year_month`>=? and a.`year_month`<=? \n" );
        queryParams.add(params.get("startMonth"));
        queryParams.add(params.get("endMonth"));
        selSql.append("	GROUP BY b.merchant_id,a.`year_month`\n" );
        selSql.append("						)temp2 on tmp.merchant_id = temp2.merchant_id and temp2.year_month=tmp.time \n" );
        selSql.append("						LEFT JOIN fx_merchant m ON temp2.merchant_id=m.id \n" );
        selSql.append("						where temp2.merchant_id is not null ");
        selSql.append("						ORDER BY m.id,temp2.year_month asc ");

        return dao.findPage(selSql.toString(),queryParams.toArray(),initPage);
    }

    private String showMonth(){
        StringBuffer sql = new StringBuffer();
        sql.append(" select date_format(curdate(),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 1 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 2 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 3 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 4 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 5 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 6 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 7 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 8 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 9 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 10 month),'%Y-%m') AS `year_month` \n");
        sql.append(" union select date_format((curdate() - interval 11 month),'%Y-%m') AS `year_month`");
        return sql.toString();
    }
    
    
    
    @Override
    public Page<Map<String, Object>> findMerchantSignByPage(Map<String, Object> params, Pageable initPage) {
        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("SELECT t.*,m.pos_code,m.unit_name FROM `fx_merchant_sign` t LEFT JOIN fx_merchant m ON t.merchant_id=m.id  where 1=1 ");
        if(params.containsKey("merchantId") && StringUtils.isNotEmpty(params.get("merchantId").toString())){
            selSql.append(" AND t.merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        if(params.containsKey("type") && StringUtils.isNotEmpty(params.get("type").toString())){
            selSql.append(" AND t.type=? ");
            queryParams.add(params.get("type"));
        }
        if(params.containsKey("orderId") && StringUtils.isNotEmpty(params.get("orderId").toString())){
            selSql.append(" AND t.order_id=? ");
            queryParams.add(params.get("orderId"));
        }
        
        selSql.append(" ORDER BY t.create_time DESC,t.end_sign_time DESC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
    }
    
    
	@Override
	public Page<Map<String, Object>> findMerchantSignStatusByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.pos_code,t.unit_name,o.type,o.num,o.lib_service,o.unit_price,m.sign_time,m.end_sign_time from fx_merchant t     ");
        selSql.append(" LEFT JOIN fx_merchant_sign m ON t.id=m.merchant_id ");
        selSql.append(" LEFT JOIN fx_merchant_sign_order o on m.order_id=o.id  ");
        selSql.append(" WHERE t.sign_type>=1 ");
        if(!"".equals(params.get("merchantId"))){
            selSql.append(" and t.id=? ");
            queryParams.add(params.get("merchantId"));
        }
        if(!"".equals(params.get("type"))){
            selSql.append(" and o.type=? ");
            queryParams.add(params.get("type"));
        }
        if(!"".equals(params.get("signTime")) && !"".equals(params.get("endSignTime"))){
            selSql.append(" AND ( ");
            selSql.append(" (TO_DAYS(m.sign_time) >= TO_DAYS(?) AND TO_DAYS(m.sign_time) <= TO_DAYS(?)) ");
            selSql.append(" OR (TO_DAYS(m.sign_time) <= TO_DAYS(?) AND TO_DAYS(m.end_sign_time) >= TO_DAYS(?)) ");
            selSql.append(" OR (TO_DAYS(m.end_sign_time) >= TO_DAYS(?) AND TO_DAYS(m.end_sign_time) <= TO_DAYS(?)) ");
            selSql.append(" ) ");
            queryParams.add(params.get("signTime"));
            queryParams.add(params.get("signTime"));
            queryParams.add(params.get("signTime"));
            queryParams.add(params.get("endSignTime"));
            queryParams.add(params.get("endSignTime"));
            queryParams.add(params.get("endSignTime"));
        }
        selSql.append(" ORDER BY t.create_time ASC ,m.sign_time ASC,m.end_sign_time ASC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}


    
	@Override
	public Page<Map<String, Object>> findMerchantSignOrderByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,(t.unit_price * t.num) as price,m.unit_name from fx_merchant_sign_order t  ");
        selSql.append(" LEFT JOIN fx_merchant m ON t.merchant_id=m.id  ");
        selSql.append(" where 1=1 ");
        if(!"".equals(params.get("merchantId"))){
            selSql.append(" and t.merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        if(!"".equals(params.get("type"))){
            selSql.append(" and t.type=? ");
            queryParams.add(params.get("type"));
        }
        if(!"".equals(params.get("endSignTime"))){
            selSql.append(" and TO_DAYS(t.create_time)<=TO_DAYS(?) ");
            queryParams.add(params.get("endSignTime"));
        }
        
        if(!"".equals(params.get("signTime"))){
            selSql.append(" and TO_DAYS(t.create_time)>=TO_DAYS(?) ");
            queryParams.add(params.get("signTime"));
        }
        selSql.append(" ORDER BY t.create_time DESC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

    
    
	@Override
	public Page<Map<String, Object>> findMerchantMealOrderPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,m.unit_name from fx_merchant_meal_order t  ");
        selSql.append(" LEFT JOIN fx_merchant m ON t.merchant_id=m.id  ");
        selSql.append(" where 1=1 ");
        if(!"".equals(params.get("merchantId"))){
            selSql.append(" and t.merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        if(!"".equals(params.get("signType"))){
            selSql.append(" and t.sign_type=? ");
            queryParams.add(params.get("signType"));
        }
        
        if(!"".equals(params.get("endSignTime"))){
            selSql.append(" and TO_DAYS(t.create_time)<=TO_DAYS(?) ");
            queryParams.add(params.get("endSignTime"));
        }
        
        if(!"".equals(params.get("signTime"))){
            selSql.append(" and TO_DAYS(t.create_time)>=TO_DAYS(?) ");
            queryParams.add(params.get("signTime"));
        }
        selSql.append(" ORDER BY t.create_time DESC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public Page<Map<String, Object>> findMerchantMealPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,m.unit_name,o.unit_price from fx_merchant_meal t    ");
        selSql.append(" LEFT JOIN fx_merchant_meal_order o on t.order_id=o.id ");
        selSql.append(" LEFT JOIN fx_merchant m ON t.merchant_id=m.id  ");
        selSql.append(" where 1=1 ");
        if(!"".equals(params.get("orderId"))){
            selSql.append(" and t.order_id=? ");
            queryParams.add(params.get("orderId"));
        }
        if(!"".equals(params.get("merchantId"))){
            selSql.append(" and t.merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        selSql.append(" ORDER BY t.create_time DESC,t.end_sign_time DESC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}
	
	
	
	@Override
	public Page<Map<String, Object>> findMerchantMealStatusPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.pos_code,t.unit_name,o.sign_type,o.buy_month,o.give_month,o.lib_service,o.unit_price,o.price,m.sign_time,m.end_sign_time from fx_merchant t     ");
        selSql.append(" LEFT JOIN fx_merchant_meal m ON t.id=m.merchant_id ");
        selSql.append(" LEFT JOIN fx_merchant_meal_order o on m.order_id=o.id  ");
        selSql.append(" WHERE t.sign_type>=1 ");
        if(!"".equals(params.get("merchantId"))){
            selSql.append(" and t.id=? ");
            queryParams.add(params.get("merchantId"));
        }
        if(!"".equals(params.get("signType"))){
            selSql.append(" and o.sign_type=? ");
            queryParams.add(params.get("signType"));
        }
        if(!"".equals(params.get("signTime")) && !"".equals(params.get("endSignTime"))){
            selSql.append(" AND ( ");
            selSql.append(" (TO_DAYS(m.sign_time) >= TO_DAYS(?) AND TO_DAYS(m.sign_time) <= TO_DAYS(?)) ");
            selSql.append(" OR (TO_DAYS(m.sign_time) <= TO_DAYS(?) AND TO_DAYS(m.end_sign_time) >= TO_DAYS(?)) ");
            selSql.append(" OR (TO_DAYS(m.end_sign_time) >= TO_DAYS(?) AND TO_DAYS(m.end_sign_time) <= TO_DAYS(?)) ");
            selSql.append(" ) ");
            queryParams.add(params.get("signTime"));
            queryParams.add(params.get("signTime"));
            queryParams.add(params.get("signTime"));
            queryParams.add(params.get("endSignTime"));
            queryParams.add(params.get("endSignTime"));
            queryParams.add(params.get("endSignTime"));
        }
        selSql.append(" ORDER BY t.create_time ASC,m.sign_time ASC,m.end_sign_time ASC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public Page<Map<String, Object>> findMerchantPlatOrderByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT t.*,m.unit_name from fx_merchant_plat_order t  ");
        selSql.append(" LEFT JOIN fx_merchant m ON t.merchant_id=m.id  ");
        selSql.append(" where 1=1 ");
        if(!"".equals(params.get("merchantId"))){
            selSql.append(" and t.merchant_id=? ");
            queryParams.add(params.get("merchantId"));
        }
        if(!"".equals(params.get("endSignTime"))){
            selSql.append(" and TO_DAYS(t.create_time)<=TO_DAYS(?) ");
            queryParams.add(params.get("endSignTime"));
        }
        
        if(!"".equals(params.get("signTime"))){
            selSql.append(" and TO_DAYS(t.create_time)>=TO_DAYS(?) ");
            queryParams.add(params.get("signTime"));
        }
        selSql.append(" ORDER BY t.create_time DESC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}

	@Override
	public Page<Map<String, Object>> findMerchantCapByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append(" SELECT * from (  ");
        selSql.append(" 	SELECT m.id,m.pos_code,m.unit_name,t.unit_price,t.price,t.buy_month,t.give_month,t.sign_type,t.type,t.lib_service,t.create_time as time,'1' as cap_type from fx_merchant_meal_order t    ");
        selSql.append("  	LEFT JOIN fx_merchant m ON t.merchant_id=m.id ");
        selSql.append("  UNION ");
        selSql.append("  	SELECT m.id,m.pos_code,m.unit_name,o.unit_price,o.price,o.buy_month,o.give_month,o.sign_type,o.type,t.lib_service,t.sign_time as time,2 as cap_type from fx_merchant_meal t  ");
        selSql.append("  	LEFT JOIN fx_merchant m ON t.merchant_id=m.id ");
        selSql.append(" 	LEFT JOIN fx_merchant_meal_order o ON t.order_id=o.id  ");
        selSql.append("  UNION ");
        selSql.append("  	SELECT m.id,m.pos_code,m.unit_name,0 as price,t.price,0 as buy_month,0 as give_month,0 as sign_type,0 as type,0 as lib_service,t.create_time as time,3 as cap_type from fx_merchant_plat_order t  ");
        selSql.append("   	LEFT JOIN fx_merchant m ON t.merchant_id=m.id ");
        selSql.append("  UNION ");
        selSql.append("   	SELECT m.id,m.pos_code,m.unit_name,0 as price,t.price,0 as buy_month,0 as give_month,0 as sign_type,0 as type,0 as lib_service,t.create_time as time,4 as cap_type from fx_merchant_plat_order t ");
        selSql.append("  	LEFT JOIN fx_merchant m ON t.merchant_id=m.id ");
        selSql.append("  UNION	");
        selSql.append(" 	SELECT m.id,m.pos_code,m.unit_name,t.unit_price,0 as price,t.num as buy_month,0 as give_month,0 as sign_type,t.type,t.lib_service,t.create_time as time,'5' as cap_type from fx_merchant_sign_order t 	");
        selSql.append(" 	LEFT JOIN fx_merchant m ON t.merchant_id=m.id	");
        selSql.append("  UNION	");
        selSql.append(" 	SELECT m.id,m.pos_code,m.unit_name,o.unit_price,0 as price,o.num as buy_month,0 as give_month,0 as sign_type,o.type,t.lib_service,t.sign_time as time,'6' as cap_type from fx_merchant_sign t ");
        selSql.append(" 	LEFT JOIN fx_merchant m ON t.merchant_id=m.id ");
        selSql.append(" 	LEFT JOIN fx_merchant_sign_order o ON t.order_id=o.id ");
        selSql.append("  )as tmp  ");
        selSql.append("  WHERE tmp.time<=NOW()  ");
        if(!"".equals(params.get("merchantId"))){
            selSql.append(" and tmp.id=? ");
            queryParams.add(params.get("merchantId"));
        }
        if(!"".equals(params.get("endSignTime"))){
            selSql.append(" and TO_DAYS(tmp.time)<=TO_DAYS(?) ");
            queryParams.add(params.get("endSignTime"));
        }
        
        if(!"".equals(params.get("signTime"))){
            selSql.append(" and TO_DAYS(tmp.time)>=TO_DAYS(?) ");
            queryParams.add(params.get("signTime"));
        }
        selSql.append(" ORDER BY tmp.time DESC ");
        return dao.findPage(selSql.toString(),queryParams.toArray(), initPage);
	}


    
    
    
}
