SetCloudGoodsStatus.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\jobs;
  8. use app\constants\OptionSetting;
  9. use app\models\Goods;
  10. use yii\base\BaseObject;
  11. use yii\queue\JobInterface;
  12. /**
  13. * 用于平台修改商品后自动同步所有门店该商品属性
  14. */
  15. class SetCloudGoodsStatus extends BaseObject implements JobInterface
  16. {
  17. public int $cloud_goods_id;
  18. public int $store_id;
  19. public string $attr;
  20. public array $attr_id_list;
  21. public string $goods_name;
  22. public function execute($queue)
  23. {
  24. try {
  25. $goods_url = "/goods/getAttrParams";
  26. $cloud_goods_id = $this->cloud_goods_id;
  27. $store_id = $this->store_id;
  28. $attr = $this->attr;
  29. $attr_id_list = $this->attr_id_list;
  30. $goods_name = $this->goods_name;
  31. $param = [
  32. 'id' => $cloud_goods_id
  33. ];
  34. //请求接口
  35. $domain = (new OptionSetting)->getCloudDomainName();
  36. $goodsInfo = cloud_post($domain.$goods_url,$param);
  37. $goodsInfo = json_decode($goodsInfo, true);
  38. // debug_log($goodsInfo ,'ptOrderLog.log');
  39. $goodsInfo = $goodsInfo['data'];
  40. //如果没有上架或审核未通过则不允许购买
  41. if ((int)$goodsInfo['status'] === 0 || (int)$goodsInfo['is_audit'] !== 2) {
  42. //商品下架
  43. Goods::updateAll(['status' => 0, 'store_id' => $store_id],['cloud_goods_id' => $cloud_goods_id]);
  44. }
  45. //如果使用规格
  46. if (!empty($goodsInfo['use_attr']) && (int)$goodsInfo['use_attr'] === 1) {
  47. $goods_2 = new Goods();
  48. $goods_2->attr = $attr;
  49. $res_attr = $goods_2->getAttrGroupList();
  50. if ($res_attr) {
  51. foreach ($res_attr as &$attr_item) {
  52. unset($attr_item->attr_group_id);
  53. foreach ($attr_item->attr_list as &$list_item) {
  54. unset($list_item->attr_id);
  55. }
  56. }
  57. }
  58. //判断规格名称是否一致
  59. $res_attr = json_decode(json_encode($res_attr), true);
  60. if ($res_attr != $goodsInfo['AttrGroupList']) {
  61. //商品下架
  62. Goods::updateAll(['status' => 0, 'store_id' => $store_id],['cloud_goods_id' => $cloud_goods_id]);
  63. }
  64. $attrs = json_decode($goodsInfo['attrs'], true);
  65. foreach ($attrs as $attr_item) {
  66. //如果供货商的商品规格包含购买的
  67. if (in_array($attr_item['attr_id'], $attr_id_list)) {
  68. //判断库存无 或者 供货商库存小于购买的商品数量
  69. if ((int)$attr_item['num'] <= 0) {
  70. //商品下架
  71. Goods::updateAll(['status' => 0, 'store_id' => $store_id],['cloud_goods_id' => $cloud_goods_id]);
  72. }
  73. }
  74. }
  75. } else {
  76. //如果不使用规格
  77. if ((int)$goodsInfo['num'] <= 0 || $goodsInfo['name'] != $goods_name) {
  78. Goods::updateAll(['status' => 0, 'store_id' => $store_id],['cloud_goods_id' => $cloud_goods_id]);
  79. }
  80. }
  81. } catch (\Exception $e) {
  82. debug_log('云仓规格:' . $e->getMessage() . $e->getLine() . $e->getFile(),'ptOrderLog.log');
  83. }
  84. }
  85. }