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

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

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

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

	@Override
	public Pager queryMgmOrderSchedulesByOrderId(Pager page, Map<String, String> map) {
		// TODO Auto-generated method stub
		StringBuffer sql=new StringBuffer();
		String orderId= map.get("orderId");
		String type=map.get("type");
		String fillflg=map.get("fillflg");//是否是销售人员填写（）
		String fillcode=map.get("fillcode");//填写步骤
		
//		////////
		String orderBreak=map.get("orderBreak");//终止申请标志（0 无申请，1申请终止单子）
		String applyState=map.get("applyState");//1 申请终止中，2,订单终止
//		///////
		
		//sql.append("select a.id,a.name,a.action,b.create_date,b.fill_content,b.fill_time,b.fill_username,b.fill_userid");
		
		if(StringUtil.isBlank(orderBreak)) {
			sql.append("SELECT a.id AS \"id\",a.name AS \"name\",a.action AS \"action\""
					+ ",b.create_date AS \"create_date\",b.fill_content AS \"fill_content\",b.fill_time AS \"fill_time\",b.fill_username AS \"fill_username\",b.fill_userid AS \"fill_userid\",b.imge_url1 as \"imge_url1\"");
			if(StringUtil.isNotBlank(fillflg)) {
				if(fillflg.equals("1")) {
					sql.append(",DECODE(a.code,'"+fillcode+"','1','0') as \"fillflg\"");
				}else {
					sql.append(",0 AS \"fillflg\"");
				}
			}else {
				sql.append(",0 AS \"fillflg\"");
			}
					
			sql.append(" FROM progress_dic a");
			sql.append(" lEFT JOIN mgm_order_schedule b ON a.code = b.order_state  AND b.order_id="+orderId);
			sql.append(" WHERE a.type = "+type);
			sql.append(" ORDER BY a.id");
		}else {
			if(orderBreak.equals("1")) {
				sql.append("SELECT c.*");
				sql.append(" FROM (");
				sql.append(" SELECT a.id AS \"id\",a.name AS \"name\",a.action AS \"action\",");
				sql.append("b.create_date AS \"create_date\",b.fill_content AS \"fill_content\",");
				sql.append("b.fill_time AS \"fill_time\",b.fill_username AS \"fill_username\",");
				sql.append("b.fill_userid AS \"fill_userid\", b.imge_url1 AS \"imge_url1\",");
				sql.append("DECODE(a.code, '-1', '1', '0') AS \"fillflg\"");
				sql.append(" FROM progress_dic a");
				sql.append(" JOIN mgm_order_schedule b ON a.code = b.order_state  AND b.order_id="+orderId);
				sql.append(" WHERE a.type = "+type);
				sql.append(" UNION ");
				sql.append(" SELECT a.id AS \"id\",a.name AS \"name\", a.action AS \"action\",");
				sql.append("b.create_date AS \"create_date\",b.APPLY_REASON AS \"fill_content\",");
				sql.append(" b.APPLY_TIME AS \"fill_time\",'' AS \"fill_username\",b.USER_ID AS \"fill_userid\",");
				sql.append("'' AS \"imge_url1\",'0' AS \"fillflg\"");
				sql.append("  FROM progress_dic a");
				sql.append(" JOIN mgm_order_break b ON b.order_id="+orderId);
				sql.append(" WHERE a.type = 3");
				
				if(StringUtil.isNotBlank(applyState)) {
					sql.append(" AND a.code="+applyState);
				}
				
				sql.append(" ) c");				
				sql.append(" ORDER BY c.\"id\" ");				
				sql.append("");
				sql.append("");
				
			}else {
				sql.append("SELECT a.id AS \"id\",a.name AS \"name\",a.action AS \"action\""
						+ ",b.create_date AS \"create_date\",b.fill_content AS \"fill_content\",b.fill_time AS \"fill_time\",b.fill_username AS \"fill_username\",b.fill_userid AS \"fill_userid\",b.imge_url1 as \"imge_url1\"");
				if(StringUtil.isNotBlank(fillflg)) {
					if(fillflg.equals("1")) {
						sql.append(",DECODE(a.code,'"+fillcode+"','1','0') as \"fillflg\"");
					}else {
						sql.append(",0 AS \"fillflg\"");
					}
				}else {
					sql.append(",0 AS \"fillflg\"");
				}
						
				sql.append(" FROM progress_dic a");
				sql.append(" lEFT JOIN mgm_order_schedule b ON a.code = b.order_state  AND b.order_id="+orderId);
				sql.append(" WHERE a.type = "+type);
				sql.append(" ORDER BY a.id");
			}			
		}
		
		
		
		
		return this.findPageBySql(sql.toString(), page, null);
	}
}
