| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace addons\QiJuhe\common\service;
- use addons\QiJuhe\common\models\MallGoodsModel;
- use addons\QiJuhe\common\models\NoticeModel;
- use addons\QiJuhe\common\models\StorageModel;
- use addons\QiJuhe\common\models\SupplysModel;
- use common\helpers\FormatHelper;
- use yii\helpers\Json;
- /**
- * 消息处理服务
- */
- class NoticeHandleService extends BaseService
- {
- public $mall_id = 0;
- public $supply_id = 0;
- public $data;
- // 通知处理方法
- public static function handle(array $param)
- {
- $supplys_id = $param['supplys_id'] ?? 0;
- $data = $param['data'] ?? [];
- $method = $param['method'] ?? '';
- if (empty($method)) {
- return true;
- }
- $notice_id = $data['notice_id'] ?? '';
- $sign = $data['member_sign'] ?? '';
- $notice_id = NoticeModel::addLog($data['message_type'], $data);
- $supply = !empty($supplys_id)
- ? SupplysModel::find()->where(['id' => $supplys_id])->one()
- : SupplysModel::find()->where(['app_secret' => $sign])->one();
- if (empty($supply)) {
- NoticeModel::updateAll([
- 'response' =>"supply 不存在,sign:{$sign}",
- 'response_time' => time(),
- ], ['id' => $notice_id]);
- \Yii::error("supply 不存在,sign:{$sign}");
- return true;
- }
- $service = new self(['mall_id' => $supply->mall_id, 'supply_id' => $supply->id, 'data' => $data]);
- if (!method_exists($service, $method)) {
- NoticeModel::updateAll([
- 'response' => "qijuhe供应链回调:类型处理方法不存在",
- 'response_time' => time(),
- ], ['id' => $notice_id]);
- \Yii::error("qijuhe供应链回调:类型处理方法不存在");
- return true;
- }
- $service->mall_id = $supply->mall_id;
- $service->supply_id = $supply->id;
- $service->before($data);
- try {
- $res = $service->$method();
- } catch (\Exception $e) {
- \Yii::error("qijuhe供应链处理失败:" . $e->getMessage());
- $res = $e->getMessage();
- }
- $service->after($notice_id, $res);
- return -4;
- }
- public function before($data)
- {
-
-
- }
- public function after($id, $response)
- {
- $res = is_array($response) ? Json::encode($response) : $response;
- $notice_id = $this->data['notice_id'] ?? '';
- NoticeModel::updateAll([
- 'mall_id' => $this->mall_id,
- 'supply_id' => $this->supply_id,
- 'response' => $res,
- 'response_time' => time(),
- ], ['id' => $id]);
- }
- // 商品价格修改
- public function goods_price_alter()
- {
- $goods_id = $this->data['product_id'] ?? 0;
- if (empty($goods_id)) {
- return true;
- }
- $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
- return $this->_update_goods($goods_id);
- }
- // 商品修改
- public function goods_alter()
- {
- $goods_id = $this->data['product_id'] ?? 0;
- if (empty($goods_id)) {
- return true;
- }
- $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
- return $this->_update_goods($goods_id);
- }
- // 商品上架
- public function goods_on_sale()
- {
- $goods_id = $this->data['product_id'] ?? 0;
- if (empty($goods_id)) {
- return true;
- }
- $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
- return $this->_update_goods($goods_id);
- }
- // 商品下架
- public function goods_undercarriage()
- {
- $goods_id = $this->data['product_id'] ?? 0;
- if (empty($goods_id)) {
- return true;
- }
- $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
- return $this->_update_goods($goods_id);
- }
- // 商品删除
- public function goods_delete()
- {
- $goods_id = $this->data['product_id'] ?? 0;
- if (empty($goods_id)) {
- return true;
- }
- $goods_id = is_array($goods_id) ? $goods_id : [$goods_id];
- return $this->_del_goods($goods_id);
- }
- // 订单取消
- public function order_cancel()
- {
- }
- /**
- * 售后确认收货
- *
- * @return void
- */
- public function after_sales_receiving()
- {
- // 不处理
- // 商家发货 {"order_item_id":452,"third_order_sn":"s20240611195720484922","after_sales_id":58,"message_type":"after_sales.receiving","member_sign":"0e2207362367b5803930dc4da38e0003"}
- $third_order_sn = $this->data['third_order_sn'] ?? 0;
- $after_sales_id = $this->data['after_sales_id'] ?? 0;
- if (empty($third_order_sn)) {
- return true;
- }
- $service = new RefundService(['mall_id' => $this->mall_id]);
- try {
- $service->after_sales_receiving($third_order_sn, $after_sales_id);
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- return true;
- }
- /**
- * 售后审核成功
- *
- * @return boolean
- */
- public function after_sales_pass()
- {
- $third_order_sn = $this->data['third_order_sn'] ?? 0;
- $after_sales_id = $this->data['after_sales_id'] ?? 0;
- if (empty($third_order_sn)) {
- return true;
- }
- $service = new RefundService(['mall_id' => $this->mall_id]);
- $service->after_sales_pass($third_order_sn, $after_sales_id, $this->data['order_item_id'] ?? 0);
- return true;
- }
- /**
- * 售后确认退款
- *
- * @return boolean
- */
- public function after_sales_complete()
- {
- $third_order_sn = $this->data['third_order_sn'] ?? 0;
- if (empty($third_order_sn)) {
- return true;
- }
- $service = new RefundService(['mall_id' => $this->mall_id]);
- $service->after_sales_complete($third_order_sn, $this->data['after_sales_id'] ?? 0);
- return true;
- }
- /**
- * 售后审核驳回
- *
- * @return boolean
- */
- public function after_sales_reject()
- {
- $third_order_sn = $this->data['third_order_sn'] ?? 0;
- $after_sales_id = $this->data['after_sales_id'] ?? 0;
- $order_item_id = $this->data['order_item_id'] ?? 0;
- $service = new RefundService(['mall_id' => $this->mall_id]);
- $service->after_sales_reject($third_order_sn, $after_sales_id, $order_item_id);
-
- return true;
- }
- // 订单发货
- public function order_delivery()
- {
- $order_sn = $this->data['order_sn'] ?? 0;
- if (empty($order_sn)) {
- return true;
- }
- $service = new OrderService([]);
- $res = $service->updateLogistic($order_sn);
- return ['msg' => 'ok'];
- }
- public function order_sending()
- {
- return $this->order_delivery();
- }
- public function after_sales_close()
- {
- // $order_sn = $this->data['order_sn'] ?? 0;
- // $after_sales_id = $this->data['after_sales_id'] ?? 0;
- // if (empty($order_sn) || empty($after_sales_id)) {
- // return true;
- // }
- //
- // $service = new RefundService([]);
- // $res = $service->thirdCancel($order_sn, $after_sales_id);
- return $this->after_sales_reject();
- }
- private function _update_goods($goods_id)
- {
- // 获取选品库id
- $storage_id = StorageModel::byGoodsIdFindStorageId($goods_id);
- // 更新插件商品库的商品信息
- $service = new GoodsService();
- $service->importLocal($storage_id);
- // 更新系统商品库的信息
- QiGoodsService::queueAdd($storage_id, $this->mall_id, true);
- return ['msg' => 'ok'];
- }
- /**
- * 处理删除商品
- *
- * @param array $goods_ids
- * @return void
- */
- protected function _del_goods(array $goods_ids)
- {
- // 修改选品库
- StorageModel::changeStatusByMidGoodsIds($this->mall_id, $goods_ids, 3);
- // 直接下架商品
- MallGoodsModel::updateDown($this->mall_id, StorageModel::getMallGoodsIds($this->mall_id, $goods_ids));
- return ['msg' => 'ok'];
- }
- }
|