GoodsCategoryService.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. if (isset($info['menu_icon'])) {
  94. $info['menu_icon'] = get_image_url($info['menu_icon']);
  95. }
  96. }
  97. }
  98. return message(MESSAGE_OK, true, $info);
  99. }
  100. /**
  101. * 添加或编辑
  102. * @return array
  103. * @since 2020/11/11
  104. * @author laravel开发员
  105. */
  106. public function edit()
  107. {
  108. // 参数
  109. $param = request()->all();
  110. // 验证分类名称
  111. if (empty($param['name'])) {
  112. return message('分类名称不能为空', false);
  113. }
  114. $id = isset($param['id'])?$param['id']:0;
  115. $type = isset($param['type'])?$param['type']:0;
  116. $name = isset($param['name'])?$param['name']:'';
  117. $checkId = $this->model->where(['name'=>$name,'type'=>$type,'mark'=>1])->whereNotIn('id', [$id])->value('id');
  118. if($checkId){
  119. return message('分类名称已经存在', false);
  120. }
  121. // 如果 pid 为 0,表示一级分类
  122. if (!isset($param['pid'])) {
  123. $param['pid'] = 0;
  124. }
  125. // 设置默认值
  126. if (!isset($param['status'])) {
  127. $param['status'] = 1; // 默认有效
  128. }
  129. if (!isset($param['sort'])) {
  130. $param['sort'] = 0; // 默认排序为0
  131. }
  132. if (!isset($param['remark'])) {
  133. $param['remark'] = ''; // 默认备注为空
  134. }
  135. if (!isset($param['icon'])) {
  136. $param['icon'] = ''; // 默认图标为空
  137. }
  138. // 处理图标
  139. if (isset($param['icon'])) {
  140. $param['icon'] = get_image_path($param['icon']);
  141. }
  142. // 保存数据
  143. $result = parent::edit($param);
  144. return $result;
  145. }
  146. /**
  147. * 设置状态
  148. * @param array $params
  149. * @return bool
  150. */
  151. public function setStatus($params)
  152. {
  153. $id = isset($params['id']) ? intval($params['id']) : 0;
  154. $status = isset($params['status']) ? intval($params['status']) : 1;
  155. if (!$id) {
  156. $this->error = '分类ID不能为空';
  157. return false;
  158. }
  159. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  160. if (!$info) {
  161. $this->error = '分类信息不存在';
  162. return false;
  163. }
  164. // 更新状态
  165. $this->model->where('id', $id)->update([
  166. 'status' => $status,
  167. 'update_time' => time()
  168. ]);
  169. $this->error = $status == 1 ? '启用成功' : '禁用成功';
  170. return true;
  171. }
  172. /**
  173. * 删除(重写父类方法)
  174. * @return array
  175. */
  176. public function delete()
  177. {
  178. // 参数
  179. $param = request()->all();
  180. // 记录ID
  181. $id = getter($param, "id");
  182. if (empty($id)) {
  183. return message("分类ID不能为空", false);
  184. }
  185. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  186. if (!$info) {
  187. return message("分类信息不存在", false);
  188. }
  189. // 检查是否有子分类
  190. $childCount = $this->model->where(['pid' => $id, 'mark' => 1])->count();
  191. if ($childCount > 0) {
  192. return message("存在子分类,无法删除", false);
  193. }
  194. // 检查是否有商品使用此分类
  195. $goodsModel = new \App\Models\GoodsModel();
  196. $goodsCount = $goodsModel->where(['category_id' => $id, 'mark' => 1])->count();
  197. if ($goodsCount > 0) {
  198. return message("该分类下有商品,无法删除", false);
  199. }
  200. // 删除(软删除)
  201. $this->model->where('id', $id)->update([
  202. 'mark' => 0,
  203. 'update_time' => time()
  204. ]);
  205. return message("删除成功", true);
  206. }
  207. /**
  208. * 批量删除
  209. * @param array $params
  210. * @return bool
  211. */
  212. public function deleteAll($params)
  213. {
  214. $ids = isset($params['ids']) ? $params['ids'] : [];
  215. if (empty($ids) || !is_array($ids)) {
  216. $this->error = '请选择要删除的分类';
  217. return false;
  218. }
  219. // 检查是否有子分类或商品
  220. foreach ($ids as $id) {
  221. $childCount = $this->model->where(['pid' => $id, 'mark' => 1])->count();
  222. if ($childCount > 0) {
  223. $this->error = '存在子分类,无法删除';
  224. return false;
  225. }
  226. $goodsModel = new \App\Models\GoodsModel();
  227. $goodsCount = $goodsModel->where(['category_id' => $id, 'mark' => 1])->count();
  228. if ($goodsCount > 0) {
  229. $this->error = '该分类下有商品,无法删除';
  230. return false;
  231. }
  232. }
  233. // 批量删除(软删除)
  234. $this->model->whereIn('id', $ids)->update([
  235. 'mark' => 0,
  236. 'update_time' => time()
  237. ]);
  238. $this->error = '删除成功';
  239. return true;
  240. }
  241. /**
  242. * 获取分类选项(用于下拉选择)
  243. * @return array
  244. */
  245. public function options()
  246. {
  247. $type = request()->post('type',0);
  248. $map = ['type'=>$type,'status'=>1,'mark'=>1];
  249. if($type<=0){
  250. unset($map['type']);
  251. }
  252. $list = $this->model->where($map)
  253. ->orderBy('sort', 'desc')
  254. ->orderBy('id', 'asc')
  255. ->select(['id', 'name','type', 'pid', 'icon'])
  256. ->get()
  257. ->toArray();
  258. return message(MESSAGE_OK, true, $list);
  259. }
  260. }