123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\GoodsModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- use wxkxklmyt\Scws;
- /**
- * 商品管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Common
- */
- class GoodsService extends BaseService
- {
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * AdService constructor.
- */
- public function __construct()
- {
- $this->model = new GoodsModel();
- }
- /**
- * 列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $query = $this->getQuery($params);
- $list = $query->select(['a.*'])
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
- $item['discount_start_time'] = $item['discount_start_time']? date('Y-m-d H:i:s', $item['discount_start_time']):'';
- $item['discount_end_time'] = $item['discount_end_time']? date('Y-m-d H:i:s', $item['discount_end_time']):'';
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? $list['data'] : []
- ];
- }
- /**
- * 查询
- * @param $params
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function getQuery($params)
- {
- $where = ['a.status'=>1,'a.mark' => 1];
- $status = isset($params['status'])? $params['status'] : 1;
- if($status>0){
- $where['a.status'] = $status;
- }else{
- unset($where['a.status']);
- }
- $model = $this->model->with(['category'])->from('goods as a')
- ->leftJoin('goods_categorys as b','b.cate_id','=','a.cate_id')
- ->where($where)
- ->where(function($query) use($params){
- // 分类
- $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
- if($cateId){
- $query->where(function($query) use($cateId){
- $prefix = env('DB_PREFIX','lev_');
- $query->where('a.cate_id', $cateId)
- ->orWhereRaw("FIND_IN_SET({$cateId},{$prefix}b.pids)");
- });
- }
- // 品牌
- $brandId = isset($params['brand_id'])? $params['brand_id'] : 0;
- if($brandId>0){
- $query->where('a.brand_id', $brandId);
- }
- $type = isset($params['type'])? $params['type'] : 0;
- if($type==2){
- $query->where('a.is_discount', 1)->where(function($query){
- $query->where('a.discount_start_time', '<=', time())
- ->where('a.discount_end_time', '>=', time())
- ->orWhere('a.discount_end_time', '<=', 0);
- });
- }
- })
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $scws = new Scws();
- $kws = $scws->scws($keyword,3,false);
- if(count($kws) >1){
- $query->where(function ($query) use ($kws) {
- foreach ($kws as $kw) {
- $kw = trim($kw);
- $query->where('a.goods_name', 'like', "%{$kw}%")
- ->where('a.keywords', 'like', "%{$kw}%");
- }
- });
- }else{
- $query->where(function ($query) use ($keyword){
- $query->where('a.goods_name','like',"%{$keyword}%")
- ->orWhere('a.goods_pinyin','like',"%{$keyword}%")
- ->orWhere('a.keywords', 'like', "%{$keyword}%");
- });
- }
- }
- });
- return $model;
- }
- /**
- * 导入
- */
- public function import()
- {
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- // 图片处理
- if(isset($data['thumb'])){
- $data['thumb'] = get_image_path($data['thumb']);
- }
- // 折扣时间
- if (isset($data['discount_start_time'])) {
- $data['discount_start_time'] = strtotime($data['discount_start_time']);
- }
- if (isset($data['discount_end_time'])) {
- $data['discount_end_time'] = strtotime($data['discount_end_time']);
- }
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- }
|