package com.ssj.dao.sys.poster.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.poster.dao.PosterQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;

@Repository
public class PosterQueryDaoImpl  implements PosterQueryDao {

    @Autowired
    private PagingHibernateJdbcDao dao;

	@Override
	public Page<Map<String, Object>> findActivityByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder sb = new StringBuilder();
        sb.append("  SELECT ");
        sb.append("  t.*,group_concat(p.back_img) as back_img, ");
        sb.append("  	( SELECT count(1) from poster_activity_join j WHERE j.activity_id=t.id 	) as count ");
        sb.append(" from poster_activity t,poster_template p  ");
        sb.append(" WHERE t.`status`=1  and FIND_IN_SET(p.id,t.template_id) ");
        List<Object> param = new ArrayList<>();
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
        	 sb.append(" and t.name like concat('%',?,'%')  ");
        	 param.add(params.get("name"));
        }
        sb.append(" group by t.id ");
        sb.append(" ORDER BY t.create_time desc ");
        return dao.findPage(sb.toString(),param.toArray(),initPage);
	}
	
	
	@Override
	public Page<Map<String, Object>> findJoinByPage(Map<String, Object> params, Pageable initPage) {
		StringBuilder sb = new StringBuilder();
        sb.append("  SELECT t.* from poster_join t  ");
        sb.append("  WHERE t.`status`=1    ");
        List<Object> param = new ArrayList<>();
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
        	 sb.append(" and t.name like concat('%',?,'%')  ");
        	 param.add(params.get("name"));
        }
        sb.append(" ORDER BY t.create_time desc ");
        return dao.findPage(sb.toString(),param.toArray(),initPage);
	}

    @Override
    public Page<Map<String, Object>> findActivityJoinByPage(Map<String, Object> params, Pageable initPage) {
        StringBuilder sb = new StringBuilder();
        sb.append("  SELECT a.name,t.name as join_name,b.id,b.create_time,b.poster_img,t.phone,t.logo_img,t.logo_img2,t.qrcode_img1,t.qrcode_img2 from poster_activity a  ");
        sb.append("   left join poster_activity_join b on a.id = b.activity_id ");
        sb.append("   left join poster_join t on b.join_id = t.id ");
        sb.append("  WHERE t.`status`=1    ");
        List<Object> param = new ArrayList<>();
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
            sb.append(" and t.name like concat('%',?,'%')  ");
            param.add(params.get("name"));
        }
        if(params.get("id")!=null && StringUtils.isNotEmpty(params.get("id").toString())) {
            sb.append(" and a.id =? ");
            param.add(params.get("id"));
        }
        sb.append(" ORDER BY b.create_time desc ");
        return dao.findPage(sb.toString(),param.toArray(),initPage);
    }

    @Override
    public List<Map<String, Object>> exportActivityList(Map<String, Object> params) {
	    StringBuilder sb = new StringBuilder();
        sb.append("  SELECT a.name,t.name as join_name,b.poster_img from poster_activity a  ");
        sb.append("   join poster_activity_join b on a.id = b.activity_id ");
        sb.append("   join poster_join t on b.join_id = t.id ");
        sb.append("  WHERE t.`status`=1    ");

        List<Object> param = new ArrayList<>();
        if(params.get("name")!=null && StringUtils.isNotEmpty(params.get("name").toString())) {
            sb.append(" and t.name like concat('%',?,'%')  ");
            param.add(params.get("name"));
        }
        sb.append("  group by a.id   ");
        return dao.findMap(sb.toString(),param.toArray());
    }


}
