GoodsSpecs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace common\models\goods;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "qimall_goods_specs".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int $store_id 店铺ID
  12. * @property string $name 规格名称
  13. * @property string $values 规格值
  14. * @property int|null $updated_at
  15. * @property int|null $created_at
  16. */
  17. class GoodsSpecs extends \common\models\base\BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return 'qimall_goods_specs';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id', 'store_id', 'name', 'values'], 'required'],
  33. [['mall_id', 'store_id', 'updated_at', 'created_at'], 'integer'],
  34. [['values'], 'string'],
  35. [['name'], 'string', 'max' => 64],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => 'Mall ID',
  46. 'store_id' => 'Store ID',
  47. 'name' => 'Name',
  48. 'values' => 'Values',
  49. 'updated_at' => 'Updated At',
  50. 'created_at' => 'Created At',
  51. ];
  52. }
  53. /**
  54. * 添加规格
  55. *
  56. * @param integer $mall_id
  57. * @param string $name
  58. * @param array $values
  59. * @param integer $store_id
  60. * @return boolean
  61. */
  62. public static function addGoodsSpecs(int $mall_id, string $name, array $values, int $store_id = 0)
  63. {
  64. $model = new static();
  65. $model->mall_id = $mall_id;
  66. $model->store_id = $store_id;
  67. return $model->editSpecs($name, $values);
  68. }
  69. /**
  70. * ID获取规格
  71. *
  72. * @param integer $mall_id
  73. * @param integer $id
  74. * @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
  75. */
  76. public static function getSpecsById(int $mall_id, int $id)
  77. {
  78. return static::find()
  79. ->where(['mall_id' => $mall_id, 'id' => $id])
  80. ->one();
  81. }
  82. /**
  83. * 修改规格
  84. *
  85. * @param string $name
  86. * @param array $values
  87. * @return boolean
  88. */
  89. public function editSpecs(string $name, array $values)
  90. {
  91. $this->name = $name;
  92. $this->values = Json::encode($values);
  93. return $this->save();
  94. }
  95. /**
  96. * 获取分页数据
  97. *
  98. * @param integer $page
  99. * @param integer $limit
  100. * @param callable $callback
  101. * @param string $order_by
  102. * @param callable|null $list_callback
  103. * @return array
  104. */
  105. public static function getPageList(
  106. int $page, int $limit, callable $callback, string $order_by = 'id', $list_callback = null
  107. ) :array
  108. {
  109. $model = static::find();
  110. if (!empty($callback)) call_user_func($callback, $model);
  111. $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
  112. $model->limit($pagination->limit)->offset($pagination->offset);
  113. $list = $model->asArray()->orderBy($order_by)->all();
  114. // list处理
  115. if (!empty($list_callback)) $list = call_user_func($list_callback, $list);
  116. $data['list'] = $list;
  117. $data['page_count'] = $pagination->pageCount;
  118. $data['current_page'] = $page + 1;
  119. return $data;
  120. }
  121. }