GoodsCategoryService.php 8.6 KB

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