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

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

import java.util.HashMap;
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.order.model.MgmOrderBreak;
import org.cs.mgr.order.dao.IMgmOrderBreakDao;

/**
 * 
 * @ClassName: MgmOrderBreakDao
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:03
 */
@Repository
public class MgmOrderBreakDao extends BaseDao<MgmOrderBreak> implements IMgmOrderBreakDao {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public Pager queryMgmOrderBreaks(Pager page){
		String hql = "from MgmOrderBreak where isDelete = false";
		return findPageByHql(hql, page, null);
	}

	@Override
	public Pager getMgmOrderBreaksByS(Pager page, Map<String, String> map) {
		// TODO Auto-generated method stub
		StringBuffer sqlbf=new StringBuffer();
		String orderNo= map.get("orderNo");
		String name=map.get("name");
		
		String roleCodeN= map.get("roleCodeN");
		String one_category_id= map.get("one_category_id");
		String two_category_id= map.get("two_category_id");
		String three_category_id= map.get("three_category_id");
		
		Map<String,Object> params = new HashMap<String,Object>();	
		sqlbf.append("SELECT t.* ," +roleCodeN+" as \"roleCodeN\""
				+ " ,t1.gc_id as \"gc_id\",t1.gc_parent_id as \"gc_parent_id\""
				+ "  FROM ORDER_VIEW_BREAK t"
				+ "  ,order_view_product_role t1"
				+ "  WHERE  t.\"id\" = t1.order_id  AND t.\"is_delete\"=0 ");
		
		if(StringUtil.isNotBlank(roleCodeN)) {
			if("2".equals(roleCodeN)) {
				if(StringUtil.isNotBlank(one_category_id)) {
					sqlbf.append(" and t1.gc_id = :gc_id  ");
					params.put("gc_id",one_category_id);
				}
				if(StringUtil.isNotBlank(two_category_id)) {
					sqlbf.append(" and t1.gc_parent_id = :gc_parent_id  ");
					params.put("gc_parent_id",two_category_id);
				}
			}else if("1".equals(roleCodeN)) {
				if(StringUtil.isNotBlank(one_category_id)) {
					sqlbf.append(" and t1.gc_id = :gc_id  ");
					params.put("gc_id",one_category_id);
				}
			}
		}
		
		if(StringUtil.isNotBlank(orderNo)) {
			params.put("orderNo", "%"+orderNo+"%");
			sqlbf.append(" AND t.\"order_no\" like :orderNo");
		}
				
		if(StringUtil.isNotBlank(name)) {
			params.put("name", "%"+name+"%");
			sqlbf.append(" AND t.\"customer_name\" like :name ");
			
		}
		
		sqlbf.append(" order by t.\"apply_time\" desc");
		return this.findPageBySql(sqlbf.toString(), page, params);
	}

	@Override
	public Pager queryMgmOrderBreaksById(Pager page, Map<String, String> map) {
		// TODO Auto-generated method stub
		String orderId = map.get("orderId");
		
		Map<String,Object> params = new HashMap<String,Object>();	
		
		String hql = "from MgmOrderBreak where isDelete = false and applyState=1";
		
		if(StringUtil.isNotBlank(orderId)) {
			hql += " and orderId="+orderId;
			params.put("orderId", orderId)	;
		}
		
		return findPageByHql(hql, page, params);
	}
}
