ConfigValueRepository.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\system\config;
  12. use app\common\dao\system\config\SystemConfigValueDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use app\common\repositories\system\groupData\GroupDataRepository;
  16. use app\common\repositories\system\groupData\GroupRepository;
  17. use crmeb\jobs\SyncProductTopJob;
  18. use crmeb\services\DownloadImageService;
  19. use think\exception\ValidateException;
  20. use think\facade\Db;
  21. use think\facade\Queue;
  22. /**
  23. * Class ConfigValueRepository
  24. * @package app\common\repositories\system\config
  25. * @mixin SystemConfigValueDao
  26. */
  27. class ConfigValueRepository extends BaseRepository
  28. {
  29. /**
  30. * ConfigValueRepository constructor.
  31. * @param SystemConfigValueDao $dao
  32. */
  33. public function __construct(SystemConfigValueDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * @param array $keys
  39. * @param int $merId
  40. * @return array
  41. * @author xaboy
  42. * @day 2020-03-27
  43. */
  44. public function more(array $keys, int $merId): array
  45. {
  46. $config = $this->dao->fields($keys, $merId);
  47. foreach ($keys as $key) {
  48. if (!isset($config[$key])) $config[$key] = '';
  49. }
  50. return $config;
  51. }
  52. /**
  53. * @param string $key
  54. * @param int $merId
  55. * @return mixed|string|null
  56. * @author xaboy
  57. * @day 2020-05-08
  58. */
  59. public function get(string $key, int $merId)
  60. {
  61. $value = $this->dao->value($key, $merId);
  62. return $value ?? '';
  63. }
  64. /**
  65. * @param int|array $cid
  66. * @param array $formData
  67. * @param int $merId
  68. * @author xaboy
  69. * @day 2020-03-27
  70. */
  71. public function save($cid, array $formData, int $merId)
  72. {
  73. $keys = array_keys($formData);
  74. $keys = app()->make(ConfigRepository::class)->intersectionKey($cid, $keys);
  75. if (!count($keys)) return;
  76. foreach ($keys as $key => $info) {
  77. if (!isset($formData[$key]))
  78. unset($formData[$key]);
  79. else {
  80. if ($info['config_type'] == 'number') {
  81. if ($formData[$key] === '' || $formData[$key] < 0)
  82. throw new ValidateException($info['config_name'] . '不能小于0');
  83. $formData[$key] = floatval($formData[$key]);
  84. }
  85. $this->separate($key,$formData[$key],$merId);
  86. }
  87. }
  88. $this->setFormData($formData, $merId);
  89. }
  90. /**
  91. * TODO 需要做特殊处理的配置参数
  92. * @param $key
  93. * @author Qinii
  94. * @day 2022/11/17
  95. */
  96. public function separate($key,$value,$merId)
  97. {
  98. switch($key) {
  99. case 'mer_svip_status':
  100. //修改商户的会员状态
  101. app()->make(ProductRepository::class)->getSearch([])->where(['mer_id' => $merId,'product_type' => 0])->update([$key => $value]);
  102. break;
  103. // case 'site_ico':
  104. // //修改ico图标
  105. // $stie_ico = systemConfig('site_ico');
  106. // $ico = substr($value,-3);
  107. // if ($stie_ico != $value && $ico != 'ico') {
  108. // $path = app()->make(DownloadImageService::class)->downloadImage($value,'def','favicon.ico',1)['path'];
  109. // $value = public_path().$path;
  110. // if (!is_file($value)) throw new ValidateException('Ico图标文件不存在');
  111. // rename($value, public_path() . 'favicon.ico');
  112. // }
  113. // break;
  114. //热卖排行
  115. case 'hot_ranking_switch':
  116. if ($value) {
  117. Queue::push(SyncProductTopJob::class, []);
  118. }
  119. break;
  120. case 'svip_switch_status':
  121. if ($value == 1) {
  122. $groupDataRepository = app()->make(GroupDataRepository::class);
  123. $groupRepository = app()->make(GroupRepository::class);
  124. $group_id = $groupRepository->getSearch(['group_key' => 'svip_pay'])->value('group_id');
  125. $where['group_id'] = $group_id;
  126. $where['status'] = 1;
  127. $count = $groupDataRepository->getSearch($where)->field('group_data_id,value,sort,status')->count();
  128. if (!$count)
  129. throw new ValidateException('请先添加会员类型');
  130. }
  131. break;
  132. default:
  133. break;
  134. }
  135. return ;
  136. }
  137. public function setFormData(array $formData, int $merId)
  138. {
  139. Db::transaction(function () use ($merId, $formData) {
  140. foreach ($formData as $key => $value) {
  141. if ($this->dao->merExists($key, $merId))
  142. $this->dao->merUpdate($merId, $key, ['value' => $value]);
  143. else
  144. $this->dao->create([
  145. 'mer_id' => $merId,
  146. 'value' => $value,
  147. 'config_key' => $key
  148. ]);
  149. }
  150. });
  151. }
  152. }