GoodsCategoryService.php 8.1 KB

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