ActivityNewUserGoods.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\behaviors\TimestampBehavior;
  9. use yii\db\ActiveRecord;
  10. /**
  11. * This is the model class for table "{{%activity_new_user_goods}}".
  12. *
  13. * @property integer $id
  14. * @property integer $activity_id
  15. * @property integer $goods_id
  16. * @property string $attr
  17. * @property integer $virtual_sales
  18. * @property float $price
  19. * @property integer $store_id
  20. * @property integer $is_delete
  21. * @property integer $created_at
  22. * @property integer $updated_at
  23. * @property integer $use_attr
  24. * @property integer $sale_num
  25. */
  26. class ActivityNewUserGoods extends \yii\db\ActiveRecord
  27. {
  28. /**
  29. * @inheritdoc
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%activity_new_user_goods}}';
  34. }
  35. public function behaviors()
  36. {
  37. return [
  38. [
  39. 'class' => TimestampBehavior::class,
  40. 'attributes' => [
  41. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  42. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  43. ]
  44. ]
  45. ];
  46. }
  47. /**
  48. * @inheritdoc
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['id', 'goods_id', 'virtual_sales', 'store_id', 'is_delete', 'activity_id', 'use_attr', 'sale_num'], 'integer'],
  54. [['attr'], 'string'],
  55. [['price'], 'number'],
  56. [['created_at', 'updated_at'], 'safe']
  57. ];
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => 'ID',
  66. 'activity_id' => '活动ID',
  67. 'goods_id' => '商品ID',
  68. 'attr' => '商品规格',
  69. 'virtual_sales' => '虚拟销量',
  70. 'price' => '活动价格',
  71. 'store_id' => 'Store Id',
  72. 'is_delete' => 'Is Delete',
  73. 'created_at' => '创建时间',
  74. 'updated_at' => '修改时间',
  75. 'use_attr' => '是否使用规格',
  76. ];
  77. }
  78. public static function saveList($list = [], $activity_id = 0, &$is_platform_audit = false) {
  79. $new_goods_id = array_column($list, 'goods_id');
  80. //如果相同时间段存在相同产品,则禁止
  81. try {
  82. foreach ($new_goods_id as $goods_item) {
  83. $activity = ActivityNewUser::findOne($activity_id);
  84. $is_exist_goods = self::find()->alias('sag')->where(['sag.goods_id' => $goods_item])
  85. ->leftJoin(['sg' => ActivityNewUser::tableName()], 'sag.activity_id = sg.id')
  86. ->andWhere(['<>', 'sg.id', $activity_id])
  87. ->andWhere(['OR',
  88. ['AND',
  89. ['<=' , 'sg.start_time', strtotime($activity->start_time)],
  90. ['>=' , 'sg.end_time',strtotime($activity->end_time)]
  91. ],
  92. ['AND',
  93. ['<=' , 'sg.start_time', strtotime($activity->start_time)],
  94. ['<=' , 'sg.end_time',strtotime($activity->end_time)],
  95. ['>=' , 'sg.end_time',strtotime($activity->start_time)]
  96. ],
  97. ['AND',
  98. ['>=' , 'sg.start_time', strtotime($activity->start_time)],
  99. ['<=' , 'sg.end_time',strtotime($activity->end_time)]
  100. ],
  101. ['AND',
  102. ['>=' , 'sg.start_time', strtotime($activity->start_time)],
  103. ['>=' , 'sg.end_time',strtotime($activity->end_time)],
  104. ['<=' , 'sg.start_time',strtotime($activity->end_time)]
  105. ],
  106. ])->andWhere(['sg.is_delete' => 0, 'sag.is_delete' => 0])->select('sag.id, sag.activity_id')->one();
  107. if ($is_exist_goods) {
  108. throw new \Exception("部分商品已经在其他未开始/进行中的活动中");
  109. }
  110. }
  111. } catch (\Exception $e) {
  112. return [
  113. 'code' => 1,
  114. 'msg' => $e->getMessage()
  115. ];
  116. }
  117. $old_goods_id = [];
  118. if($activity_id){
  119. //删除
  120. $oldList = self::find()->where(['activity_id' => $activity_id, 'is_delete' => 0])->all();
  121. foreach($oldList as $item){
  122. array_push($old_goods_id, $item->goods_id);
  123. $continue = 0;
  124. foreach($list as $i){
  125. if($item->id == $i['id']){
  126. $continue = 1;
  127. break;
  128. }
  129. }
  130. if($continue){
  131. continue;
  132. }
  133. $item->is_delete = 1;
  134. $item->save();
  135. }
  136. }
  137. sort($new_goods_id);
  138. sort($old_goods_id);
  139. if (array_diff($new_goods_id, $old_goods_id)) {
  140. $is_platform_audit = true;
  141. }
  142. //修改、新增
  143. foreach($list as $item){
  144. $id = $item['id'];
  145. if ($id) {
  146. $model = self::findOne($id);
  147. $old_item = $model->attributes;
  148. unset(
  149. $old_item['created_at'],
  150. $old_item['is_delete'],
  151. $old_item['updated_at'],
  152. $old_item['sale_num'],
  153. $old_item['virtual_sales'],
  154. $item['cover_pic'],
  155. $item['name'],
  156. $item['oldPrice'],
  157. $item['goods_num'],
  158. );
  159. $item['price'] = sprintf("%.2f", $item['price']);
  160. ksort($old_item);
  161. ksort($item);
  162. if (array_diff($old_item, $item)) {
  163. $is_platform_audit = true;
  164. }
  165. } else {
  166. $model = new self();
  167. }
  168. $model->attributes = $item;
  169. $save = $model->save();
  170. if(!$save){
  171. return [
  172. 'code' => 1,
  173. 'msg' => '商品信息保存失败:' . json_encode($model->getFirstErrors()),
  174. ];
  175. }
  176. }
  177. return [
  178. 'code' => 0,
  179. 'msg' => 'ok',
  180. ];
  181. }
  182. }