// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\GoodsCategoryModel; use App\Models\GoodsModel; use App\Services\BaseService; /** * 商品分类管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Common */ class GoodsCategoryService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new GoodsCategoryModel(); } /** * 获取数据列表 * @param array $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { // 分页查询 $list = $this->model->orderBy('sort', 'desc') ->where('mark', 1) ->where(function ($query) use ($params) { // 分类名称 if (isset($params['name']) && $params['name']) { $query->where('name', 'like', "%{$params['name']}%"); } // 状态筛选 if (isset($params['status']) && $params['status'] !== null && $params['status']>0) { $query->where('status', intval($params['status'])); } // 类型 if (isset($params['type']) && $params['type'] !== null && $params['type']>0) { $query->where('type', intval($params['type'])); } }) ->orderBy('id', 'asc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list->toArray(); // 格式化数据 if (isset($list['data']) && !empty($list['data'])) { foreach ($list['data'] as &$val) { $val['create_time'] = $val['create_time'] ? datetime($val['create_time'], 'Y-m-d H:i:s') : ''; // 处理图标 if (isset($val['icon'])) { $val['icon'] = get_image_url($val['icon']); } } } return [ 'msg' => '操作成功', 'code' => 0, 'data' => $list['data'], 'count' => $list['total'] ]; } /** * 获取分类详情 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function info() { // 记录ID $id = request()->input("id", 0); $info = []; if ($id) { $info = $this->model->getInfo($id); if ($info) { if (isset($info['create_time'])) { $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : ''; } // 处理图标 if (isset($info['icon'])) { $info['icon'] = get_image_url($info['icon']); } if (isset($info['menu_icon'])) { $info['menu_icon'] = get_image_url($info['menu_icon']); } } } return message(MESSAGE_OK, true, $info); } /** * 添加或编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { // 参数 $param = request()->all(); // 验证分类名称 if (empty($param['name'])) { return message('分类名称不能为空', false); } $id = isset($param['id'])?$param['id']:0; $type = isset($param['type'])?$param['type']:0; $name = isset($param['name'])?$param['name']:''; $checkId = $this->model->where(['name'=>$name,'type'=>$type,'mark'=>1])->whereNotIn('id', [$id])->value('id'); if($checkId){ return message('分类名称已经存在', false); } // 如果 pid 为 0,表示一级分类 if (!isset($param['pid'])) { $param['pid'] = 0; } // 设置默认值 if (!isset($param['status'])) { $param['status'] = 1; // 默认有效 } if (!isset($param['sort'])) { $param['sort'] = 0; // 默认排序为0 } if (!isset($param['remark'])) { $param['remark'] = ''; // 默认备注为空 } if (!isset($param['icon'])) { $param['icon'] = ''; // 默认图标为空 } // 处理图标 if (isset($param['icon'])) { $param['icon'] = get_image_path($param['icon']); } // 保存数据 $result = parent::edit($param); return $result; } /** * 设置状态 * @param array $params * @return bool */ public function setStatus($params) { $id = isset($params['id']) ? intval($params['id']) : 0; $status = isset($params['status']) ? intval($params['status']) : 1; if (!$id) { $this->error = '分类ID不能为空'; return false; } $info = $this->model->where(['id' => $id, 'mark' => 1])->first(); if (!$info) { $this->error = '分类信息不存在'; return false; } // 更新状态 $this->model->where('id', $id)->update([ 'status' => $status, 'update_time' => time() ]); $this->error = $status == 1 ? '启用成功' : '禁用成功'; return true; } /** * 删除(重写父类方法) * @return array */ public function delete() { // 参数 $param = request()->all(); // 记录ID $ids = getter($param, "id"); if (empty($ids)) { return message("分类ID不能为空", false); } // 清理已删除的记录 $this->model->where(['mark' => 0])->where('update_time', '<=', time() - 600)->delete(); if (is_array($ids)) { $childCount = $this->model->whereIn('pid',$ids)->where(['mark' => 1])->count(); if ($childCount > 0) { return message("存在有子分类的数据,无法删除", false); } $goodsCount = GoodsModel::whereIn('category_id',$ids)->where(['mark' => 1])->count(); if ($goodsCount > 0) { return message("有分类存在商品,无法删除", false); } // 批量删除 $result = $this->model->whereIn('id', $ids)->update(['mark' => 0, 'update_time' => time()]); if (!$result) { return message("删除失败", false); } return message("删除成功"); } else { $childCount = $this->model->where(['pid'=>$ids,'mark' => 1])->count(); if ($childCount > 0) { return message("存在子分类,无法删除", false); } $goodsCount = GoodsModel::where(['category_id' => $ids, 'mark' => 1])->count(); if ($goodsCount > 0) { return message("有分类存在商品,无法删除", false); } // 单个删除 $result = $this->model->where('id', $ids)->update(['mark' => 0, 'update_time' => time()]); if ($result !== false) { return message(); } return message("删除失败", false); } } /** * 批量删除 * @param array $params * @return bool */ public function deleteAll($params) { $ids = isset($params['ids']) ? $params['ids'] : []; if (empty($ids) || !is_array($ids)) { $this->error = '请选择要删除的分类'; return false; } // 检查是否有子分类或商品 foreach ($ids as $id) { $childCount = $this->model->where(['pid' => $id, 'mark' => 1])->count(); if ($childCount > 0) { $this->error = '存在子分类,无法删除'; return false; } $goodsModel = new \App\Models\GoodsModel(); $goodsCount = $goodsModel->where(['category_id' => $id, 'mark' => 1])->count(); if ($goodsCount > 0) { $this->error = '该分类下有商品,无法删除'; return false; } } // 批量删除(软删除) $this->model->whereIn('id', $ids)->update([ 'mark' => 0, 'update_time' => time() ]); $this->error = '删除成功'; return true; } /** * 获取分类选项(用于下拉选择) * @return array */ public function options() { $type = request()->post('type',0); $kw = request()->post('kw',''); $map = ['type'=>$type,'status'=>1,'mark'=>1]; if($type<=0){ unset($map['type']); } $list = $this->model->where($map) ->where(function ($query) use($kw){ if($kw){ $query->where('name','like',"%{$kw}%"); } }) ->orderBy('sort', 'desc') ->orderBy('id', 'asc') ->select(['id', 'name','type', 'pid', 'icon']) ->get() ->toArray(); return message(MESSAGE_OK, true, $list); } }