SystemConfigValueDao.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\dao\system\config;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\config\SystemConfigValue;
  15. use think\db\exception\DbException;
  16. /**
  17. * Class SystemConfigValueDao
  18. * @package app\common\dao\system\config
  19. * @author xaboy
  20. * @day 2020-03-27
  21. */
  22. class SystemConfigValueDao extends BaseDao
  23. {
  24. /**
  25. * @return BaseModel
  26. * @author xaboy
  27. * @day 2020-03-30
  28. */
  29. protected function getModel(): string
  30. {
  31. return SystemConfigValue::class;
  32. }
  33. /**
  34. * @param int $merId
  35. * @param string $key
  36. * @param array $data
  37. * @return int
  38. * @throws DbException
  39. * @author xaboy
  40. * @day 2020-03-27
  41. */
  42. public function merUpdate(int $merId, string $key, array $data)
  43. {
  44. if (isset($data['value'])) $data['value'] = json_encode($data['value']);
  45. return SystemConfigValue::getDB()->where('mer_id', $merId)->where('config_key', $key)->update($data);
  46. }
  47. /**
  48. * @param array $keys
  49. * @param int $merId
  50. * @return array
  51. * @author xaboy
  52. * @day 2020-04-22
  53. */
  54. public function fields(array $keys, int $merId)
  55. {
  56. $result = SystemConfigValue::getDB()->whereIn('config_key', $keys)->where('mer_id', $merId)->withAttr('value', function ($val, $data) {
  57. return json_decode($val, true);
  58. })->column('value', 'config_key');
  59. foreach ($result as $k => $val) {
  60. $result[$k] = json_decode($val, true);
  61. }
  62. return $result;
  63. }
  64. /**
  65. * @param array $keys
  66. * @param int $merId
  67. * @return int
  68. * @throws DbException
  69. * @author xaboy
  70. * @day 2020-05-18
  71. */
  72. public function clear(array $keys, int $merId)
  73. {
  74. return SystemConfigValue::getDB()->whereIn('config_key', $keys)->where('mer_id', $merId)->delete();
  75. }
  76. /**
  77. * @param string $key
  78. * @param int $merId
  79. * @return mixed|null
  80. * @author xaboy
  81. * @day 2020-05-08
  82. */
  83. public function value(string $key, int $merId)
  84. {
  85. $value = SystemConfigValue::getDB()->where('config_key', $key)->where('mer_id', $merId)->value('value');
  86. $value = is_null($value) ? null : json_decode($value, true);
  87. return $value;
  88. }
  89. /**
  90. * @param string $key
  91. * @param int $merId
  92. * @return bool
  93. * @author xaboy
  94. * @day 2020-03-27
  95. */
  96. public function merExists(string $key, int $merId): bool
  97. {
  98. return SystemConfigValue::getDB()->where('config_key', $key)->where('mer_id', $merId)->count() > 0;
  99. }
  100. }