import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.ruchuapp.tools.DbUtil;
import com.ruchuapp.tools.StringUtil;
import com.youzan.open.sdk.client.auth.Token;
import com.youzan.open.sdk.client.core.DefaultYZClient;
import com.youzan.open.sdk.client.core.YZClient;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanTradesSoldGet;
import com.youzan.open.sdk.gen.v3_0_0.api.YouzanUserWeixinOpenidGet;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanTradesSoldGetParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanTradesSoldGetResult;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanUserWeixinOpenidGetParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanUserWeixinOpenidGetResult;
/**
* 开启一个线程,用于在固定的时间段从有赞数据库获取订单记录
*/
@WebServlet("/YouZanInitServlet")
public class YouZanInitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
super.init();
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
Timer time=new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
//在这里做获取订单消息
JSONObject tradesInfo = getTradesInfo();
getMustMess(tradesInfo);
}
}, cal.getTimeInMillis(),24*60*60*1000);
}
/**
* 获取有赞商品订单
* 发起网络请求获取返回的订单信息
* @return
*/
private static JSONObject getTradesInfo(){
JSONObject json=null;
//"8dd8ca0de6e631e396be624e659e7b5a"
String accessToken = getAccessToken.getAccessToken();
YZClient client = new DefaultYZClient(new Token(accessToken)); //new Sign(appKey, appSecret)
// YZClient client = new DefaultYZClient(new Token("18378bf25aa7305f8436ff9c37f15965")); //new Sign(appKey, appSecret)
YouzanTradesSoldGetParams youzanTradesSoldGetParams = new YouzanTradesSoldGetParams();
// youzanTradesSoldGetParams.setPageNo(1L);
youzanTradesSoldGetParams.setType("ALL");
youzanTradesSoldGetParams.setStatus("TRADE_BUYER_SIGNED");//只查找订单完成签收的订单列表
YouzanTradesSoldGet youzanTradesSoldGet = new YouzanTradesSoldGet();
youzanTradesSoldGet.setAPIParams(youzanTradesSoldGetParams);
YouzanTradesSoldGetResult result = client.invoke(youzanTradesSoldGet);
try {
json=new JSONObject(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
/**
* 通过手机号获取微信用户的openid
* @param tel
* @return
*/
private static String getUserOpenid(String tel){
String str="";
YZClient client = new DefaultYZClient(new Token("token")); //new Sign(appKey, appSecret)
YouzanUserWeixinOpenidGetParams youzanUserWeixinOpenidGetParams = new YouzanUserWeixinOpenidGetParams();
youzanUserWeixinOpenidGetParams.setMobile(tel);
youzanUserWeixinOpenidGetParams.setCountryCode("+86");
YouzanUserWeixinOpenidGet youzanUserWeixinOpenidGet = new YouzanUserWeixinOpenidGet();
youzanUserWeixinOpenidGet.setAPIParams(youzanUserWeixinOpenidGetParams);
YouzanUserWeixinOpenidGetResult result = client.invoke(youzanUserWeixinOpenidGet);
JSONObject json=new JSONObject(result);
// json.getString("");
boolean has = json.has("response");
if(!has){//有的情况下
str="";
}else{
try {
JSONObject jo= (JSONObject) json.get("response");
String opneid = jo.getString("open_id");
str=opneid;
} catch (JSONException e) {
e.printStackTrace();
}
}
return str;
}
/**
* 获取订单信息并进行比较,插入操作
* @param jo
*/
private void getMustMess(JSONObject jo){
try {
JSONArray ja=new JSONArray(jo.get("trades").toString());
if(ja.length()==0){
return;
}
for(int i=0;i<ja.length();i++){
JSONObject json = (JSONObject) ja.get(i);
String tid = json.getString("tid");//订单唯一标识
String title = json.getString("title");//订单标题
String outerUserId = json.getString("outerUserId");//订单用户id
String payment = json.getString("payment");//订单实际收费金额
// String payType = json.getString("payType");//订单支付类型,如支付宝,微信支付
//因为在微信情况下,不能获取到微信用户的账户系统,所以现在获取手机号作为判断的一个条件
String receiverState = json.getString("receiverState");//订单中收货人的手机号
String sign_time = json.getString("sign_time");//订单签收的日期,钱到账的日期,获取的时间格式需要转一下
Date date = getDate(sign_time);
//需要判断这个订单表是否在表中已经有了,如果有了就不要插入
if(outerUserId.equals("0")){//说明这个订单是从微信服务号来的
//根据手机号获取用户opneid
String userOpenid = getUserOpenid(receiverState);
if(StringUtil.isEmpty(userOpenid)){//为null表示这个订单不是从当前关注的公众号发出的,可能是分享给别人购买的订单
continue; //如果不是关注当前公众号的人购买的暂不做处理
}else{//确实是从微信公众号出来的订单
//通过openid换取u_id
String userUid = getUserUid(userOpenid);
boolean compareIsHandOrders = compareIsHandOrders(tid);
if(compareIsHandOrders){//确实有这个订单id
continue;
}else{//没有这个订单id
insertOrderInfo(userUid,"微信商城",tid,payment,title,date);
}
}
}else{//订单从app里面来的有账号系统
boolean compareIsHandOrders = compareIsHandOrders(tid);
if(compareIsHandOrders){//确实有这个订单id
continue;
}else{//没有这个订单id
String uid = getUserTid(outerUserId);
insertOrderInfo(uid,"app商城",tid,payment,title,date);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
//转换日期
private Date getDate(String time){
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", java.util.Locale.US);
Date date = null;
try {
date = sdf.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return new java.sql.Date(date.getTime());
}
/**
*判断在订单表中是否含有这条订单信息,如果有就不进行插入操作,没有在插入
*/
private static boolean compareIsHandOrders(String tid){
boolean flag=false;
String sql="select order_id from usercost where order_id=?";
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn=DbUtil.getConnection();
ps=conn.prepareStatement(sql);
ps.setString(1, tid);
rs=ps.executeQuery();
if(rs.next()){
flag=true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.closeResultSet(rs);
DbUtil.closeStatement(ps);
DbUtil.closeConnection(conn);
}
return flag;
}
/**
* 插入一条订单记录
* @return
*/
private static void insertOrderInfo(String usertid,String order_from,String oderidid,String mount,String costname,Date time){
String uid = getUserTid(usertid);
String sql="insert into usercost(user_id,order_id,order_from,amount,cost_type,cost_name,uc_createtime)values(?,?,?,?,?,?,?)";
Connection conn=null;
PreparedStatement ps=null;
try {
conn=DbUtil.getConnection();
ps=conn.prepareStatement(sql);
ps.setString(1, uid);
ps.setString(2, oderidid);
ps.setString(3, order_from);
ps.setString(4, mount);
ps.setString(5, "2");
ps.setString(6, costname);
ps.setDate(7, (java.sql.Date) time);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.closeStatement(ps);
DbUtil.closeConnection(conn);
}
}
/**
* 获取user表中的u_id
* @param tid
* @return
*/
private static String getUserTid(String tid){
String sql="select u_id from user where user_id=?";
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String usertid="";
try {
conn=DbUtil.getConnection();
ps=conn.prepareStatement(sql);
ps.setString(1, tid);
rs=ps.executeQuery();
if(rs.next()){
usertid=rs.getString("u_id");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.closeResultSet(rs);
DbUtil.closeStatement(ps);
DbUtil.closeConnection(conn);
}
return usertid;
}
/**
* 通过opneid获取到用户的u_id
* @param opneid
* @return
*/
private static String getUserUid(String opneid){
String sql="select u_id from user where u_openid=?";
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String uid="";
try {
conn=DbUtil.getConnection();
ps=conn.prepareStatement(sql);
ps.setString(1, opneid);
rs=ps.executeQuery();
if(rs.next()){
uid=rs.getString("u_id");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DbUtil.closeResultSet(rs);
DbUtil.closeStatement(ps);
DbUtil.closeConnection(conn);
}
return uid;
}
}
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287.