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

import com.ssj.bean.sys.cardinfo.domain.CardAward;
import com.ssj.bean.sys.cardinfo.domain.CardAwardLog;
import com.ssj.dao.sys.cardinfo.dao.CardQueryDao;
import com.ssj.framework.core.persistence.PagingHibernateJdbcDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Repository
public class CardQueryDaoImpl implements CardQueryDao { 
	@Autowired
	private PagingHibernateJdbcDao dao;

	@Override
	public Map<String, Object> getCardByIdAward(String id) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  select   id,sn_code, ");
        selSQL.append("  c_name_one,c_num_one, ");
        selSQL.append("  c_name_two,c_num_two, ");
        selSQL.append("    c_name_three ,c_num_three, ");
        selSQL.append("   c_name_four, c_num_four, ");
        selSQL.append("   c_name_five ,c_num_five , ");
        selSQL.append("   c_name_six ,c_num_six  from  tb_card  ");
        selSQL.append(" where   id = ? ");
    	queryParams.add(id);
    	return dao.findMap(selSQL.toString(), queryParams.toArray()).get(0);
	}

	@Override
	public void batchInsertCardAward(List<CardAward> list) {
	    dao.batchInsert(list);
	}

	@Override
	public Page<Map<String, Object>> findCardAward(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT  t.id,t.award_place,t.sn_code,t.award_type,t.award_status,t.award_tel,t.award_time,t.user_id,  ");
        selSQL.append(" t.use_time,t.card_id,u.wx_name  from  tb_card_award   t left join   tb_user  u   on  t.user_id =  u.id ");
        selSQL.append(" where t.card_id = ? ");
        queryParams.add(searchParams.get("id"));
        if (searchParams.get("award_type") != null && !searchParams.get("award_type").equals("")) {
            selSQL.append(" and  t.award_type LIKE  CONCAT(CONCAT('%',?,'%')) ");
            queryParams.add(searchParams.get("award_type"));
        }
        if (searchParams.get("award_status") != null && !searchParams.get("award_status").equals("")) {
        	selSQL.append(" and  t.award_status = ? ");
        	queryParams.add(searchParams.get("award_status"));
        }
        if (searchParams.get("wx_name") != null && !searchParams.get("wx_name").equals("")) {
        	selSQL.append(" and  u.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("wx_name"));
        }
        selSQL.append("   ORDER BY  t.award_place asc   ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public void saveCardAwardLog(List<CardAwardLog> cardAwardLog) {
		dao.batchInsert(cardAwardLog);
	}

	@Override
	public List<Map<String, Object>> getWinning(String cardId) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT   t.id ,t.award_type ,t.award_place ,u.wx_name  from  tb_card_award t LEFT JOIN   tb_user  u  on  t.user_id =  u.id   ");
        selSQL.append("  where  t.award_status = 1  and  t.card_id = ?  ");
        selSQL.append("  ORDER BY  t.award_time desc   ");
    	queryParams.add(cardId);
    	return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public Page<Map<String, Object>> findAwardLog(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT   t.id,t.sn_code,t.stat,t.create_time , a.award_place,a.award_type,u.wx_name   ");
        selSQL.append("   from  tb_card_award_log   t  LEFT JOIN   tb_user  u  on  t.user_id =   u.id  LEFT JOIN   tb_card_award  a  on  a.id =  t.award_id  ");
        selSQL.append(" where t.card_id = ? ");
        queryParams.add(searchParams.get("id"));
        if (searchParams.get("stat") != null && !searchParams.get("stat").equals("")) {
            selSQL.append(" and  t.stat = ? ");
            queryParams.add(searchParams.get("stat"));
        }
        if (searchParams.get("wx_name") != null && !searchParams.get("wx_name").equals("")) {
        	selSQL.append(" and  u.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("wx_name"));
        }
        
        if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
        	selSQL.append(" and TO_DAYS(t.create_time) >=TO_DAYS(?) ");
        	queryParams.add(searchParams.get("beginDate"));
        }
        if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
        	selSQL.append(" and TO_DAYS(t.create_time) <=TO_DAYS(?) ");
        	queryParams.add(searchParams.get("endDate"));
        }
        selSQL.append("   ORDER BY  t.create_time asc   ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	@Override
	public List<Map<String, Object>> getCardByKeyword(String keyword) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append("  select   id from  tb_card  where  keyword  LIKE  CONCAT(CONCAT('%',?,'%'))   ");
    	queryParams.add(keyword);
    	return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> zhongjiang(Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT  t.id,t.award_place,t.sn_code,t.award_type,t.award_tel, date_format( t.award_time, '%Y-%m-%d %H:%i:%s')as award_time ,t.user_id,  ");
        selSQL.append("  CASE t.award_status WHEN 0 THEN 	'未中奖' WHEN 1 THEN	'被抽中' WHEN 2 THEN	'已领奖' ELSE '已兑奖' END  as species ,  ");
        selSQL.append(" t.use_time,t.card_id,u.wx_name  from  tb_card_award   t left join   tb_user  u   on  t.user_id =  u.id ");
        selSQL.append(" where t.card_id = ? ");
        queryParams.add(searchParams.get("id"));
        if (searchParams.get("award_type") != null && !searchParams.get("award_type").equals("")) {
            selSQL.append(" and  t.award_type LIKE  CONCAT(CONCAT('%',?,'%')) ");
            queryParams.add(searchParams.get("award_type"));
        }
        if (searchParams.get("award_status") != null && !searchParams.get("award_status").equals("")) {
        	selSQL.append(" and  t.award_status = ? ");
        	queryParams.add(searchParams.get("award_status"));
        }
        if (searchParams.get("wx_name") != null && !searchParams.get("wx_name").equals("")) {
        	selSQL.append(" and  u.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("wx_name"));
        }
        selSQL.append("   ORDER BY  t.award_place asc   ");
        
    	return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public List<Map<String, Object>> exportCJlog(
			Map<String, Object> searchParams) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT   t.id,t.sn_code,date_format( t.create_time, '%Y-%m-%d %H:%i:%s')as create_time , a.award_place,a.award_type,u.wx_name,   ");
        selSQL.append("  CASE t.stat WHEN 1 THEN 	'已中奖'  ELSE '未中奖' END  as stat   ");
        selSQL.append("   from  tb_card_award_log   t  LEFT JOIN   tb_user  u  on  t.user_id =   u.id  LEFT JOIN   tb_card_award  a  on  a.id =  t.award_id  ");
        selSQL.append(" where t.card_id = ? ");
        queryParams.add(searchParams.get("id"));
        if (searchParams.get("stat") != null && !searchParams.get("stat").equals("")) {
            selSQL.append(" and  t.stat = ? ");
            queryParams.add(searchParams.get("stat"));
        }
        if (searchParams.get("wx_name") != null && !searchParams.get("wx_name").equals("")) {
        	selSQL.append(" and  u.wx_name LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("wx_name"));
        }
        
        if (searchParams.get("beginDate") != null && !searchParams.get("beginDate").equals("")) {
        	selSQL.append(" and TO_DAYS(t.create_time) >=TO_DAYS(?) ");
        	queryParams.add(searchParams.get("beginDate"));
        }
        if (searchParams.get("endDate") != null && !searchParams.get("endDate").equals("")) {
        	selSQL.append(" and TO_DAYS(t.create_time) <=TO_DAYS(?) ");
        	queryParams.add(searchParams.get("endDate"));
        }
        selSQL.append("   ORDER BY  t.create_time asc   ");
        
    	return dao.findMap(selSQL.toString(), queryParams.toArray());
	}

	@Override
	public Page<Map<String, Object>> findByPage(
			Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT  t.* ,j.league_name  from  tb_card   t LEFT JOIN  tb_lib_join  j   ");
        selSQL.append("  on t.lib_id = j.id  ");
        selSQL.append(" where 1=1 ");
        if (searchParams.get("LIKE_name") != null && !searchParams.get("LIKE_name").equals("")) {
        	selSQL.append(" and  t.`name` LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("LIKE_name"));
        }
        if (searchParams.get("LIKE_keyword") != null && !searchParams.get("LIKE_keyword").equals("")) {
        	selSQL.append(" and  t.keyword LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("LIKE_keyword"));
        }
        selSQL.append("   ORDER BY  t.create_time asc   ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 
	 * 新版抽奖活动 
	 * 2020年5月27日10:28:11
	 * 
	 */
	
	@Override
	public Page<Map<String, Object>> findCardMaterialBylist(Map<String, Object> searchParams, Pageable pageable) {
		StringBuilder selSQL = new StringBuilder();
        List<Object> queryParams = new ArrayList<Object>();
        selSQL.append(" SELECT * from tb_card_material t WHERE t.status=1  ");
        if (searchParams.get("name") != null && !searchParams.get("name").equals("")) {
        	selSQL.append(" and  t.`name` LIKE  CONCAT(CONCAT('%',?,'%')) ");
        	queryParams.add(searchParams.get("name"));
        }
        selSQL.append(" ORDER BY t.create_time desc ");
        return dao.findPage(selSQL.toString(), queryParams.toArray(), pageable);
	}
	
	
	
	
	
	
	
	
	
	
}
