AreaOrderSettlementLog.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace addons\Area\common\models;
  3. use common\enums\AddonsEnum;
  4. use common\enums\StatusEnum;
  5. use common\helpers\FormatHelper;
  6. use common\models\common\CapitalLog;
  7. use common\models\order\Order;
  8. use Exception;
  9. use RuntimeException;
  10. use Yii;
  11. /**
  12. * This is the model class for table "qimall_addons_area_order_settlement_log".
  13. *
  14. * @property int $id
  15. * @property int $mall_id
  16. * @property int $status
  17. * @property int $order_id
  18. * @property int $created_at
  19. * @property int $updated_at
  20. */
  21. class AreaOrderSettlementLog extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return 'qimall_addons_area_order_settlement_log';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'status', 'order_id', 'created_at', 'updated_at'], 'integer'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => 'Mall ID',
  47. 'status' => 'Status',
  48. 'order_id' => 'Order ID',
  49. 'created_at' => 'Created At',
  50. 'updated_at' => 'Updated At',
  51. ];
  52. }
  53. /**
  54. * @param array $params
  55. *
  56. * @return bool|self
  57. */
  58. public static function setData(array $params)
  59. {
  60. try {
  61. $model = self::findOne(['order_id' => $params['order_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  62. if (!$model) {
  63. $model = new self();
  64. $model->loadDefaultValues();
  65. }
  66. if (!$model->load($params, '')) {
  67. throw new RuntimeException($model->getErrorMessage());
  68. }
  69. if (!$model->save()) {
  70. throw new RuntimeException($model->getErrorMessage());
  71. }
  72. return $model;
  73. } catch (Exception $e) {
  74. self::$static_error = $e->getMessage();
  75. return false;
  76. }
  77. }
  78. /**
  79. * @param array $insertData
  80. *
  81. * @return bool
  82. */
  83. public static function batchInsertData(array $insertData): bool
  84. {
  85. if (empty($insertData)) {
  86. return true;
  87. }
  88. $time = time();
  89. $batchInsertData = [];
  90. foreach ($insertData as $insertDataItem) {
  91. $batchInsertData[] = array_merge($insertDataItem, [
  92. 'created_at' => $time,
  93. 'updated_at' => $time,
  94. ]);
  95. }
  96. try {
  97. Yii::$app
  98. ->db
  99. ->createCommand()
  100. ->batchInsert(self::tableName(), array_keys($batchInsertData[0]), $batchInsertData)
  101. ->execute();
  102. } catch (Exception $e) {
  103. Yii::$app->custom->logs(FormatHelper::exception($e, sprintf('【区域管理】同步已结算订单到%s表失败', self::tableName())));
  104. return false;
  105. }
  106. return true;
  107. }
  108. /**
  109. * @return void
  110. */
  111. public static function fixData()
  112. {
  113. $commission_wallet_type = 'commission';
  114. CapitalLog::$capitalType = "{$commission_wallet_type}_frozen";
  115. $find = CapitalLog::find();
  116. $params = [
  117. 'where' => [
  118. ['from_type' => AddonsEnum::ADDONS_NAME_AREA]
  119. ],
  120. 'select' => 'order_no'
  121. ];
  122. CapitalLog::paramPack($params, $find);
  123. foreach ($find->asArray()->batch(2000) as $batchItem) {
  124. self::batchInsertData(
  125. Order::find()
  126. ->where([
  127. 'order_no' => array_column($batchItem, 'order_no')
  128. ])
  129. ->select('id order_id, mall_id')
  130. ->asArray()
  131. ->all()
  132. );
  133. }
  134. }
  135. }