ConfigValueRepository.php 2.3 KB

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