DictService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的多应用管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\dict;
  12. use app\model\dict\Dict;
  13. use core\base\BaseAdminService;
  14. /**
  15. * 数据字典服务层
  16. * Class DictService
  17. * @package app\service\admin\dict
  18. */
  19. class DictService extends BaseAdminService
  20. {
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->model = new Dict();
  25. }
  26. /**
  27. * 获取数据字典列表
  28. * @param array $where
  29. * @return array
  30. */
  31. public function getPage(array $where = [])
  32. {
  33. $field = 'id,name,key,memo,create_time,update_time';
  34. $order = 'id desc';
  35. $search_model = $this->model->withSearch(["name","key"], $where)->field($field)->order($order);
  36. $list = $this->pageQuery($search_model);
  37. return $list;
  38. }
  39. /**
  40. * 获取数据字典信息
  41. * @param int $id
  42. * @return array
  43. */
  44. public function getInfo(int $id)
  45. {
  46. $field = 'id,name,key,dictionary,memo,create_time,update_time';
  47. $info = $this->model->field($field)->where([['id', '=', $id]])->findOrEmpty()->toArray();
  48. if($info['dictionary'] == null)
  49. {
  50. $info['dictionary'] = [];
  51. }
  52. return $info;
  53. }
  54. /**
  55. * 添加数据字典
  56. * @param array $data
  57. * @return mixed
  58. */
  59. public function add(array $data)
  60. {
  61. $res = $this->model->create($data);
  62. return $res->id;
  63. }
  64. /**
  65. * 数据字典编辑
  66. * @param int $id
  67. * @param array $data
  68. * @return bool
  69. */
  70. public function edit(int $id, array $data)
  71. {
  72. $data['update_time'] = time();
  73. $this->model->where([['id', '=', $id]])->update($data);
  74. return true;
  75. }
  76. /**
  77. * 删除数据字典
  78. * @param int $id
  79. * @return bool
  80. */
  81. public function del(int $id)
  82. {
  83. $res = $this->model->where([['id', '=', $id]])->delete();
  84. return $res;
  85. }
  86. /**
  87. * 获取全部数据字典
  88. * @param array $where
  89. * @return array
  90. */
  91. public function getAll()
  92. {
  93. $field = 'id,name,key,dictionary,memo,create_time,update_time';
  94. $list = $this->model->field($field)->select()->toArray();
  95. return $list;
  96. }
  97. public function getKeyInfo($key)
  98. {
  99. $field = 'id,name,key,dictionary,memo,create_time,update_time';
  100. $info = $this->model->field($field)->where([['key', '=', $key]])->findOrEmpty()->toArray();
  101. if($info['dictionary'] == null)
  102. {
  103. $info['dictionary'] = [];
  104. }
  105. return $info;
  106. }
  107. }