ActivityCutPriceGoods.php 5.2 KB

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