StoreProductRuleServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\sku;
  12. use app\dao\product\sku\StoreProductRuleDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. /**
  16. * Class StoreProductRuleService
  17. * @package app\services\product\sku
  18. * @mixin StoreProductRuleDao
  19. */
  20. class StoreProductRuleServices extends BaseServices
  21. {
  22. public function __construct(StoreProductRuleDao $dao)
  23. {
  24. $this->dao = $dao;
  25. }
  26. /**
  27. * 获取商品规格列表
  28. * @param array $where
  29. * @return array
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function getList(array $where = [])
  35. {
  36. [$page, $limit] = $this->getPageValue();
  37. $list = $this->dao->getList($where, $page, $limit);
  38. foreach ($list as &$item) {
  39. $attr_name = $attr_value = [];
  40. if ($item['rule_value']) {
  41. $specs = json_decode($item['rule_value'], true);
  42. if ($specs) {
  43. foreach ($specs as $key => $value) {
  44. $attr_name[] = $value['value'];
  45. $attr_value[] = implode(',', $value['detail']);
  46. }
  47. } else {
  48. $attr_name[] = '';
  49. $attr_value[] = '';
  50. }
  51. $item['attr_name'] = implode(',', $attr_name);
  52. $item['attr_value'] = $attr_value;
  53. }
  54. }
  55. $count = $this->dao->count($where);
  56. return compact('list', 'count');
  57. }
  58. /**
  59. * 保存数据
  60. * @param int $id
  61. * @param array $data
  62. * @param int $type
  63. * @param int $relation_id
  64. * @return void
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function save(int $id, array $data, int $type = 0, int $relation_id = 0)
  70. {
  71. $data['type'] = $type;
  72. $data['relation_id'] = $relation_id;
  73. $data['rule_value'] = json_encode($data['spec']);
  74. unset($data['spec']);
  75. $rule = $this->dao->getOne(['rule_name' => $data['rule_name'], 'type' => $type, 'relation_id' => $relation_id]);
  76. if ($id) {
  77. if ($rule && $rule['id'] != $id) {
  78. throw new AdminException('分类名称已存在');
  79. }
  80. $res = $this->dao->update($id, $data);
  81. } else {
  82. if ($rule) {
  83. throw new AdminException('分类名称已存在');
  84. }
  85. $res = $this->dao->save($data);
  86. }
  87. if (!$res) throw new AdminException('保存失败');
  88. }
  89. /**
  90. * 获取一条数据
  91. * @param int $id
  92. * @return array
  93. */
  94. public function getInfo(int $id)
  95. {
  96. $info = $this->dao->get($id);
  97. $info['spec'] = json_decode($info['rule_value'], true);
  98. return compact('info');
  99. }
  100. /**
  101. * 删除数据
  102. * @param string $ids
  103. */
  104. public function del(string $ids)
  105. {
  106. if ($ids == '') throw new AdminException('请至少选择一条数据');
  107. $this->dao->del($ids);
  108. }
  109. /**
  110. * @param array $where
  111. * @param string $field
  112. * @return array
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function getProductRuleList(array $where, $field = "*")
  118. {
  119. return $this->dao->getProductRuleList($where, $field);
  120. }
  121. }