| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use Yii;
- use yii\behaviors\TimestampBehavior;
- use yii\db\ActiveRecord;
- /**
- * This is the model class for table "{{%activity_cut_price_goods}}".
- *
- * @property integer $id
- */
- class ActivityCutPriceGoods extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%activity_cut_price_goods}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- ]
- ];
- }
- public function rules()
- {
- return [
- [['id', 'goods_id', 'virtual_sales', 'store_id', 'is_delete', 'activity_id', 'use_attr'], 'integer'],
- [['attr'], 'string'],
- [['price'], 'number'],
- [['created_at', 'updated_at', 'cat_id'], 'safe']
- ];
- }
- public static function saveList($list = [], $activity_id = 0, &$is_platform_audit = false) {
- $new_goods_id = array_column($list, 'goods_id');
- //如果相同时间段存在相同产品,则禁止
- try {
- foreach ($new_goods_id as $goods_item) {
- $activity = ActivityCutPrice::findOne($activity_id);
- $is_exist_goods = self::find()->alias('sag')->where(['sag.goods_id' => $goods_item])
- ->leftJoin(['sg' => ActivityCutPrice::tableName()], 'sag.activity_id = sg.id')
- ->andWhere(['<>', 'sg.id', $activity_id])
- ->andWhere(['OR',
- ['AND',
- ['<=' , 'sg.start_time', strtotime($activity->start_time)],
- ['>=' , 'sg.end_time',strtotime($activity->end_time)]
- ],
- ['AND',
- ['<=' , 'sg.start_time', strtotime($activity->start_time)],
- ['<=' , 'sg.end_time',strtotime($activity->end_time)],
- ['>=' , 'sg.end_time',strtotime($activity->start_time)]
- ],
- ['AND',
- ['>=' , 'sg.start_time', strtotime($activity->start_time)],
- ['<=' , 'sg.end_time',strtotime($activity->end_time)]
- ],
- ['AND',
- ['>=' , 'sg.start_time', strtotime($activity->start_time)],
- ['>=' , 'sg.end_time',strtotime($activity->end_time)],
- ['<=' , 'sg.start_time',strtotime($activity->end_time)]
- ],
- ])->andWhere(['sg.is_delete' => 0, 'sag.is_delete' => 0])->select('sag.id, sag.activity_id')->one();
- if ($is_exist_goods) {
- throw new \Exception("部分商品已经在其他未开始/进行中的活动中");
- }
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- $old_goods_id = [];
- if($activity_id){
- //删除
- $oldList = self::find()->where(['activity_id' => $activity_id, 'is_delete' => 0])->all();
- foreach($oldList as $item){
- array_push($old_goods_id, $item->goods_id);
- $continue = 0;
- foreach($list as $i){
- if($item->id == $i['id']){
- $continue = 1;
- break;
- }
- }
- if($continue){
- continue;
- }
- $item->is_delete = 1;
- $item->save();
- }
- }
- sort($new_goods_id);
- sort($old_goods_id);
- if (array_diff($new_goods_id, $old_goods_id)) {
- $is_platform_audit = true;
- }
- //修改、新增
- foreach($list as $item){
- $id = $item['id'];
- if ($id) {
- $model = self::findOne($id);
- $old_item = $model->attributes;
- unset(
- $old_item['created_at'],
- $old_item['is_delete'],
- $old_item['updated_at'],
- $old_item['sale_num'],
- $item['cover_pic'],
- $item['name'],
- $item['oldPrice'],
- $item['goods_num'],
- );
- $item['price'] = sprintf("%.2f", $item['price']);
- ksort($old_item);
- ksort($item);
- if (array_diff($old_item, $item)) {
- $is_platform_audit = true;
- }
- } else {
- $model = new self();
- }
- $model->attributes = $item;
- $save = $model->save();
- if(!$save){
- \Yii::error([__METHOD__, $model->attributes]);
- return [
- 'code' => 1,
- 'msg' => '商品信息保存失败:' . json_encode($model->getFirstErrors()),
- ];
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- ];
- }
- }
|