package com.palmnest.dao.dinner.impl;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import cn.jiguang.common.utils.StringUtils;

import com.palmnest.application.bean.Page;
import com.palmnest.application.dto.output.IntegralRecodeDto;
import com.palmnest.dao.dinner.IntegralRecodeInfoDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.dinner.IntegralRecodeInfo;

@Repository("integralRecodeInfoDao")
public class IntegralRecodeInfoDaoImpl extends GenericDaoHibernate<IntegralRecodeInfo, Long> implements IntegralRecodeInfoDao {
	private static Logger LOG = LoggerFactory.getLogger(IntegralRecodeInfoDaoImpl.class);

	public IntegralRecodeInfoDaoImpl() {
		super(IntegralRecodeInfo.class);
	}

	/**
	 * 根据项目手机号查找该用户下相关的操作记录
	 *
	 * @param mobile
	 * @param orgId
	 * @return
	 */
	@Override
	public List<IntegralRecodeInfo> getRecodeInfo(String mobile, String orgId) {
		StringBuilder sql = new StringBuilder(" from IntegralRecodeInfo where 1=1 ");
		if (StringUtils.isNotEmpty(orgId)) {
			sql.append(" and orgId in (" + orgId + ") ");
		}
		if (StringUtils.isNotEmpty(mobile)) {
			sql.append(" and targetNumber='" + mobile + "'   ");
		}
		sql.append(" order by createTime desc,type ");
		return getSession().createQuery(sql.toString()).list();
	}
	
	/**
	 * 根据项目手机号查找该用户下相关的操作记录
	 *
	 * @param mobile
	 * @param orgId
	 * @return
	 */
	@Override
	public Page getPageRecodeInfo(Map<String, Object> map, int pageSize, int pageNo) {
		
		StringBuilder sql = getRecodeInfoSql(map);
		
		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}
	
	/**
	 * 根据项目手机号查找该用户下相关的操作记录
	 *
	 * @param mobile
	 * @param orgId
	 * @return
	 */
	@Override
	public List<IntegralRecodeInfo> getRecodeInfoExcelList(Map<String, Object> map) {
		
		StringBuilder sql = getRecodeInfoSql(map);
		
		return getSession().createSQLQuery(sql.toString()).addEntity(IntegralRecodeInfo.class).list();
	}

	private StringBuilder getRecodeInfoSql(Map<String, Object> map) {
		StringBuilder sql = new StringBuilder(" select *  from tb_integral_record where 1=1 ");
		if (map.containsKey("orgId") && StringUtils.isNotEmpty(map.get("orgId") == null ? "" : map.get("orgId").toString())) {
			sql.append(" and orgId in (" + map.get("orgId").toString() + ") ");
		}
		if (map.containsKey("mobile") && StringUtils.isNotEmpty(map.get("mobile") == null ? "" : map.get("mobile").toString())) {
			sql.append(" and targetNumber='" + map.get("mobile").toString() + "'   ");
		}
		
		if (map.containsKey("type") && StringUtils.isNotEmpty(map.get("type") == null ? "" : map.get("type").toString())) {
			sql.append(" and type='" + map.get("type").toString() + "'   ");
		}	
		
		//下单时间
		if (null!=map.get("beginTime") && org.apache.commons.lang3.StringUtils.isNotEmpty(map.get("beginTime").toString())) {
			String beginTime = map.get("beginTime").toString() + " 00:00:00 ";
			if (null!=map.get("endTime") && org.apache.commons.lang3.StringUtils.isNotEmpty(map.get("endTime").toString())) {
				String endTime = map.get("endTime").toString() + " 23:59:59 ";
				sql.append(" and (createTime>='" + beginTime + "' and createTime<='" + endTime + "' ) ");
			} else {
				sql.append(" and  createTime>='" + beginTime + "' ");
			}
		} else if (null!=map.get("endTime") && org.apache.commons.lang3.StringUtils.isNotEmpty(map.get("endTime").toString())) {
			String endTime = map.get("endTime").toString() + " 23:59:59 ";
			sql.append(" and createTime<='" + endTime + "'  ");
		}
		
		sql.append(" order by createTime desc,type ");
		return sql;
	}
	

	@Override
	public Page findList(Map<String, Object> map, int pageSize, int pageNo) {
		StringBuilder sql = new StringBuilder("");
		sql.append(" select r.id,r.type,r.targetName,r.targetNumber,r.creator,ifnull(au.USERNAME,u.userName) creatorName ");
		sql.append(" ,ifnull(au.TELEPHONE,u.userMobile) creatorMobile,date_format(r.createTime,'%Y-%m-%d %T') createTime,r.oldIntegral,r.newIntegral ");
		sql.append(" ,r.exchangeIntegral,r.targetId ");
		sql.append(" from tb_integral_record r ");
		sql.append(" left join tb_app_user au on au.id=r.creator ");
		sql.append(" left join tb_user u on u.id=r.creator ");
		sql.append(" where 1=1  ");
		if (map.containsKey("orgId") && StringUtils.isNotEmpty(map.get("orgId") == null ? "" : map.get("orgId").toString())) {
			sql.append(" and r.orgId in (" + map.get("orgId").toString() + ") ");
		}
		if (map.containsKey("opName") && StringUtils.isNotEmpty(map.get("opName") == null ? "" : map.get("opName").toString())) {
			sql.append(" and (au.USERNAME like '%" + map.get("opName").toString() + "%' or u.userName like '%" + map.get("opName").toString() + "%')  ");
		}
		if (map.containsKey("opType") && StringUtils.isNotEmpty(map.get("opType") == null ? "" : map.get("opType").toString()) && !"0".equals(map.get("opType").toString())) {
			sql.append(" and r.type=" + map.get("opType") + "  ");
		}
		if (map.containsKey("targetName") && StringUtils.isNotEmpty(map.get("targetName") == null ? "" : map.get("targetName").toString())) {
			sql.append(" and r.targetName=" + map.get("targetName") + "  ");
		}
		if (map.containsKey("targetNumber") && StringUtils.isNotEmpty(map.get("targetNumber") == null ? "" : map.get("targetNumber").toString())) {
			sql.append(" and r.targetNumber=" + map.get("targetNumber") + "  ");
		}
		//创建时间
		if (map.containsKey("beginTime") && StringUtils.isNotEmpty(map.get("beginTime") == null ? "" : map.get("beginTime").toString())) {
			String beginTime = map.get("beginTime").toString() + " 00:00:00 ";
			if (map.containsKey("endTime") && StringUtils.isNotEmpty(map.get("endTime") == null ? "" : map.get("endTime").toString())) {
				String endTime = map.get("endTime").toString() + " 23:59:59 ";
				sql.append(" and (r.createTime>='" + beginTime + "' and r.createTime<='" + endTime + "' ) ");
			} else {
				sql.append(" and  r.createTime>='" + beginTime + "' ");
			}
		} else if (map.containsKey("endTime") && StringUtils.isNotEmpty(map.get("endTime") == null ? "" : map.get("endTime").toString())) {
			String endTime = map.get("endTime").toString() + " 23:59:59 ";
			sql.append(" and r.createTime<='" + endTime + "'  ");
		}
		sql.append(" order by r.createTime desc,r.type ");
		return pagedQueryByJdbcTemplate(sql.toString(), pageNo, pageSize);
	}
}
