SystemCrudDataService.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\dao\system\SystemCrudDataDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\FormBuilder as Form;
  16. use think\facade\Route as Url;
  17. /**
  18. * Class SystemCrudDataService
  19. * @author 等风来
  20. * @email 136327134@qq.com
  21. * @date 2023/7/28
  22. * @package app\services\system
  23. */
  24. class SystemCrudDataService extends BaseServices
  25. {
  26. /**
  27. * SystemCrudDataService constructor.
  28. * @param SystemCrudDataDao $dao
  29. */
  30. public function __construct(SystemCrudDataDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取全部数据
  36. * @return array
  37. * @throws \ReflectionException
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @author 等风来
  42. * @email 136327134@qq.com
  43. * @date 2023/8/1
  44. */
  45. public function getlistAll(string $name = '')
  46. {
  47. [$page, $limit] = $this->getPageValue();
  48. $list = $this->dao->selectList(['name' => $name], '*', $page, $limit, '', [], true)->toArray();
  49. $count = $this->dao->count(['name' => $name]);
  50. if ($page && $limit) {
  51. return compact('list', 'count');
  52. } else {
  53. return $list;
  54. }
  55. }
  56. /**
  57. * 获取数据字典列表
  58. * @param $cid
  59. * @return array
  60. * @throws \ReflectionException
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @author wuhaotian
  65. * @email 442384644@qq.com
  66. * @date 2024/5/20
  67. */
  68. public function dataDictionaryInfoList($cid)
  69. {
  70. $level = app()->make(SystemCrudListServices::class)->value($cid, 'level');
  71. if ($level == 0) {
  72. [$page, $limit] = $this->getPageValue();
  73. $list = $this->dao->selectList(['cid' => $cid], '*', $page, $limit, 'sort desc')->toArray();
  74. foreach ($list as &$item) {
  75. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  76. }
  77. } else {
  78. $list = $this->fullListTree($this->dao->selectList(['cid' => $cid], '*', 0, 0, 'sort desc')->toArray());
  79. }
  80. $count = $this->dao->count(['cid' => $cid]);
  81. return compact('list', 'count');
  82. }
  83. /**
  84. * 格式化获取数据字典列表
  85. * @param $data
  86. * @param int $pid
  87. * @param array $navList
  88. * @return array|mixed
  89. * @author wuhaotian
  90. * @email 442384644@qq.com
  91. * @date 2024/5/20
  92. */
  93. function fullListTree($data, $pid = 0, $navList = [])
  94. {
  95. foreach ($data as $k => $item) {
  96. if ($item['pid'] == $pid) {
  97. unset($item['pid']);
  98. unset($data[$k]);
  99. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  100. $item['children'] = $this->fullListTree($data, $item['id']);
  101. if (!count($item['children'])) unset($item['children']);
  102. $navList[] = $item;
  103. }
  104. }
  105. return $navList;
  106. }
  107. /**
  108. * 数据字典内容添加修改表单
  109. * @param $cid
  110. * @param int $id
  111. * @param int $pid
  112. * @return array
  113. * @throws \FormBuilder\Exception\FormBuilderException
  114. * @throws \ReflectionException
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\DbException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. * @author wuhaotian
  119. * @email 442384644@qq.com
  120. * @date 2024/5/20
  121. */
  122. public function dataDictionaryInfoCreate($cid, int $id = 0, int $pid = 0)
  123. {
  124. $info = $this->dao->get($id);
  125. $field = [];
  126. $level = app()->make(SystemCrudListServices::class)->value(['id' => $cid], 'level');
  127. if ($level == 1) {
  128. $dataList = $this->dao->selectList(['cid' => $cid], 'id as value,name as label,pid')->toArray();
  129. if (isset($info['pid']) && $info['pid']) {
  130. $data = get_tree_value($dataList, $info['pid']);
  131. } else {
  132. $data = [0];
  133. }
  134. if ($pid) {
  135. $data = get_tree_value($dataList, $pid);
  136. }
  137. $dataList = get_tree_children($dataList, 'children', 'value');
  138. array_unshift($dataList, ['value' => 0, 'pid' => 0, 'label' => '顶级']);
  139. $field[] = Form::cascader('pid', '上级', array_reverse($data))->options($dataList)->filterable(true)->props(['props' => ['multiple' => false, 'checkStrictly' => true, 'emitPath' => true]]);
  140. } else {
  141. $field[] = Form::hidden('pid', 0);
  142. }
  143. $field[] = Form::input('name', '名称', $info['name'] ?? '')->required();
  144. $count = $this->dao->count(['cid' => $cid]);
  145. $field[] = Form::input('value', '值', $info['value'] ?? $count)->required();
  146. $field[] = Form::input('sort', '排序', $info['sort'] ?? 0)->required();
  147. return create_form($id ? '编辑' : '新增', $field, Url::buildUrl('/system/crud/data_dictionary/info_save/' . $cid . '/' . $id), 'POST');
  148. }
  149. /**
  150. * 数据字典内容添加修改
  151. * @param $cid
  152. * @param $id
  153. * @param $data
  154. * @return bool
  155. * @author wuhaotian
  156. * @email 442384644@qq.com
  157. * @date 2024/5/20
  158. */
  159. public function dataDictionaryInfoSave($cid, $id, $data)
  160. {
  161. if (is_array($data['pid'])) $data['pid'] = end($data['pid']);
  162. if ($id) {
  163. $this->dao->update($id, $data);
  164. } else {
  165. $data['cid'] = $cid;
  166. $data['add_time'] = time();
  167. $this->dao->save($data);
  168. }
  169. return true;
  170. }
  171. /**
  172. * 数据字典内容删除
  173. * @param $id
  174. * @return bool
  175. * @throws \ReflectionException
  176. * @author wuhaotian
  177. * @email 442384644@qq.com
  178. * @date 2024/5/20
  179. */
  180. public function dataDictionaryInfoDel($id)
  181. {
  182. $count = $this->dao->count(['pid' => $id]);
  183. if ($count) {
  184. throw new AdminException('请先删除子级');
  185. }
  186. $this->dao->delete($id);
  187. return true;
  188. }
  189. }