package com.sino.rrs.oper;

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

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

public class RcWeixinUserOper extends BaseBean{
	public static final String TABLENAME="RC_WEIXIN_USER";

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

	@Override
	protected String getTableName() {
		return TABLENAME;
	}

	@Override
	protected Object resultAdaptor() throws Exception {
		RcWeixinUserPO entity = new RcWeixinUserPO();
		entity.setId(getInt("id"));
		entity.setOpen_id(getString("open_id"));
		entity.setNick_name(getString("nick_name"));
		entity.setSex(getString("sex"));
		entity.setProvince(getString("province"));
		entity.setCity(getString("city"));
	//	entity.setCreate_time(getDate("create_time"));
		return entity;
	}
	
	public int create(RcWeixinUserPO entity) throws CException {
		ConstructSQL sql = new ConstructInsertSQL();
		sql.setTableName(TABLENAME);
		sql.setColumn("open_id", entity.getOpen_id());
		sql.setColumn("nick_name", entity.getNick_name());
		sql.setColumn("sex", entity.getSex());
		sql.setColumn("province", entity.getProvince());
		sql.setColumn("city", entity.getCity());
		sql.setColumnTimestamp("create_time", new Date());
		return update(sql.getSQL());
	}
	
	public RcWeixinUserPO findByOpenId(String openId) throws CException{
		String sql=SELECT_ALL_FROM + TABLENAME + WHERE + "open_id" + EQ
				+ convert(openId);
		return (RcWeixinUserPO)findOne(sql);
	}
	
	public List findAll() throws CException{
		String sql=SELECT_ALL_FROM + TABLENAME;
		return  findAll(sql);
	}
	
	//根据openid查询对应的服务商编号
	public RcWeixinUserPO findRescueCode(String openId) throws CException{
		String sql=SELECT_ALL_FROM + TABLENAME + WHERE + "open_id" + EQ + convert(openId);
		return (RcWeixinUserPO)findOne(sql);
	}	
	
	//根据订单查询微信用户
		public RcWeixinUserPO findUserByCode(String orderCode) throws CException{
			String sql="select a.nick_name,a.open_id from " + TABLENAME + " a left join weixin_order b on a.open_id=b.OPENID" + WHERE + "ORDERCODE='" + orderCode +"'";
			return (RcWeixinUserPO)findOne(sql,"resultFindUserByCode");
		}
		
		public RcWeixinUserPO resultFindUserByCode() throws Exception{
			RcWeixinUserPO po=new RcWeixinUserPO();
			po.setNick_name(getString("nick_name"));
			po.setOpen_id(getString("open_id"));
			return po;
		}
	//根据openid查询是服务商或司机是否注册过
		public Map<String,Object> findUserByOpenID(String openId) throws CException{
			String sql="select r.WEIXIN_USER_ID company_weixin_user_id,r.RESCUE_COMPANY_CODE company_rescue_code,r.DISPATCH_MODE company_dispatch_mode," +
					"v.ID ID,v.WEIXIN_USER_ID vehicle_weixin_user_id,v.RESCUE_COMPANY_CODE vehicle_rescue_code,v.DISPATCH_MODE vehicle_dispatch_mode,m.RESCUE_COMPANY_CODE manager_rescue_code,m.LOGIN_NAME " +
					"from RC_WEIXIN_USER w LEFT JOIN RC_RESCUE_COMPANY r on w.ID = r.WEIXIN_USER_ID LEFT JOIN RC_RESCUE_VEHICLE v "+ 
                     "on w.ID = v.WEIXIN_USER_ID LEFT JOIN RC_RESCUE_MANAGER m on w.ID=m.WEIXIN_USER_ID  WHERE w.OPEN_ID='"+openId+"'";
			return (Map<String, Object>) findOne(sql,"resultFindUserByOpenID");
		}
		public Map<String,Object> resultFindUserByOpenID() throws Exception{
			Map<String,Object> po=new HashMap<String, Object>();
			po.put("company_weixin_user_id",getInt("company_weixin_user_id"));
			po.put("vehicle_weixin_user_id",getInt("vehicle_weixin_user_id"));
			po.put("company_rescue_code", getString("company_rescue_code"));
			po.put("vehicle_rescue_code", getString("vehicle_rescue_code"));
			po.put("company_dispatch_mode", getInt("company_dispatch_mode"));
			po.put("vehicle_dispatch_mode", getInt("vehicle_dispatch_mode"));
			po.put("manager_rescue_code", getString("manager_rescue_code"));
			po.put("loginName", getString("LOGIN_NAME"));
			po.put("ID", getInt("ID"));
			return po;
		}
		
	//当服务商选择是否接受通知后修改相应状态
			public int updateCompanyAcceptFlag(int flag,String openId) throws Exception{
				String sql="update RC_WEIXIN_USER a set a.accept_flag="+flag+" where a.open_id='"+openId+"'";
				return update(sql);
			}	
}
