package org.cs.mgr.user.dao.impl;

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

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

import org.cs.core.dao.impl.BaseDao;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.cs.mgr.user.model.MgmCustomer;
import org.cs.mgr.user.model.MgmInviteRecord;
import org.cs.mgr.user.model.MgmUser;
import org.cs.mgr.user.dao.IMgmCustomerDao;

/**
 * 
 * @ClassName: MgmCustomerDao
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:56
 */
@Repository
public class MgmCustomerDao extends BaseDao<MgmCustomer> implements IMgmCustomerDao {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Pager queryMgmCustomers(Pager page,Map<String, String> map){
		StringBuffer sb = new StringBuffer("select t.id as \"id\",mo.id as \"orderId\",t.create_date as \"createDate\",t.name as \"name\",t.phone as \"phone\",t.attribution_area as \"attributionArea\",u.name as \"recommendName\",mo.amount as \"amount\"");
		sb.append("  from mgm_customer t  left join mgm_order mo on t.id = mo.customer_Id  left join mgm_user u on u.id=t.recommend_id");
		sb.append(" where 0=0");
		/*if(map != null&&StringUtil.isNotBlank(map.get("name"))){
			sb.append(" and u.name like '%"+map.get("name")+"%'");
		}
		if(map != null&&StringUtil.isNotBlank(map.get("phone"))){
			sb.append(" and u.phone like '%"+map.get("phone")+"%'");
		}*/
		return this.findPageBySql(sb.toString(), page, null);
	}
	
	@Override
	public Pager queryMgmCustomerByUserId(Pager page,String userId){
		String hql = "from MgmCustomer where isDelete = false";
		if(StringUtil.isNotBlank(userId)){
			hql+=" and recommendId="+userId;
		}
		hql+="order by id";
		return findPageByHql(hql, page, null);
	}
	
	
	@Override
	public List<MgmCustomer> queryMgmCustomerByUserId(String userId){
		String hql = "from MgmCustomer where isDelete = false ";
		if(StringUtil.isNotBlank(userId)){
			hql+=" and recommendId='"+userId+"'"; 
		}
		hql+=" order by name ";
		return (List<MgmCustomer>) findByHql(hql,null);
	}
	
	@Override
	public Pager queryMyMgmCustomer(Pager page, Integer userId) {
		// TODO Auto-generated method stub
		String hql = "from MgmCustomer where isDelete = false and recommendId = "+userId;
		hql+="  order by name ";
		return findPageByHql(hql, page, null);
	}

	@Override
	public List<MgmCustomer> queryMgmCustomerByMap(Map<String, String> map) {
		// TODO Auto-generated method stub
		Map<String, Object> params = new HashMap<>(); 
		
		String hql = "from MgmCustomer where isDelete = false ";
		
		if(StringUtil.isNotBlank(map.get("name"))){
			hql+=" and name=:name"; 
			params.put("name", map.get("name"));
			
		}
		if(StringUtil.isNotBlank(map.get("phone"))){
			hql+=" and phone=:phone"; 
			params.put("phone", map.get("phone"));
			
		}
		
		//hql+=" order by name ";
		return (List<MgmCustomer>) findByHql(hql,params);
	}
	
	@Override
	public MgmCustomer isPhone(String phone,Integer userId) {
		Map<String, Object> params = new HashMap<>();
		params.put("phone", phone);
		params.put("userId", userId);
		return this.findOneByHql("from MgmCustomer where isDelete = false and phone=:phone and recommendId=:userId", params);
	}
}
