SystemConfigValueDao.php 2.5 KB

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