// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\GoodsModel; use App\Models\MemberModel; use App\Models\ShopModel; use App\Models\TradeModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; /** * 商品管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class GoodsService * @package App\Services\Common */ class GoodsService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * GoodsService constructor. */ public function __construct() { $this->model = new GoodsModel(); } /** * 静态入口 * @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) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; if($status>0){ $where['a.status'] = $status; } $isTrade = isset($params['is_trade'])? $params['is_trade'] : 0; if($isTrade>0){ $where['a.is_trade'] = $isTrade; } $list = $this->model->from('goods as a') ->leftJoin('member as b', 'b.id', '=', 'a.user_id') ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; if($keyword){ $query->where('a.goods_name','like',"%{$keyword}%")->orWhere('c.name','like',"%{$keyword}%")->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%"); } $username = isset($params['username'])? $params['username'] : ''; if($username){ $query->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%"); } }) ->where(function($query) use ($params){ $time = isset($params['time'])? $params['time'] : ''; if($time){ $query->where('a.last_sell_time','<=', $time); } }) ->where(function($query) use ($params){ $notUserId = isset($params['not_user_id'])? $params['not_user_id'] : 0; if($notUserId){ $query->whereNotIn('a.user_id', [$notUserId]); } }) ->select(['a.*','b.nickname','b.code as user_code','b.mobile as mobile','c.name as shop_name','c.code as shop_code']) ->orderBy('a.id','desc') ->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['thumb'] = isset($item['thumb']) && $item['thumb']? get_image_url($item['thumb']) : ''; $item['real_price'] = round($item['price'] - $item['sell_price'] + $item['source_price']); } } return [ 'pageSize'=> $pageSize, 'total'=>isset($list['total'])? $list['total'] : 0, 'list'=> isset($list['data'])? $list['data'] : [] ]; } /** * 上架审核商品 * @param $params * @param int $pageSize * @return array */ public function getTradeGoods($params, $pageSize = 15) { $where = ['a.mark' => 1]; $status = isset($params['status'])? $params['status'] : 0; $userId = isset($params['user_id'])? $params['user_id'] : 0; $shopId = isset($params['shop_id'])? $params['shop_id'] : 0; $isSell = isset($params['is_sell'])? $params['is_sell'] : 0; if($status>0){ $where['a.status'] = $status; } if($shopId>0){ $where['a.shop_id'] = $shopId; } if($isSell>0){ $where['a.is_sell'] = $isSell; } $list = $this->model->from('trade as a') ->leftJoin('member as b', 'b.id', '=', 'a.user_id') ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid') ->leftJoin('shop as d', 'd.id', '=', 'a.shop_id') ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id') ->where($where) ->where(function ($query) use($params){ $keyword = isset($params['keyword'])? $params['keyword'] : ''; if($keyword){ $query->orWhere('b.nickname','like',"%{$keyword}%")->orWhere('b.mobile','like',"%{$keyword}%"); } }) ->select(['a.*','b.nickname','b.mobile as mobile','c.nickname as sell_nickname', 'c.mobile as sell_mobile','d.name as shop_name','g.goods_name','g.thumb','g.code','g.split_stop','g.status as goods_status']) ->orderBy('a.confirm_time','desc') ->orderBy('a.id','desc') ->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['confirm_time'] = $item['confirm_time']? datetime($item['confirm_time'],'Y-m-d H:i:s') : ''; $item['thumb'] = isset($item['thumb']) && $item['thumb']? get_image_url($item['thumb']) : ''; } } 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(); // thumb处理 $thumb = isset($data['thumb'])? trim($data['thumb']) : ''; if ($thumb && strpos($thumb, "temp")) { $data['thumb'] = save_image($thumb, 'member'); } else if($thumb){ $data['thumb'] = str_replace(IMG_URL, "", $data['thumb']); } if(!isset($data['code']) || empty($data['code'])){ $data['code'] = isset($data['id'])&&$data['id']? 'G'.$data['id'] :'G'.(GoodsModel::max('id')+1); } if(!isset($data['price']) || empty($data['price'])){ $data['price'] = $data['sell_price']; } // // $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate'); // $feeRate = $feeRate? $feeRate : '2.5'; // $data['fee'] = round($data['price'] * $feeRate/100, 0); return parent::edit($data); // TODO: Change the autogenerated stub } /** * 获取资料详情 * @param $where * @param array $field */ public function getInfo($where, array $field = []) { $cacheKey = "caches:goods:".(!is_array($where)? $where : md5(json_encode($where))); $info = RedisService::get($cacheKey); if($info){ return $info; } $field = $field ? $field : '*'; if (is_array($where)) { $info = $this->model->where($where)->select($field)->first(); } else { $info = $this->model->where(['id' => (int)$where])->select($field)->first(); } $info = $info ? $info->toArray() : []; if($info){ $info['thumb'] = $info['thumb'] ? get_image_url($info['thumb']) : ''; RedisService::set($cacheKey, $info, rand(5,10)); } return $info; } /** * 验证是否抢购有新的商品 * @param $userId * @return mixed */ public function checkNewGoods($userId) { return $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1]) ->where('create_time','>=', strtotime(date('Y-m-d'))) ->count('id'); } /** * 拆分 * @param $info * @param $goods * @return bool */ public function split($goodsId, $info=[]) { $goods = $this->model->where(['id'=> $goodsId,'mark'=>1])->first(); $splitNum = isset($goods['split_num'])? $goods['split_num'] : 0; if($splitNum<=0){ return false; } $datas = []; $sumPrice = 0; for($i=1; $i<= $splitNum; $i++){ if($i < $splitNum){ $price = round($goods['price']/$splitNum, 0); $sumPrice += $price; }else{ $price = round($goods['price'] - $sumPrice, 0); } $datas[] = [ 'user_id'=> $goods['user_id'], 'shop_id'=> $goods['shop_id'], 'goods_name'=> $goods['goods_name'], 'code'=> $goods['code'].'-'.$i, 'source_price'=> round($goods['source_price']/$splitNum, 0), 'sell_price'=> round($goods['sell_price']/$splitNum, 0), 'price'=> $price, 'fee'=> 0, 'thumb'=> $goods['thumb'], 'albums'=> $goods['albums'], 'content'=> $goods['content'], 'split_num'=> $goods['split_num'], 'split_price'=> $goods['split_price'], 'last_sell_time'=> time(), 'create_time'=> time(), 'update_time'=> time(), 'mark'=>1, 'status'=>1, ]; } DB::beginTransaction(); if($datas){ if(!$this->model->insert($datas)){ DB::rollBack(); return false; } if(!$this->model->where(['id'=> $goods['id'],'mark'=>1])->update(['status'=>2,'mark'=>0,'remark'=>'已被拆分','update_time'=>time()])){ DB::rollBack(); return false; } if($info && !TradeModel::where(['id'=> $info['id'],'mark'=>1])->update(['status'=>-1,'is_split'=>1,'mark'=>0,'remark'=>'已被拆分','update_time'=>time()])){ DB::rollBack(); return false; } } DB::commit(); return true; } /** * 转场 * @param $goodsId */ public function change($goodsId) { $params = request()->all(); $shopId = isset($params['shop_id'])? $params['shop_id']:0; $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first(); if(empty($goods)){ $this->error = 2061; return false; } $shopInfo = ShopModel::where(['id'=> $shopId,'mark'=>1])->first(); if(empty($shopInfo)){ $this->error = 2062; return false; } if($this->model->where(['id'=> $goodsId])->update(['shop_id'=> $shopId,'update_time'=> time(),'remark'=> '店长转场'])){ $this->error = 1002; return true; } return false; } /** * 转会员 * @param $goodsId */ public function switchUser($params) { $userId = isset($params['user_id'])? $params['user_id']:0; $ids = isset($params['ids'])? $params['ids']:[]; if(empty($ids)){ $this->error = 2063; return false; } $goodsList = $this->whereIn('id',$ids)->where(['mark'=>1])->first(); if(empty($goodsList)){ $this->error = 2061; return false; } $memberInfo = MemberModel::where(['id'=> $userId,'mark'=>1])->first(); if(empty($memberInfo)){ $this->error = 2062; return false; } if($this->model->whereIn('id', $ids)->update(['user_id'=> $userId,'update_time'=> time(),'remark'=> '商品转会员'])){ $this->error = 1002; return true; } return false; } /** * 封存/解封 * @param $goodsId */ public function lock($goodsId) { $params = request()->all(); $status = isset($params['status'])? $params['status'] : 2; $goods = $this->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 updateData($goodsId) { $params = request()->all(); $goods = $this->where(['id'=> $goodsId,'mark'=>1])->first(); if(empty($goods)){ $this->error = 2061; return false; } $data = [ 'thumb'=> isset($params['thumb'])? $params['thumb']: '', 'goods_name'=> isset($params['goods_name'])? $params['goods_name']: '', 'update_time'=>time(), ]; if($this->model->where(['id'=> $goodsId])->update($data)){ $this->error = 1008; return true; } $this->error = 1009; return false; } }