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.MgmOrder;
import org.cs.mgr.order.dao.IMgmOrderDao;

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

	@Override
	public Pager queryMyMgmOrders(Pager page, Long userId) {
		// TODO Auto-generated method stub
		
		Map<String,Object> params = new HashMap<String,Object>();	
		params.put("userId", userId);
		String hql = "from MgmOrder where isDelete = false and (userId=:userId or salerId=:userId)  order by createDate desc ";
		
		return findPageByHql(hql, page,params);
	}

	@Override
	public Pager getMgmOrdersBySearch(Pager page, Map<String, String> map) {
		// TODO Auto-generated method stub
		String orderNo= map.get("orderNo");
		String name=map.get("name");
		String wanging= map.get("wanging");
		
		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>();
		
		StringBuffer hsql = new StringBuffer();
//		hsql.append("SELECT a.id AS \"id\" ,a.order_no AS \"order_no\",a.amount AS \"amount\"  ");
//		hsql.append(",a.order_state AS \"order_state\",a.order_type AS \"order_type\",a.order_state_name AS \"order_state_name\"");
//		hsql.append(",a.commission AS \"commission\"  ,a.remark AS \"remark\" ");
//		hsql.append(",a.attr1 AS \"attr1\",a.attr2 AS \"attr2\",a.attr3 AS \"attr3\"");
//		hsql.append(",a.customer_id AS \"customer_id\",a.customer_name AS \"customer_name\",a.customer_phone AS \"customer_phone\"");
//		hsql.append(",a.assign_userid AS \"assign_userid\",a.assign_username AS \"assign_username\"");
//		hsql.append(",a.saler_id AS \"saler_id\",a.saler_master AS \"saler_master\",a.saler_name AS \"saler_name\"");
//		hsql.append(",a.user_id AS \"user_id\",a.user_name AS \"user_name\"");
//		hsql.append(",TO_CHAR(a.create_date,'yyyy-mm-dd hh24:mi:ss') AS \"create_date_str\",a.create_date AS \"create_date\"");
//		hsql.append(",TO_CHAR(a.modify_date,'yyyy-mm-dd hh24:mi:ss') AS \"modify_date_str\",a.modify_date AS \"modify_date\"");
//		hsql.append(",TO_CHAR(a.invite_time,'yyyy-mm-dd hh24:mi:ss') AS \"invite_time_str\",a.invite_time AS \"invite_time\"");
//		hsql.append(",TO_CHAR(a.contract_time,'yyyy-mm-dd hh24:mi:ss') AS \"contract_time_str\",a.contract_time AS \"contract_time\"");
//		hsql.append(",TO_CHAR(a.receive_time ,'yyyy-mm-dd hh24:mi:ss') AS \"receive_time_str\",a.receive_time AS \"receive_time\"");
//		hsql.append(",TO_CHAR(a.recommend_time ,'yyyy-mm-dd hh24:mi:ss') AS \"recommend_time_str\",a.recommend_time AS \"recommend_time\"");
//		hsql.append(",TO_CHAR(a.break_time,'yyyy-mm-dd hh24:mi:ss') AS \"break_time_str\",a.break_time AS \"break_time\"");
//		hsql.append(",TO_CHAR(a.check_time,'yyyy-mm-dd hh24:mi:ss') AS \"check_time_str\",a.check_time AS \"check_time\"");
//		hsql.append(",TO_CHAR(a.assign_time ,'yyyy-mm-dd hh24:mi:ss') AS \"assign_time_str\",a.assign_time AS \"assign_time\"");
//		hsql.append(",b.name AS \"state_name\",b.action AS \"action\"");
//		hsql.append("  FROM mgm_order a");
//		hsql.append("  JOIN progress_dic b ON a.order_type=b.type AND a.order_state = b.code");
//		hsql.append("  WHERE a.is_delete = 0");
//		hsql.append("");
		
		hsql.append("SELECT a.* ,t1.gc_id as \"gc_id\",t1.gc_parent_id as \"gc_parent_id\" ");
		hsql.append("," +roleCodeN+" as \"roleCodeN\"" );
		hsql.append(" FROM order_view_delivery a");
		hsql.append(",order_view_product_role t1");
		hsql.append(" WHERE a.\"id\" = t1.order_id AND a.\"is_delete\" = 0");
		
		
		if(StringUtil.isNotBlank(roleCodeN)) {
			if("2".equals(roleCodeN)) {
				if(StringUtil.isNotBlank(one_category_id)) {
					hsql.append(" and t1.gc_id = :gc_id  ");
					params.put("gc_id",one_category_id);
				}
				if(StringUtil.isNotBlank(two_category_id)) {
					hsql.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)) {
					hsql.append(" and t1.gc_id = :gc_id  ");
					params.put("gc_id",one_category_id);
				}
			}
		}
		
		//hsql.append(" WHERE a.\"is_delete\" = 0");
		
		if(StringUtil.isNotBlank(orderNo)) {
			params.put("orderNo", "%"+orderNo+"%");
			hsql.append(" AND a.\"order_no\" like :orderNo");
		}
		
		
		if(StringUtil.isNotBlank(name)) {
			params.put("name", "%"+name+"%");
			hsql.append(" AND a.\"customer_name\" like :name ");
			
		}
		
		///预警 订单 一个月没有操作的订单
		if(StringUtil.isNotBlank(wanging)) {
			if("1".equals(wanging)) {
				hsql.append(" AND trunc(SYSDATE-a.\"modify_date\") >= 30 AND to_number(a.\"order_state\") < 6");
			}
		}
		hsql.append(" order by a.\"create_date\" desc");
		return this.findPageBySql(hsql.toString(), page, params);
	}
	
	@Override
	public Pager queryOrderList(Pager page, Map<String, String> map){
		StringBuffer hql = new StringBuffer("select ID, CUSTOMER_ID , CUSTOMER_NAME , CUSTOMER_PHONE , ORDER_TYPE , CREATE_DATE , SALER_ID ,SALER_NAME, SALER_MASTER , ORDER_STATE from MGM_ORDER where IS_DELETE =:isdelete");
		Map<String, Object> params = new HashMap<>();
		params.put("isdelete", false);
		if(StringUtil.isNotBlank(map.get("userId"))){
			params.put("userId", Long.valueOf(map.get("userId")));
			hql.append(" and USER_ID=:userId");
		}
		if(StringUtil.isNotBlank(map.get("customerId"))){
			params.put("customerId", Long.valueOf(map.get("customerId")));
			hql.append(" and CUSTOMER_ID=:customerId");
		}
		hql.append(" order by CREATE_DATE desc");
		return findPageBySql(hql.toString(), page, params);
	}

	@Override
	public Pager getMgmOrderStatistics(Pager page) {
		StringBuffer hsql = new StringBuffer(); 
		hsql.append("SELECT t.* ");
		hsql.append(" FROM");
		hsql.append(" mgm_view_order_product t");
		hsql.append(" ORDER BY t.\"id\" ");
		return this.findPageBySql(hsql.toString(), page, null);
	}
}
