| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?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 "{{%food_ext_goods}}".
- *
- * @property integer $id
- */
- class FoodExtGoods extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%food_ext_goods}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ]
- ]
- ];
- }
- public static function setDefault($id, $store_id, $md_id) {
- if(!$store_id || !$id){
- \Yii::error([__METHOD__, $id, $store_id]);
- throw new \Exception('参数错误.');
- }
- $model = self::findOne($id);
- if($model->is_default){
- $model->is_default = 0;
- if(!$model->save()){
- \Yii::error([__METHOD__, func_get_args(), $model]);
- throw new \Exception(array_shift($model->getFirstErrors()));
- }
- }else{
- self::updateAll(['is_default' => 0], ['and', ['store_id' => $store_id], ['md_id' => $md_id], ['!=', 'id', $id]]);
- self::updateAll(['is_default' => 1], ['id' => $id, 'store_id' => $store_id]);
- }
- return true;
- }
- public static function saveOne($store_id, $name, $desc = '', $cover_pic = '', $attr = [], $id = 0, $md_id = 0) {
- if(!$name){
- \Yii::error([__METHOD__, func_get_args()]);
- throw new \Exception('参数错误.');
- }
- $model = $id ? self::findOne($id) : new self();
- $model->store_id = $store_id;
- $model->name = $name;
- $model->desc = $desc;
- $model->cover_pic = $cover_pic;
- foreach ($attr as &$item) {
- $item['price'] = !empty($item['price']) ? $item['price'] : 0;
- }
- $model->attr = json_encode($attr);
- $model->md_id = $md_id;
- if(!$model->save()){
- \Yii::error([__METHOD__, func_get_args(), $model]);
- throw new \Exception(array_shift($model->getFirstErrors()));
- }
- return true;
- }
- public static function del($id, $store_id) {
- if(!$store_id || !$id){
- \Yii::error([__METHOD__, $id, $store_id]);
- throw new \Exception('参数错误.');
- }
- $model = self::findOne(['id' => $id, 'store_id' => $store_id]);
- $model->is_delete = 1;
- if(!$model->save()){
- \Yii::error([__METHOD__, func_get_args(), $model]);
- throw new \Exception(array_shift($model->getFirstErrors()));
- }
- return true;
- }
- public static function list($params, $store_id) {
- $query = self::find()->where(['is_delete' => 0])->orderBy('id DESC');
- $store_id && $query->andWhere(['store_id' => $store_id]);
- $params['name'] && $query->andWhere(['like', 'name', $params['name']]);
- $query->andWhere(['md_id' => $params['md_id']]);
- return pagination_make($query);
- }
- public static function getAttr($id, $store_id, $md_id, &$model = []) {
- if($id){
- $model = self::findOne(['id' => $id]);
- }else{
- $model = self::findOne(['store_id' => $store_id, 'is_default' => 1, 'md_id' => $md_id]);
- }
- return $model->attr;
- }
- }
|