package com.pack.service;

import com.data.util.ErrorCode;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.routes.ActionKey;
import com.taurus.core.util.StringUtil;
import com.taurus.web.Controller;

public class CommandService extends Controller {

	
	@ActionKey(value = "get_user")
	public final void getUser() throws Exception {
		ITObject params = this.getParams();
		String key = params.getUtfString("key");
		int uid = params.getInt("uid");
		ITObject resData = TObject.newInstance();
		String res_v = null; 
		switch(key) {
		case "acc":
			res_v = Redis.use("group1_db0").hget("{user}:"+uid,"acc");
			break;
		}
		res_v = StringUtil.isEmpty(res_v)?StringUtil.Empty:res_v;
		resData.putUtfString("value", res_v);
		this.sendResponse(ErrorCode._SUCC, resData);
	}

	
	@ActionKey(value = "set_user")
	public final void setUser() throws Exception {
		ITObject params = this.getParams();
		String key = params.getUtfString("key");
		int uid = params.getInt("uid");
		int value = params.getInt("value");
		String sql = null;
		switch(key) {
		case "diamo":
			sql = String.format("update account set diamo=%s where id=%s", value,uid);
			if(DataBase.use().executeUpdate(sql)==0) {
				this.sendResponse(ErrorCode._FAILED, null);
				return; 
			}
			Redis.use("group1_db0").hset("{user}:"+uid, "diamo", value+"");
			
			break;
		case "type":
			sql = String.format("update account set type=%s where id=%s", value,uid);
			if(DataBase.use().executeUpdate(sql)==0) {
				this.sendResponse(ErrorCode._FAILED, null);
				return; 
			}
			Redis.use("group1_db0").hset("{user}:"+uid, "type", value+"");
			break;
		case "mng":
			sql = String.format("update account set mng=%s where id=%s", value,uid);
			if(DataBase.use().executeUpdate(sql)==0) {
				this.sendResponse(ErrorCode._FAILED, null);
				return; 
			}
			Redis.use("group1_db0").hset("{user}:"+uid, "mng", value+"");
			break;
		}
		this.sendResponse(ErrorCode._SUCC, null);
	}
}
