ActivityWechatRoomGoods.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_wechat_room_goods}}".
  13. *
  14. * @property integer $id
  15. */
  16. class ActivityWechatRoomGoods extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%activity_wechat_room_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', 'store_id', 'is_delete', 'activity_id', 'use_attr'], 'integer'],
  37. [['attr'], 'string'],
  38. [['price'], 'number'],
  39. [['created_at', 'updated_at'], 'safe']
  40. ];
  41. }
  42. public static function saveList($list = [], $activity_id = 0) {
  43. if($activity_id){
  44. //删除
  45. $oldList = self::find()->where(['activity_id' => $activity_id, 'is_delete' => 0])->all();
  46. foreach($oldList as $item){
  47. $continue = 0;
  48. foreach($list as $i){
  49. if($item->id == $i['id']){
  50. $continue = 1;
  51. break;
  52. }
  53. }
  54. if($continue){
  55. continue;
  56. }
  57. $item->is_delete = 1;
  58. $item->save();
  59. }
  60. }
  61. //修改、新增
  62. foreach($list as $item){
  63. $id = $item['id'];
  64. $model = $id ? self::findOne($id) : new self();
  65. $model->attributes = $item;
  66. $save = $model->save();
  67. if(!$save){
  68. \Yii::error([__METHOD__, $model->attributes]);
  69. return [
  70. 'code' => 1,
  71. 'msg' => '商品信息保存失败:' . json_encode($model->getFirstErrors()),
  72. ];
  73. }
  74. }
  75. return [
  76. 'code' => 0,
  77. 'msg' => 'ok',
  78. ];
  79. }
  80. }