FixController.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace addons\QiJuhe\console\controllers;
  3. use addons\QiJuhe\common\enums\QiJuheEnum;
  4. use addons\QiJuhe\common\enums\RefundTypeEnum;
  5. use addons\QiJuhe\common\models\GoodsModel;
  6. use addons\QiJuhe\common\models\GoodsSkuModel;
  7. use addons\QiJuhe\common\models\MallOrderModel;
  8. use addons\QiJuhe\common\models\OrderModel;
  9. use addons\QiJuhe\common\models\OrderRefundModel;
  10. use addons\QiJuhe\common\models\StorageModel;
  11. use addons\QiJuhe\common\service\ApiService;
  12. use addons\QiJuhe\common\service\GoodsService;
  13. use addons\QiJuhe\common\service\QiGoodsService;
  14. use common\enums\AddonsEnum;
  15. use common\enums\RabbitMqEnum;
  16. use common\models\goods\Goods;
  17. use common\models\mall\Mall;
  18. use common\models\order\Order;
  19. use common\models\order\OrderDetail;
  20. use yii\console\Controller;
  21. class FixController extends Controller
  22. {
  23. public function actionGoods()
  24. {
  25. $mall_ids = [1, 11];
  26. foreach ($mall_ids as $mall_id) {
  27. /**
  28. * @var $each GoodsModel
  29. */
  30. foreach (GoodsModel::find()->where(['mall_id' => $mall_id])->each() as $each) {
  31. echo $each->goods_id . "\n";
  32. $storage_id = StorageModel::byGoodsIdFindStorageId([$each->goods_id]);
  33. // 更新插件商品库的商品信息
  34. $service = new GoodsService();
  35. $service->importLocal($storage_id);
  36. // 更新系统商品库的信息
  37. QiGoodsService::queueAdd($storage_id, $mall_id);
  38. }
  39. }
  40. }
  41. public function actionOrder()
  42. {
  43. try {
  44. // order_source = '' AND pay_status = 1 AND created_at > 1718097620 AND order_status = 1
  45. foreach (\common\models\order\Order::find()
  46. ->where(['and', ['order_source' => 'QiJuhe'], ['pay_status' => 1],
  47. ['>', 'created_at', 1718097620], ['order_status' => 1], ['not in', 'id', [806, 807, 808, 809, 888, 889]]])
  48. ->each() as $each) {
  49. echo "{$each->id} \n";
  50. try {
  51. \addons\QiJuhe\common\service\OrderService::createOrderV2($each->id);
  52. } catch (\Exception $e) {
  53. echo ($e->getMessage() .'--'. $e->getFile() .'--'. $e->getLine() . "\n");
  54. }
  55. }
  56. // \addons\QiJuhe\common\service\OrderService::createOrderV2(889);
  57. } catch (\Exception $e) {
  58. var_dump($e->getMessage(), $e->getFile(), $e->getLine());
  59. }
  60. }
  61. public function actionTest()
  62. {
  63. // (new \addons\QiJuhe\common\service\OrderService([]))->updateLogistic('s20240724225942824475');
  64. // \addons\QiJuhe\common\listeners\OrderPaidListener::async_handle(['id' => 4218]);
  65. $RefundService = new \addons\QiJuhe\common\service\RefundService(['mall_id' => 1]);
  66. $RefundService->after_sales_pass('s20241220184903474265', 1028);
  67. }
  68. public function actionRefund()
  69. {
  70. $db = \Yii::$app->db->beginTransaction();
  71. try {
  72. $params = [
  73. 'order_detail_id' => 7373,
  74. 'type' => 1
  75. ];
  76. $order_detail = OrderDetail::findOne(['id' => $params['order_detail_id']]);
  77. // 订单是否聚合
  78. $order = MallOrderModel::find()->where(['id' => $order_detail['order_id']])->select('id, order_no, mall_id, shipping_money, order_source')->one();
  79. if($order->order_source == AddonsEnum::ADDONS_NAME_CARD_COUPONS){
  80. $goods = Goods::findOne($order_detail['goods_id']);
  81. if(!$goods || $goods['goods_source'] != QiJuheEnum::PLUGIN_SIGN) throw new \Exception('该订单不是聚合供应链订单');
  82. }else{
  83. if ($order->order_source != QiJuheEnum::PLUGIN_SIGN) throw new \Exception('该订单不是聚合供应链订单');
  84. }
  85. // 中台订单
  86. $supply_order = OrderModel::find()->where(['order_id' => $order->id,'order_detail_id'=>$order_detail['id']])->one();
  87. if (empty($supply_order)) {
  88. throw new \Exception('供应链订单不存在');
  89. }
  90. // 添加聚合售后
  91. OrderRefundModel::add([
  92. 'user_id' => $order_detail->user_id,
  93. 'order_no' => $order->order_no,
  94. 'mall_id' => $order->mall_id,
  95. 'supply_id' => $supply_order->supply_id,
  96. 'order_id' => $order_detail['order_id'],
  97. 'order_detail_id' => $order_detail['id'],
  98. 'order_refund_id' => 529,
  99. 'num' => $order_detail->num,
  100. 'data' => $this->getSendData($params, $order, $supply_order),
  101. ]);
  102. $db->commit();
  103. } catch (\Exception $e) {
  104. $db->rollBack();
  105. var_dump($e->getMessage());
  106. }
  107. }
  108. /**
  109. * 获取推送数据
  110. *
  111. * @param array $params
  112. * @return array
  113. */
  114. public function getSendData($params, $order, $supply_order)
  115. {
  116. return [
  117. 'third_order_sn' => $order->order_no, // 订单id与采购端订单号两个必传一个
  118. 'order_item_id' => (int)$supply_order->getOrderItemId(), // 子订单id
  119. 'reason_type' => (int)$params['reason_id'], // 售后原因(售后原因API返回)
  120. 'reason' => $params['remark'], // 售后原因是 其他的的时候填写
  121. 'description' => $params['remark'], // 描述
  122. 'is_received' => (int)RefundTypeEnum::getReasonParam($params['type'])['is_received'], // 是否收到货 1是0否
  123. 'detail_images' => $params['pic_list'] ?? [], // 图片凭证
  124. 'refund_type' => (int)RefundTypeEnum::getReasonParam($params['type'])['after_sale_type'], // 0退款 1退货退款
  125. ];
  126. }
  127. public function actionStorage()
  128. {
  129. foreach (Mall::getAllMallIds() as $mall_id) {
  130. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, ['mall_id' => $mall_id], self::class, 'handleSync');
  131. echo "同步选品库: $mall_id \n";
  132. }
  133. }
  134. public function actionAudit()
  135. {
  136. $cids = [];
  137. /**
  138. * @var $storage StorageModel
  139. */
  140. foreach (StorageModel::find()->batch() as $storages) {
  141. $ids = [];
  142. foreach ($storages as $storage) {
  143. $ids[] = $storage->middle_goods_id;
  144. }
  145. $_ids = GoodsSkuModel::find()->where(['supply_goods_id' => $ids])->select('supply_goods_id')->di->column();
  146. if (count($_ids) != count($ids)) {
  147. // echo implode(',', array_diff($ids, $_ids)) . "\n";
  148. array_push($cids, ...array_diff($ids, $_ids));
  149. }
  150. }
  151. $__ids = array_unique($cids);
  152. /**
  153. * @var $good GoodsSkuModel
  154. */
  155. foreach (GoodsSkuModel::find()->where(['supply_goods_id' => $__ids])->batch() as $goods) {
  156. foreach ($goods as $good) {
  157. $api = ApiService::getConnect($good->mall_id, $good->supply_id);
  158. // 添加选品库
  159. $ret = $api->addStorage([$good->supply_goods_id]);
  160. if ($ret === false) {
  161. echo "添加选品库失败: {$good->supply_goods_id} $api->error \n";
  162. }
  163. }
  164. }
  165. }
  166. public function actionBasic()
  167. {
  168. /**
  169. * @var $good GoodsSkuModel
  170. */
  171. foreach (GoodsSkuModel::find()->where(['mall_id' => 7])->batch() as $goods) {
  172. foreach ($goods as $good) {
  173. $api = ApiService::getConnect($good->mall_id, $good->supply_id);
  174. // 添加选品库
  175. $ret = $api->addStorage([$good->supply_goods_id]);
  176. if ($ret === false) {
  177. echo "添加选品库失败: {$good->supply_goods_id} $api->error \n";
  178. } else {
  179. echo "{$good->supply_goods_id} \n";
  180. }
  181. }
  182. }
  183. }
  184. public static function handleSync($params)
  185. {
  186. $mall_id = $params['mall_id'];
  187. $supply_ids = StorageModel::find()->where(['mall_id' => $mall_id])->select('supply_id')->distinct()->column();
  188. if (empty($supply_ids)) return;
  189. foreach ($supply_ids as $supply_id) {
  190. if (empty($supply_id)) continue;
  191. $t = 1;
  192. /**
  193. * @var $storage StorageModel
  194. */
  195. foreach (StorageModel::find()->where(['mall_id' => $mall_id])->andWhere(['supply_id' => $supply_id])->batch() as $storages) {
  196. $ids = [];
  197. foreach ($storages as $storage) {
  198. $ids[] = $storage->middle_goods_id;
  199. }
  200. }
  201. /**
  202. * @var $storage StorageModel
  203. */
  204. foreach (StorageModel::find()->where(['mall_id' => $mall_id])->andWhere(['is_delete' => 0])->andWhere(['supply_id' => $supply_id])->each() as $storage) {
  205. $ids[] = $storage->middle_goods_id;
  206. if (count($ids) === 20) {
  207. \Yii::error("同步选品库: $mall_id $supply_id $t -- " . json_encode($ids));
  208. $api = ApiService::getConnect($mall_id, $supply_id);
  209. // 添加选品库
  210. $ret = $api->addStorage($ids);
  211. if (!$ret) {
  212. \Yii::error("同步选品库失败: $mall_id $supply_id $t -- ". $api->error .'--'. json_encode($ids));
  213. }
  214. $ids = [];
  215. ++$t;
  216. }
  217. }
  218. }
  219. }
  220. }