| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\GoodsCategoryModel;
- use App\Models\StoreModel;
- 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'] !== '') {
- $query->where('status', intval($params['status']));
- }
- })
- ->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']);
- }
- }
- }
- return message(MESSAGE_OK, true, $info);
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- // 参数
- $param = request()->all();
- // 验证分类名称
- if (empty($param['name'])) {
- $this->error = '分类名称不能为空';
- return 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 = $this->model->edit($param);
- if (!$result) {
- $this->error = '操作失败';
- return false;
- }
- return true;
- }
- /**
- * 设置状态
- * @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
- $id = getter($param, "id");
- if (empty($id)) {
- return message("分类ID不能为空", false);
- }
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (!$info) {
- return message("分类信息不存在", false);
- }
- // 检查是否有子分类
- $childCount = $this->model->where(['pid' => $id, 'mark' => 1])->count();
- if ($childCount > 0) {
- return message("存在子分类,无法删除", false);
- }
- // 检查是否有商品使用此分类
- $goodsModel = new \App\Models\GoodsModel();
- $goodsCount = $goodsModel->where(['category_id' => $id, 'mark' => 1])->count();
- if ($goodsCount > 0) {
- return message("该分类下有商品,无法删除", false);
- }
- // 删除(软删除)
- $this->model->where('id', $id)->update([
- 'mark' => 0,
- 'update_time' => time()
- ]);
- return message("删除成功", true);
- }
- /**
- * 批量删除
- * @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()
- {
- $map = [['mark', '=', 1], ['status', '=', 1]];
- $list = $this->model->where($map)
- ->orderBy('sort', 'desc')
- ->orderBy('id', 'asc')
- ->select(['id', 'name', 'pid', 'icon'])
- ->get()
- ->toArray();
- return message(MESSAGE_OK, true, $list);
- }
- }
|