StoreProductRuleServices.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. */
  19. class StoreProductRuleServices extends BaseServices
  20. {
  21. public function __construct(StoreProductRuleDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. /**
  26. * 获取商品规格列表
  27. * @param array $where
  28. * @return array
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function getList(array $where = [])
  34. {
  35. [$page, $limit] = $this->getPageValue();
  36. $list = $this->dao->getList($where, $page, $limit);
  37. foreach ($list as &$item) {
  38. $attr_name = $attr_value = [];
  39. if ($item['rule_value']) {
  40. $specs = json_decode($item['rule_value'], true);
  41. if ($specs) {
  42. foreach ($specs as $key => $value) {
  43. $attr_name[] = $value['value'] ?? '';
  44. $attr_value[] = implode(',', $value['detail'] ?? '');
  45. }
  46. } else {
  47. $attr_name[] = '';
  48. $attr_value[] = '';
  49. }
  50. $item['attr_name'] = implode(',', $attr_name);
  51. $item['attr_value'] = $attr_value;
  52. }
  53. }
  54. $count = $this->dao->count($where);
  55. return compact('list', 'count');
  56. }
  57. /**
  58. * 保存数据
  59. * @param int $id
  60. * @param array $data
  61. */
  62. public function save(int $id, array $data)
  63. {
  64. $data['rule_value'] = json_encode($data['spec']);
  65. unset($data['spec']);
  66. if ($id) {
  67. $res = $this->dao->update($id, $data);
  68. } else {
  69. $res = $this->dao->save($data);
  70. }
  71. if (!$res) throw new AdminException(100006);
  72. }
  73. /**
  74. * 获取一条数据
  75. * @param int $id
  76. * @return array
  77. */
  78. public function getInfo(int $id)
  79. {
  80. $info = $this->dao->get($id);
  81. $info['spec'] = json_decode($info['rule_value'], true);
  82. return compact('info');
  83. }
  84. /**
  85. * 删除数据
  86. * @param string $ids
  87. */
  88. public function del(string $ids)
  89. {
  90. if ($ids == '') throw new AdminException(100100);
  91. $this->dao->del($ids);
  92. }
  93. }