StepYuedongQ


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;

import com.skyi.servlet.util.MysqlDbManager;
import com.skyi.servlet.util.YueDongmm;
import com.skyi.weixin.message.InputMessage;
import com.skyi.weixin.message.entity.Yue;
import com.skyi.weixin.message.entity.Yued;

/**
 * 悦动圈刷步数
 * @author 
 */
public class StepYuedongQ {

	private static final String TABLE_NAME = "yued"; // 表名
	
	// 推荐注册Url
	public static final String TuiJianUirl	= "http://circle.51yund.com/invite/inviteQQUser?invite_member_id=42390219&type=2&from=singlemessage&isappinstalled=1";
	public static final String LogninTs		= "登陆格式为:zh#账号mm#密码\n\n例如:zh#138888mm#1234";
	
	public static String CaiDan =
			  "\n\n请选择悦动刷步菜单:"
			+ "\n\n[001] 用户注册"
			+ "\n[002] 下载App"
			+ "\n[003] 查看个人信息"
			+ "\n[1~99999]提交步数"
			+ "\n\n①注册用户 ②下载App ③回复步数"
			+ "\n\n______系统说明______"
			+ "\n\n刷步数->微信运动V1"
			+ "\n\n刷步数->微信运动V2"
			+ "\n\n消息刷步->[强烈推荐]"
			;

	public static String zhucTs = 
			"用户注册" 
			+ "\n\n请回复[zc#您手机号]" 
			+ "\n\n回复:zc#13800000000"
			;
	
	public static final String StrShubushu = "\n\n现在可以刷步数了~"
			+ "\n\n刷步数[请回复 #您步数]"
			+ "\n\n回复:#1000";

	public static final String guanzhu =  "感谢您关注!"// 【消息刷步系统】 免费开放中~
			+  CaiDan;
	
