GoodsCategoryService.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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\GoodsModel;
  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']>0) {
  50. $query->where('status', intval($params['status']));
  51. }
  52. // 类型
  53. if (isset($params['type']) && $params['type'] !== null && $params['type']>0) {
  54. $query->where('type', intval($params['type']));
  55. }
  56. })
  57. ->orderBy('id', 'asc')
  58. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  59. $list = $list->toArray();
  60. // 格式化数据
  61. if (isset($list['data']) && !empty($list['data'])) {
  62. foreach ($list['data'] as &$val) {
  63. $val['create_time'] = $val['create_time'] ? datetime($val['create_time'], 'Y-m-d H:i:s') : '';
  64. // 处理图标
  65. if (isset($val['icon'])) {
  66. $val['icon'] = get_image_url($val['icon']);
  67. }
  68. }
  69. }
  70. return [
  71. 'msg' => '操作成功',
  72. 'code' => 0,
  73. 'data' => $list['data'],
  74. 'count' => $list['total']
  75. ];
  76. }
  77. /**
  78. * 获取分类详情
  79. * @return array
  80. * @since 2020/11/11
  81. * @author laravel开发员
  82. */
  83. public function info()
  84. {
  85. // 记录ID
  86. $id = request()->input("id", 0);
  87. $info = [];
  88. if ($id) {
  89. $info = $this->model->getInfo($id);
  90. if ($info) {
  91. if (isset($info['create_time'])) {
  92. $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  93. }
  94. // 处理图标
  95. if (isset($info['icon'])) {
  96. $info['icon'] = get_image_url($info['icon']);
  97. }
  98. if (isset($info['menu_icon'])) {
  99. $info['menu_icon'] = get_image_url($info['menu_icon']);
  100. }
  101. }
  102. }
  103. return message(MESSAGE_OK, true, $info);
  104. }
  105. /**
  106. * 添加或编辑
  107. * @return array
  108. * @since 2020/11/11
  109. * @author laravel开发员
  110. */
  111. public function edit()
  112. {
  113. // 参数
  114. $param = request()->all();
  115. // 验证分类名称
  116. if (empty($param['name'])) {
  117. return message('分类名称不能为空', false);
  118. }
  119. $id = isset($param['id'])?$param['id']:0;
  120. $type = isset($param['type'])?$param['type']:0;
  121. $name = isset($param['name'])?$param['name']:'';
  122. $checkId = $this->model->where(['name'=>$name,'type'=>$type,'mark'=>1])->whereNotIn('id', [$id])->value('id');
  123. if($checkId){
  124. return message('分类名称已经存在', false);
  125. }
  126. // 如果 pid 为 0,表示一级分类
  127. if (!isset($param['pid'])) {
  128. $param['pid'] = 0;
  129. }
  130. // 设置默认值
  131. if (!isset($param['status'])) {
  132. $param['status'] = 1; // 默认有效
  133. }
  134. if (!isset($param['sort'])) {
  135. $param['sort'] = 0; // 默认排序为0
  136. }
  137. if (!isset($param['remark'])) {
  138. $param['remark'] = ''; // 默认备注为空
  139. }
  140. if (!isset($param['icon'])) {
  141. $param['icon'] = ''; // 默认图标为空
  142. }
  143. // 处理图标
  144. if (isset($param['icon'])) {
  145. $param['icon'] = get_image_path($param['icon']);
  146. }
  147. // 保存数据
  148. $result = parent::edit($param);
  149. return $result;
  150. }
  151. /**
  152. * 设置状态
  153. * @param array $params
  154. * @return bool
  155. */
  156. public function setStatus($params)
  157. {
  158. $id = isset($params['id']) ? intval($params['id']) : 0;
  159. $status = isset($params['status']) ? intval($params['status']) : 1;
  160. if (!$id) {
  161. $this->error = '分类ID不能为空';
  162. return false;
  163. }
  164. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  165. if (!$info) {
  166. $this->error = '分类信息不存在';
  167. return false;
  168. }
  169. // 更新状态
  170. $this->model->where('id', $id)->update([
  171. 'status' => $status,
  172. 'update_time' => time()
  173. ]);
  174. $this->error = $status == 1 ? '启用成功' : '禁用成功';
  175. return true;
  176. }
  177. /**
  178. * 删除(重写父类方法)
  179. * @return array
  180. */
  181. public function delete()
  182. {
  183. // 参数
  184. $param = request()->all();
  185. // 记录ID
  186. $ids = getter($param, "id");
  187. if (empty($ids)) {
  188. return message("分类ID不能为空", false);
  189. }
  190. // 清理已删除的记录
  191. $this->model->where(['mark' => 0])->where('update_time', '<=', time() - 600)->delete();
  192. if (is_array($ids)) {
  193. $childCount = $this->model->whereIn('pid',$ids)->where(['mark' => 1])->count();
  194. if ($childCount > 0) {
  195. return message("存在有子分类的数据,无法删除", false);
  196. }
  197. $goodsCount = GoodsModel::whereIn('category_id',$ids)->where(['mark' => 1])->count();
  198. if ($goodsCount > 0) {
  199. return message("有分类存在商品,无法删除", false);
  200. }
  201. // 批量删除
  202. $result = $this->model->whereIn('id', $ids)->update(['mark' => 0, 'update_time' => time()]);
  203. if (!$result) {
  204. return message("删除失败", false);
  205. }
  206. return message("删除成功");
  207. } else {
  208. $childCount = $this->model->where(['pid'=>$ids,'mark' => 1])->count();
  209. if ($childCount > 0) {
  210. return message("存在子分类,无法删除", false);
  211. }
  212. $goodsCount = GoodsModel::where(['category_id' => $ids, 'mark' => 1])->count();
  213. if ($goodsCount > 0) {
  214. return message("有分类存在商品,无法删除", false);
  215. }
  216. // 单个删除
  217. $result = $this->model->where('id', $ids)->update(['mark' => 0, 'update_time' => time()]);
  218. if ($result !== false) {
  219. return message();
  220. }
  221. return message("删除失败", false);
  222. }
  223. }
  224. /**
  225. * 批量删除
  226. * @param array $params
  227. * @return bool
  228. */
  229. public function deleteAll($params)
  230. {
  231. $ids = isset($params['ids']) ? $params['ids'] : [];
  232. if (empty($ids) || !is_array($ids)) {
  233. $this->error = '请选择要删除的分类';
  234. return false;
  235. }
  236. // 检查是否有子分类或商品
  237. foreach ($ids as $id) {
  238. $childCount = $this->model->where(['pid' => $id, 'mark' => 1])->count();
  239. if ($childCount > 0) {
  240. $this->error = '存在子分类,无法删除';
  241. return false;
  242. }
  243. $goodsModel = new \App\Models\GoodsModel();
  244. $goodsCount = $goodsModel->where(['category_id' => $id, 'mark' => 1])->count();
  245. if ($goodsCount > 0) {
  246. $this->error = '该分类下有商品,无法删除';
  247. return false;
  248. }
  249. }
  250. // 批量删除(软删除)
  251. $this->model->whereIn('id', $ids)->update([
  252. 'mark' => 0,
  253. 'update_time' => time()
  254. ]);
  255. $this->error = '删除成功';
  256. return true;
  257. }
  258. /**
  259. * 获取分类选项(用于下拉选择)
  260. * @return array
  261. */
  262. public function options()
  263. {
  264. $type = request()->post('type',0);
  265. $kw = request()->post('kw','');
  266. $map = ['type'=>$type,'status'=>1,'mark'=>1];
  267. if($type<=0){
  268. unset($map['type']);
  269. }
  270. $list = $this->model->where($map)
  271. ->where(function ($query) use($kw){
  272. if($kw){
  273. $query->where('name','like',"%{$kw}%");
  274. }
  275. })
  276. ->orderBy('sort', 'desc')
  277. ->orderBy('id', 'asc')
  278. ->select(['id', 'name','type', 'pid', 'icon'])
  279. ->get()
  280. ->toArray();
  281. return message(MESSAGE_OK, true, $list);
  282. }
  283. }