$mall_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]] ]; if (!empty($params['refund_no'])) { $where[] = ['like', 'refund_no', $params['refund_no']]; } if (!empty($params['order_no'])) { $where[] = ['like', 'order_no', $params['order_no']]; } if (!empty($params['third_refund_no'])) { $where[] = ['like', 'third_order_no', $params['third_refund_no']]; } if (!empty($params['third_refund_id'])) { $where[] = ['like', 'third_refund_id', $params['third_refund_id']]; } $ret = QiDingDongRefund::listPage([ 'where' => $where, 'order' => 'id desc', 'limit' => $params['limit'] ?? '', 'with' => ['refund' => function($query) { $query->with(['orderDetail']); }] ]); if (!empty(QiDingDongRefund::getStaticError())) return QiDingDongRefund::getStaticError(); return $ret; } public function create(array $refund) { // 只有退货退款/换货维修才需要过来这里? // 未收到货的无需处理 $aOrder = QiDingDongOrder::findOne(['order_id' => $refund['order_id']]); if (empty($aOrder)) { return true; } if (in_array($refund['order_detail_id'], $aOrder->order_detail_ids)) { // 可以执行退款操作 $model = new QiDingDongRefund(); $model->mall_id = $refund['mall_id']; $model->order_id = $aOrder->order_id; $model->order_no = $aOrder->order_no; $model->refund_id = $refund['id']; $model->refund_no = $refund['order_no']; $model->third_order_no = $aOrder->third_order_no; $model->order_detail_id = $refund['order_detail_id']; $model->user_id = $refund['user_id']; if (!$model->save()) throw new \Exception($model->getErrorMessage()); } } /** * 同步 * @param int $mall_id * @param int $id * @return string|true */ public function async(int $mall_id, int $id) { // 执行同步 $aRefund = QiDingDongRefund::findOne(['id' => $id, 'mall_id' => $mall_id]); if (empty($aRefund)) return true; try { $refund = OrderRefund::findOne(['id' => $aRefund->refund_id]); $order = Order::findOne(['id' => $aRefund->order_id]); $detail = OrderDetail::findOne(['id' => $aRefund->order_detail_id]); $contentModel = RequestHelper::contentModelInstance($mall_id); $contentModel->content->data = new CreateRefundModel(); $specItem = QiDingDongGoodsAttr::findOne(['goods_attr_id' => $detail->goods_attr_id]); $contentModel->content->data->goods_id = $specItem->third_goods_id; $contentModel->content->data->spec_id = $specItem->third_spec_id; $contentModel->content->data->order_sn = $aRefund->third_order_no; $contentModel->content->data->service_type = QiDingDongEnums::REFUND_TYPE_MAP[$refund->type]; $contentModel->content->data->service_num = $detail->num; $contentModel->content->data->service_desc = $refund->reason; $contentModel->content->data->mobile = $order->mobile; // 资源 - 图片资源 $resources = []; if (!empty($refund->pic_list)) { $images = Json::decode($refund->pic_list); if (!empty($images)) { foreach ($images as $image) { $iInfo = getimagesize($image); $iContentModel = RequestHelper::contentModelInstance($mall_id); $iContentModel->content->data = new RefundUploadResourceModel(); $iContentModel->content->data->file = "data:{$iInfo['mime']};base64,".base64_encode(file_get_contents($image)); $iContentModel->content->data->resource_type = StatusEnum::ENABLED; // 图片 $iReq = new RefundUploadResourceRequest(); $iRes = RequestHelper::send($iReq, RequestHelper::enCrypto($iContentModel, $mall_id), $mall_id); if (!is_array($iRes)) { \Yii::error("upload failed {$iRes} " . __METHOD__); } else { $resource = new RefundResourceModel(); $resource->resource_type = StatusEnum::ENABLED; $resource->resource_name = $iRes['object']; $resources[] = $resource; } } } } $contentModel->content->data->resources = $resources; $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE; // 商品信息 $req = new CreateRefundRequest(); $res = RequestHelper::send($req, RequestHelper::enCrypto($contentModel, $mall_id), $mall_id); $req_success = 100; if (!is_array($res)) { // throw new \Exception($res); $req_success = StatusEnum::ENABLED; } else { $aRefund->third_refund_id = $res['id']; } // 更新数据 $aRefund->post = ['create' => $contentModel->toArray()]; $aRefund->content = ['create' => $res]; $aRefund->req_success = $req_success; $aRefund->last_req = 'async'; $aRefund->last_resp = var_export($res, true); // 如果请求失败那么就需要解密响应数据,否则就不需要 if (!$aRefund->save()) throw new \Exception($aRefund->getErrorMessage()); } catch (\Exception $e) { \Yii::error(FormatHelper::exception($e, __METHOD__)); dd($e->getMessage(), $e->getLine(), $e->getFile()); return $e->getMessage(); } return true; } /** * 取消售后 * @param array $refund * @return int|string|true * @throws \Exception */ public function cancel(array $refund) { // 执行同步 $mall_id = $refund['mall_id']; $aRefund = QiDingDongRefund::findOne(['id' => $refund['id'], 'mall_id' => $mall_id]); if (empty($aRefund)) return true; $contentModel = RequestHelper::contentModelInstance($mall_id); $contentModel->content->data = new CancelRefundModel(); $contentModel->content->data->id = $aRefund->third_refund_id; $res = RequestHelper::send(new CreateRefundRequest(), RequestHelper::enCrypto($contentModel, $mall_id), $mall_id); $req_success = 100; if (!is_array($res)) { // return $res; $req_success = StatusEnum::ENABLED; } else { $aRefund->refund_status = RefundStatusEnum::REVOKE; // 取消 } $aRefund->content = array_merge($aRefund->content, ['cancel' => $res]); $aRefund->post = array_merge($aRefund->post, ['cancel' => $contentModel->toArray()]); $aRefund->req_success = $req_success; $aRefund->last_req = 'cancel'; $aRefund->last_resp = var_export($res, true); if (!$aRefund->save()) throw new \Exception($aRefund->getErrorMessage()); return true; } public function task() { foreach (QiDingDongRefund::find()->select('id, mall_id')->where(['not in', 'refund_status', [RefundStatusEnum::REFUSE, RefundStatusEnum::FINISH, RefundStatusEnum::REVOKE]]) ->andWhere(['>', 'third_refund_id', 0])->each() as $item) { $data = [ 'mall_id' => $item->mall_id, 'id' => $item->id ]; \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, $data, self::class, 'taskSync'); // $this->taskSync($data); } } /** * 同步数据 * @param array $data * @return bool */ public function taskSync(array $data, array $otherData = []) { $aRefund = QiDingDongRefund::findOne(['id' => $data['id']]); if (empty($aRefund) || $aRefund->refund_status > 2) return true; try { $contentModel = RequestHelper::contentModelInstance($data['mall_id']); $contentModel->content->data = new RefundInfoModel(); $contentModel->content->data->order_sn = $aRefund->third_order_no; // $contentModel->content->data->spec_id = $aRefund->third_order_no; $res = RequestHelper::send(new RefundInfoRequest(), RequestHelper::enCrypto($contentModel, $data['mall_id']), $data['mall_id']); $aRefund->last_resp = var_export($res, true); $aRefund->req_success = 100; if (!is_array($res)) { $aRefund->req_success = StatusEnum::ENABLED; // throw new \Exception($res); } $t = \Yii::$app->db->beginTransaction(); $step_status = 0; if (!empty($res) && !empty($res[0]['refund'])) { foreach ($res as $content) { foreach ($content['refund'] as $item) { // echo "{$aRefund->third_order_no}, {$item['status']} \n"; switch ($item['status']) { case 1: // 提交 break; case 21: // 驳回 // 拒绝 // if (!in_array($refund->refund_status, [RefundStatusEnum::REFUSE, RefundStatusEnum::REVOKE])) { // 如果是非取消状态,那么就需要执行取消 // 执行取消 // $step_status = RefundStatusEnum::REFUSE; // $aRefund->refund_status = RefundStatusEnum::REFUSE; // } break; case 5: // 完成 // if ($refund->refund_status != RefundStatusEnum::FINISH) { // 如果是非完成状态,那么就需要执行完成 // 执行统一退款操作 // $refund = OrderRefund::findOne(['id' => $aRefund->refund_id]); $step_status = $refund->type == RefundTypeEnum::MONEY ? RefundStatusEnum::STEP_6 : RefundStatusEnum::STEP_4; // $step_status = RefundStatusEnum::STEP_6; // } $aRefund->refund_status = RefundStatusEnum::FINISH; break; case 61: // 已取消 // if (!in_array($refund->refund_status, [RefundStatusEnum::REFUSE, RefundStatusEnum::REVOKE])) { // 如果是非取消状态,那么就需要执行取消 // 执行取消 // $step_status = RefundStatusEnum::REVOKE; // } $aRefund->refund_status = RefundStatusEnum::REVOKE; // $step_status = RefundStatusEnum::REVOKE; $params['mall_id'] = $aRefund->mall_id; $params['user_id'] = $aRefund->user_id; $params['id'] = $aRefund->refund_id; $res = OrderRefund::cancelRefund($params); if (!$res) throw new \Exception($res); break; case 2: // 进行中的状态 case 22: case 23: case 24: case 25: case 3: case 31: case 32: case 4: case 41: case 43: case 46: case 47: case 6: case 7: case 71: case 72: case 73: } } $aRefund->third_detail = $res; if (!empty($step_status)) { // 退货地址 // echo "{$aRefund->refund_no}, $step_status \n"; $address = ['consignee' => '','mobile' => '','address' => '']; // 换货物流信息 $express = ['saler_express' => '','saler_express_no' => '','customer_name' => '']; $data = [ 'id' => $aRefund->refund_id, 'step_status' => $step_status, 'address' => $address, 'express' => $express, 'refund_reason' => '', 'is_admin' => $otherData['is_admin'] ?? 0, 'operator_id' => $otherData['operator_id'] ?? 0, ]; $logic = new OrderRefundLogic(); $res = $logic->execute($aRefund->mall_id, $data); if (!$res) throw new \Exception($logic->getError()); } } } $aRefund->content = array_merge(!empty($aRefund->content) ? $aRefund->content : [], ["task_{$aRefund->refund_status}" => $res]); $aRefund->post = array_merge(!empty($aRefund->post) ? $aRefund->post : [], ["task_{$aRefund->refund_status}" => $contentModel->toArray()]); $aRefund->last_req = 'taskSync'; if (!$aRefund->save()) throw new \Exception($aRefund->getErrorMessage()); $t->commit(); } catch (\Exception $e) { if (!empty($t)) $t->rollBack(); var_dump($e->getMessage(), $e->getFile(), $e->getLine()); \Yii::error(FormatHelper::exception($e, __METHOD__)); return false; } return true; } /** * 拒绝售后 * @param int $mall_id * @param int $id * @return string|true */ public function reject(int $mall_id, int $id, array $otherData = []) { $aRefund = QiDingDongRefund::findOne(['id' => $id, 'mall_id' => $mall_id]); if (empty($aRefund)) return '售后记录不存在'; // 退货地址 $address = ['consignee' => '','mobile' => '','address' => '']; // 换货物流信息 $express = ['saler_express' => '','saler_express_no' => '','customer_name' => '']; $data = [ 'id' => $aRefund->refund_id, 'step_status' => RefundStatusEnum::REFUSE, 'address' => $address, 'express' => $express, 'refund_reason' => '暂无拒绝理由', 'is_admin' => $otherData['is_admin'] ?? 0, 'operator_id' => $otherData['operator_id'] ?? 0, ]; $logic = new OrderRefundLogic(); $res = $logic->execute($mall_id, $data); if (!$res) return $logic->getError(); // 执行拒绝动作 return true; } public function compensate(int $mall_id, $id, array $otherData = []) { $aRefund = QiDingDongRefund::findOne(['id' => $id, 'mall_id' => $mall_id]); if (empty($aRefund)) return '售后记录不存在'; if ($aRefund->req_success == 100) return '当前状态无效补偿'; try { switch ($aRefund->last_req) { case 'async': return $this->async($mall_id, $id); case 'taskSync': return $this->taskSync($aRefund->toArray(), $otherData); case 'cancel': return $this->cancel(['mall_id' => $mall_id, 'id' => $aRefund->refund_id]); } } catch (\Exception $e) { return $e->getMessage(); } return true; } public static function hasPic($order_detail_id, $images) { $orderDetail = OrderDetail::findOne(['id' => $order_detail_id]); $attr = QiDingDongGoodsAttr::findOne(['goods_attr_id' => $orderDetail->goods_attr_id]); if (!empty($attr) && (empty($images) || $images == '[]')) return false; return true; } }