NoticeHandleService.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace addons\QiJuhe\common\service;
  3. use addons\QiJuhe\common\models\MallGoodsModel;
  4. use addons\QiJuhe\common\models\NoticeModel;
  5. use addons\QiJuhe\common\models\StorageModel;
  6. use addons\QiJuhe\common\models\SupplysModel;
  7. use common\helpers\FormatHelper;
  8. use yii\helpers\Json;
  9. /**
  10. * 消息处理服务
  11. */
  12. class NoticeHandleService extends BaseService
  13. {
  14. public $mall_id = 0;
  15. public $supply_id = 0;
  16. public $data;
  17. // 通知处理方法
  18. public static function handle(array $param)
  19. {
  20. $supplys_id = $param['supplys_id'] ?? 0;
  21. $data = $param['data'] ?? [];
  22. $method = $param['method'] ?? '';
  23. if (empty($method)) {
  24. return true;
  25. }
  26. $notice_id = $data['notice_id'] ?? '';
  27. $sign = $data['member_sign'] ?? '';
  28. $notice_id = NoticeModel::addLog($data['message_type'], $data);
  29. $supply = !empty($supplys_id)
  30. ? SupplysModel::find()->where(['id' => $supplys_id])->one()
  31. : SupplysModel::find()->where(['app_secret' => $sign])->one();
  32. if (empty($supply)) {
  33. NoticeModel::updateAll([
  34. 'response' =>"supply 不存在,sign:{$sign}",
  35. 'response_time' => time(),
  36. ], ['id' => $notice_id]);
  37. \Yii::error("supply 不存在,sign:{$sign}");
  38. return true;
  39. }
  40. $service = new self(['mall_id' => $supply->mall_id, 'supply_id' => $supply->id, 'data' => $data]);
  41. if (!method_exists($service, $method)) {
  42. NoticeModel::updateAll([
  43. 'response' => "qijuhe供应链回调:类型处理方法不存在",
  44. 'response_time' => time(),
  45. ], ['id' => $notice_id]);
  46. \Yii::error("qijuhe供应链回调:类型处理方法不存在");
  47. return true;
  48. }
  49. $service->mall_id = $supply->mall_id;
  50. $service->supply_id = $supply->id;
  51. $service->before($data);
  52. try {
  53. $res = $service->$method();
  54. } catch (\Exception $e) {
  55. \Yii::error("qijuhe供应链处理失败:" . $e->getMessage());
  56. $res = $e->getMessage();
  57. }
  58. $service->after($notice_id, $res);
  59. return -4;
  60. }
  61. public function before($data)
  62. {
  63. }
  64. public function after($id, $response)
  65. {
  66. $res = is_array($response) ? Json::encode($response) : $response;
  67. $notice_id = $this->data['notice_id'] ?? '';
  68. NoticeModel::updateAll([
  69. 'mall_id' => $this->mall_id,
  70. 'supply_id' => $this->supply_id,
  71. 'response' => $res,
  72. 'response_time' => time(),
  73. ], ['id' => $id]);
  74. }
  75. // 商品价格修改
  76. public function goods_price_alter()
  77. {
  78. $goods_id = $this->data['product_id'] ?? 0;
  79. if (empty($goods_id)) {
  80. return true;
  81. }
  82. $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
  83. return $this->_update_goods($goods_id);
  84. }
  85. // 商品修改
  86. public function goods_alter()
  87. {
  88. $goods_id = $this->data['product_id'] ?? 0;
  89. if (empty($goods_id)) {
  90. return true;
  91. }
  92. $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
  93. return $this->_update_goods($goods_id);
  94. }
  95. // 商品上架
  96. public function goods_on_sale()
  97. {
  98. $goods_id = $this->data['product_id'] ?? 0;
  99. if (empty($goods_id)) {
  100. return true;
  101. }
  102. $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
  103. return $this->_update_goods($goods_id);
  104. }
  105. // 商品下架
  106. public function goods_undercarriage()
  107. {
  108. $goods_id = $this->data['product_id'] ?? 0;
  109. if (empty($goods_id)) {
  110. return true;
  111. }
  112. $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
  113. return $this->_update_goods($goods_id);
  114. }
  115. // 商品删除
  116. public function goods_delete()
  117. {
  118. $goods_id = $this->data['product_id'] ?? 0;
  119. if (empty($goods_id)) {
  120. return true;
  121. }
  122. $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
  123. return $this->_del_goods($goods_id);
  124. }
  125. // 订单取消
  126. public function order_cancel()
  127. {
  128. }
  129. /**
  130. * 售后确认收货
  131. *
  132. * @return void
  133. */
  134. public function after_sales_receiving()
  135. {
  136. // 不处理
  137. // 商家发货 {"order_item_id":452,"third_order_sn":"s20240611195720484922","after_sales_id":58,"message_type":"after_sales.receiving","member_sign":"0e2207362367b5803930dc4da38e0003"}
  138. $third_order_sn = $this->data['third_order_sn'] ?? 0;
  139. $after_sales_id = $this->data['after_sales_id'] ?? 0;
  140. if (empty($third_order_sn)) {
  141. return true;
  142. }
  143. $service = new RefundService(['mall_id' => $this->mall_id]);
  144. try {
  145. $service->after_sales_receiving($third_order_sn, $after_sales_id);
  146. } catch (\Exception $e) {
  147. \Yii::error(FormatHelper::exception($e, __METHOD__));
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * 售后审核成功
  154. *
  155. * @return boolean
  156. */
  157. public function after_sales_pass()
  158. {
  159. $third_order_sn = $this->data['third_order_sn'] ?? 0;
  160. $after_sales_id = $this->data['after_sales_id'] ?? 0;
  161. if (empty($third_order_sn)) {
  162. return true;
  163. }
  164. $service = new RefundService(['mall_id' => $this->mall_id]);
  165. $service->after_sales_pass($third_order_sn, $after_sales_id, $this->data['order_item_id'] ?? 0);
  166. return true;
  167. }
  168. /**
  169. * 售后确认退款
  170. *
  171. * @return boolean
  172. */
  173. public function after_sales_complete()
  174. {
  175. $third_order_sn = $this->data['third_order_sn'] ?? 0;
  176. if (empty($third_order_sn)) {
  177. return true;
  178. }
  179. $service = new RefundService(['mall_id' => $this->mall_id]);
  180. $service->after_sales_complete($third_order_sn, $this->data['after_sales_id'] ?? 0);
  181. return true;
  182. }
  183. /**
  184. * 售后审核驳回
  185. *
  186. * @return boolean
  187. */
  188. public function after_sales_reject()
  189. {
  190. $third_order_sn = $this->data['third_order_sn'] ?? 0;
  191. $after_sales_id = $this->data['after_sales_id'] ?? 0;
  192. $order_item_id = $this->data['order_item_id'] ?? 0;
  193. $service = new RefundService(['mall_id' => $this->mall_id]);
  194. $service->after_sales_reject($third_order_sn, $after_sales_id, $order_item_id);
  195. return true;
  196. }
  197. // 订单发货
  198. public function order_delivery()
  199. {
  200. $order_sn = $this->data['order_sn'] ?? 0;
  201. if (empty($order_sn)) {
  202. return true;
  203. }
  204. $service = new OrderService([]);
  205. $res = $service->updateLogistic($order_sn);
  206. return ['msg' => 'ok'];
  207. }
  208. public function order_sending()
  209. {
  210. return $this->order_delivery();
  211. }
  212. public function after_sales_close()
  213. {
  214. // $order_sn = $this->data['order_sn'] ?? 0;
  215. // $after_sales_id = $this->data['after_sales_id'] ?? 0;
  216. // if (empty($order_sn) || empty($after_sales_id)) {
  217. // return true;
  218. // }
  219. //
  220. // $service = new RefundService([]);
  221. // $res = $service->thirdCancel($order_sn, $after_sales_id);
  222. return $this->after_sales_reject();
  223. }
  224. private function _update_goods($goods_id)
  225. {
  226. // 获取选品库id
  227. $storage_id = StorageModel::byGoodsIdFindStorageId($goods_id);
  228. // 更新插件商品库的商品信息
  229. $service = new GoodsService();
  230. $service->importLocal($storage_id);
  231. // 更新系统商品库的信息
  232. QiGoodsService::queueAdd($storage_id, $this->mall_id, true);
  233. return ['msg' => 'ok'];
  234. }
  235. /**
  236. * 处理删除商品
  237. *
  238. * @param array $goods_ids
  239. * @return void
  240. */
  241. protected function _del_goods(array $goods_ids)
  242. {
  243. // 修改选品库
  244. StorageModel::changeStatusByMidGoodsIds($this->mall_id, $goods_ids, 3);
  245. // 直接下架商品
  246. MallGoodsModel::updateDown($this->mall_id, StorageModel::getMallGoodsIds($this->mall_id, $goods_ids));
  247. return ['msg' => 'ok'];
  248. }
  249. }