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

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

import org.cs.core.dao.impl.BaseDao;
import org.cs.mgr.product.dao.IMgmIntentOrderDao;
import org.cs.mgr.product.model.MgmIntentOrder;
import org.cs.util.Pager;
import org.cs.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 * 
 * @ClassName: MgmProductFollowDao
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:10
 */
@Repository
public class MgmIntentOrderDao extends BaseDao<MgmIntentOrder> implements IMgmIntentOrderDao {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Pager queryIntentPager(Pager pager, Map<String, String> map){
		 /*left join mgr_admin ma on ma.id=mo.user_id left join mgr_user_role mur on mur.user_code=ma.code left join mgr_role mr on mr.code = mur.role_code */
		StringBuffer hql = new StringBuffer("select mo.*, mp.product_name as productName from mgm_intent_order mo left join mgm_product mp on mp.id=mo.product_id where mo.is_delete=:isdelete");
		Map<String, Object> params = new HashMap<>();
		params.put("isdelete", false);
		if(StringUtil.isNotBlank(map.get("cname"))){
			hql.append(" and mo.customer_name like :cname");
			params.put("cname", "%"+map.get("cname")+"%");
		}
		if(StringUtil.isNotBlank(map.get("cphone"))){
			hql.append(" and mo.customer_phone like :cphone");
			params.put("cphone", "%"+map.get("cphone")+"%");
		}
		if(StringUtil.isNotBlank(map.get("pname"))){
			hql.append(" and mp.product_name like :pname");
			params.put("pname", "%"+map.get("pname")+"%");
		}
		
		if(StringUtil.isNotBlank(map.get("oneid").trim()) && !"0".equals(map.get("oneid"))){
			hql.append(" and mp.gc_id =:oneid");
			params.put("oneid", Integer.valueOf(map.get("oneid").trim()));
		}
		
		if(StringUtil.isNotBlank(map.get("twoid").trim())&& !"0".equals(map.get("twoid"))){
			hql.append(" and mp.gc_parent_id =:twoid");
			params.put("twoid", Integer.valueOf(map.get("twoid").trim()));
		}
		
		if(StringUtil.isNotBlank(map.get("threeid").trim())&& !"0".equals(map.get("threeid"))){
			hql.append(" and mp.gc_three_id =:threeid");
			params.put("threeid", Integer.valueOf(map.get("threeid").trim()));
		}
		
		if(StringUtil.isNotBlank(map.get("fourid").trim())&& !"0".equals(map.get("fourid"))){
			hql.append(" and mp.gc_four_id =:fourid");
			params.put("fourid", Integer.valueOf(map.get("fourid").trim()));
		}
		
		hql.append(" order by mo.create_date desc");
		return this.findPageBySql(hql.toString(), pager, params);
	}
	
	
	@Override
	public Map<String, Object> queryIntent(Map<String, String> map){
		StringBuffer hql = new StringBuffer("select mo.*, mp.product_name as productName from mgm_intent_order mo left join mgm_product mp on mp.id=mo.product_id where mo.id=?");
//		Map<String, Object> params = new HashMap<>();
//		params.put("id", Integer.valueOf(map.get("id")));
		List<Map<String, Object>> mlsit = this.findBySql(hql.toString(), new Object[]{Integer.valueOf(map.get("id"))});
		if(mlsit != null && !mlsit.isEmpty()){
			return mlsit.get(0);
		}
		else{
			return new HashMap<>();
		}
	}
}
