package com.idorabox.manage.task;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.idorabox.entity.JdToken;
import com.idorabox.entity.MallProductCare;
import com.idorabox.entity.Notice;
import com.idorabox.entity.SuUser;
import com.idorabox.manage.entity.jd.GetNewStockByIdEntity;
import com.idorabox.manage.jpush.DolaGouJPushUtil;
import com.idorabox.manage.jpush.JPushConstantUtil;
import com.idorabox.manage.service.jd.JdTokenService;
import com.idorabox.manage.service.mall.MallProductCareService;
import com.idorabox.manage.service.su.SuUserService;
import com.idorabox.manage.service.sys.NoticeSrvice;
import com.idorabox.manage.web.util.HttpUtils;
/**
* 京东到货通知
* @author chen
*
*/
@Controller
public class JdDaoHuoNotice {
@Resource
MallProductCareService mallProductCareService;
@Resource
JdTokenService jdTokenService;
@Resource
NoticeSrvice noticeSrvice;
@Resource
SuUserService userService;
/**
* 早上8:00的任务,京东到货通知推送
*/
@Scheduled(cron="0 0 8 * * ?")
public void toGetJdImgs(){
String addressId = null;
Long sku = null;
Long userId = null;
Long productId = null;
String token = null;
List skuNums = null;
Map<String,Object> jpush=new HashMap<String,Object>();
jpush.put("type", JPushConstantUtil.DH_MSG);
MallProductCare address = new MallProductCare();
//查询京东token
Notice notice=new Notice();
Date date = new Date();
List<JdToken> tokenList = jdTokenService.queryList();
if(tokenList != null){
token = tokenList.get(0).getToken();
}
//查询关心列表
List <MallProductCare> careList = mallProductCareService.selectProductCareList();
if(careList != null){
for(MallProductCare care:careList){
sku = care.getSku();
userId = care.getUserId();
productId = care.getProductId();
addressId = care.getAddressId();
if(addressId != null){
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("skuId", sku);
map1.put("num", 1);
skuNums = new ArrayList();
skuNums.add(map1);
if(token != null){
Map<String, String> params = new HashMap<String, String>();
params.put("token", token);
params.put("skuNums",JSON.toJSON(skuNums).toString());//转化为json数据
params.put("area",addressId);
//调用京东接口,获取json数据。有的接口直接返回正确的json数据,就不用再处理。
//反正就要对数据进行处理,得到正确的json数据。
String jsonStr = HttpUtils.sendPost(GetNewStockByIdEntity.jd_url, params);
String str = jsonStr.replace("\\", "").replace(":\"[{", ":[{").replace("}]\"}", "}]}");
//通过gson将json字符串转对象,进行序列化
Gson g = new Gson();
GetNewStockByIdEntity pd = g.fromJson(str, GetNewStockByIdEntity.class);
if(pd != null){
if(pd.isSuccess()){
String stockStateId = pd.getResult().get(0).getStockStateId();
//33 有货 现货-下单立即发货,39 有货 在途-正在内部配货,预计2~6天到达本仓库
//40 有货 可配货-下单后从有货仓库配货,36 预订,34 无货
if(stockStateId == "33" || "33".equals(stockStateId)){//说明有货
notice.setProductId(productId);
notice.setPushType((byte)3);//有货推送
notice.setNoticeTypeId(1l);//不需要首页弹框
notice.setUserId(userId);
notice.setCreateDateTime(date);
notice.setUpdateTime(date);
notice.setStatus((byte)0);
notice.setInvalid(false);
noticeSrvice.insertSelective(notice);
//将推送时间添加进去
address.setId(care.getId());
address.setUpdateTime(date);
address.setNoticeTime(date);
mallProductCareService.updateByPrimaryKeySelective(address);
SuUser user = userService.selectByPrimaryKey(userId);
DolaGouJPushUtil.SendPush_To_One(user.getRegId(),userId.toString(),"朵拉购",jpush);
}
}
}
}
}
}
}
}
}
GetNewStockByIdEntity的实体类,可以通过工具生成:
package com.idorabox.manage.entity.jd;
import java.util.List;
/**
* 查询库存
* @author Administrator
*
*/
public class GetNewStockByIdEntity {
public static String jd_url = "https://bizapi.jd.com/api/stock/getNewStockById";
private boolean success;
private String resultMessage;
private String resultCode;
private List<ResultBeanX> result;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getResultMessage() {
return resultMessage;
}
public void setResultMessage(String resultMessage) {
this.resultMessage = resultMessage;
}
public String getResultCode() {
return resultCode;
}
public List<ResultBeanX> getResult() {
return result;
}
public void setResult(List<ResultBeanX> result) {
this.result = result;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
public static class ResultBeanX {
/**
* id : 1785319316
* result : {"price":"","skuId":723749,"jdPrice":""}
* time : 2017-06-30 01:02:20
* type : 2
*/
private Long skuId;
private String areaId;
private String stockStateId;
private String stockStateDesc;
private String remainNum;
public Long getSkuId() {
return skuId;
}
public void setSkuId(Long skuId) {
this.skuId = skuId;
}
public String getAreaId() {
return areaId;
}
public void setAreaId(String areaId) {
this.areaId = areaId;
}
public String getStockStateId() {
return stockStateId;
}
public void setStockStateId(String stockStateId) {
this.stockStateId = stockStateId;
}
public String getStockStateDesc() {
return stockStateDesc;
}
public void setStockStateDesc(String stockStateDesc) {
this.stockStateDesc = stockStateDesc;
}
public String getRemainNum() {
return remainNum;
}
public void setRemainNum(String remainNum) {
this.remainNum = remainNum;
}
}
}
JPushConstantUtil 实体类,作为推送的标志
package com.idorabox.manage.jpush;
/**
*
* @ClassName: JPushConstantUtil
* @Description: 极光推送常量类
* @author qinzz
* @date 2016年9月23日
* @time 下午1:29:57
*/
public class JPushConstantUtil {
/**
* 朵拉购APP_KEY
*/
public static final String APP_KEY_BUSINESS="77857a3190fca8f9af0f735b";
/**
* 朵拉购 master_key
*/
public static final String MASTER_SECRET_BUSINESS="ebfa8ca1913e2f3ca7eefc7a";
/**
* 订单消息推送
*/
public static final String ORDER_MSG="1";
/**
* 回购消息推送
*/
public static final String BACK_MSG="2";
/**
* 一般消息推送
*/
public static final String MSG="3";
/**
* 产品详情 推送 (推送此类型的消息,附带productId)
*/
public static final String PRODUCT_MSG="4";
/**
* 商品url详情 推送
*/
public static final String URL_MSG="5";
/**
* 红包快过期推送
*/
public static final String REDPACKET_MSG="6";
/**
* 到货提醒推送
*/
public static final String DH_MSG="7";
}
推送类:
package com.idorabox.manage.jpush;
import java.util.Map;
import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
public class DolaGouJPushUtil {
/**
*
* @Title: SendPush_To_One
* @Description: 朵拉购推送给单个用户
* @param masterKey
* @param appKey
* @param registrationId 用户的reg_id
* @param content 推送内容
* @param title 推送的标题
* @param alert 显示的内容
* void 返回类型
* @throws
*/
public static void SendPush_To_One(String registrationId,String alert, String title,Map map){
// JPushClient jpushClient = new JPushClient(JPushConstantUtil.MASTER_SECRET_BUSINESS,
// JPushConstantUtil.APP_KEY_BUSINESS);
//使用单例模式创建极光推送客户端
JPushClient jpushClient=SingletonJPushClient.getJpushClient();
try {
PushPayload paylosd=PushPayload.newBuilder()
.setPlatform(Platform.android_ios())//设置接受的平台
.setAudience(Audience.registrationId(registrationId))//推送给单个用户
.setNotification(Notification.newBuilder()
.setAlert(alert)
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title)//推送给安卓端的标题
.addExtras(map).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1)
.setBadge(0)
.addExtras(map).build())
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();//ApnsProduction:true:正式环境,false:测试环境
PushResult result = jpushClient.sendPush(paylosd);
if(result!=null){
System.out.println("result......"+result);
System.out.println("推送成功!!!!!!");
}
else{
System.out.println("result is null..........");
}
} catch (APIConnectionException e) {
System.out.println("Connection error. Should retry later");
e.printStackTrace();
} catch (APIRequestException e) {
System.out.println("Error response from JPush server. Should review and fix it.");
e.printStackTrace();
} finally {
System.out.println("推送已经推出");
}
}
}