// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\GoodsCategoryModel; use App\Models\GoodsModel; use App\Services\BaseService; /** * 商品分类管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class GoodsCategoryService * @package App\Services\Api */ class GoodsCategoryService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * GoodsCategoryService constructor. */ public function __construct() { $this->model = new GoodsCategoryModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 列表数据 * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15, $field = '') { $where = ['a.mark' => 1]; $field = $field? $field : 'lev_a.id,lev_a.merch_id,lev_a.name,lev_a.type,lev_a.icon,lev_a.status'; $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1; $order = 'id desc'; if($sortType == 1){ $order = 'lev_a.sort desc, lev_a.id desc'; } $list = $this->model->from('goods_category as a') ->where($where) ->where(function ($query) use ($params) { $type = isset($params['type']) ? $params['type'] : 0; if ($type > 0) { $query->where('a.type', $type); } $status = isset($params['status']) ? $params['status'] : 0; $status = $status > 0 ? $status : 1; if ($status > 0) { $query->where('a.status', $status); } // 商户 $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0; if ($merchId > 0) { $query->where('a.merch_id', $merchId); } }) ->where(function ($query) use ($params) { $keyword = isset($params['kw']) ? $params['kw'] : ''; if ($keyword) { $query->where('a.name', 'like', "%{$keyword}%"); } }) ->selectRaw($field) ->orderByRaw($order) ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['icon'] = isset($item['icon']) && $item['icon'] ? get_image_url($item['icon']) : ''; } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 添加编辑 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { // 请求参数 $data = request()->all(); // 图标 $icon = isset($data['icon']) ? trim($data['icon']) : ''; if ($icon && strpos($icon, "temp")) { $data['icon'] = save_image($icon, 'images'); } else if ($icon) { $data['icon'] = str_replace(IMG_URL, "", $data['icon']); } return parent::edit($data); // TODO: Change the autogenerated stub } /** * 封存/解封 * @param $goodsId */ public function lock($goodsId) { $params = request()->all(); $status = isset($params['status']) ? $params['status'] : 2; $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first(); if (empty($goods)) { $this->error = 2061; return false; } if ($this->model->where(['id' => $goodsId])->update(['status' => $status, 'update_time' => time(), 'remark' => '店长封存'])) { $this->error = 1002; return true; } return false; } /** * 修改信息 * @param $goodsId * @return bool */ public function modify($id) { $params = request()->all(); $info = $this->model->where(['id' => $id, 'mark' => 1])->first(); if (empty($info)) { $this->error = 2061; return false; } $data = [ 'name' => isset($params['name']) ? $params['name'] : '', 'type' => isset($params['type']) ? $params['type'] : 0, 'status' => isset($params['status']) ? $params['status'] : 0, 'update_time' => time(), ]; $icon = isset($data['icon']) ? trim($data['icon']) : ''; if ($icon && strpos($icon, "temp")) { $data['icon'] = save_image($icon, 'images'); } else if ($icon) { $data['icon'] = str_replace(IMG_URL, "", $data['icon']); } if ($this->model->where(['id' => $id])->update($data)) { $this->error = 1008; return true; } $this->error = 1009; return false; } /** * 删除 * @return array */ public function delete() { // 参数 $param = request()->all(); $ids = getter($param, "id"); if (empty($ids)) { return message("记录ID不能为空", false); } $ids = is_array($ids) ? $ids : [$ids]; if(GoodsModel::whereIn('cate_id', $ids)->count()){ $this->error = 2541; return false; } $result = parent::delete(); // TODO: Change the autogenerated stub $success = isset($result['success'])? $result['success'] : false; return $success? true : false; } }