GoodsCategoryService.php 9.2 KB

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