StoreProductSpecsServices.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\specs;
  12. use app\dao\product\specs\StoreProductSpecsDao;
  13. use app\services\BaseServices;
  14. use think\exception\ValidateException;
  15. /**
  16. * 商品参数
  17. * Class StoreProductSpecsServices
  18. * @package app\services\product\ensure
  19. * @mixin StoreProductSpecsDao
  20. */
  21. class StoreProductSpecsServices extends BaseServices
  22. {
  23. /**
  24. * 商品参数字段
  25. * @var array
  26. */
  27. protected $specs = [
  28. 'id' => 0,
  29. 'temp_id' => 0,
  30. 'name' => '',
  31. 'value' => '',
  32. 'sort' => 0
  33. ];
  34. /**
  35. * StoreProductSpecsServices constructor.
  36. * @param StoreProductSpecsDao $dao
  37. */
  38. public function __construct(StoreProductSpecsDao $dao)
  39. {
  40. $this->dao = $dao;
  41. }
  42. /**
  43. * 获取参数模版列表(带参数)
  44. * @param array $where
  45. * @return array
  46. */
  47. public function getSpecsTemplateList(array $where)
  48. {
  49. [$page, $limit] = $this->getPageValue();
  50. $list = $this->dao->getList($where, '*', $page, $limit);
  51. $count = $this->dao->count($where);
  52. return compact('list', 'count');
  53. }
  54. /**
  55. * 检测
  56. * @param array $data
  57. * @return array
  58. */
  59. public function checkSpecsData(array $data)
  60. {
  61. $data = array_merge($this->specs, array_intersect_key($data, $this->specs));
  62. if (!isset($data['name']) || !$data['name']) {
  63. throw new ValidateException('请填写参数名称');
  64. }
  65. if (!isset($data['value']) || !$data['value']) {
  66. throw new ValidateException('请填写参数值');
  67. }
  68. return $data;
  69. }
  70. /**
  71. * 修改参数模版(商品参数)
  72. * @param int $id
  73. * @param array $specsArr
  74. * @param int $type
  75. * @param int $relation_id
  76. * @return bool
  77. */
  78. public function updateData(int $id, array $specsArr, int $type = 0, int $relation_id = 0)
  79. {
  80. $this->dao->delete(['temp_id' => $id]);
  81. $insert = [];
  82. $time = time();
  83. foreach ($specsArr as $specs) {
  84. $specs = $this->checkSpecsData($specs);
  85. $specs['type'] = $type;
  86. $specs['relation_id'] = $relation_id;
  87. $specs['temp_id'] = $id;
  88. if (isset($specs['id'])) {
  89. unset($specs['id']);
  90. }
  91. $specs['add_time'] = $time;
  92. $insert[] = $specs;
  93. }
  94. if ($insert) {
  95. if (!$this->dao->saveAll($insert)) {
  96. throw new ValidateException('新增商品参数失败');
  97. }
  98. }
  99. return true;
  100. }
  101. /**
  102. * 保存参数模版(商品参数)
  103. * @param int $id
  104. * @param array $specsArr
  105. * @param int $type
  106. * @param int $relation_id
  107. * @return bool
  108. */
  109. public function saveData(int $id, array $specsArr, int $type = 0, int $relation_id = 0)
  110. {
  111. if (!$specsArr) return true;
  112. $dataAll = [];
  113. $time = time();
  114. foreach ($specsArr as $specs) {
  115. $specs = $this->checkSpecsData($specs);
  116. $specs['type'] = $type;
  117. $specs['relation_id'] = $relation_id;
  118. $specs['temp_id'] = $id;
  119. $specs['add_time'] = $time;
  120. $dataAll[] = $specs;
  121. }
  122. if ($dataAll) {
  123. $this->dao->saveAll($dataAll);
  124. }
  125. return true;
  126. }
  127. }