| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace backend\modules\mall\services;
- use common\models\goods\GoodsSpecs;
- use yii\helpers\Json;
- /**
- * 规格服务类
- * SpecsService class
- * @package backend\modules\mall\services
- */
- class SpecsService extends BaseService
- {
- /**
- * StoreId
- *
- * @var integer
- */
- public $store_id = 0;
- /**
- * 获取分页列表
- *
- * @return array
- */
- public function getPageList($params = [], $store_id = 0)
- {
- $page = $this->getPage($params);
- $limit = $this->getLimit($params);
- // list处理
- $list_callback = function($list) {
- return array_map(function($item) {
- $item['values'] = Json::decode($item['values']);
- return $item;
- }, $list);
- };
- return GoodsSpecs::getPageList($page, $limit, function($model) use ($store_id) {
- $model->where(['mall_id' => $this->mall_id, 'store_id' => $store_id]);
- }, 'id desc', $list_callback);
- }
- /**
- * 添加规格
- *
- * @param string $name
- * @param array $values
- * @return boolean
- */
- public function add(string $name, array $values, $store_id = 0)
- {
- return GoodsSpecs::addGoodsSpecs(
- $this->mall_id,
- $name,
- $values,
- $store_id
- );
- }
- /**
- * 修改规格
- *
- * @param integer $id
- * @param string $name
- * @param array $values
- * @return boolean
- */
- public function edit(int $id, string $name, array $values)
- {
- /**
- * @var GoodsSpecs|null
- */
- $specs = GoodsSpecs::getSpecsById($this->mall_id, $id);
- if (empty($specs)) {
- return $this->error('规格不存在');
- }
- return $specs->editSpecs($name, $values);
- }
- /**
- * 删除
- *
- * @return boolean
- */
- public function del(int $id)
- {
- /**
- * @var GoodsSpecs|null
- */
- $specs = GoodsSpecs::getSpecsById($this->mall_id, $id);
- if (empty($specs)) {
- return $this->error('规格不存在');
- }
- return $specs->delete();
-
- }
- }
|