diff --git a/src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java b/src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java index b0da7a3..8fc978e 100644 --- a/src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java +++ b/src/main/java/cn/chjyj/szwh/service/impl/GoodsServiceImpl.java @@ -72,10 +72,6 @@ public class GoodsServiceImpl implements GoodsService { */ @Override public int getApiGoods() { - // 日期时间格式 - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - int count = 0; //统计更新数量 String host = ProperUtils.getSzwhProp("ENTRUST_URL"); String apiurl = host + "/consign/exchange/v1/selectEntrustSource/exchange"; @@ -88,120 +84,7 @@ public class GoodsServiceImpl implements GoodsService { JSONObject jsonObject = RequestUtils.GetData(apiurl, hmap,true); // 产品详情地址 String gdurl = host + "/consign/exchange/v1/selectEntrustDetails"; - // 循环查询商品的详情 - if(jsonObject!=null && jsonObject.containsKey("data") ) { - // json的data节点 - JSONArray jsonArray = jsonObject.getJSONArray("data"); - for (int i = 0; i < jsonArray.size(); i++) { - // 获取到sourceData字段信息 - JSONObject sdjson = (JSONObject) jsonArray.get(i); - String nsdstr = sdjson.getString("sourceData"); - if (nsdstr == null) { - continue; - } else { - JSONObject nsdjson = JSONObject.parseObject(nsdstr); - // 从sourcedata节点中提取商品唯一编号 - String goodsId = nsdjson.getString("identifier"); - // 以identifier为主键查询详情 - String gdetailUrl = gdurl + "/" + goodsId; - // 请求商品的详情 - JSONObject gdJson = RequestUtils.GetData(gdetailUrl, hmap,true); - log.info(goodsId + "详情:" + gdJson); - //处理返回的结果 - //JSONArray gdArr = gdJson.getJSONArray("data"); - // 如果goods表中存在记录,不进行操作 - Goods tgoods = goodsMapper.getGoodsByIsli(goodsId); - if (tgoods != null) { - continue; - } - // add goodsSource,循环插入 商品资源 -// for (int x = 0; x < gdArr.size(); x++) { - // 从里面取出数据 - JSONObject gditem = gdJson.getJSONObject("data"); - // 商品详情json非空时候 - if(gditem!=null){ - GoodsSource goodsSource = ApiGoodsUtils.jsonToGoodsSource(gditem, goodsId); - goodsSourceMapper.add(goodsSource); - } -// } - // insertGoodsDetail - GoodsDetail goodsDetail = ApiGoodsUtils.jsonToGoodsDetail(nsdjson, sdjson); - // 返回商品详情 - int gdid = goodsDetailMapper.addGoodsDetail(goodsDetail); - - //抽取target 字段中的json,转为jsonobject - JSONObject targetJson = sdjson.getJSONObject("targetData"); - //处理状态 - int istatus = 0; - if (sdjson.getInteger("status") == 1 || sdjson.getInteger("status") == 4 || - sdjson.getInteger("status") == 5) { - istatus = 1; - } else if (sdjson.getInteger("status") == 2) { - istatus = 2; - } else { - istatus = 4; - } - - Goods goods = new Goods(); - //商品起止时间 - String cstart = sdjson.getString("contractualPeriodStart"); - String curTime = new SimpleDateFormat("HH:mm:ss").format(new Date()); - String ncstart = cstart + " " + curTime; - //转换为date - String cend = nsdjson.getString("contractualPeriodEnd"); - String ncend = cend + " 23:59:59"; - try { - //起止时间转为timestamp - SimpleDateFormat tsdf = new SimpleDateFormat("yyyyMMddHHmmss"); - //开始 - Timestamp ncst = new Timestamp(tsdf.parse(ncstart).getTime()); - //截至 - Timestamp ncet = new Timestamp(tsdf.parse(ncend).getTime()); - goods.setContractualStartTime(ncst); - goods.setContractualtimeEndTime(ncet); - }catch (Exception ex){ - // do nothing - } - - goods.setGoodsDetailId(goodsDetail.getId()); - goods.setContractStatus(nsdjson.getInteger("contractStatus")); - goods.setGoodsStatus(istatus); - goods.setOldStatus(0); // 默认为0 - goods.setEntrustStatus(sdjson.getInteger("status")); - // - goods.setUserIslicode(targetJson.getString("identifier")); - goods.setUsername(targetJson.getString("titleName")); - goods.setIsLicode(sdjson.getString("isliCode")); - goods.setGoodsIslicode(nsdjson.getString("identifier")); - goods.setIsRecommend(0); - goods.setRecommendSort(1); // 默认排序1 - goods.setSaleCount(0); - //其他默认 - goods.setUserId(0); - goods.setClickCount(BigInteger.ZERO); - goods.setIsDeleted(0); - goods.setApplyOut(0); - //当前时间戳 - Timestamp nowst=new Timestamp(System.currentTimeMillis()); - goods.setCreateTime(nowst); - goods.setUpdateTime(nowst); - // 添加,返回当前的编号 - int ret_gid = goodsMapper.addGoods(goods); - //添加操作日志 - OperationLog oplog = new OperationLog(); - oplog.setLogid(goods.getId()); - String nowStr = sdf.format(new Date()); - String message = nowStr + " 发布标的成功"; - oplog.setMessage(message); - oplog.setType("goods"); - // 日志入库 - operationLogMapper.addLog(oplog); - } - // 计算器增加1 - count++; - } - } - return count; + return opGoodsInfo(jsonObject,host,hmap); } @Override @@ -585,16 +468,26 @@ public class GoodsServiceImpl implements GoodsService { */ @Override public int manuImportGoods(JSONObject jsonObject) { - // 日期时间格式 - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - int count = 0; //统计更新数量 String host = ProperUtils.getSzwhProp("ENTRUST_URL"); //创建签名 String sign = SignUtils.createSign("entrust"); // 请求头 Map hmap = new HashMap(); hmap.put("entrust_token", sign); + return opGoodsInfo(jsonObject,host,hmap); + } + /** + * 操作商品信息 + * @param jsonObject 待处理的json内容 + * @param host 目标网址 + * @param hmap 请求主机头集合 + * @return + */ + private int opGoodsInfo(JSONObject jsonObject,String host,Map hmap){ + // 日期时间格式 + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + int count = 0; //统计更新数量 // 产品详情地址 String gdurl = host + "/consign/exchange/v1/selectEntrustDetails"; // 循环查询商品的详情 diff --git a/src/test/java/cn/chjyj/szwh/NewTest.java b/src/test/java/cn/chjyj/szwh/NewTest.java new file mode 100644 index 0000000..ab0367a --- /dev/null +++ b/src/test/java/cn/chjyj/szwh/NewTest.java @@ -0,0 +1,35 @@ +package cn.chjyj.szwh; + +import cn.chjyj.szwh.mapper.FestivalsMapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class NewTest { + @Autowired + private FestivalsMapper festivalsMapper; + @Test + public void funTest() throws Exception{ + Calendar calendar = Calendar.getInstance(); + //当前年份 + int iyear= calendar.get(1); + // + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String today =sdf.format(new Date()); + //今天 + int dweek = calendar.get(Calendar.DAY_OF_WEEK); + System.out.println(dweek); + //10天后 + calendar.add(Calendar.DATE,10); + Date afdate = calendar.getTime(); + System.out.println(afdate); + } +}