SystemCrudDataService.php 6.7 KB

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