| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <?php
- namespace addons\QiDingDong\common\service;
- use addons\QiDingDong\common\enums\QiDingDongEnums;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\CancelRefundModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\CreateRefundModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\RefundDetailModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\RefundInfoModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\RefundResourceModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\RefundUploadResourceModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\CreateRefundRequest;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\RefundDetailRequest;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\RefundInfoRequest;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\RefundUploadResourceRequest;
- use addons\QiDingDong\common\helper\RequestHelper;
- use addons\QiDingDong\common\models\QiDingDongGoodsAttr;
- use addons\QiDingDong\common\models\QiDingDongOrder;
- use addons\QiDingDong\common\models\QiDingDongRefund;
- use common\enums\AddonsEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\RefundStatusEnum;
- use common\enums\RefundTypeEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\logic\order\OrderRefundLogic;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\order\OrderRefund;
- use yii\helpers\Json;
- class RefundService
- {
- public function page(int $mall_id, array $params)
- {
- $where = [
- ['mall_id' => $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;
- }
- }
|