KafkaConsumer.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package zs.payment.messages;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;
  7. import org.springframework.kafka.annotation.KafkaListener;
  8. import org.springframework.stereotype.Component;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import zs.payment.dto.OrderDetailDTO;
  11. import zs.payment.dto.SkuDTO;
  12. import zs.payment.entity.mysql.*;
  13. import zs.payment.enums.ProductSupplyRedis;
  14. import zs.payment.req.ProductDetailReq;
  15. import zs.payment.resp.Result;
  16. import zs.payment.service.Imseweishopgoods.ImsEweiShopGoodsService;
  17. import zs.payment.service.imseweishopcategory.ImsEweiShopCategoryService;
  18. import zs.payment.service.imseweishopgoodsoption.ImsEweiShopGoodsOptionService;
  19. import zs.payment.service.imseweishopgoodsspec.ImsEweiShopGoodsSpecService;
  20. import zs.payment.service.imseweishopgoodsspecitem.ImsEweiShopGoodsSpecItemService;
  21. import zs.payment.service.supply.paysupply.PaySupplyService;
  22. import zs.payment.service.supply.product.ProductSupplyService;
  23. import zs.payment.utils.RedisUtils;
  24. import zs.payment.utils.TimeUtils;
  25. import java.math.BigDecimal;
  26. import java.util.*;
  27. @Slf4j
  28. @Component
  29. @RefreshScope
  30. public class KafkaConsumer {
  31. @Autowired
  32. private PaySupplyService paySupplyService;
  33. @Autowired
  34. private ProductSupplyService productSupplyService;
  35. @Autowired
  36. private RedisUtils redisUtils;
  37. @Autowired
  38. private ImsEweiShopCategoryService imsEweiShopCategoryService;
  39. //商品表
  40. @Autowired
  41. private ImsEweiShopGoodsService imsEweiShopGoodsService;
  42. //规格
  43. @Autowired
  44. private ImsEweiShopGoodsSpecService imsEweiShopGoodsSpecService;
  45. //规格项目
  46. @Autowired
  47. private ImsEweiShopGoodsSpecItemService imsEweiShopGoodsSpecItemService;
  48. @Autowired
  49. private ImsEweiShopGoodsOptionService imsEweiShopGoodsOptionService;
  50. /**
  51. * 商品下架
  52. *
  53. * @param messsage
  54. */
  55. @KafkaListener(
  56. topics = "goods.undercarriage",
  57. groupId = "pay2-group"
  58. )
  59. public void goodsUndercarriage(String messsage) {
  60. JSONObject jsonObject = JSONObject.parseObject(messsage);
  61. int productId = jsonObject.getIntValue("product_id");
  62. // 更新商品状态为下架 update ims_ewei_shop_goods set status = 0 where catch_source=yunxin and catch_id=productId
  63. Boolean b = redisUtils.sismember(ProductSupplyRedis.SYNC_SUPPLY_ID, productId + "");
  64. if (b) {
  65. b = imsEweiShopGoodsService.updateStatusBySupplyId(String.valueOf(productId), "yunxin", 0);
  66. if (!b) {
  67. log.error("--------商品{}下架状态修改失败--------", productId);
  68. }
  69. }
  70. }
  71. /**
  72. * 商品上架
  73. *
  74. * @param messsage
  75. */
  76. @KafkaListener(
  77. topics = "goods.on.sale",
  78. groupId = "pay2-group"
  79. )
  80. public void goodsOnSale(String messsage) {
  81. JSONObject jsonObject = JSONObject.parseObject(messsage);
  82. int productId = jsonObject.getIntValue("product_id");
  83. // 更新商品状态为上架 update ims_ewei_shop_goods set status = 1 where catch_source=yunxin and catch_id=productId
  84. Boolean b = redisUtils.sismember(ProductSupplyRedis.SYNC_SUPPLY_ID, productId + "");
  85. if (b) {
  86. b = imsEweiShopGoodsService.updateStatusBySupplyId(String.valueOf(productId), "yunxin", 1);
  87. if (!b) {
  88. log.error("--------商品{}上架状态修改失败--------", productId);
  89. }
  90. }
  91. }
  92. /**
  93. * 商品删除
  94. *
  95. * @param messsage
  96. */
  97. @KafkaListener(
  98. topics = "goods.delete",
  99. groupId = "pay2-group"
  100. )
  101. public void goodsDelete(String messsage) {
  102. JSONObject jsonObject = JSONObject.parseObject(messsage);
  103. int productId = jsonObject.getIntValue("product_id");
  104. // 更新商品状态为下架 update ims_ewei_shop_goods set status = 0 where catch_source=yunxin and catch_id=productId
  105. Boolean b = redisUtils.sismember(ProductSupplyRedis.SYNC_SUPPLY_ID, productId + "");
  106. if (b) {
  107. b = imsEweiShopGoodsService.deleteBySupplyId(String.valueOf(productId), "yunxin");
  108. if (!b) {
  109. log.error("--------商品{}删除状态修改失败--------", productId);
  110. } else {
  111. //删除key
  112. redisUtils.srem(ProductSupplyRedis.SYNC_SUPPLY_ID, productId + "");
  113. }
  114. }
  115. }
  116. /**
  117. * 商品添加
  118. *
  119. * @param messsage
  120. */
  121. @KafkaListener(
  122. topics = "goods.sync",
  123. groupId = "pay2-group"
  124. )
  125. @Transactional(rollbackFor = Exception.class)
  126. public void goodsSync(String messsage) {
  127. log.info("---准备开始商品入库{}---",messsage);
  128. List<Integer> ids = JSONArray.parseArray(messsage, Integer.class);
  129. ProductDetailReq req = new ProductDetailReq();
  130. req.setIds(ids);
  131. Result result = productSupplyService.detailList(req);
  132. if (result.isSuccess()) {
  133. List<OrderDetailDTO> list = JSONObject.parseArray(
  134. result.getBody().get("data").toString(),
  135. OrderDetailDTO.class
  136. );
  137. //todo 导入到企云商城
  138. list.forEach(dto -> {
  139. //todo 先查看该商品编号是否已同步企云商城。若没同步,则判断商品分类是否存在。不存在则创建
  140. Boolean b = redisUtils.sismember(ProductSupplyRedis.SYNC_SUPPLY_ID, dto.getId().toString());
  141. if (!b) {
  142. //不存在,则需要添加
  143. Integer uniacid = 465;
  144. //第一级
  145. Long pcate = 0L;
  146. //第二级
  147. Long ccate = 0L;
  148. //第三级
  149. Long tcate = 0L;
  150. //未同步,则判断商品分类
  151. ImsEweiShopCategory category1 = imsEweiShopCategoryService.selectByName(dto.getCategory_1().getName(), dto.getCategory_1().getLevel(), 0L, uniacid);
  152. if (!Optional.ofNullable(category1).isPresent()) {
  153. //创建新的商品分类
  154. category1 = new ImsEweiShopCategory();
  155. category1.setLevel(1);
  156. category1.setName(dto.getCategory_1().getName());
  157. category1.setParentId(0);
  158. category1.setUniacId(uniacid);
  159. imsEweiShopCategoryService.save(category1);
  160. }
  161. pcate = category1.getId();
  162. // 存在,再查询商品分类第二级
  163. ImsEweiShopCategory category2 = imsEweiShopCategoryService.selectByName(dto.getCategory_2().getName(), dto.getCategory_2().getLevel(), pcate, uniacid);
  164. if (!Optional.ofNullable(category2).isPresent()) {
  165. category2 = new ImsEweiShopCategory();
  166. category2.setLevel(2);
  167. category2.setName(dto.getCategory_2().getName());
  168. category2.setParentId(pcate.intValue());
  169. category2.setUniacId(uniacid);
  170. imsEweiShopCategoryService.save(category2);
  171. }
  172. ccate = category2.getId();
  173. // 再查询商品分类第三级
  174. ImsEweiShopCategory category3 = imsEweiShopCategoryService.selectByName(dto.getCategory_3().getName(), dto.getCategory_3().getLevel(), ccate, uniacid);
  175. if (!Optional.ofNullable(category3).isPresent()) {
  176. category3 = new ImsEweiShopCategory();
  177. category3.setLevel(3);
  178. category3.setName(dto.getCategory_3().getName());
  179. category3.setParentId(ccate.intValue());
  180. category3.setUniacId(uniacid);
  181. imsEweiShopCategoryService.save(category3);
  182. }
  183. tcate = category3.getId();
  184. //todo 创建商品
  185. ImsEweiShopGoods goods = new ImsEweiShopGoods();
  186. //直接作为推荐商品展示
  187. goods.setIsrecommand(1);
  188. //商品分类
  189. goods.setPcate(pcate);
  190. goods.setCcate(ccate);
  191. goods.setTcate(tcate);
  192. goods.setPcates("");
  193. goods.setCcates("");
  194. goods.setTcates("");
  195. goods.setCates(pcate+","+ccate+","+tcate);
  196. goods.setType(1);
  197. //启用商品规格
  198. goods.setHasoption(1);
  199. //状态 1=上架
  200. goods.setStatus(1);
  201. goods.setDisplayorder(0);
  202. goods.setTitle(dto.getTitle());
  203. //商品图
  204. goods.setThumb(dto.getImage_url());
  205. //商品单位: 件
  206. goods.setUnit(dto.getUnit());
  207. goods.setContent(dto.getDetail_images());
  208. //================以下价格对应企业商城商品价格====================
  209. // 商品售价-用户需要支付的价格 ,要求售价不能大于原价【原需求如此,这边处理方案为原价=商品售价】
  210. goods.setMarketprice(new BigDecimal(dto.getMarket_price() ).divide(new BigDecimal(100)));
  211. // 原价
  212. goods.setProductprice(new BigDecimal(dto.getMarket_price() ).divide(new BigDecimal(100)));
  213. // 生态供应价
  214. goods.setCostprice(new BigDecimal(dto.getCost_price() ).divide(new BigDecimal(100)));
  215. // 成本价
  216. goods.setCost_of_price(new BigDecimal(dto.getCost_price() ).divide(new BigDecimal(100)));
  217. goods.setTotal(dto.getStock());
  218. // 0 拍下减库存 1 付款减库存 2 永久不减
  219. goods.setTotalcnf(0);
  220. goods.setSales(dto.getSales());
  221. goods.setSalesreal(dto.getSales());
  222. goods.setIsnew(0);
  223. goods.setIshot(0);
  224. goods.setIsdiscount(0);
  225. //默认值,避免金额为0
  226. goods.setWeight(new BigDecimal(1));
  227. //商品来源
  228. goods.setSupply_source("yunxin");
  229. //芸信商城的商品主键
  230. goods.setSupply_id(dto.getId().toString());
  231. // goods.setCates("");
  232. //数据库里的时间戳是秒级
  233. goods.setCreatetime(TimeUtils.getLongFromCurrentTime() / 1000);
  234. goods.setUpdatetime(TimeUtils.getLongFromCurrentTime() / 1000);
  235. goods.setDeleted(0);
  236. //多商户id
  237. goods.setMerchid(0);
  238. // merchid==0?0:1
  239. goods.setMerchsale(0);
  240. // goods.setShorttitle();
  241. //商品编号
  242. goods.setGoodssn(dto.getSn());
  243. //商品条码
  244. goods.setProductsn(dto.getBarcode());
  245. //默认发布给云享生态
  246. goods.setUniacid(uniacid);
  247. //创建商品表
  248. boolean b2 = imsEweiShopGoodsService.save(goods);
  249. if (!b2) {
  250. log.error("商品{}创建失败", dto.getId().toString());
  251. return;
  252. }
  253. //创建规格
  254. ImsEweiShopGoodsSpec goodsSpec = new ImsEweiShopGoodsSpec();
  255. goodsSpec.setTitle("规格");
  256. goodsSpec.setUniacid(uniacid);
  257. goodsSpec.setGoodsid(goods.getId().intValue());
  258. boolean b3 = imsEweiShopGoodsSpecService.save(goodsSpec);
  259. for (int i = 0; i < dto.getSkus().size(); i++) {
  260. SkuDTO sku = dto.getSkus().get(i);
  261. ImsEweiShopGoodsSpecItem goodsSpecItem = new ImsEweiShopGoodsSpecItem();
  262. goodsSpecItem.setShowVar(1);
  263. goodsSpecItem.setUniacid(uniacid);
  264. goodsSpecItem.setSpecid(goodsSpec.getId().intValue());
  265. goodsSpecItem.setTitle(sku.getTitle());
  266. goodsSpecItem.setDisplayorder(i);
  267. //创建规格项目
  268. boolean b4 = imsEweiShopGoodsSpecItemService.save(goodsSpecItem);
  269. //创建规格项目的价格
  270. ImsEweiShopGoodsOption goodsOption = new ImsEweiShopGoodsOption();
  271. goodsOption.setTitle(sku.getTitle());
  272. goodsOption.setUniacid(uniacid);
  273. goodsOption.setGoodsid(goods.getId().intValue());
  274. goodsOption.setSpecs(goodsSpecItem.getId().toString());
  275. //原价,可用于打折,最大价格
  276. goodsOption.setProductprice(new BigDecimal(sku.getGuide_price() ).divide(new BigDecimal(100)));
  277. //现价 不能大于原价
  278. goodsOption.setMarketprice(new BigDecimal(sku.getGuide_price() ).divide(new BigDecimal(100)));
  279. //成本价
  280. goodsOption.setCostprice(new BigDecimal(sku.getCost_price() ).divide(new BigDecimal(100)));
  281. //预售价 = 售价
  282. goodsOption.setPresellprice(new BigDecimal(sku.getPrice() ).divide(new BigDecimal(100)));
  283. goodsOption.setStock(sku.getStock());
  284. goodsOption.setWeight(new BigDecimal(sku.getWeight()));
  285. //下单与获取运费验证是否可售等使用这个
  286. goodsOption.setSkuId(sku.getId().toString());
  287. goodsOption.setGoodssn(sku.getSn());
  288. goodsOption.setDay(0);
  289. goodsOption.setAllfullbackprice(new BigDecimal(0));
  290. goodsOption.setFullbackprice(new BigDecimal(0));
  291. goodsOption.setExchange_postage(new BigDecimal(0));
  292. goodsOption.setIsfullback(0);
  293. goodsOption.setIslive(0);
  294. goodsOption.setLiveprice(new BigDecimal(0));
  295. //企云商城的商品重量为0时,金额为0
  296. goodsOption.setWeight(sku.getWeight() == 0 ? new BigDecimal(1) : new BigDecimal(sku.getWeight()));
  297. boolean b5 = imsEweiShopGoodsOptionService.save(goodsOption);
  298. }
  299. redisUtils.sadd(ProductSupplyRedis.SYNC_SUPPLY_ID, dto.getId().toString());
  300. } else {
  301. log.error("====商品{}已同步过=====", dto.getId().toString());
  302. }
  303. });
  304. log.info("=======productId:{} goodsAlter end=======", ids);
  305. } else {
  306. log.error("kafka consumer goods.sync 商品{}详情查询失败", messsage);
  307. }
  308. }
  309. //------------订单----------------
  310. /**
  311. * 物流订单发货
  312. *
  313. * @param messsage
  314. */
  315. @KafkaListener(
  316. topics = "order.delivery",
  317. groupId = "pay2-group"
  318. )
  319. public void goodsDelivery(String orderSn) {
  320. //通过这个第三方订单编号获取物流信息
  321. paySupplyService.updateOrderLogistic(orderSn);
  322. log.info("=======订单发货-第三方订单编号:{} =======", orderSn);
  323. }
  324. }