| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace common\models\goods;
- use Yii;
- use yii\data\Pagination;
- use yii\helpers\Json;
- /**
- * This is the model class for table "qimall_goods_specs".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $store_id 店铺ID
- * @property string $name 规格名称
- * @property string $values 规格值
- * @property int|null $updated_at
- * @property int|null $created_at
- */
- class GoodsSpecs extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'qimall_goods_specs';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'store_id', 'name', 'values'], 'required'],
- [['mall_id', 'store_id', 'updated_at', 'created_at'], 'integer'],
- [['values'], 'string'],
- [['name'], 'string', 'max' => 64],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => 'Mall ID',
- 'store_id' => 'Store ID',
- 'name' => 'Name',
- 'values' => 'Values',
- 'updated_at' => 'Updated At',
- 'created_at' => 'Created At',
- ];
- }
- /**
- * 添加规格
- *
- * @param integer $mall_id
- * @param string $name
- * @param array $values
- * @param integer $store_id
- * @return boolean
- */
- public static function addGoodsSpecs(int $mall_id, string $name, array $values, int $store_id = 0)
- {
- $model = new static();
- $model->mall_id = $mall_id;
- $model->store_id = $store_id;
- return $model->editSpecs($name, $values);
- }
- /**
- * ID获取规格
- *
- * @param integer $mall_id
- * @param integer $id
- * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
- */
- public static function getSpecsById(int $mall_id, int $id)
- {
- return static::find()
- ->where(['mall_id' => $mall_id, 'id' => $id])
- ->one();
- }
- /**
- * 修改规格
- *
- * @param string $name
- * @param array $values
- * @return boolean
- */
- public function editSpecs(string $name, array $values)
- {
- $this->name = $name;
- $this->values = Json::encode($values);
- return $this->save();
- }
- /**
- * 获取分页数据
- *
- * @param integer $page
- * @param integer $limit
- * @param callable $callback
- * @param string $order_by
- * @param callable|null $list_callback
- * @return array
- */
- public static function getPageList(
- int $page, int $limit, callable $callback, string $order_by = 'id', $list_callback = null
- ) :array
- {
- $model = static::find();
- if (!empty($callback)) call_user_func($callback, $model);
- $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
- $model->limit($pagination->limit)->offset($pagination->offset);
- $list = $model->asArray()->orderBy($order_by)->all();
- // list处理
- if (!empty($list_callback)) $list = call_user_func($list_callback, $list);
- $data['list'] = $list;
- $data['page_count'] = $pagination->pageCount;
- $data['current_page'] = $page + 1;
- return $data;
- }
- }
|