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.MgmUser;
import org.cs.mgr.user.dao.IMgmUserDao;

/**
 * 
 * @ClassName: MgmUserDao
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:28:55
 */
@Repository
public class MgmUserDao extends BaseDao<MgmUser> implements IMgmUserDao {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Pager queryMgmUsers(Pager page){
		String hql = "from MgmUser where isDelete = false";
		return findPageByHql(hql, page, null);
	}
	
	@Override
	public Map<String, Object> loginNamePwd(String phone) {
		StringBuilder sb = new StringBuilder(); 
		sb.append("SELECT a.address as \"address\",a.qq as \"qq\",a.weixin as \"weixin\",a.id as \"id\",a.type as \"type\",a.personnelcode as \"personnelcode\",a.pwd as \"pwd\",a.name as \"name\",a.dept_Name as \"deptName\",a.dept_Code as \"deptCode\",a.dept_Path as \"deptPath\",a.sex as \"sex\",a.mail as \"mail\",a.phone as \"phone\",a.idnumber as \"idnumber\",a.state as \"state\",a.classify as \"classify\",a.invitecode as \"invitecode\",a.first_Login as \"firstLogin\"");
		sb.append(" FROM mgm_user a where a.is_Delete =0 and ( a.personnelcode='"+phone+"' or a.phone='"+phone+"')");
		
		List<Map<String,Object>> list = jdbcTemplate.queryForList(sb.toString());
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public Map<String, Object> findByPersonnalcode(String personnalcode) {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT a.address as \"address\",a.qq as \"qq\",a.weixin as \"weixin\",a.id as \"id\",a.type as \"type\",a.personnelcode as \"personnelcode\",a.pwd as \"pwd\",a.name as \"name\",a.dept_Name as \"deptName\",a.dept_Code as \"deptCode\",a.dept_Path as \"deptPath\",a.sex as \"sex\",a.mail as \"mail\",a.phone as \"phone\",a.idnumber as \"idnumber\",a.state as \"state\",a.classify as \"classify\",a.invitecode as \"invitecode\",a.first_Login as \"firstLogin\"");
		sb.append(" FROM mgm_user a where a.is_Delete =0 and a.personnelcode='"+personnalcode+"' ");

		List<Map<String,Object>> list = jdbcTemplate.queryForList(sb.toString());
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public MgmUser getByPropertie(String value,String entry) {
		Map<String, Object> params = new HashMap<>();
		params.put("value", value);
		return this.findOneByHql("from MgmUser where isDelete = false and "+entry+"=:value", params);
	}
	
	@Override
	public Pager findMgmUser(Pager page, Map<String, String> map){
		StringBuffer sb = new StringBuffer("select id,type,phone,personnelcode,pwd,name,sex,mail,idnumber,state,classify,careerpath,post,job,professionallevel,weixin,qq,address,invitecode,province,city,area");
		sb.append(" from mgm_user u");
		sb.append(" where 0=0 and u.is_delete =0");
		
		if(map != null&&StringUtil.isNotBlank(map.get("personnelcode"))){
			sb.append(" and u.personnelcode='"+map.get("personnelcode")+"'");
		}
		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")+"%'");
		}
		if(map != null&&StringUtil.isNotBlank(map.get("type"))){
			sb.append(" and u.type='"+map.get("type")+"'");
		}
		return this.findPageBySql(sb.toString(), page, null);
	}

	@Override
	public Pager getMgmUsersBySeach(Pager page, Map<String, String> map) {
		// TODO Auto-generated method stub
		String hql = "from MgmUser where isDelete = false and type=0 ";
		Map<String, Object> params = new HashMap<>();
		
		String search=map.get("name");
		
		if(StringUtil.isNotBlank(search)) {
			params.put("search", "%"+search+"%");
			hql+= " and (name like :search or phone like :search)";
		}
		
		return findPageByHql(hql, page, params);
		
		
	}

	@Override
	public Pager getMgmUsersBySeachTj(Pager page, Map<String, String> map) {
		// TODO Auto-generated method stub
		StringBuffer sqlbf=new StringBuffer();
		sqlbf.append("select a.id as \"id\",a.name as \"name\",a.phone as \"phone\",a.dept_name as \"dept_name\",a.post as \"post\" ");
		sqlbf.append(" from mgm_user a ");
		sqlbf.append(" where  a.is_delete = 0 ");
		sqlbf.append(" and a.id in (");
		sqlbf.append(" select a1.recommend_id");
		sqlbf.append(" from mgm_customer a1,mgm_customer b");
		sqlbf.append("  where a1.name=b.name");
		sqlbf.append("  and a1.is_delete =0");
		sqlbf.append("  and a1.phone=b.phone");		
		if(StringUtil.isNotBlank(map.get("customerId"))) {					
			sqlbf.append(" and b.id="+map.get("customerId"));
		}		
		sqlbf.append(" )");
		
		return this.findPageBySql(sqlbf.toString(), page, null);
	}
	
/*	@Override
	public MgmUser getByPropertie(String value,String entry) {
		Map<String, Object> params = new HashMap<>();
		params.put("value", value);
		return this.findOneByHql("from MgmUser where "+entry+"=:value", params);
	}*/
}
