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

import com.ssj.dao.sys.fx.dao.FinanceQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import com.ssj.framework.core.util.StringUtil;
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 FinanceQueryDaoImpl implements FinanceQueryDao {
    @Autowired
    private PagingHibernateJdbcDao dao;

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

        selSql.append("	 select a.cost as cost_count,b.add_total,b_add_total-b_cost as last_left,b_add_total-b_cost+b.add_total-a.cost as this_left,a.months \n" );
        selSql.append("	from (\n" );
        selSql.append("	SELECT sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append("sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')<t13.months ,t11.lib_service,0)) as b_cost,t13.months from  \n" );
        selSql.append("(\n" );
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m\\:=@m+1 mon from sys_role ,(select @m\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13 left join tb_lib_join_consume t11    on  t11.type in (2,3) and (t11.from_merchant_id is not null or t11.to_merchant_id is not null)\n" );
        selSql.append("left join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t13.months\n" );
        selSql.append(") a RIGHT  join (		\n" );
        selSql.append("			SELECT sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months,t11.lib_service,0)) as add_total, \n" );
        selSql.append("sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')<t13.months,t11.lib_service,0)) as b_add_total ,t13.months from  \n" );
        selSql.append("(\n" );
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @n\\:=@n+1 mon from sys_role ,(select @n\\:=0) a ) aa limit 12\n" );
        selSql.append("		) t13 left join tb_lib_join_consume t11    on  t11.type =1	and (t11.from_merchant_id is not null or t11.to_merchant_id is not null)\n" );
        selSql.append("		left join fx_merchant t on t11.to_merchant_id = t.id\n" );
        selSql.append("		where t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("		group by t13.months\n" );
        selSql.append("		) b on a.months = b.months");
        return dao.findMap(selSql.toString(),queryParams.toArray());
    }

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

        selSql.append("	 select ifnull(tt.league_name,t.unit_name) as unit_name,t.months,ifnull(a.cost,0) as cost_count,ifnull(a0.cost,0) as cost_count0,ifnull(a1.cost,0) as cost_count1,ifnull(a2.cost,0) as cost_count2,ifnull(a3.cost,0) as cost_count3,ifnull(b.add_total,0) as add_total,ifnull(b_add_total,0)-ifnull(b_cost,0) as last_left,ifnull(b_add_total,0)-ifnull(b_cost,0)+ifnull(b.add_total,0)-ifnull(a.cost,0) as this_left \n" );
        selSql.append(" ,t.pos_code,t.org_code,t.unit_name as unit_name2,t.type,t.`level` ");
        selSql.append("	from \n" );
        selSql.append("(select t.id,t.lib_id,t.pos_code,t.org_code,t.unit_name,case when t.type=1 then '电销' when t.type=2 then '直销' when t.type=3 then '代理' else '' end type,\n");
        selSql.append("case when t.`level`=1 then '市代' when t.`level`=2 then '区代' when t.`level`=3 then '学校独家' when t.`level`=4 then '普通' else '' end `level`, \n");
        selSql.append("t13.months from fx_merchant t left join(\n");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n");
        selSql.append("		from (select @l\\:=@l+1 mon from sys_role ,(select @l\\:=0) a ) aa limit 12\n");
        selSql.append(") t13 on 1=1 where  t13.months<=? and t13.months>=concat(left(CURRENT_DATE,5),'01') ");
        queryParams.add(params.get("month"));
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append(" GROUP BY t.id,t13.months)t  ");
        
        
        
        //总的
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append("sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')<t13.months ,t11.lib_service,0)) as b_cost,t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m\\:=@m+1 mon from sys_role ,(select @m\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type in (2,3,5)\n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=? and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        queryParams.add(params.get("month"));
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a on t.id=a.id and  t.months = a.months \n" );
        
        
        //消费的，有小塾的
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append("t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m0\\:=@m0+1 mon from sys_role ,(select @m0\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11 on t11.type=2 and t11.derver is not null \n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=? and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        queryParams.add(params.get("month"));
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a0 on t.id=a0.id and  t.months = a0.months \n" );
        
        
        //消费的，无小塾的
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append("t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m1\\:=@m1+1 mon from sys_role ,(select @m1\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type=2  and t11.derver is null \n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=? and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        queryParams.add(params.get("month"));
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a1 on t.id=a1.id and  t.months = a1.months \n" );
        
        
        //扣币的
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append(" t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m2\\:=@m2+1 mon from sys_role ,(select @m2\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type=3\n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=? and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        queryParams.add(params.get("month"));
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a2 on t.id=a2.id and  t.months = a2.months \n" );
        
        
        //分配的
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append(" t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m3\\:=@m3+1 mon from sys_role ,(select @m3\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type=5\n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=? and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        queryParams.add(params.get("month"));
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a3 on t.id=a3.id and  t.months = a3.months \n" );
        
        
        
        
        selSql.append(" left  join (		\n" );
        selSql.append("			SELECT t.id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months,t11.lib_service,0)) as add_total, \n" );
        selSql.append("sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')<t13.months,t11.lib_service,0)) as b_add_total ,t13.months from  \n" );
        selSql.append("(\n" );
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @n\\:=@n+1 mon from sys_role ,(select @n\\:=0) a ) aa limit 12\n" );
        selSql.append("		) t13 left join tb_lib_join_consume t11    on  t11.type in (1,4)	\n" );
        selSql.append("		left join fx_merchant t on t11.to_merchant_id = t.id\n" );
        selSql.append("		where t13.months<=? and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        queryParams.add(params.get("month"));
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("		group by t.id,t13.months\n" );
        selSql.append("		) b on t.id = b.id and  t.months = b.months ");
        selSql.append(" left join  tb_lib_join tt on t.lib_id = tt.id \n");
        selSql.append(" where t.months = ?  \n");

        queryParams.add(params.get("month"));
        return dao.findPage(selSql.toString(),queryParams.toArray(),initPage);
    }

    @Override
    public Page<Map<String, Object>> littleDetailList(Map<String, Object> params, Pageable initPage) {

        StringBuilder selSql = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSql.append("select b.pos_code,b.org_code,b.unit_name2,b.type,b.level,b.unit_name,b.cost_count,b.cost_count0,b.cost_count1,b.cost_count2,b.cost_count3,b.add_total,b.last_left,b.this_left,b.months from (");
        selSql.append("	 select t.pos_code,t.org_code,t.unit_name as unit_name2,t.type,t.level,ifnull(tt.league_name,t.unit_name) as unit_name,t.months,ifnull(a.cost,0) as cost_count,ifnull(a0.cost,0) as cost_count0,ifnull(a1.cost,0) as cost_count1,ifnull(a2.cost,0) as cost_count2,ifnull(a3.cost,0) as cost_count3,ifnull(b.add_total,0) as add_total,ifnull(b_add_total,0)-ifnull(b_cost,0) as last_left,ifnull(b_add_total,0)-ifnull(b_cost,0)+ifnull(b.add_total,0)-ifnull(a.cost,0) as this_left \n" );
        selSql.append("	from \n" );

        selSql.append("(select t.id,t.lib_id,t.pos_code,t.org_code,t.unit_name,case when t.type=1 then '电销' when t.type=2 then '直销' when t.type=3 then '代理' else '' end type,\n");
        selSql.append("case when t.`level`=1 then '市代' when t.`level`=2 then '区代' when t.`level`=3 then '学校独家' when t.`level`=4 then '普通' else '' end `level`, \n");
        selSql.append(" t13.months from fx_merchant t left join(\n");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n");
        selSql.append("		from (select @l\\:=@l+1 mon from sys_role ,(select @l\\:=0) a ) aa limit 12\n");
        selSql.append(") t13 on 1=1 where  t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01') ");
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append(" GROUP BY t.id,t13.months)t  ");
        
        
        //总的
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append("sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')<t13.months ,t11.lib_service,0)) as b_cost,t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m\\:=@m+1 mon from sys_role ,(select @m\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type in (2,3,5)\n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a on t.id=a.id and  t.months = a.months \n" );
        
        
        
        //消费的，有小塾
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append(" t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m1\\:=@m1+1 mon from sys_role ,(select @m1\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type=2 and t11.derver is not null \n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a0 on t.id=a0.id and  t.months = a0.months \n" );
        
      //消费的,无小塾
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append(" t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m1\\:=@m1+1 mon from sys_role ,(select @m1\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type=2 and t11.derver is null \n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a1 on t.id=a1.id and  t.months = a1.months \n" );
        
        
        //扣币的
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append(" t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m2\\:=@m2+1 mon from sys_role ,(select @m2\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type=3 \n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a2 on t.id=a2.id and  t.months = a2.months \n" );
        
        
        //分配的
        selSql.append("left join(\n" );
        selSql.append("	SELECT  t.id,t.unit_name,t.lib_id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months  ,t11.lib_service,0)) as cost,\n" );
        selSql.append(" t13.months from  \n" );
        selSql.append("(");
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @m3\\:=@m3+1 mon from sys_role ,(select @m3\\:=0) a ) aa limit 12\n" );
        selSql.append(") t13  left join tb_lib_join_consume t11    on  t11.type=5 \n" );
        selSql.append("right join fx_merchant t on t11.from_merchant_id = t.id\n" );
        selSql.append("where t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("group by t.id,t13.months\n" );
        selSql.append(") a3 on t.id=a3.id and  t.months = a3.months \n" );
        

        selSql.append(" left  join (		\n" );
        selSql.append("			SELECT t.id,sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')=t13.months,t11.lib_service,0)) as add_total, \n" );
        selSql.append("sum(if(DATE_FORMAT(t11.create_time,'%Y-%m')<t13.months,t11.lib_service,0)) as b_add_total ,t13.months from  \n" );
        selSql.append("(\n" );
        selSql.append("		select case when length(mon)=1 then concat(left(CURRENT_DATE,5),'0',mon) else concat(left(CURRENT_DATE,5),mon) end months\n" );
        selSql.append("		from (select @n\\:=@n+1 mon from sys_role ,(select @n\\:=0) a ) aa limit 12\n" );
        selSql.append("		) t13 left join tb_lib_join_consume t11    on  t11.type in (1,4)	\n" );
        selSql.append("		left join fx_merchant t on t11.to_merchant_id = t.id\n" );
        selSql.append("		where t13.months<=left(CURRENT_DATE,7) and t13.months>=concat(left(CURRENT_DATE,5),'01')\n" );
        if(!StringUtil.isEmpty(params.get("posCode"))){
            selSql.append("  and t.org_code like concat(?,'%')\n");
            queryParams.add(params.get("posCode"));
        }
        selSql.append("		group by t.id,t13.months\n" );
        selSql.append("		) b on t.id = b.id and  t.months = b.months");
        selSql.append(" left join  tb_lib_join tt on t.lib_id = tt.id \n");
        selSql.append(" )b where 1=1 \n");


        if(!StringUtil.isEmpty(params.get("level"))){
            selSql.append("  and b.level like concat('%',?,'%')\n");
            queryParams.add(params.get("level"));
        }
        if(!StringUtil.isEmpty(params.get("unitName"))){
            selSql.append("  and b.unit_name like concat('%',?,'%')\n");
            queryParams.add(params.get("unitName"));
        }
        if(!StringUtil.isEmpty(params.get("startMonth"))){
            selSql.append("  and b.months >=?\n");
            queryParams.add(params.get("startMonth"));
        }
        if(!StringUtil.isEmpty(params.get("endMonth"))){
            selSql.append("  and b.months <=?\n");
            queryParams.add(params.get("endMonth"));
        }

        return dao.findPage(selSql.toString(),queryParams.toArray(),initPage);
    }
    
    
    @Override
	public Page<Map<String, Object>> findDeviceBalanceAndLossList(Map<String, Object> params, Pageable pageable) {
    	  StringBuilder selSql = new StringBuilder();
          List<Object> queryParams = new ArrayList<Object>();
          selSql.append(" SELECT a.start_time AS start_time,a.league_name AS league_name,a.id AS lib_id,a.success AS success,a.success2 AS success2,a.success3 AS success3,a.success4 AS success4,a.success5 AS success5,a.success6 AS success6,	a.success5 + a.success - a.success3 AS success7,a.success6 + a.success2 - a.success4 AS success8 ");
          selSql.append(" FROM ( ");
	          selSql.append(" SELECT ? AS start_time, 	t12.league_name,t12.id,");
	          selSql.append(" count(IF (t.derver IS NOT NULL AND t11.end_time > ? AND t11.start_time < ?, TRUE, NULL)) AS success, ");
	          selSql.append(" count(IF (t.derver IS NULL AND t11.end_time > ? AND t11.start_time < ?, TRUE,NULL)) AS success2, ");
	          selSql.append(" count(IF (t.derver IS NOT NULL AND DATE_FORMAT(t11.end_time, '%Y-%m')= ?, TRUE, NULL)) AS success3, ");
	          selSql.append(" count(IF (t.derver IS NULL AND DATE_FORMAT(t11.end_time, '%Y-%m')= ?, TRUE, NULL )) AS success4, ");
	          selSql.append(" count(IF (t.derver IS NOT NULL AND t11.start_time REGEXP ?,TRUE,NULL)) AS success5, ");
	          selSql.append(" count(IF (t.derver IS NULL AND t11.start_time REGEXP ?,TRUE,NULL)) AS success6 ");
	       selSql.append(" FROM tb_lib_vip t ");
	       selSql.append(" JOIN tb_lib_vip_service t11 ON t.id = t11.vip_id ");
           selSql.append(" JOIN tb_lib_join t12 ON t11.lib_id = t12.id ");
           selSql.append(" WHERE t11.species = 'S003'  AND DATE_FORMAT(t11.start_time, '%Y-%m') <= ? ");
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           queryParams.add(params.get("startMonth"));
           if(!StringUtil.isEmpty(params.get("unitName"))){
               selSql.append(" AND t12.league_name  like concat('%',?,'%') ");
               queryParams.add(params.get("unitName"));
           }
           selSql.append("  GROUP BY t12.id ");
          selSql.append("  ) a ");
          return dao.findPage(selSql.toString(),queryParams.toArray(),pageable);
	}

    
	@Override
	public Page<Map<String, Object>> findDeviceBalanceList(Map<String, Object> params, Pageable pageable) {
		 StringBuilder selSql = new StringBuilder();
         List<Object> queryParams = new ArrayList<Object>();
         selSql.append(" SELECT t.*,j.league_name,case when f.`level`=1 then '市代' when f.`level`=2 then '区代' when f.`level`=3 then '学校独家' when f.`level`=4 then '普通' else '' end `level`  ");
         selSql.append(" ,f.pos_code,f.org_code,f.unit_name as unit_name2   ");
         selSql.append(" FROM `fx_device_balance` t LEFT JOIN tb_lib_join j ON t.lib_id=j.id left join fx_merchant f on j.id = f.lib_id where 1=1 ");
         if(!StringUtil.isEmpty(params.get("startMonth"))){
        	 selSql.append(" AND t.balance_month=? ");
             queryParams.add(params.get("startMonth"));
         }
         if(!StringUtil.isEmpty(params.get("unitName"))){
             selSql.append(" AND j.league_name  like concat('%',?,'%') ");
             queryParams.add(params.get("unitName"));
         }
         selSql.append("   ORDER BY t.create_time DESC ");
         return dao.findPage(selSql.toString(),queryParams.toArray(),pageable);
	}

	@Override
	public Page<Map<String, Object>> balanceConsumeDetailList(Map<String, Object> params, Pageable pageable) {
		 StringBuilder selSql = new StringBuilder();
         List<Object> queryParams = new ArrayList<Object>();
         
         selSql.append(" SELECT tmp.* ");
         selSql.append(" ,((cons_count+deduct_count) * unit_price) AS unit_price_count ");
         selSql.append(" ,(cons_count+deduct_count) AS port_count ");
//         2020.11.2修改内容
//         selSql.append(" ,((cons_count+deduct_count) * 20 + derver_count*10) AS cost_count ");
         selSql.append(" ,((cons_count+deduct_count) * 20 ) AS cost_count ");
         selSql.append(" ,? as balance_month ");
         queryParams.add(params.get("startMonth"));
         
         selSql.append(" from ( ");
         
         selSql.append(" SELECT 	t.*,j.league_name,case when t.`level`=1 then '市代' when t.`level`=2 then '区代' when t.`level`=3 then '学校独家' when t.`level`=4 then '普通' else '' end `level_name`  ");
         selSql.append(" ,( ");
         selSql.append(" 	SELECT IFNULL(SUM(c.lib_service),0) from tb_lib_join_consume c, fx_merchant d where d.org_code like concat(t.org_code,'%') and c.from_merchant_id = d.id AND c.type=2 ");
         if(!StringUtil.isEmpty(params.get("startMonth"))){
        	 selSql.append(" AND DATE_FORMAT(c.create_time,'%Y-%m')=?  ");
             queryParams.add(params.get("startMonth"));
         }
         selSql.append(" ) AS cons_count");
         selSql.append(",( ");
         selSql.append(" 	SELECT IFNULL(SUM(c.lib_service),0) from tb_lib_join_consume c,fx_merchant d where d.org_code like concat(t.org_code,'%') and c.from_merchant_id = d.id AND c.type=3 ");
         if(!StringUtil.isEmpty(params.get("startMonth"))){
        	 selSql.append(" AND DATE_FORMAT(c.create_time,'%Y-%m')=?  ");
             queryParams.add(params.get("startMonth"));
         }
         selSql.append(" ) AS deduct_count");
         selSql.append(" ,IFNULL(t.price,0) AS unit_price ");
         selSql.append(" ,( ");
         selSql.append(" 	SELECT IFNULL(count(1),0) from tb_lib_join_consume c where c.from_merchant_id=t.id AND c.type=2 AND c.derver IS NOT NULL AND c.derver !='' ");
         if(!StringUtil.isEmpty(params.get("startMonth"))){
        	 selSql.append(" AND DATE_FORMAT(c.create_time,'%Y-%m')=?  ");
             queryParams.add(params.get("startMonth"));
         }
         selSql.append(" ) AS derver_count");
         selSql.append(" from fx_merchant t ");
         selSql.append(" LEFT JOIN tb_lib_join j on t.lib_id=j.id ");
         selSql.append(" where t.`status`!=4 and t.type in (1,2,5,6)  ");
         selSql.append(" and (t.sign_type=0 OR t.id in (SELECT m.from_merchant_id from tb_lib_join_consume m WHERE m.type in (2,3)    ");
         if(!StringUtil.isEmpty(params.get("startMonth"))){
        	 selSql.append(" AND DATE_FORMAT(m.create_time,'%Y-%m')=?  ");
             queryParams.add(params.get("startMonth"));
         }
         selSql.append(" GROUP BY m.from_merchant_id )) ");
         if(!StringUtil.isEmpty(params.get("unitName"))){
             selSql.append(" AND (t.unit_name LIKE concat('%',?,'%')  OR j.league_name LIKE concat('%',?,'%')  ) ");
             queryParams.add(params.get("unitName"));
             queryParams.add(params.get("unitName"));
         }
         selSql.append(" ORDER BY FIELD(t.`type`,1,2,5,6),t.create_time asc ");
         selSql.append(" ) as tmp ");
         return dao.findPage(selSql.toString(),queryParams.toArray(),pageable);
	}

	
	

	@Override
	public Page<Map<String, Object>> balanceConsumeDetailList_New(Map<String, Object> params, Pageable pageable) {
		 StringBuilder selSql = new StringBuilder();
         List<Object> queryParams = new ArrayList<Object>();
         
         selSql.append(" SELECT t.*, ");
         selSql.append(" (IFNULL((SELECT MAX(c.lib_service) from tb_lib_join_consume c WHERE c.from_merchant_id=t.id AND c.type=7 AND DATE_FORMAT(c.create_time,'%Y-%m')=? ),0)+  ");
         selSql.append(" IFNULL((SELECT SUM(c.lib_service) from tb_lib_join_consume c WHERE c.from_merchant_id=t.id AND c.type=10 AND DATE_FORMAT(c.create_time,'%Y-%m')=? ),0)) ");
         selSql.append(" as sum_1,  ");
         selSql.append(" (IFNULL((SELECT MAX(c.lib_service) from tb_lib_join_consume c WHERE c.from_merchant_id=t.id AND c.type=8 AND DATE_FORMAT(c.create_time,'%Y-%m')=? ),0)+ ");
         selSql.append(" IFNULL((SELECT SUM(c.lib_service) from tb_lib_join_consume c WHERE c.from_merchant_id=t.id AND c.type=11 AND DATE_FORMAT(c.create_time,'%Y-%m')=? ),0)) ");
         selSql.append(" as sum_2,  ");
         selSql.append(" IFNULL((SELECT SUM(c.lib_service) from tb_lib_join_consume c WHERE c.from_merchant_id=t.id AND c.type=10 AND DATE_FORMAT(c.create_time,'%Y-%m')=? ),0) as sum_3, ");
         selSql.append(" IFNULL((SELECT SUM(c.lib_service) from tb_lib_join_consume c WHERE c.from_merchant_id=t.id AND c.type=11 AND DATE_FORMAT(c.create_time,'%Y-%m')=? ),0) as sum_4, ");
         selSql.append(" (SELECT COUNT(*) from tb_lib_join_consume c WHERE c.from_merchant_id=t.id AND c.type=7 AND DATE_FORMAT(c.create_time,'%Y-%m')=? ) as count_1, ");
         selSql.append(" IFNULL((SELECT SUM(c.compute_num) from tb_lib_join_consume c WHERE c.from_merchant_id=t.id AND c.type=10 AND DATE_FORMAT(c.create_time,'%Y-%m')=? ),0) as count_2, ");
         selSql.append(" ? as balance_month ");
         queryParams.add(params.get("startMonth"));
         queryParams.add(params.get("startMonth"));
         queryParams.add(params.get("startMonth"));
         queryParams.add(params.get("startMonth"));
         queryParams.add(params.get("startMonth"));
         queryParams.add(params.get("startMonth"));
         queryParams.add(params.get("startMonth"));
         
         queryParams.add(params.get("startMonth"));
         queryParams.add(params.get("startMonth"));

         selSql.append(" from fx_merchant t ");
         selSql.append(" LEFT JOIN tb_lib_join j on t.lib_id=j.id ");
         selSql.append(" where t.`status`!=4 and t.sign_type>=1 and t.type in (1,2,5) ");
         if(!StringUtil.isEmpty(params.get("unitName"))){
             selSql.append(" AND (t.unit_name LIKE concat('%',?,'%')  OR j.league_name LIKE concat('%',?,'%')  ) ");
             queryParams.add(params.get("unitName"));
             queryParams.add(params.get("unitName"));
         }
         selSql.append(" ORDER BY FIELD(t.`type`,1,2),t.create_time asc  ");
         return dao.findPage(selSql.toString(),queryParams.toArray(),pageable);
	}

	
	
}
