ParameterValueRepository.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\store\parameter;
  12. use think\exception\ValidateException;
  13. use app\common\dao\store\parameter\ParameterValueDao;
  14. use app\common\repositories\BaseRepository;
  15. use think\facade\Db;
  16. class ParameterValueRepository extends BaseRepository
  17. {
  18. /**
  19. * @var ParameterValueDao
  20. */
  21. protected $dao;
  22. /**
  23. * ParameterRepository constructor.
  24. * @param ParameterValueDao $dao
  25. */
  26. public function __construct(ParameterValueDao $dao)
  27. {
  28. $this->dao = $dao;
  29. }
  30. /**
  31. * 创建产品参数
  32. *
  33. * 该方法用于根据提供的产品ID和参数数据集,创建产品的参数信息。
  34. * 参数数据集中的每个数据项都应该包含名称、值,以及可选的排序、参数ID和商家ID。
  35. * 如果数据项有效(即名称和值都不为空),则将其组装成参数记录并准备插入数据库。
  36. *
  37. * @param int $id 产品ID,用于关联产品参数。
  38. * @param array $data 参数数据集,包含多个参数项,每个参数项是一个包含名称、值等信息的数组。
  39. * @param int $merId 商家ID,用于指定参数所属的商家,如果数据中没有提供,则使用此默认值。
  40. */
  41. public function create($data, $productId = 0, $merId = 0)
  42. {
  43. // 检查数据集是否为空,如果为空则直接返回,不进行后续处理
  44. if (empty($data)) return ;
  45. // 遍历数据集中的每个参数项
  46. $toProduct = [];
  47. foreach ($data as &$datum) {
  48. //$values = $datum['value'] ?? $datum['values'];
  49. /**
  50. * 有parameter_id,是存在的模板中增加值
  51. * 不存在则是新增的商品独有的属性
  52. */
  53. foreach ($datum['values'] as &$v) {
  54. if (!isset($v['parameter_value_id'])) {
  55. $createData = [
  56. 'name' => $datum['name'],
  57. 'value' => $v['value'],
  58. 'mer_id' => $merId,
  59. //如果是存在$datum['parameter_id'],则表示这个属性是模板中定义的 不需要和商品绑定
  60. 'product_id' => isset($datum['parameter_id']) ? 0 : $productId,
  61. 'parameter_id' => $datum['parameter_id'] ?? 0,
  62. ];
  63. $create = $this->dao->create($createData);
  64. $v['parameter_value_id'] = $create->parameter_value_id;
  65. } else {
  66. $this->update($v['parameter_value_id'], ['value' => $v['value'], 'name' => $datum['name']]);
  67. }
  68. if ($productId){
  69. $toProduct[] = [
  70. 'product_id' => $productId,
  71. 'parameter_value_id' => $v['parameter_value_id']
  72. ];
  73. }
  74. }
  75. }
  76. // 如果有有效的参数记录,则插入到数据库
  77. if($toProduct){
  78. $productRepository = app()->make(ParameterProductRepository::class);
  79. $productRepository->clear(array_column($toProduct,'product_id'),'product_id');
  80. $productRepository->insertAll($toProduct);
  81. }
  82. return $data;
  83. }
  84. /**
  85. * 获取所有参数的值,并合并所关联的商品ID
  86. * @param $where
  87. * @return array
  88. * @author Qinii
  89. * @day 2023/10/21
  90. */
  91. public function getOptions($where)
  92. {
  93. $data = $this->dao->getSearch($where)->column('parameter_value_id,parameter_id,name,value');
  94. return $data;
  95. }
  96. public function dostory($id, $merId)
  97. {
  98. $parameterRepository = app()->make(ParameterRepository::class);
  99. $parameterProductRepository = app()->make(ParameterProductRepository::class);
  100. $ids = [];
  101. foreach ($id as $i) {
  102. $data = $parameterRepository->getWhere(['parameter_id' => $i, 'mer_id' => $merId]);
  103. if ($data) {$ids[] = $i;}
  104. }
  105. if ($ids) {
  106. return Db::transaction(function () use ($ids,$parameterRepository,$parameterProductRepository) {
  107. $parameterRepository->getSearch([])->whereIn('parameter_id',$ids)->delete();
  108. $query = $this->dao->getSearch(['parameter_id' => $ids]);
  109. $parameter_value_id = $query->column('parameter_value_id');
  110. $parameterProductRepository->clear($parameter_value_id,'parameter_value_id');
  111. $query->delete();
  112. });
  113. }
  114. }
  115. /**
  116. * 根据筛选的参数,查询出商品ID
  117. * @param $filter_params
  118. * @param $page
  119. * @param $limit
  120. * @return array
  121. * @author Qinii
  122. * @day 2023/11/14
  123. */
  124. public function filter_params($filter_params)
  125. {
  126. $productId = [];
  127. if (!empty($filter_params)) {
  128. if (!is_array($filter_params)) $filter_params = json_decode($filter_params,true);
  129. $value = [];
  130. foreach ($filter_params as $k => $v) {
  131. $id[] = $k;
  132. $value = array_merge($value,$v);
  133. }
  134. if (empty($id) || empty($value)) return false;
  135. $productData = $this->dao->getSearch([])->alias('P')
  136. ->join('ParameterValue V','P.product_id = V.product_id')
  137. ->whereIn('P.parameter_id',$id)->whereIn('P.value',$value)
  138. ->whereIn('V.parameter_id',$id)->whereIn('V.value',$value)
  139. ->group('P.product_id')
  140. ->field('P.product_id')
  141. ->select();
  142. if ($productData) {
  143. $productData = $productData->toArray();
  144. $productId = array_column($productData,'product_id');
  145. }
  146. }
  147. return $productId;
  148. }
  149. }