| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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.pid,lev_a.name,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) {
- $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;
- }
- }
|