GoodsCategoryService.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\GoodsCategoryModel;
  13. use App\Services\BaseService;
  14. /**
  15. * 商品分类管理-服务类
  16. * @author laravel开发员
  17. * @since 2020/11/11
  18. * @package App\Services\Common
  19. */
  20. class GoodsCategoryService extends BaseService
  21. {
  22. /**
  23. * 构造函数
  24. * @author laravel开发员
  25. * @since 2020/11/11
  26. */
  27. public function __construct()
  28. {
  29. $this->model = new GoodsCategoryModel();
  30. }
  31. /**
  32. * 获取数据列表
  33. * @param array $params
  34. * @param int $pageSize
  35. * @return array
  36. */
  37. public function getDataList($params, $pageSize = 15)
  38. {
  39. // 分页查询
  40. $list = $this->model->orderBy('sort', 'desc')
  41. ->where('mark', 1)
  42. ->where(function ($query) use ($params) {
  43. // 分类名称
  44. if (isset($params['name']) && $params['name']) {
  45. $query->where('name', 'like', "%{$params['name']}%");
  46. }
  47. // 状态筛选
  48. if (isset($params['status']) && $params['status'] !== null && $params['status'] !== '') {
  49. $query->where('status', intval($params['status']));
  50. }
  51. })
  52. ->orderBy('id', 'asc')
  53. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  54. $list = $list->toArray();
  55. // 格式化数据
  56. if (isset($list['data']) && !empty($list['data'])) {
  57. foreach ($list['data'] as &$val) {
  58. $val['create_time'] = $val['create_time'] ? datetime($val['create_time'], 'Y-m-d H:i:s') : '';
  59. // 处理图标
  60. if (isset($val['icon'])) {
  61. $val['icon'] = get_image_url($val['icon']);
  62. }
  63. }
  64. }
  65. return [
  66. 'msg' => '操作成功',
  67. 'code' => 0,
  68. 'data' => $list['data'],
  69. 'count' => $list['total']
  70. ];
  71. }
  72. /**
  73. * 获取分类详情
  74. * @return array
  75. * @since 2020/11/11
  76. * @author laravel开发员
  77. */
  78. public function info()
  79. {
  80. // 记录ID
  81. $id = request()->input("id", 0);
  82. $info = [];
  83. if ($id) {
  84. $info = $this->model->getInfo($id);
  85. if ($info) {
  86. if (isset($info['create_time'])) {
  87. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  88. }
  89. // 处理图标
  90. if (isset($info['icon'])) {
  91. $info['icon'] = get_image_url($info['icon']);
  92. }
  93. }
  94. }
  95. return message(MESSAGE_OK, true, $info);
  96. }
  97. /**
  98. * 添加或编辑
  99. * @return array
  100. * @since 2020/11/11
  101. * @author laravel开发员
  102. */
  103. public function edit()
  104. {
  105. // 参数
  106. $param = request()->all();
  107. // 验证分类名称
  108. if (empty($param['name'])) {
  109. $this->error = '分类名称不能为空';
  110. return false;
  111. }
  112. // 如果 pid 为 0,表示一级分类
  113. if (!isset($param['pid'])) {
  114. $param['pid'] = 0;
  115. }
  116. // 设置默认值
  117. if (!isset($param['status'])) {
  118. $param['status'] = 1; // 默认有效
  119. }
  120. if (!isset($param['sort'])) {
  121. $param['sort'] = 0; // 默认排序为0
  122. }
  123. if (!isset($param['remark'])) {
  124. $param['remark'] = ''; // 默认备注为空
  125. }
  126. if (!isset($param['icon'])) {
  127. $param['icon'] = ''; // 默认图标为空
  128. }
  129. // 处理图标
  130. if (isset($param['icon'])) {
  131. $param['icon'] = get_image_path($param['icon']);
  132. }
  133. // 保存数据
  134. $result = $this->model->edit($param);
  135. if (!$result) {
  136. $this->error = '操作失败';
  137. return false;
  138. }
  139. return true;
  140. }
  141. /**
  142. * 设置状态
  143. * @param array $params
  144. * @return bool
  145. */
  146. public function setStatus($params)
  147. {
  148. $id = isset($params['id']) ? intval($params['id']) : 0;
  149. $status = isset($params['status']) ? intval($params['status']) : 1;
  150. if (!$id) {
  151. $this->error = '分类ID不能为空';
  152. return false;
  153. }
  154. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  155. if (!$info) {
  156. $this->error = '分类信息不存在';
  157. return false;
  158. }
  159. // 更新状态
  160. $this->model->where('id', $id)->update([
  161. 'status' => $status,
  162. 'update_time' => time()
  163. ]);
  164. $this->error = $status == 1 ? '启用成功' : '禁用成功';
  165. return true;
  166. }
  167. /**
  168. * 删除(重写父类方法)
  169. * @return array
  170. */
  171. public function delete()
  172. {
  173. // 参数
  174. $param = request()->all();
  175. // 记录ID
  176. $id = getter($param, "id");
  177. if (empty($id)) {
  178. return message("分类ID不能为空", false);
  179. }
  180. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  181. if (!$info) {
  182. return message("分类信息不存在", false);
  183. }
  184. // 检查是否有子分类
  185. $childCount = $this->model->where(['pid' => $id, 'mark' => 1])->count();
  186. if ($childCount > 0) {
  187. return message("存在子分类,无法删除", false);
  188. }
  189. // 检查是否有商品使用此分类
  190. $goodsModel = new \App\Models\GoodsModel();
  191. $goodsCount = $goodsModel->where(['category_id' => $id, 'mark' => 1])->count();
  192. if ($goodsCount > 0) {
  193. return message("该分类下有商品,无法删除", false);
  194. }
  195. // 删除(软删除)
  196. $this->model->where('id', $id)->update([
  197. 'mark' => 0,
  198. 'update_time' => time()
  199. ]);
  200. return message("删除成功", true);
  201. }
  202. /**
  203. * 批量删除
  204. * @param array $params
  205. * @return bool
  206. */
  207. public function deleteAll($params)
  208. {
  209. $ids = isset($params['ids']) ? $params['ids'] : [];
  210. if (empty($ids) || !is_array($ids)) {
  211. $this->error = '请选择要删除的分类';
  212. return false;
  213. }
  214. // 检查是否有子分类或商品
  215. foreach ($ids as $id) {
  216. $childCount = $this->model->where(['pid' => $id, 'mark' => 1])->count();
  217. if ($childCount > 0) {
  218. $this->error = '存在子分类,无法删除';
  219. return false;
  220. }
  221. $goodsModel = new \App\Models\GoodsModel();
  222. $goodsCount = $goodsModel->where(['category_id' => $id, 'mark' => 1])->count();
  223. if ($goodsCount > 0) {
  224. $this->error = '该分类下有商品,无法删除';
  225. return false;
  226. }
  227. }
  228. // 批量删除(软删除)
  229. $this->model->whereIn('id', $ids)->update([
  230. 'mark' => 0,
  231. 'update_time' => time()
  232. ]);
  233. $this->error = '删除成功';
  234. return true;
  235. }
  236. /**
  237. * 获取分类选项(用于下拉选择)
  238. * @return array
  239. */
  240. public function options()
  241. {
  242. $map = [['mark', '=', 1], ['status', '=', 1]];
  243. $list = $this->model->where($map)
  244. ->orderBy('sort', 'desc')
  245. ->orderBy('id', 'asc')
  246. ->select(['id', 'name', 'pid', 'icon'])
  247. ->get()
  248. ->toArray();
  249. return message(MESSAGE_OK, true, $list);
  250. }
  251. }