| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace backend\modules\mall\services;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\goods\GoodsDescriptionTemplate;
- use common\models\goods\GoodsExtra;
- class DescriptionTemplateService extends BaseService
- {
- /**
- * @param array $params
- *
- * @return array
- */
- public function list(array $params): array
- {
- $baseWhere = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
- $where = [$baseWhere];
- if (isset($params['name']) && $params['name'] !== '') {
- $where[] = ['like', 'name', $params['name']];
- }
- return GoodsDescriptionTemplate::listPage([
- 'where' => $where,
- 'order' => 'id desc'
- ]);
- }
- /**
- * @param int $id
- *
- * @return bool
- */
- public function del(int $id): bool
- {
- $goodsIds = GoodsExtra::find()
- ->where([
- 'description_template_id' => $id,
- 'status' => StatusEnum::ENABLED,
- 'mall_id' => $this->mall_id
- ])
- ->select('goods_id')
- ->column();
- if (
- !empty($goodsIds) &&
- Goods::find()
- ->where([
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- 'id' => $goodsIds
- ])
- ->exists()
- ) {
- $this->error('当前模板已有商品关联,不允许删除');
- return false;
- }
- if (!$this->edit(['id' => $id, 'status' => StatusEnum::DELETE])) {
- return false;
- }
- return true;
- }
- /**
- * @param array $data
- *
- * @return bool
- */
- public function edit(array $data): bool
- {
- $data['mall_id'] = $this->mall_id;
- if (GoodsDescriptionTemplate::setData($data) === false) {
- $this->error(GoodsDescriptionTemplate::getStaticError());
- return false;
- }
- return true;
- }
- /**
- * @param int $id
- *
- * @return array|\yii\db\ActiveRecord|null
- */
- public function detail(int $id)
- {
- return GoodsDescriptionTemplate::find()
- ->select('id, name, frontend_name, fields')
- ->where(['id' => $id, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])
- ->one();
- }
- /**
- * @return array
- */
- public function selectList(): array
- {
- $list = GoodsDescriptionTemplate::find()
- ->select('id, name, fields')
- ->where(['status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id])
- ->indexBy('id')
- ->asArray()
- ->all();
- foreach ($list as &$item) {
- $item['fields'] = json_decode($item['fields'], true) ?: [];
- }
- return $list;
- }
- }
|