ConfigValueRepository.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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\common\repositories\system\config;
  12. use app\common\dao\system\config\SystemConfigValueDao;
  13. use app\common\repositories\BaseRepository;
  14. use think\exception\ValidateException;
  15. use think\facade\Db;
  16. /**
  17. * Class ConfigValueRepository
  18. * @package app\common\repositories\system\config
  19. * @mixin SystemConfigValueDao
  20. */
  21. class ConfigValueRepository extends BaseRepository
  22. {
  23. /**
  24. * ConfigValueRepository constructor.
  25. * @param SystemConfigValueDao $dao
  26. */
  27. public function __construct(SystemConfigValueDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * @param array $keys
  33. * @param int $merId
  34. * @return array
  35. * @author xaboy
  36. * @day 2020-03-27
  37. */
  38. public function more(array $keys, int $merId): array
  39. {
  40. $config = $this->dao->fields($keys, $merId);
  41. foreach ($keys as $key) {
  42. if (!isset($config[$key])) $config[$key] = '';
  43. }
  44. return $config;
  45. }
  46. /**
  47. * @param string $key
  48. * @param int $merId
  49. * @return mixed|string|null
  50. * @author xaboy
  51. * @day 2020-05-08
  52. */
  53. public function get(string $key, int $merId)
  54. {
  55. $value = $this->dao->value($key, $merId);
  56. return $value ?? '';
  57. }
  58. /**
  59. * @param int $cid
  60. * @param array $formData
  61. * @param int $merId
  62. * @author xaboy
  63. * @day 2020-03-27
  64. */
  65. public function save(int $cid, array $formData, int $merId)
  66. {
  67. $keys = array_keys($formData);
  68. $keys = app()->make(ConfigRepository::class)->intersectionKey($cid, $keys);
  69. if (!count($keys)) return;
  70. foreach ($keys as $key => $info) {
  71. if (!isset($formData[$key]))
  72. unset($formData[$key]);
  73. else {
  74. if ($info['config_type'] == 'number') {
  75. if ($formData[$key] === '' || $formData[$key] < 0)
  76. throw new ValidateException($info['config_name'] . '不能小于0');
  77. $formData[$key] = floatval($formData[$key]);
  78. }
  79. }
  80. }
  81. $this->setFormData($formData, $merId);
  82. }
  83. public function setFormData(array $formData, int $merId)
  84. {
  85. Db::transaction(function () use ($merId, $formData) {
  86. foreach ($formData as $key => $value) {
  87. if ($this->dao->merExists($key, $merId))
  88. $this->dao->merUpdate($merId, $key, ['value' => $value]);
  89. else
  90. $this->dao->create([
  91. 'mer_id' => $merId,
  92. 'value' => $value,
  93. 'config_key' => $key
  94. ]);
  95. }
  96. });
  97. }
  98. }