123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\services\product\label;
- use app\dao\product\label\StoreProductLabelDao;
- use app\services\BaseServices;
- use crmeb\services\FormBuilder;
- use think\facade\Route as Url;
- /**
- * 商品标签
- * Class StoreProductLabelServices
- * @package app\services\product\label
- * @mixin StoreProductLabelDao
- */
- class StoreProductLabelServices extends BaseServices
- {
- /**
- * StoreProductLabelServices constructor.
- * @param StoreProductLabelDao $dao
- */
- public function __construct(StoreProductLabelDao $dao)
- {
- $this->dao = $dao;
- }
- /**
- * 获取所有商品标签
- * @param int $type
- * @param int $relation_id
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getAllLabel(int $type = 0, int $relation_id = 0)
- {
- $where['relation_id'] = $relation_id;
- $where['type'] = $type;
- return $this->dao->getList($where, 'id,label_name');
- }
- /**
- * 获取商品标签 树形结构
- * @param int $type
- * @param int $relation_id
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getProductLabelTreeList(int $type = 0, int $relation_id = 0)
- {
- /** @var StoreProductLabelCateServices $productLabelCateServices */
- $productLabelCateServices = app()->make(StoreProductLabelCateServices::class);
- $cate = $productLabelCateServices->getAllProductLabelCate($type, $relation_id);
- $data = [];
- $label = [];
- $where = [];
- if ($cate) {
- foreach ($cate as $value) {
- $data[] = [
- 'id' => $value['id'] ?? 0,
- 'value' => $value['id'] ?? 0,
- 'label_cate' => 0,
- 'label_name' => $value['name'] ?? '',
- 'label' => $value['name'] ?? '',
- 'relation_id' => $value['relation_id'] ?? 0,
- 'type' => $value['type'] ?? 0,
- ];
- }
- $label = $this->dao->getList($where);
- if ($label) {
- foreach ($label as &$item) {
- $item['label'] = $item['label_name'];
- $item['value'] = $item['id'];
- }
- }
- }
- return $this->get_tree_children($data, $label);
- }
- /**
- * @param array $ids
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author 等风来
- * @email 136327134@qq.com
- * @date 2022/11/1
- */
- public function getLabelCache(array $ids, array $field = null)
- {
- if (app()->config->get('cache.is_data')) {
- $list = $this->dao->cacheInfoByIds($ids);
- } else {
- $list = null;
- }
- if (!$list) {
- $list = $this->dao->getList(['ids' => $ids]);
- foreach ($list as $item) {
- $this->dao->cacheUpdate($item);
- }
- }
- if ($field && $list) {
- $newList = [];
- foreach ($list as $item) {
- $data = [];
- foreach ($field as $k) {
- $data[$k] = $item[$k] ?? null;
- }
- $newList[] = $data;
- }
- $list = $newList;
- }
- return $list;
- }
- /**
- * 获取商品标签表单
- * @param int $type
- * @param int $relation_id
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getLabelForm(int $type = 0, int $relation_id = 0)
- {
- /** @var StoreProductLabelCateServices $service */
- $service = app()->make(StoreProductLabelCateServices::class);
- $options = $service->getAllProductLabelCate($type, $relation_id);
- $data = [];
- foreach ($options as $option) {
- $data[] = ['label' => $option['name'], 'value' => $option['id']];
- }
- $rule = [
- FormBuilder::select('label_cate', '标签分组')->options($data),
- FormBuilder::input('label_name', '标签名称')->maxlength(20),
- ];
- return create_form('添加商品标签', $rule, Url::buildUrl('/product/label/0'), 'POST');
- }
- }
|