FoodExtGoods.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "{{%food_ext_goods}}".
  13. *
  14. * @property integer $id
  15. */
  16. class FoodExtGoods extends \yii\db\ActiveRecord
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%food_ext_goods}}';
  24. }
  25. public function behaviors()
  26. {
  27. return [
  28. [
  29. 'class' => TimestampBehavior::class,
  30. 'attributes' => [
  31. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  32. ]
  33. ]
  34. ];
  35. }
  36. public static function setDefault($id, $store_id, $md_id) {
  37. if(!$store_id || !$id){
  38. \Yii::error([__METHOD__, $id, $store_id]);
  39. throw new \Exception('参数错误.');
  40. }
  41. $model = self::findOne($id);
  42. if($model->is_default){
  43. $model->is_default = 0;
  44. if(!$model->save()){
  45. \Yii::error([__METHOD__, func_get_args(), $model]);
  46. throw new \Exception(array_shift($model->getFirstErrors()));
  47. }
  48. }else{
  49. self::updateAll(['is_default' => 0], ['and', ['store_id' => $store_id], ['md_id' => $md_id], ['!=', 'id', $id]]);
  50. self::updateAll(['is_default' => 1], ['id' => $id, 'store_id' => $store_id]);
  51. }
  52. return true;
  53. }
  54. public static function saveOne($store_id, $name, $desc = '', $cover_pic = '', $attr = [], $id = 0, $md_id = 0) {
  55. if(!$name){
  56. \Yii::error([__METHOD__, func_get_args()]);
  57. throw new \Exception('参数错误.');
  58. }
  59. $model = $id ? self::findOne($id) : new self();
  60. $model->store_id = $store_id;
  61. $model->name = $name;
  62. $model->desc = $desc;
  63. $model->cover_pic = $cover_pic;
  64. foreach ($attr as &$item) {
  65. $item['price'] = !empty($item['price']) ? $item['price'] : 0;
  66. }
  67. $model->attr = json_encode($attr);
  68. $model->md_id = $md_id;
  69. if(!$model->save()){
  70. \Yii::error([__METHOD__, func_get_args(), $model]);
  71. throw new \Exception(array_shift($model->getFirstErrors()));
  72. }
  73. return true;
  74. }
  75. public static function del($id, $store_id) {
  76. if(!$store_id || !$id){
  77. \Yii::error([__METHOD__, $id, $store_id]);
  78. throw new \Exception('参数错误.');
  79. }
  80. $model = self::findOne(['id' => $id, 'store_id' => $store_id]);
  81. $model->is_delete = 1;
  82. if(!$model->save()){
  83. \Yii::error([__METHOD__, func_get_args(), $model]);
  84. throw new \Exception(array_shift($model->getFirstErrors()));
  85. }
  86. return true;
  87. }
  88. public static function list($params, $store_id) {
  89. $query = self::find()->where(['is_delete' => 0])->orderBy('id DESC');
  90. $store_id && $query->andWhere(['store_id' => $store_id]);
  91. $params['name'] && $query->andWhere(['like', 'name', $params['name']]);
  92. $query->andWhere(['md_id' => $params['md_id']]);
  93. return pagination_make($query);
  94. }
  95. public static function getAttr($id, $store_id, $md_id, &$model = []) {
  96. if($id){
  97. $model = self::findOne(['id' => $id]);
  98. }else{
  99. $model = self::findOne(['store_id' => $store_id, 'is_default' => 1, 'md_id' => $md_id]);
  100. }
  101. return $model->attr;
  102. }
  103. }