OrderExtra.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace common\models\order;
  3. use common\models\order\Order;
  4. /**
  5. * This is the model class for table "{{%order_detail}}".
  6. *
  7. *
  8. * @property int $id 订单项ID
  9. * @property int|null $mall_id 商城ID
  10. * @property int|null $order_id 订单ID
  11. * @property int|null $user_id 用户id
  12. * @property int|null $mch_id 多商户ID
  13. * @property int|null $store_id 门店ID
  14. * @property string $message 留言信息
  15. * @property int|null $created_at 创建时间
  16. * @property int|null $updated_at 更新时间
  17. * @property int $share_user_id 分享用户ID
  18. */
  19. class OrderExtra extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%order_extra}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['order_id', 'mall_id', 'mch_id', 'store_id', 'created_at', 'updated_at'], 'integer'],
  35. [['share_user_id'], 'safe'],
  36. [['message'], 'string'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => '留言项ID',
  46. 'mall_id' => '商城ID',
  47. 'order_id' => '订单ID',
  48. 'mch_id' => '多商户ID',
  49. 'store_id' => '门店id',
  50. 'message' => '留言信息',
  51. 'created_at' => 'Created At',
  52. 'updated_at' => 'Updated At',
  53. 'share_user_id' => '分享的用户Id'
  54. ];
  55. }
  56. /**
  57. * 批量插入
  58. *
  59. * @param array $insert_data
  60. * @return int
  61. */
  62. public static function batchInsert(array $insert_data)
  63. {
  64. return \Yii::$app->db
  65. ->createCommand()
  66. ->batchInsert(static::tableName(), array_keys($insert_data[0]), $insert_data)
  67. ->execute();
  68. }
  69. /**
  70. * 留言批量插入
  71. * @param array $params
  72. * @return bool
  73. */
  74. public static function batchLeave(array $params): bool
  75. {
  76. try {
  77. $orderIds = explode(',', $params['order_ids']);
  78. foreach ($orderIds as $id) {
  79. $orderInfo = Order::find()->where(['id' => $id])->select('id,mch_id,store_id,mall_id')->one();
  80. if ($orderInfo) {
  81. $orderExtra = self::findOne(['order_id' => $id]);
  82. if (!$orderExtra) {
  83. $orderExtra = new self();
  84. }
  85. $orderExtra->mall_id = $orderInfo->mall_id;
  86. $orderExtra->mch_id = $orderInfo->mch_id;
  87. $orderExtra->store_id = $orderInfo->store_id;
  88. $orderExtra->message = $params['content'];
  89. $orderExtra->order_id = $id;
  90. $orderExtra->save();
  91. }
  92. }
  93. return true;
  94. } catch (\Exception $e) {
  95. if (isset($trans)) $trans->rollback();
  96. self::$static_error = $e->getMessage();
  97. return false;
  98. }
  99. }
  100. /**
  101. * 根据条件获取留言数据
  102. * @param array $where
  103. * @param string $field
  104. * @return array|\yii\db\ActiveRecord[]
  105. */
  106. public static function getList(array $where, string $field = '*'): array
  107. {
  108. return self::find()->where($where)->select($field)->orderBy('id desc')->asArray()->all();
  109. }
  110. /**
  111. * 获取留言信息
  112. * @param $where
  113. * @param string $field
  114. * @return array|\yii\db\ActiveRecord|null
  115. */
  116. public static function getInfo($where, string $field = '*')
  117. {
  118. return self::find()->where($where)->select($field)->orderBy('id desc')->asArray()->one();
  119. }
  120. public static function updateShareUserId($order,$data) {
  121. try {
  122. $model = OrderExtra::findOne(['order_id'=>$order['id']]);
  123. if(empty($model)){
  124. $model = new OrderExtra();
  125. $model->loadDefaultValues();
  126. $model->mall_id = $order['mall_id'];
  127. $model->order_id = $order['id'];
  128. $model->mch_id = $order['mch_id'];
  129. $model->store_id = $order['store_id'];
  130. }
  131. $model->share_user_id = $data['share_user_id'];
  132. if(!$model->save()){
  133. throw new \Exception($model->getErrorMessage());
  134. }
  135. return true;
  136. } catch (\Exception $e) {
  137. var_dump('订单创建事件维护share_user_id失败,错误:' . $e->getMessage() . ',行数:' . $e->getLine());
  138. return true;
  139. }
  140. }
  141. }