js
//异步发送优惠券
sendcoupon:function(){
var txt;
if($(“#coupontype”).val()==”1”){
txt=”优惠券”
}else{
txt=”积客活动”
}
if($(“#selectcouponno”).val()==””){
layer.msg(“请选择”+txt);
return;
}
var selectsharecount = 0;
if($(“#selectsharecount”).val()==”无限制”){
selectsharecount = 0;
}else{
selectsharecount = $(“#selectsharecount”).val();
}
if (!$(“#formsendcoupon”).validate().form()) return;
var issendall = 0;
var sendallcouponinfo = {}
var data = window.params;
if (data && Object.prototype.toString.call(data) == ‘[object Array]’) {
if (data && data.length > 0) {
} else {
layer.msg(“请选择用户”);
return;
}
}else if (data && data.content && data.content == “all”) {
data = [];
issendall = 1;
sendallcouponinfo = {“selectcouponno”:$(“#selectcouponno”).val(),
“agent”:top.log_name,
“grantnum”:$(“#selectgrantnum”).val(),
“sharecount”:selectsharecount
}
}
var nicknames = “”;
var arrData = [];
console.log(data);
$.each(data,function(index,item){
arrData.push({“selectcouponno”:$(“#selectcouponno”).val(),
“openid”:item.openid,
“nickname”:item.nickname,
“custname”:item.custname,
“custtel”:item.custtel,
“compno”:top.log_compno,
“agent”:top.log_name,
“grantnum”:$(“#selectgrantnum”).val(),
“sharecount”:selectsharecount
});
//nicknames += item.nickname+”
“;
});
var tipmsg = “”;
if(issendall){
tipmsg = “所有粉丝将将收到”+($(“#coupontype”).val()==”1”?”优惠券”: “积客活动”);
}else{
tipmsg = “选中粉丝将收到”+($(“#coupontype”).val()==”1”?”优惠券”: “积客活动”);
}
tipmsg += “,确认发送吗?”
layer.confirm(tipmsg, function(index) {
layer.close(index);
/var loadIndex = parent.layer.load(1,{ // 此处1没有意义,随便写个东西
icon: 1, // 0~2 ,
shade: [0.5,’black’] // 黑色透明度0.5背景
});/
$.ajax({
type : “POST”,
url : “admin/weixin/sendWeiXinMpCoupon/“+$(“#coupontype”).val()+”.do”,
data : {
“data”:JSON.stringify(arrData),
“content”:$(“#msgcontent”).val(),
“issendall”:issendall,
“sendallcouponinfo”:JSON.stringify(sendallcouponinfo),
“compno”: top.log_compno,
“operator”: top.log_name,
“toyorno”: top.ToyorNo
},
success : function(data, status) {
if (data.success) {
$(“#selectcouponno”).val(“”);
$(“#selectcouponname”).val(“”);
$(“#selectmoney”).val(“”);
$(“#selectgrantnum”).val(“”);
$(“#selectsharecount”).val(“”);
$(“#selectperiod”).val(“”);//
parent.layer.open({
type: 1,
title: false,
closeBtn: 0,
anim: -1,
content: ‘
});
var params = {compno: top.log_compno, keyno: data.root.keyno};
if(!top.sendCouponIntervalHandle){
top.sendCouponIntervalHandle = setInterval(function(){
crud.onSearchSendCouponAsynResultNode(params);
},10000);
} else {
clearInterval(top.sendCouponIntervalHandle);
top.sendCouponIntervalHandle = setInterval(function(){
crud.onSearchSendCouponAsynResultNode(params);
},10000);
}
}
else {
parent.layer.closeAll(‘loading’);
layer.msg(data.errors);
}
},
error : function(data) {
layer.msg(data.errors?data.errors:”发送失败!”);
}
});
});
},
//查询发送优惠券结果
onSearchSendCouponAsynResultNode: function(params){
if(!params) return;
$.ajax({
type : “POST”,
url : “BaseList/searchAsynResultNode.do”,
data : {compno: params.compno, keyno: params.keyno},
success : function(data) {
if(!data.success){
parent.layer.msg(data.errors?data.errors:”查询发送异步结果失败!”);
return;
}
var r = data.root;
if (r) {
var tips = “总计: “+r.exectotal + “,成功:”+r.execcountok+”,失败:” + r.execcountfail ;
parent.$(“.sendcount”).text(tips);
//异步过程尚未执行完毕,继续循环查询
if (r.resultcode == 0) {
}
//异步过程已经执行完毕,停止循环查询
else {
if(top.sendCouponIntervalHandle){
clearInterval(top.sendCouponIntervalHandle);
}
//确认后台执行完毕后删除记录
parent.$.ajax({
type: ‘POST’,
url: ‘BaseList/deleteAsynResultNode.do’,//发送请求
data: {compno: params.compno, keyno: params.keyno},
success:function(r){
},
error : function(data) {
parent.layer.msg(data.errors?data.errors:”删除异步处理失败!”);
}
});
// 异步执行成功
if (r.resultcode == 1) {
parent.layer.closeAll();
parent.layer.msg(“发送成功!”,{
offset: ‘t’,
anim: 6
});
}
else if (r.resultcode == 2) {
if (r.resulturl) {
parent.layer.confirm(‘存在发送失败的记录,您需要下载失败反馈的Csv文件吗?’, function (index) {
//console.table(r);
parent.layer.close(index);
var save_link = top.document.createElementNS(‘http://www.w3.org/1999/xhtml’, ‘a’);
save_link.href = r.resulturl;
save_link.download = r.resultfilename;
var event = top.document.createEvent(‘MouseEvents’);
event.initMouseEvent(‘click’, true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
parent.layer.closeAll();
},function () {
$.ajax({
type: ‘POST’,
url: ‘BaseList/delDownloadExcelFile.do’,
data: {
fileKey: r.resultfilekey,
fileName: r.resultfilename
},
success: function (data) {
if (data.success) {
parent.layer.closeAll();
}else{
parent.layer.closeAll();
parent.layer.msg(data.errors, {
icon: 1,
time: 2000
});
}
},
error: function (data) {
//console.log(data.msg);
parent.layer.closeAll();
parent.layer.msg(data.errors?data.errors:”出错了”);
}
});
});
} else {
parent.layer.closeAll();
parent.layer.msg(‘存在发送失败的记录,但是在生成失败反馈的Csv文件时出错!’);
}
} else if (r.resultcode == 3) {
parent.layer.closeAll();
parent.layer.msg(‘存在导入失败的记录,但是在生成失败反馈的Excel文件时出错!’);
}
}
} else {
if(top.sendCouponIntervalHandle){
clearInterval(top.sendCouponIntervalHandle);
}
}
},
error : function(data) {
parent.layer.closeAll();
parent.layer.msg(data.errors?data.errors:”发送失败!”);
}
});
},
控制器
//发送优惠券asyn
@RequestMapping(“/sendWeiXinMpCoupon/{coupontype}”)
public void sendWeiXinMpCoupon(@PathVariable(“coupontype”) int coupontype,String data,
String issendall,String sendallcouponinfo,String compno,
String operator,String toyorno, HttpServletRequest request, HttpServletResponse response) {
try {
List
CouponInfo couponInfo = null;
if(StringUtils.isEmpty(issendall)) issendall = “0”;
if(issendall.equals(“0”)) {
ArrayList
JSONArray jsonData = JSONArray.fromObject(data);
for (int i = 0; i < jsonData.size(); i++) {
String openid = jsonData.getJSONObject(i).getString(“openid”);
if (arrOpenid.contains(openid)) continue;//去重
arrOpenid.add(openid);
WxOnLineCustomer wxOnLineCustomer = weiXinService.getByOpenIdWxOnLineCustomer(openid);
String couponno = jsonData.getJSONObject(i).getString(“selectcouponno”);
if (couponInfo == null) {
couponInfo = weiXinService.getByKeyCouponInfo(couponno);
}
String grantnum = jsonData.getJSONObject(i).getString(“grantnum”);
String agent = jsonData.getJSONObject(i).getString(“agent”);
couponInfo.setGrantnum(Integer.valueOf(grantnum));
if (coupontype == 2) {
String sharecount = “0”;
sharecount = jsonData.getJSONObject(i).getString(“sharecount”);
couponInfo.setSharecount(Integer.valueOf(sharecount));
}
wxOnLineCustomer.setCustname(jsonData.getJSONObject(i).getString(“custname”));
wxOnLineCustomer.setCusttel(jsonData.getJSONObject(i).getString(“custtel”));
list.add(wxOnLineCustomer);
}
}else{
if(StringUtils.isEmpty(sendallcouponinfo)) {
outError(response,”发送信息为空”);
return;
}
JSONObject jsonObject = JSONObject.fromObject(sendallcouponinfo);
String grantnum = jsonObject.getString(“grantnum”);
String agent = jsonObject.getString(“agent”);
String couponno = jsonObject.getString(“selectcouponno”);
if (couponInfo == null) {
couponInfo = weiXinService.getByKeyCouponInfo(couponno);
}
couponInfo.setGrantnum(Integer.valueOf(grantnum));
if (coupontype == 2) {
String sharecount = “0”;
sharecount = jsonObject.getString(“sharecount”);
couponInfo.setSharecount(Integer.valueOf(sharecount));
}
WxOnLineCustomer wxOnLineCustomerTmp = new WxOnLineCustomer();
wxOnLineCustomerTmp.setLimit(900000);
wxOnLineCustomerTmp.setStart(0);
wxOnLineCustomerTmp.setConditions(“ and isnull(w.openid,’’) != ‘’ “);
list = weiXinService.getAllWxOnLineCustomer(wxOnLineCustomerTmp);
}
if(list.size() <= 0){
outError(response,”可发送粉丝为空!”);
return;
}
String keyno = UUID.randomUUID().toString().replaceAll(“-“, “”).substring(0, 32);
AsynResult asynResult = new AsynResult();
asynResult.setCompno(compno);
asynResult.setUsername(operator);
asynResult.setKeyno(keyno);
asynResult.setExecdesc(“正在初始化数据!”);
baseService.createAsynResultNode(asynResult);
asynResult.setToyorno(toyorno);
SendCouponParam sendCouponParam = new SendCouponParam();
sendCouponParam.setList(list);
sendCouponParam.setToyorno(toyorno);
sendCouponParam.setAgent(operator);
sendCouponParam.setUsername(operator);
sendCouponParam.setKeyno(keyno);
sendCouponParam.setCompno(compno);
sendCouponParam.setCouponInfo(couponInfo);
sendCouponParam.setRequest(request);
SendCouponRunnable sendCouponRunnable = new SendCouponRunnable(sendCouponParam);
Thread sendCouponThread = new Thread(sendCouponRunnable);
sendCouponThread.start();
outObjectString(asynResult, response);
} catch (Exception e) {
e.printStackTrace();
outError(response);
}
}
//查询异步执行结果
@RequestMapping(“/searchAsynResultNode”)
public void searchAsynResultNode(AsynResult searchAsynResult, HttpServletRequest request, HttpServletResponse response) {
try {
if (StringUtils.isEmpty(searchAsynResult.getCompno())) {
this.outError(response, “公司编码不能为空!”);
return;
}
if (StringUtils.isEmpty(searchAsynResult.getKeyno())) {
this.outError(response, “结果回执编码不能为空!”);
return;
}
AsynResult asynResult = baseService.searchAsynResultNode(searchAsynResult);
if(asynResult != null){
this.outObjectString(asynResult, response);
} else {
this.outError(response, “未查询到异步执行结果!”);
}
} catch (Exception e) {
e.printStackTrace();
this.outError(response, “查询异步执行结果出错!”);
}
}
//删除异步执行结果
@RequestMapping(“/deleteAsynResultNode”)
public void deleteAsynResultNode(AsynResult searchAsynResult, HttpServletRequest request, HttpServletResponse response) {
try {
if (StringUtils.isEmpty(searchAsynResult.getCompno())) {
this.outError(response, “公司编码不能为空!”);
return;
}
if (StringUtils.isEmpty(searchAsynResult.getKeyno())) {
this.outError(response, “结果回执编码不能为空!”);
return;
}
Map
if(mapResult.get(“result”).toString().equals(“0”)){
this.outObjectString(null, response);
} else {
this.outError(response, mapResult.get(“msg”).toString());
}
} catch (Exception e) {
e.printStackTrace();
this.outError(response, “删除异步执行结果出错!”);
}
}
//删除Excel导入时的错误反馈文件
@RequestMapping(“/delDownloadExcelFile”)
public void delDownloadExcelFile(String fileKey, String fileName, HttpServletResponse response, HttpServletRequest request) {
try {
if (StringUtils.isNotEmpty(fileKey) && StringUtils.isNotEmpty(fileName)) {
String vistBasePath = SystemConfigUtil.getProperty(“urlMsimsService”);
if (StringUtils.isNotEmpty(vistBasePath)) {
vistBasePath = vistBasePath + “/BaseList/delDownloadExcelFile.do”;
Map
params.put(“fileKey”, fileKey);
params.put(“fileName”, fileName);
PlatUtil.post(vistBasePath, params);
outObjectString(null, response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
线程处理执行
public class SendCouponRunnable implements Runnable {
private BaseService baseService;
private SendCouponParam params;
public SendCouponRunnable(SendCouponParam sendCouponParam) {
this.params = sendCouponParam;
PlatUtil.setDbInfo(params.getToyorno(),””,null,null);
baseService = SpringContextUtil.getBean(“baseServiceImpl”);
}
@Override
public void run() {
PlatUtil.setDbInfo(params.getToyorno(),””,null,null);
AsynResult asynResult = null;
asynResult = sendCoupon();
asynResult.setCompno(params.getCompno());
asynResult.setKeyno(params.getKeyno());
baseService.updateAsynResultNode(asynResult);
}
/
/
private AsynResult sendCoupon() {
AsynResult asynResult = new AsynResult();
LinkedHashMap
put(“发送状态”, “sendstate”);
put(“昵称”, “nickname”);
put(“关联客户”, “custname”);
put(“关联客户编码”, “custno”);
put(“省”, “province”);
put(“市”, “city”);
put(“手机”, “userphone”);
put(“用户名”, “username”);
put(“OpenId”, “openid”);
}};
long startInitData = System.currentTimeMillis();
/try {
} catch (Exception e) {
e.printStackTrace();
asynResult.setResultcode(-1);
asynResult.setResultmsg(“出错!”);
return asynResult;
}/
long endInitData = System.currentTimeMillis();
try {
List
List
long startImportData = System.currentTimeMillis();
AsynResult updateAsynResult = new AsynResult();
updateAsynResult.setCompno(params.getCompno());
updateAsynResult.setUsername(params.getUsername());
updateAsynResult.setKeyno(params.getKeyno());
updateAsynResult.setExectotal(list.size());
updateAsynResult.setExecdesc(“正在发送优惠券!”);
baseService.updateAsynResultNode(updateAsynResult);
// 开始 将任务分给多个线程去处理
List
int size = list.size();
int num = 100;
int div = size / num;
int mod = size % num;
for(int i = 0;i < div; i++){
List newList = list.subList(inum,inum + num);
SendCouponSubCallable task = new SendCouponSubCallable(newList, params);
FutureTask