package com.sino.rrs.oper;

import java.sql.Connection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.sino.publicclass.base.BaseBean;
import com.sino.publicclass.base.CException;
import com.sino.publicclass.sql.ConstructInsertSQL;
import com.sino.publicclass.sql.ConstructSQL;

public class RcTempleteOrder extends BaseBean{
	public static final String TABLENAME="RC_TEMPLETE_ORDER";

	public RcTempleteOrder(Connection curConn) {
		super(curConn);
	}

	@Override
	protected String getTableName() {
		return TABLENAME;
	}
	
	public static String getUUID() {
		 Random random = new Random();
	     int num = random.nextInt(900);
	     num = num + 100;
	     return String.valueOf(System.currentTimeMillis()+num);
	 }
	
	public static void main(String[] args) {
		System.out.println(getUUID());
	}

	@Override
	protected Map<String,String> resultAdaptor() throws Exception {
		Map<String,String> map=new HashMap<String,String>();
		map.put("id", getString("ID"));
		map.put("orderId", getString("ORDER_ID"));
		map.put("rescueCompanyCode", getString("RESCUE_COMPANY_CODE"));
		return map;
	}
	
	/**
	 * 对应模板消息和订单
	 * @param orderId
	 * @param rescueCode
	 * @return
	 * @throws CException
	 */
	public String insertTempleteMessage(int orderId,String rescueCode) throws CException{
		ConstructSQL sql=new ConstructInsertSQL();
		String id = getUUID();
		sql.setTableName(TABLENAME);
		sql.setColumn("ID", id);
		sql.setColumn("ORDER_ID", orderId);
		sql.setColumn("RESCUE_COMPANY_CODE", rescueCode);
		sql.setColumnTimestamp("CREATE_TIME", new Date());
		update(sql.getSQL());
		return id;
	}
	
	public Map<String,String> queryTemplete(int orderId,String rescueCode) throws CException{
		String sql="select a.ID,a.ORDER_ID,a.RESCUE_COMPANY_CODE from RC_TEMPLETE_ORDER a inner join (select max(create_time) 'maxgdtime' from " +
				"RC_TEMPLETE_ORDER order by create_time DESC) b on  a.create_time=b.maxgdtime where a.ORDER_ID="+orderId+" and a.RESCUE_COMPANY_CODE='"+rescueCode+"'";
		//String sql="";
		return  (Map<String, String>) findOne(sql);
	}

}