	public static void main(String[] args) {
		try {
			// 数据库测试
			//MysqlDbManager.MysqlUpDate(TABLE_NAME,new String[]{"user_is","step"}, new String[]{"99","5000"},"step = '88888'");

			// 到期测试
			//System.out.println(onIsExpired(YueDongmm.onGetDate(1)));
			//System.out.println(onIsExpired(YueDongmm.onGetDate(0)));
			//System.out.println(onIsExpired("20151212"));
			//System.out.println(onReplaceAll("1234567890", "1","2"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 检查时间是否可用
	 * @param data 到期时间 例如:20151220
	 * @return 可用返回真,到期返回假
	 */
	public static boolean onIsExpired(String data) {
		try {
			
			// 获取当前时间截止
			String strdata1 = YueDongmm.onGetDateMillis(YueDongmm.onGetDate());
			// 到期时间截止
			String strdata2 = YueDongmm.onGetDateMillis(data);
			//当前时间  < 到期时间   说明没有到期 100<200
			//当前时间  > 到期时间   说明没有到期 100>200
			return Long.valueOf(strdata1) <= Long.valueOf(strdata2);

		} catch (Exception e) {
			System.out.println("*异常:[检查时间是否可用 ] data = '" + data + "'");
			//e.printStackTrace();
			return false;
		}

	}

	/**
	 * 自动替换为空
	 * @param context
	 * @param strings
	 * @return
	 */
	public static String onReplaceAll(String context, String... strings){
		for(int i=0; i点击下载App"
				+ "\n\n2.选择手机号登陆"
				+ "\n\n3.选择同步步数到到微信/QQ即可!"
				+ "\n\n下载App后,请使用注册的手机号登陆"
				+ "\n\n*注:(请勿用QQ/微信登陆)"
				;
		
		return context;
	}


	/**
	 * 获取绑定设备连接
	 * @param StrUserName 微信用户名
	 * @return
	 * @throws Exception
	 */
	public static String onGetweixinUrl(String StrUserName) throws Exception{
		String StrtRurnMsg= "";
		// 查询个人信息
		Yued rset = onGetInfo(StrUserName);

		String user_id = rset.getUser_id();
		String xyy = rset.getXyy();

		if(user_id.equals("") || xyy.equals("")){
			StrtRurnMsg = "您还未注册,\n"
					+ zhucTs;

			return StrtRurnMsg;
		}

		String strUrl = YueDongmm.fuwu(rset.getUser_id(), rset.getXyy());
		strUrl = "http://qr.liantu.com/api.php?bg=f3f3f3&fg=ff0000&gc=222222&el=l&w=200&m=10&text="+ strUrl;
		StrtRurnMsg = "绑定设备"
				+ "\n点击进入绑定"
				+ "\n\n打开后长按二维码->识别图中二维码"
				+ "\n\n或保存后再使用微信扫一扫"
				;
		return StrtRurnMsg;

	}


	/**
	 * 步数提交
	 * @param StrUserName 用户
	 * @param StrUserMsg 步数
	 * @return
	 * @throws Exception
	 */
	public static String onSetStep(String StrUserName, String StrUserMsg) throws Exception{
		StrUserMsg = StrUserMsg.replace("#", ""); // 消息内容
		String coctext = "";

		if(!YueDongmm.isStepsNO(StrUserMsg)){
			coctext = "输入步数有误,请输入1~99999";
			return coctext;
		}
		int StepAdd = Integer.valueOf(StrUserMsg);

		if(StepAdd>10000){
			coctext = "输入步数有误,注意:10分/1次! 一次最高1万步!";
			return coctext;
		}
		
		Yued rset = onGetInfo(StrUserName); // 查询个人信息

		String id  =  rset.getId();
		String xyy = rset.getXyy();
		String u_time= rset.getTime();
		String user_id = rset.getUser_id();

		if(user_id.equals("") || xyy.equals("")){
			coctext = "您还未注册,\n"
					+ zhucTs;
			return coctext;
		}
		
		// 判断是否超过十分钟
		if(!u_time.equals("")){
			long timem = Long.valueOf(u_time);
			long temptime = timem - System.currentTimeMillis();
			
			if(temptime > 0){
				coctext = "刚刚您提交了步数,请稍等,"+(temptime/1000)+"秒再试!";
				return coctext;
			}
			
		}
		// 获取今天时间1450713600
		String StrDateMillis = YueDongmm.onGetDateMillis(YueDongmm.onGetDate());

		// 查询用户历史步数
		String userInfo = YueDongmm.get_user_step(user_id, xyy);

		String time = "";
		String step = "0";
		int thisSpep = 0;
		if(new JSONObject(userInfo).getJSONArray("infos").length() > 0)
		{
			JSONObject StrTempjsonObject = new JSONObject(userInfo).getJSONArray("infos").getJSONObject(0);
			time = StrTempjsonObject.get("time").toString(); // 上次提交时间
			step = StrTempjsonObject.get("step").toString(); // 上次提交步数
			// 上次提交时间 是否是今天的 
			if(StrDateMillis.equals(time)){
				thisSpep = Integer.valueOf(step);
			}
		}


		// 旧步数 + 刷步数 = 总步数
		String TempSpet = "" + (thisSpep + StepAdd);

//		coctext = YueDongmm.onStepUpload(user_id, TempSpet);
//		coctext = YueDongmm.onSetStep2(user_id, TempSpet);
		coctext = YueDongmm.onSetStep2(user_id, StepAdd + "","");
		
		//{"msg": "ok", "flag": 0, "code": 0, "user_id": 76223958, "runner_id": 1260772425}// 提交成功
		//{"msg": "ok", "cnt": 1, "code": 0, "flag": 0} // 提交繁忙    ↓获取用户信息
		//{"msg": "ok", "cnt": 3, "code": 0, "infos": [{"distance": 4, "step": 9, "caloric": 0, "day": "2015-12-22", "time": 1450713600}]}
		//2016.03.21{"code":0,"msg":"ok","flag":1,"cnt":0,"steps_array_resp":[{"index":0,"step":88888,"run_ts":1458569003,"cost_time":51,"code":0,"err":0,"runner_id":763398784}],"next_cnt":10}
		
		// 查询最新步数
		userInfo = YueDongmm.get_user_step(user_id, xyy); // 查询用户历史步数
		JSONObject StrTempjsonObject = new JSONObject(userInfo).getJSONArray("infos").getJSONObject(0);
		time = StrTempjsonObject.get("time").toString(); // 提交时间
		step = StrTempjsonObject.get("step").toString(); // 现在步数

		// 显示最终步数
		JSONObject jsonObject = new JSONObject(coctext);
		
		if(!jsonObject.isNull("code")){
			String code = jsonObject.get("code").toString();
			
			if(code.equals("0")){// ok
				coctext = StepAdd + "步增加成功!";
				long TimeMillis = System.currentTimeMillis()+(1000*60*10);//now() 添加十几分钟
				String Sql = String.format("update yued set time = '%s' where id = '%s' ",TimeMillis, id);
				// 保存数据库
				MysqlDbManager.Mysqlexecute(Sql);
				
			}else if(code.equals("1")) { // param:bad args is wrong?
				
				coctext = StepAdd + jsonObject.get("msg").toString();;
				coctext =  coctext +"提交太频繁,请稍后(5~10分钟)再试!";
			}else{
				coctext = StepAdd + jsonObject.get("msg").toString();;
				coctext = coctext +"未知错误!";
			}
			coctext = coctext + "\n提交前步数:"+thisSpep;
			coctext = coctext + "\n提交后步数:"+step;
			coctext = coctext + "\n\n剩余步数:"+"无限";//rset.getStep();
			coctext = coctext + "\n\n注意:10分/1次! 一次最高1万步!";

		}else{
			coctext = StepAdd + jsonObject.get("msg").toString();;
			coctext = "提交太频繁,请稍后(5~10分钟)再试!";
			coctext = coctext + "\n\n当前步数:"+step;
			coctext = coctext + "\n\n剩余步数:"+rset.getStep();
		}

		coctext = coctext + "\n\n如果2分钟内没有显示? 请进入APP 选择同步到微信或QQ";
				//+ "\n\n"
				//+ DownloadApp();

		return coctext;

	}

	/**
	 * 查询个人信息
	 * @param StrUserName 用户微信名
	 * @return
	 * @throws Exception 
	 */
	public static String onChaxun(String StrUserName) throws Exception{

		// 查询个人信息
		Yued rset = onGetInfo(StrUserName);
		String Content = "";

		Content = Content +"系统编号: "  +rset.getId();
		Content = Content +"\n用户状态: "+(onIsExpired(rset.getTime_is())?"正常":"无限");
		Content = Content +"\n用户类型: "+(rset.getUser_is().equals("1")?"VIP":"免费内测");
		Content = Content +"\n到期时间: "+"无限";//rset.getTime_is();
		//Content = Content +"\n剩余步数: "+rset.getStep();

		if(rset.getUser_id().equals("")){
			Content = "您还未注册,\n"
					+ zhucTs;
		}else{

			// 获取今天时间1450713600
			String StrDateMillis = YueDongmm.onGetDateMillis(YueDongmm.onGetDate());
			// 查询用户历史步数
			String userInfo = YueDongmm.get_user_step(rset.getUser_id(), rset.getXyy());

			JSONArray jsonArray = new JSONObject(userInfo).getJSONArray("infos");

			String time = "0"; // 上次提交时间
			String step = "0"; // 上次提交步数

			if(jsonArray.length() > 0) // 里面是否有数据
			{
				JSONObject StrTempjsonObject = new JSONObject(userInfo).getJSONArray("infos").getJSONObject(0);
				time = StrTempjsonObject.get("time").toString();
				step = StrTempjsonObject.get("step").toString();
			}

			// 上次提交时间 是否是今天的 
			int thisSpep = StrDateMillis.equals(time)?Integer.valueOf(step):0; // 现在步数

			Content = Content +"\n\n当前步数: "+thisSpep;
			Content = Content +"\n悦动圈ID: "+rset.getUser_id();
			Content = Content +"\n\n用户手机: "+rset.getPhone();
			Content = Content +"\n登陆密码: "+rset.getPwd();

		}
		return Content ;
	}

	/**
	 * 获取数据库 用户信息
	 * @param UserNmae
	 * @return
	 * @throws SQLException
	 */
	public static Yued onGetInfo(String UserNmae) throws SQLException{
		// 获取数据库连接
		Connection conn = MysqlDbManager.getConnection();
		Statement stmt = conn.createStatement();

		// Sql
		String sql = String.format("SELECT * from %s where wx_id ='%s'", TABLE_NAME, UserNmae);

		// 执行Sql语句 
		ResultSet rset = stmt.executeQuery(sql);
		rset.next();

		if(rset.getRow() == 0)
		{
			// 添加用户
			stmt.executeUpdate("INSERT INTO yued (wx_id, step ,time_is) values('" +UserNmae+ "','50000','"+YueDongmm.onGetDate()+"')");

			// 重新执行Sql语句 
			rset = stmt.executeQuery(sql);
			rset.next();
		}

		// 保存集合
		Yued ResultSetYued = new Yued();
		ResultSetYued.setId(rset.getString(Yue.strid));
		ResultSetYued.setPhone(rset.getString(Yue.strphone));
		ResultSetYued.setPwd(rset.getString(Yue.strpwd));
		ResultSetYued.setStep(rset.getString(Yue.strstep));
		ResultSetYued.setTime(rset.getString(Yue.strtime));
		ResultSetYued.setTime_is(rset.getString(Yue.strtime_is));
		ResultSetYued.setUser_id(rset.getString(Yue.struser_id));
		ResultSetYued.setWx_id(rset.getString(Yue.strwx_id));
		ResultSetYued.setXyy(rset.getString(Yue.strxyy));
		ResultSetYued.setUser_is(rset.getString(Yue.struser_is));

		// 关闭连接
		MysqlDbManager.closeConnection(rset, stmt, conn);
		return ResultSetYued; //返回集合
	}


}

 

你可能感兴趣的:(java开发)