RefundService.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace addons\QiDingDong\common\service;
  3. use addons\QiDingDong\common\enums\QiDingDongEnums;
  4. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\CancelRefundModel;
  5. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\CreateRefundModel;
  6. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\RefundDetailModel;
  7. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\RefundInfoModel;
  8. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\RefundResourceModel;
  9. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\RefundUploadResourceModel;
  10. use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\CreateRefundRequest;
  11. use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\RefundDetailRequest;
  12. use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\RefundInfoRequest;
  13. use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\RefundUploadResourceRequest;
  14. use addons\QiDingDong\common\helper\RequestHelper;
  15. use addons\QiDingDong\common\models\QiDingDongGoodsAttr;
  16. use addons\QiDingDong\common\models\QiDingDongOrder;
  17. use addons\QiDingDong\common\models\QiDingDongRefund;
  18. use common\enums\AddonsEnum;
  19. use common\enums\RabbitMqEnum;
  20. use common\enums\RefundStatusEnum;
  21. use common\enums\RefundTypeEnum;
  22. use common\enums\StatusEnum;
  23. use common\helpers\FormatHelper;
  24. use common\logic\order\OrderRefundLogic;
  25. use common\models\order\Order;
  26. use common\models\order\OrderDetail;
  27. use common\models\order\OrderRefund;
  28. use yii\helpers\Json;
  29. class RefundService
  30. {
  31. public function page(int $mall_id, array $params)
  32. {
  33. $where = [
  34. ['mall_id' => $mall_id],
  35. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  36. ];
  37. if (!empty($params['refund_no'])) {
  38. $where[] = ['like', 'refund_no', $params['refund_no']];
  39. }
  40. if (!empty($params['order_no'])) {
  41. $where[] = ['like', 'order_no', $params['order_no']];
  42. }
  43. if (!empty($params['third_refund_no'])) {
  44. $where[] = ['like', 'third_order_no', $params['third_refund_no']];
  45. }
  46. if (!empty($params['third_refund_id'])) {
  47. $where[] = ['like', 'third_refund_id', $params['third_refund_id']];
  48. }
  49. $ret = QiDingDongRefund::listPage([
  50. 'where' => $where,
  51. 'order' => 'id desc',
  52. 'limit' => $params['limit'] ?? '',
  53. 'with' => ['refund' => function($query) {
  54. $query->with(['orderDetail']);
  55. }]
  56. ]);
  57. if (!empty(QiDingDongRefund::getStaticError())) return QiDingDongRefund::getStaticError();
  58. return $ret;
  59. }
  60. public function create(array $refund)
  61. {
  62. // 只有退货退款/换货维修才需要过来这里?
  63. // 未收到货的无需处理
  64. $aOrder = QiDingDongOrder::findOne(['order_id' => $refund['order_id']]);
  65. if (empty($aOrder)) {
  66. return true;
  67. }
  68. if (in_array($refund['order_detail_id'], $aOrder->order_detail_ids)) {
  69. // 可以执行退款操作
  70. $model = new QiDingDongRefund();
  71. $model->mall_id = $refund['mall_id'];
  72. $model->order_id = $aOrder->order_id;
  73. $model->order_no = $aOrder->order_no;
  74. $model->refund_id = $refund['id'];
  75. $model->refund_no = $refund['order_no'];
  76. $model->third_order_no = $aOrder->third_order_no;
  77. $model->order_detail_id = $refund['order_detail_id'];
  78. $model->user_id = $refund['user_id'];
  79. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  80. }
  81. }
  82. /**
  83. * 同步
  84. * @param int $mall_id
  85. * @param int $id
  86. * @return string|true
  87. */
  88. public function async(int $mall_id, int $id)
  89. {
  90. // 执行同步
  91. $aRefund = QiDingDongRefund::findOne(['id' => $id, 'mall_id' => $mall_id]);
  92. if (empty($aRefund)) return true;
  93. try {
  94. $refund = OrderRefund::findOne(['id' => $aRefund->refund_id]);
  95. $order = Order::findOne(['id' => $aRefund->order_id]);
  96. $detail = OrderDetail::findOne(['id' => $aRefund->order_detail_id]);
  97. $contentModel = RequestHelper::contentModelInstance($mall_id);
  98. $contentModel->content->data = new CreateRefundModel();
  99. $specItem = QiDingDongGoodsAttr::findOne(['goods_attr_id' => $detail->goods_attr_id]);
  100. $contentModel->content->data->goods_id = $specItem->third_goods_id;
  101. $contentModel->content->data->spec_id = $specItem->third_spec_id;
  102. $contentModel->content->data->order_sn = $aRefund->third_order_no;
  103. $contentModel->content->data->service_type = QiDingDongEnums::REFUND_TYPE_MAP[$refund->type];
  104. $contentModel->content->data->service_num = $detail->num;
  105. $contentModel->content->data->service_desc = $refund->reason;
  106. $contentModel->content->data->mobile = $order->mobile;
  107. // 资源 - 图片资源
  108. $resources = [];
  109. if (!empty($refund->pic_list)) {
  110. $images = Json::decode($refund->pic_list);
  111. if (!empty($images)) {
  112. foreach ($images as $image) {
  113. $iInfo = getimagesize($image);
  114. $iContentModel = RequestHelper::contentModelInstance($mall_id);
  115. $iContentModel->content->data = new RefundUploadResourceModel();
  116. $iContentModel->content->data->file = "data:{$iInfo['mime']};base64,".base64_encode(file_get_contents($image));
  117. $iContentModel->content->data->resource_type = StatusEnum::ENABLED; // 图片
  118. $iReq = new RefundUploadResourceRequest();
  119. $iRes = RequestHelper::send($iReq, RequestHelper::enCrypto($iContentModel, $mall_id), $mall_id);
  120. if (!is_array($iRes)) {
  121. \Yii::error("upload failed {$iRes} " . __METHOD__);
  122. } else {
  123. $resource = new RefundResourceModel();
  124. $resource->resource_type = StatusEnum::ENABLED;
  125. $resource->resource_name = $iRes['object'];
  126. $resources[] = $resource;
  127. }
  128. }
  129. }
  130. }
  131. $contentModel->content->data->resources = $resources;
  132. $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE;
  133. // 商品信息
  134. $req = new CreateRefundRequest();
  135. $res = RequestHelper::send($req, RequestHelper::enCrypto($contentModel, $mall_id), $mall_id);
  136. $req_success = 100;
  137. if (!is_array($res)) {
  138. // throw new \Exception($res);
  139. $req_success = StatusEnum::ENABLED;
  140. } else {
  141. $aRefund->third_refund_id = $res['id'];
  142. }
  143. // 更新数据
  144. $aRefund->post = ['create' => $contentModel->toArray()];
  145. $aRefund->content = ['create' => $res];
  146. $aRefund->req_success = $req_success;
  147. $aRefund->last_req = 'async';
  148. $aRefund->last_resp = var_export($res, true); // 如果请求失败那么就需要解密响应数据,否则就不需要
  149. if (!$aRefund->save()) throw new \Exception($aRefund->getErrorMessage());
  150. } catch (\Exception $e) {
  151. \Yii::error(FormatHelper::exception($e, __METHOD__));
  152. dd($e->getMessage(), $e->getLine(), $e->getFile());
  153. return $e->getMessage();
  154. }
  155. return true;
  156. }
  157. /**
  158. * 取消售后
  159. * @param array $refund
  160. * @return int|string|true
  161. * @throws \Exception
  162. */
  163. public function cancel(array $refund)
  164. {
  165. // 执行同步
  166. $mall_id = $refund['mall_id'];
  167. $aRefund = QiDingDongRefund::findOne(['id' => $refund['id'], 'mall_id' => $mall_id]);
  168. if (empty($aRefund)) return true;
  169. $contentModel = RequestHelper::contentModelInstance($mall_id);
  170. $contentModel->content->data = new CancelRefundModel();
  171. $contentModel->content->data->id = $aRefund->third_refund_id;
  172. $res = RequestHelper::send(new CreateRefundRequest(), RequestHelper::enCrypto($contentModel, $mall_id), $mall_id);
  173. $req_success = 100;
  174. if (!is_array($res)) {
  175. // return $res;
  176. $req_success = StatusEnum::ENABLED;
  177. } else {
  178. $aRefund->refund_status = RefundStatusEnum::REVOKE; // 取消
  179. }
  180. $aRefund->content = array_merge($aRefund->content, ['cancel' => $res]);
  181. $aRefund->post = array_merge($aRefund->post, ['cancel' => $contentModel->toArray()]);
  182. $aRefund->req_success = $req_success;
  183. $aRefund->last_req = 'cancel';
  184. $aRefund->last_resp = var_export($res, true);
  185. if (!$aRefund->save()) throw new \Exception($aRefund->getErrorMessage());
  186. return true;
  187. }
  188. public function task()
  189. {
  190. foreach (QiDingDongRefund::find()->select('id, mall_id')->where(['not in', 'refund_status',
  191. [RefundStatusEnum::REFUSE, RefundStatusEnum::FINISH, RefundStatusEnum::REVOKE]])
  192. ->andWhere(['>', 'third_refund_id', 0])->each() as $item) {
  193. $data = [
  194. 'mall_id' => $item->mall_id,
  195. 'id' => $item->id
  196. ];
  197. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, $data,
  198. self::class, 'taskSync');
  199. // $this->taskSync($data);
  200. }
  201. }
  202. /**
  203. * 同步数据
  204. * @param array $data
  205. * @return bool
  206. */
  207. public function taskSync(array $data, array $otherData = [])
  208. {
  209. $aRefund = QiDingDongRefund::findOne(['id' => $data['id']]);
  210. if (empty($aRefund) || $aRefund->refund_status > 2) return true;
  211. try {
  212. $contentModel = RequestHelper::contentModelInstance($data['mall_id']);
  213. $contentModel->content->data = new RefundInfoModel();
  214. $contentModel->content->data->order_sn = $aRefund->third_order_no;
  215. // $contentModel->content->data->spec_id = $aRefund->third_order_no;
  216. $res = RequestHelper::send(new RefundInfoRequest(), RequestHelper::enCrypto($contentModel, $data['mall_id']), $data['mall_id']);
  217. $aRefund->last_resp = var_export($res, true);
  218. $aRefund->req_success = 100;
  219. if (!is_array($res)) {
  220. $aRefund->req_success = StatusEnum::ENABLED;
  221. // throw new \Exception($res);
  222. }
  223. $t = \Yii::$app->db->beginTransaction();
  224. $step_status = 0;
  225. if (!empty($res) && !empty($res[0]['refund'])) {
  226. foreach ($res as $content) {
  227. foreach ($content['refund'] as $item) {
  228. // echo "{$aRefund->third_order_no}, {$item['status']} \n";
  229. switch ($item['status']) {
  230. case 1: // 提交
  231. break;
  232. case 21: // 驳回
  233. // 拒绝
  234. // if (!in_array($refund->refund_status, [RefundStatusEnum::REFUSE, RefundStatusEnum::REVOKE])) { // 如果是非取消状态,那么就需要执行取消
  235. // 执行取消
  236. // $step_status = RefundStatusEnum::REFUSE;
  237. // $aRefund->refund_status = RefundStatusEnum::REFUSE;
  238. // }
  239. break;
  240. case 5:
  241. // 完成
  242. // if ($refund->refund_status != RefundStatusEnum::FINISH) { // 如果是非完成状态,那么就需要执行完成
  243. // 执行统一退款操作
  244. //
  245. $refund = OrderRefund::findOne(['id' => $aRefund->refund_id]);
  246. $step_status = $refund->type == RefundTypeEnum::MONEY ? RefundStatusEnum::STEP_6 : RefundStatusEnum::STEP_4;
  247. // $step_status = RefundStatusEnum::STEP_6;
  248. // }
  249. $aRefund->refund_status = RefundStatusEnum::FINISH;
  250. break;
  251. case 61:
  252. // 已取消
  253. // if (!in_array($refund->refund_status, [RefundStatusEnum::REFUSE, RefundStatusEnum::REVOKE])) { // 如果是非取消状态,那么就需要执行取消
  254. // 执行取消
  255. // $step_status = RefundStatusEnum::REVOKE;
  256. // }
  257. $aRefund->refund_status = RefundStatusEnum::REVOKE;
  258. // $step_status = RefundStatusEnum::REVOKE;
  259. $params['mall_id'] = $aRefund->mall_id;
  260. $params['user_id'] = $aRefund->user_id;
  261. $params['id'] = $aRefund->refund_id;
  262. $res = OrderRefund::cancelRefund($params);
  263. if (!$res) throw new \Exception($res);
  264. break;
  265. case 2: // 进行中的状态
  266. case 22:
  267. case 23:
  268. case 24:
  269. case 25:
  270. case 3:
  271. case 31:
  272. case 32:
  273. case 4:
  274. case 41:
  275. case 43:
  276. case 46:
  277. case 47:
  278. case 6:
  279. case 7:
  280. case 71:
  281. case 72:
  282. case 73:
  283. }
  284. }
  285. $aRefund->third_detail = $res;
  286. if (!empty($step_status)) {
  287. // 退货地址
  288. // echo "{$aRefund->refund_no}, $step_status \n";
  289. $address = ['consignee' => '','mobile' => '','address' => ''];
  290. // 换货物流信息
  291. $express = ['saler_express' => '','saler_express_no' => '','customer_name' => ''];
  292. $data = [
  293. 'id' => $aRefund->refund_id,
  294. 'step_status' => $step_status,
  295. 'address' => $address,
  296. 'express' => $express,
  297. 'refund_reason' => '',
  298. 'is_admin' => $otherData['is_admin'] ?? 0,
  299. 'operator_id' => $otherData['operator_id'] ?? 0,
  300. ];
  301. $logic = new OrderRefundLogic();
  302. $res = $logic->execute($aRefund->mall_id, $data);
  303. if (!$res) throw new \Exception($logic->getError());
  304. }
  305. }
  306. }
  307. $aRefund->content = array_merge(!empty($aRefund->content) ? $aRefund->content : [], ["task_{$aRefund->refund_status}" => $res]);
  308. $aRefund->post = array_merge(!empty($aRefund->post) ? $aRefund->post : [], ["task_{$aRefund->refund_status}" => $contentModel->toArray()]);
  309. $aRefund->last_req = 'taskSync';
  310. if (!$aRefund->save()) throw new \Exception($aRefund->getErrorMessage());
  311. $t->commit();
  312. } catch (\Exception $e) {
  313. if (!empty($t)) $t->rollBack();
  314. var_dump($e->getMessage(), $e->getFile(), $e->getLine());
  315. \Yii::error(FormatHelper::exception($e, __METHOD__));
  316. return false;
  317. }
  318. return true;
  319. }
  320. /**
  321. * 拒绝售后
  322. * @param int $mall_id
  323. * @param int $id
  324. * @return string|true
  325. */
  326. public function reject(int $mall_id, int $id, array $otherData = [])
  327. {
  328. $aRefund = QiDingDongRefund::findOne(['id' => $id, 'mall_id' => $mall_id]);
  329. if (empty($aRefund)) return '售后记录不存在';
  330. // 退货地址
  331. $address = ['consignee' => '','mobile' => '','address' => ''];
  332. // 换货物流信息
  333. $express = ['saler_express' => '','saler_express_no' => '','customer_name' => ''];
  334. $data = [
  335. 'id' => $aRefund->refund_id,
  336. 'step_status' => RefundStatusEnum::REFUSE,
  337. 'address' => $address,
  338. 'express' => $express,
  339. 'refund_reason' => '暂无拒绝理由',
  340. 'is_admin' => $otherData['is_admin'] ?? 0,
  341. 'operator_id' => $otherData['operator_id'] ?? 0,
  342. ];
  343. $logic = new OrderRefundLogic();
  344. $res = $logic->execute($mall_id, $data);
  345. if (!$res) return $logic->getError();
  346. // 执行拒绝动作
  347. return true;
  348. }
  349. public function compensate(int $mall_id, $id, array $otherData = [])
  350. {
  351. $aRefund = QiDingDongRefund::findOne(['id' => $id, 'mall_id' => $mall_id]);
  352. if (empty($aRefund)) return '售后记录不存在';
  353. if ($aRefund->req_success == 100) return '当前状态无效补偿';
  354. try {
  355. switch ($aRefund->last_req) {
  356. case 'async':
  357. return $this->async($mall_id, $id);
  358. case 'taskSync':
  359. return $this->taskSync($aRefund->toArray(), $otherData);
  360. case 'cancel':
  361. return $this->cancel(['mall_id' => $mall_id, 'id' => $aRefund->refund_id]);
  362. }
  363. } catch (\Exception $e) {
  364. return $e->getMessage();
  365. }
  366. return true;
  367. }
  368. public static function hasPic($order_detail_id, $images)
  369. {
  370. $orderDetail = OrderDetail::findOne(['id' => $order_detail_id]);
  371. $attr = QiDingDongGoodsAttr::findOne(['goods_attr_id' => $orderDetail->goods_attr_id]);
  372. if (!empty($attr) && (empty($images) || $images == '[]')) return false;
  373. return true;
  374. }
  375. }