StoreProductLabelServices.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\product\label;
  12. use app\dao\product\label\StoreProductLabelDao;
  13. use app\services\BaseServices;
  14. use crmeb\services\FormBuilder;
  15. use think\facade\Route as Url;
  16. /**
  17. * 商品标签
  18. * Class StoreProductLabelServices
  19. * @package app\services\product\label
  20. * @mixin StoreProductLabelDao
  21. */
  22. class StoreProductLabelServices extends BaseServices
  23. {
  24. /**
  25. * StoreProductLabelServices constructor.
  26. * @param StoreProductLabelDao $dao
  27. */
  28. public function __construct(StoreProductLabelDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取所有商品标签
  34. * @param int $type
  35. * @param int $relation_id
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getAllLabel(int $type = 0, int $relation_id = 0)
  42. {
  43. $where['relation_id'] = $relation_id;
  44. $where['type'] = $type;
  45. return $this->dao->getList($where, 'id,label_name');
  46. }
  47. /**
  48. * 获取商品标签 树形结构
  49. * @param int $type
  50. * @param int $relation_id
  51. * @return array
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function getProductLabelTreeList(int $type = 0, int $relation_id = 0)
  57. {
  58. /** @var StoreProductLabelCateServices $productLabelCateServices */
  59. $productLabelCateServices = app()->make(StoreProductLabelCateServices::class);
  60. $cate = $productLabelCateServices->getAllProductLabelCate($type, $relation_id);
  61. $data = [];
  62. $label = [];
  63. $where = [];
  64. if ($cate) {
  65. foreach ($cate as $value) {
  66. $data[] = [
  67. 'id' => $value['id'] ?? 0,
  68. 'value' => $value['id'] ?? 0,
  69. 'label_cate' => 0,
  70. 'label_name' => $value['name'] ?? '',
  71. 'label' => $value['name'] ?? '',
  72. 'relation_id' => $value['relation_id'] ?? 0,
  73. 'type' => $value['type'] ?? 0,
  74. ];
  75. }
  76. $label = $this->dao->getList($where);
  77. if ($label) {
  78. foreach ($label as &$item) {
  79. $item['label'] = $item['label_name'];
  80. $item['value'] = $item['id'];
  81. }
  82. }
  83. }
  84. return $this->get_tree_children($data, $label);
  85. }
  86. /**
  87. * @param array $ids
  88. * @return array|mixed
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. * @author 等风来
  93. * @email 136327134@qq.com
  94. * @date 2022/11/1
  95. */
  96. public function getLabelCache(array $ids, array $field = null)
  97. {
  98. if (app()->config->get('cache.is_data')) {
  99. $list = $this->dao->cacheInfoByIds($ids);
  100. } else {
  101. $list = null;
  102. }
  103. if (!$list) {
  104. $list = $this->dao->getList(['ids' => $ids]);
  105. foreach ($list as $item) {
  106. $this->dao->cacheUpdate($item);
  107. }
  108. }
  109. if ($field && $list) {
  110. $newList = [];
  111. foreach ($list as $item) {
  112. $data = [];
  113. foreach ($field as $k) {
  114. $data[$k] = $item[$k] ?? null;
  115. }
  116. $newList[] = $data;
  117. }
  118. $list = $newList;
  119. }
  120. return $list;
  121. }
  122. /**
  123. * 获取商品标签表单
  124. * @param int $type
  125. * @param int $relation_id
  126. * @return mixed
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\DbException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. */
  131. public function getLabelForm(int $type = 0, int $relation_id = 0)
  132. {
  133. /** @var StoreProductLabelCateServices $service */
  134. $service = app()->make(StoreProductLabelCateServices::class);
  135. $options = $service->getAllProductLabelCate($type, $relation_id);
  136. $data = [];
  137. foreach ($options as $option) {
  138. $data[] = ['label' => $option['name'], 'value' => $option['id']];
  139. }
  140. $rule = [
  141. FormBuilder::select('label_cate', '标签分组')->options($data),
  142. FormBuilder::input('label_name', '标签名称')->maxlength(20),
  143. ];
  144. return create_form('添加商品标签', $rule, Url::buildUrl('/product/label/0'), 'POST');
  145. }
  146. }