该队列用于处理考试免报名的订单处理

package com.luyouchina.train.core.common.util;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;


import org.bson.types.ObjectId;


import com.luyouchina.comm.DateUtil;
import com.luyouchina.comm.MongoUtil;
import com.luyouchina.comm.RandUtil;
import com.luyouchina.comm.ShardedJedisCommands;
import com.luyouchina.comm.model.RequestObject;
import com.luyouchina.comm.model.ResponseObject;
import com.luyouchina.logClient.Pusher;
import com.luyouchina.logClient.entity.SysLog;
import com.luyouchina.train.core.common.repository.TraExmOrderinfomRepository;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;


/**
 * 该队列用于处理考试免报名的订单处理
 * @date 2017年1月10日
 */
public class OrderQueue {
//存放当月的队列
private static Map> map = new HashMap>();

//private static Queue queue = new ConcurrentLinkedQueue();
//业务加锁
private static Queue queue = new LinkedList();

private String collection = "sys_serialid";

private TraExmOrderinfomRepository traExmOrderinfomRepository;
private ShardedJedisCommands shardedJedisConn;// 缓存连接


public OrderQueue(){
traExmOrderinfomRepository = SingleBeanFactory.getBean(TraExmOrderinfomRepository.class);
shardedJedisConn = new ShardedJedisCommands();// 缓存连接
}
public OrderQueue(String test){
// traExmOrderinfomRepository = SingleBeanFactory.getBean(TraExmOrderinfomRepository.class);
// shardedJedisConn = new ShardedJedisCommands();// 缓存连接
}
/**
* @version : 1.00
* @create time : 2016年10月31日下午2:27:48
* @param param
* @return ResponseObject
* @description : 批量生成订单(免报名)
*/
@SuppressWarnings("unchecked")
public void saveOrder(Map param) {
long s = new Date().getTime();
// 考试id
final String exmid = (String) param.get("exmid");
System.out.println(exmid+"开始批量生成订单。。。。。。");
// 考试对象
final DBObject exam = (DBObject) param.get("exam");
// 指定人员
final List> examlist = (List>) exam.get("exm_vuserlist");
// 每个线程最大个数
// int threads = Constants.MS_BASIC_DB_CONNECTION;
// 能否创建订单
if (examlist != null && examlist.size() > 0) {
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("开始线程循环生成订单");
// 分批创建线程 (不超过最大单线程个数)
// if (examlist.size() <= threads) {
String yyyymm = DateUtil.formatDate(new Date(), "yyyyMM");// 年月
for (int i = 0; i < examlist.size(); i++) {
long start = new Date().getTime();
Map params = new HashMap();
// 人员accon
String accno = (String) examlist.get(i).get("refno");
params.put("accno", accno);
params.put("exmid", exmid);
params.put("exam", exam);
ThreadSaveOrder(params,yyyymm);
long end = new Date().getTime();
System.out.println("线程生成订单:" + (i + 1)+",时长 : "+(end - start));
}
// 更新考试报名数量(这里修改为统一最后更新考试人员)
BasicDBObject queryObj = new BasicDBObject(); // 构建查询条件
queryObj.clear();
queryObj.put("_id", new ObjectId(exmid));
BasicDBObject objectnum = new BasicDBObject();
BasicDBObject objectupdate = new BasicDBObject();
objectupdate.put("exmaccnum",examlist.size());
objectnum.put("$inc", objectupdate);
traExmOrderinfomRepository.updateExm(queryObj, objectnum);
//更新订单生成表(原始业务不详)
updateSerialid(yyyymm,examlist.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}


System.out.println(exmid+"批量生成订单结束:" + (new Date().getTime() - s));
}
/**
* @version : 1.00
* @create time : 2016年10月31日下午3:23:52
* @param param
*            void
* @description : 线程批量生成订单
*/
private void ThreadSaveOrder(Map param,String yyyymm) {
BasicDBObject queryObj = new BasicDBObject(); // 构建查询条件
String exmid = (String) param.get("exmid");
String accno = (String) param.get("accno");
DBObject exam = (DBObject) param.get("exam");


Map map = new HashMap();
map.put("accno", accno);
map.put("orderdate", DateUtil.getDateTime());
map.put("createtime", DateUtil.getDateTime());
map.put("lupdatetime", DateUtil.getDateTime());
map.put("isdelete", 0);
// 订单总金额 sumamt ,实付金额 realamt ?
List> list = new ArrayList>();
long realamt = 0;
Map mapparam = new HashMap();
// 处理单价、金额等信息
mapparam.put("amount", realamt);
mapparam.put("realprice", realamt);
mapparam.put("oldprice", realamt);
// 订单明细号--------------------------------------------
mapparam.put("exmid", exmid);
mapparam.put("exmorderdid", RandUtil.uuid());
mapparam.put("exmname", exam.get("exmname"));
list.add(mapparam);
map.put("sumamt", realamt);
map.put("realamt", realamt);
map.put("isinvoice", 9);
// 订单编号 orderno
long start = new Date().getTime();
ResponseObject rp = this.getNo(yyyymm);
long end = new Date().getTime();
System.out.println("获取订单编号时长:" + (end-start));
map.put("orderno", String.valueOf(rp.getData()));// 取编号信息
// ORD045 未付款 ORD046 已付款 ORD048 已取消
map.put("orderstatus", "ORD046");// 已付款
map.put("payid", "");// 支付id
map.put("paytype", "");
// 添加子文档 -------------------------------------------------
map.put("exm_orderinfod", list);// 订单子表
// 添加子文档配送方式------------------------------------------
Map map_psfs = new HashMap<>();
map_psfs.put("deliverytype", "");
map_psfs.put("expresssup", "");
map_psfs.put("expressno", "");
// -------------------------------------------添加订单跟踪信息
List> listgz = new ArrayList>();
Map mapgz1 = new HashMap<>();
mapgz1.put("trackdate", DateUtil.getDateTime());
mapgz1.put("trackbody", "新增订单 -- 考试免费 -- ");
mapgz1.put("operuse", accno);
mapgz1.put("orderstatus", "ORD046");// 已付款
listgz.add(mapgz1);
map.put("exm_ordertracking", listgz);
DBObject exmorderinfom = new BasicDBObject();
exmorderinfom.putAll(map);
// 插入订单
start = new Date().getTime();
String _id = traExmOrderinfomRepository.insert(exmorderinfom);
end = new Date().getTime();
System.out.println("插入订单时长:" + (end-start));
if ("0".equals(_id)) {
Pusher.sendBasicLog(null, "下订单主表失败", accno, "0.0.0.0", SysLog.logLevel.infolog, "0.0.0.0", false, "");
return;
} else {
// 报名信息
List memexamination = new ArrayList();
// 拼装我的报名信息
DBObject object1 = new BasicDBObject();
object1.put("exmid", exmid);
object1.put("accno", accno);// 会员标识号
object1.put("regdate", exmorderinfom.get("lupdatetime"));// 报名时间
object1.put("exmdate", exam.get("exmdates"));// 考试时间
object1.put("sortby", 0);
object1.put("exmorderid", _id);
object1.put("exmamt", 0l);// 处理每人的金额
memexamination.add(object1);// 转换加入
// 插入我的考试报名
start = new Date().getTime();
traExmOrderinfomRepository.insertmemexamination(memexamination);
end = new Date().getTime();
System.out.println("插入我的报名时长:" + (end-start));
}
}
public ResponseObject getNo(String yyyymm) {
ResponseObject response = new ResponseObject();
//String orderno=this.findAndModify(yyyymm);
String orderno=this.getOrderNo(yyyymm);
response.setData(orderno);
return  response;
}
/**
* 设置订单编号(缓存方式)
* @param yyyymm 构成12位递增的序列号
* @return
*/
public  void  setOrderNoCache(String yyyymm) {
//201510 000862
DBCollection col = MongoUtil.getCollection(collection);

DBObject query = new BasicDBObject("yyyymm",yyyymm);
DBObject serialid = col.findOne(query);
long _serialid = 0;
if (serialid != null) {
_serialid = new Long(serialid.get("serialid").toString());
}
//6位递增的订单号
DecimalFormat df = new DecimalFormat("000000");
for(int i = 1 ;i <= 2000; i++){
String temp = df.format(_serialid+i);
shardedJedisConn.lpush(yyyymm, yyyymm + temp);
}
//设置过期为31天,这里就不计算到月末最后一天了
shardedJedisConn.expire(yyyymm, 60*60*24*31);
}

/**
* 获取订单编号(缓存方式)
* @param yyyymm
* @return
*/
public  String  getOrderNoCache(String  yyyymm) {
//先整个加锁
synchronized (queue) {
//String orderNo = shardedJedisConn.rpop(yyyymm);

String orderNo = queue.poll();
if(orderNo != null && !"".equals(orderNo)){
return orderNo;
}else{
this.setOrderNo(yyyymm);
return getOrderNo(yyyymm);
}
}
}
/**
* 设置订单编号
* @param yyyymm 构成12位递增的序列号
* @return
*/
private  void  setOrderNo(String  yyyymm) {
map.clear();
DBCollection col = MongoUtil.getCollection(collection);
DBObject query = new BasicDBObject("yyyymm",yyyymm);
DBObject serialid = col.findOne(query);
long _serialid = 0;
if (serialid != null) {
_serialid = new Long(serialid.get("serialid").toString());
}
//6位递增的订单号
DecimalFormat df = new DecimalFormat("000000");
for(int i = 1 ;i <= 2000; i++){
String temp = df.format(_serialid+i);
queue.add(yyyymm+temp);
}
map.put(yyyymm, queue);


}


/**
* 获取订单编号
* @param yyyymm
* @return
*/
public  String  getOrderNo(String  yyyymm) {
//先整个加锁
synchronized (queue) {
Queue tempQueue = map.get(yyyymm);
if(tempQueue != null){
String orderNo = tempQueue.poll();
if(orderNo != null && !"".equals(orderNo)){
return orderNo;
}
}
this.setOrderNo(yyyymm);
return getOrderNo(yyyymm);
}
}
/**
* 将订单编号更新到原来的业务表中(不清楚原始系统)
* @param yyyymm
* @return
*/
public  void  updateSerialid(String  yyyymm,int num) {
DBCollection col = MongoUtil.getCollection(collection);
DBObject query = new BasicDBObject("yyyymm",yyyymm);
DBObject fields = new BasicDBObject("serialid",1);
DBObject sort = new BasicDBObject();
DBObject update = new BasicDBObject();
        update.put("$inc", new BasicDBObject("serialid",num));
        col.findAndModify(query, fields, sort, false, update, true, true);
}
private  void  setOrderNoTest(String  yyyymm) {
map.clear();
long _serialid = 0;
//6位递增的订单号
DecimalFormat df = new DecimalFormat("000000");
for(int i = 1 ;i <= 2000; i++){
String temp = df.format(_serialid+i);
queue.add(yyyymm+temp);
}
map.put(yyyymm, queue);


}
public  String  getOrderNoTest(String  yyyymm) {
//先整个加锁
synchronized (queue) {
Queue tempQueue = map.get(yyyymm);
if(tempQueue != null){
String orderNo = tempQueue.poll();
if(orderNo != null && !"".equals(orderNo)){
return orderNo;
}
}
this.setOrderNoTest(yyyymm);
return getOrderNoTest(yyyymm);
}
}
public static void main(String[] args) {
String yyyymm = DateUtil.formatDate(new Date(), "yyyyMM");// 年月
OrderQueue oq = new OrderQueue("test");
// DecimalFormat df = new DecimalFormat("000000");
// long _serialid = 0;
// for(int i = 1 ;i <= 2000; i++){
// String temp = df.format(_serialid+i);
// queue.add(yyyymm+temp);
// }
// oq.map.put(yyyymm, queue);
// Queue tempQ = oq.map.get(yyyymm);
// for (String string : tempQ) {
// System.out.println(string);
// }
for(int i = 1 ;i <= 10; i++){
new Thread(new Runnable() {
@Override
public void run() {
while(true){
OrderQueue oq = new OrderQueue("test");
String yyyymm = DateUtil.formatDate(new Date(), "yyyyMM");// 年月
System.out.println(Thread.currentThread().getName()+oq.getOrderNoTest(yyyymm));
}
}
}).start();
}
}
}

你可能感兴趣的:(java)