SystemConfigValueDao.php 2.6 KB

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