// +---------------------------------------------------------------------- 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; } $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0; if ($shopId > 0) { $where['a.shop_id'] = $shopId; } $userId = isset($params['user_id']) ? $params['user_id'] : 0; if ($userId > 0) { $where['a.user_id'] = $userId; } $type = isset($params['type']) ? $params['type'] : 0; $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($type){ if($type == 1){ $ids = TradeModel::where(['is_pay'=>2,'status'=>1,'is_out'=>0,'mark'=>1])->pluck('goods_id'); $ids = $ids? $ids->toArray() : []; if($ids){ $query->whereIn('a.id', $ids); } }else if($type == 2){ $query->where(['a.confirm_status'=> 1, 'a.status'=> 1,'a.is_trade'=>2]); }else if($type == 3){ $query->where(['a.confirm_status'=> 1, 'a.status'=> 1,'a.is_trade'=>2]); } }) ->where(function ($query) use ($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; if ($keyword) { $searchType = isset($params['search_type'])? $params['search_type'] : 0; if($searchType == 1){ $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")->orWhere('a.goods_name', 'like', "%{$keyword}%"); }else{ $query->where('c.name', 'like', "%{$keyword}%")->orWhere('c.code', 'like', "%{$keyword}%")->orWhere('a.goods_name', 'like', "%{$keyword}%"); } } $keyword1 = isset($params['keyword1']) ? $params['keyword1'] : ''; if ($keyword1) { $query->where('a.code', 'like', "%{$keyword1}%"); } $username = isset($params['username']) ? $params['username'] : ''; if ($username) { $query->where('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>0) { $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.shop_id', 'asc') // ->orderBy('a.user_id', 'asc') ->orderBy('a.create_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['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : ''; $item['price'] = round($item['price'], 0); } } 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,'g.status'=>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; } $time = strtotime(date('Y-m-d')); $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($time){ $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time); }) ->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('b.mobile', 'asc') ->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, 'goods'); } 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']; } if (!isset($data['goods_name']) || empty($data['goods_name'])) { $data['goods_name'] = 'HY' . $data['code']; } $data['real_price'] = ($data['price']-$data['sell_price']+$data['source_price']); 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('last_sell_time', '>=', strtotime(date('Y-m-d'))) ->count('id'); } /** * 拆分 * @param $goodsId * @param array $info * @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) { $splitNum = ConfigService::make()->getConfigByCode('split_num'); $splitNum = $splitNum? $splitNum : 2; } // 拆分价 if($goods['split_price']<=0){ $splitPrice = ConfigService::make()->getConfigByCode('split_price'); $splitPrice = $splitPrice? $splitPrice : 10000; $goods['split_price'] = $splitPrice; } $priceRate = ConfigService::make()->getConfigByCode('price_rate'); $priceRate = $priceRate? $priceRate : 4; $realPrice = $goods['real_price']; $sellPrice = $goods['sell_price']; // 特价 $price = $goods['price']; // 买入价格 $addPrice = intval($realPrice*$priceRate/100,0); // 涨价 $goods['price'] = $price+$addPrice; $goods['real_price'] = $realPrice+$addPrice; $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); } $sourcePrice = round($goods['source_price'] / $splitNum, 0); $sellPrice = round($goods['sell_price'] / $splitNum, 0); $datas[] = [ 'user_id' => $goods['user_id'], 'shop_id' => $goods['shop_id'], 'goods_name' => 'HY'.$goods['code'] . '-' . $i, 'code' => $goods['code'] . '-' . $i, 'source_price' => $sourcePrice, 'sell_price' => $sellPrice, 'real_price' => ($price - $sellPrice + $sourcePrice), '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(['mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) { DB::rollBack(); return false; } if ($info && !TradeModel::where(['id' => $info['id'], 'mark' => 1])->update(['is_split' => 1,'is_sell'=>2, 'remark' => '已被拆分', 'update_time' => time()])) { DB::rollBack(); return false; } } DB::commit(); return true; } /** * 手动拆分 * @param $goodsId * @return bool */ public function splitGoods($goodsId) { $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first(); $splitNum = isset($goods['split_num']) ? $goods['split_num'] : 0; $splitPrice = isset($goods['split_price']) ? $goods['split_price'] : 0; $sellPrice = isset($goods['sell_price']) ? $goods['sell_price'] : 0; $sourcePrice = isset($goods['source_price']) ? $goods['source_price'] : 0; $price = isset($goods['price']) ? $goods['price'] : 0; if ($splitNum <= 0) { $splitNum = ConfigService::make()->getConfigByCode('split_num'); $splitNum = $splitNum? $splitNum : 2; } // 拆分价 if($splitPrice<=0){ $splitPrice = ConfigService::make()->getConfigByCode('split_price'); $splitPrice = $splitPrice? $splitPrice : 10000; } $tradeInfo = TradeModel::where(['goods_id' => $goodsId, 'mark' => 1]) ->whereIn('status', [1, 2, 3, 4]) ->orderBy('create_time', 'desc') ->first(); $tradeStatus = isset($tradeInfo['status']) ? $tradeInfo['status'] : 0; $isSell = isset($tradeInfo['is_sell']) ? $tradeInfo['is_sell'] : 0; if ($tradeInfo && in_array($tradeStatus, [1, 2, 3])) { $this->error = 2067; return false; } if ($tradeInfo && $tradeStatus==4 && $isSell != 2) { $this->error = 2068; return false; } $priceRate = ConfigService::make()->getConfigByCode('price_rate'); $priceRate = $priceRate ? $priceRate : 4; $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price'); $stopSplitPrice = $stopSplitPrice ? $stopSplitPrice : 500; $splitMinPrice = ConfigService::make()->getConfigByCode('split_min_price'); $splitMinPrice = $splitMinPrice ? $splitMinPrice : 200; $realPrice = ($price - $sellPrice + $sourcePrice); $price = $goods['price']; // 当前价格 $addPrice = intval($realPrice * $priceRate / 100, 0); if($price <= $splitMinPrice){ $this->error = 2065; return false; } // 若特价拆分到停止拆分价格,且交易金额已上涨到最后一次涨价,则停止拆分 if ($sellPrice == $stopSplitPrice && ($price + $addPrice >= $splitPrice)) { $this->error = 2065; 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); } $sourcePrice = round($goods['source_price'] / $splitNum, 0); $sellPrice = round($goods['sell_price'] / $splitNum, 0); $datas[] = [ 'user_id' => $goods['user_id'], 'shop_id' => $goods['shop_id'], 'goods_name' => 'HY'.$goods['code'] . '-' . $i, 'code' => $goods['code'] . '-' . $i, 'source_price' => $sourcePrice, 'sell_price' => $sellPrice, 'real_price' => ($price - $sellPrice + $sourcePrice), '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)) { $this->error = 2070; DB::rollBack(); return false; } if (!$this->model->where(['id' => $goods['id'], 'mark' => 1])->update(['mark' => 0, 'remark' => '已被手动拆分', 'update_time' => time()])) { DB::rollBack(); $this->error = 2070; return false; } if ($tradeInfo && !TradeModel::where(['id' => $tradeInfo['id'], 'mark' => 1])->update(['is_split' => 1, 'is_sell' => 2, 'remark' => '已被手动拆分', 'update_time' => time()])) { DB::rollBack(); $this->error = 2070; return false; } } $this->error = 2069; DB::commit(); return true; } /** * 转场 * @param $goodsId */ public function change($goodsIds) { $params = request()->all(); $changeShopCode = isset($params['change_shop_code']) ? $params['change_shop_code'] : ''; $shopInfo = ShopModel::where(['code' => $changeShopCode, 'mark' => 1])->first(); if (empty($shopInfo)) { $this->error = 2062; return false; } if (empty($goodsIds)) { $this->error = 2404; return false; } if ($this->model->whereIn('id', $goodsIds)->update(['shop_id' => $shopInfo['id'], '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; } $tradeList = TradeModel::whereIn('id', $ids)->where(['mark' => 1])->first(); if (empty($tradeList)) { $this->error = 2061; return false; } $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1])->first(); if (empty($memberInfo)) { $this->error = 2062; return false; } DB::beginTransaction(); if (!TradeModel::whereIn('id', $ids)->update(['user_id' => $userId, 'update_time' => time(), 'remark' => '商品转会员'])) { DB::rollBack(); return true; } DB::commit(); $this->error = 1002; return true; } /** * 封存/解封 * @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($goodsId) { $params = request()->all(); $goods = $this->model->where(['id' => $goodsId, 'mark' => 1])->first(); if (empty($goods)) { $this->error = 2061; return false; } $data = [ 'goods_name' => isset($params['goods_name']) ? $params['goods_name'] : '', 'update_time' => time(), ]; $thumb = isset($params['thumb_path']) ? $params['thumb_path'] : ''; if ($thumb) { $data['thumb'] = $thumb; } if ($this->model->where(['id' => $goodsId])->update($data)) { $this->error = 1008; return true; } $this->error = 1009; return false; } /** * 用户商品数量 * @param $userId * @return array|mixed */ public function getCount($userId) { $cacheKey = "caches:goods:Count:{$userId}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = $this->model->where(['user_id'=>$userId,'status'=>1,'mark'=>1])->count(); RedisService::set($cacheKey, $data, rand(3,5)); return $data; } /** * 删除 * @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]; DB::beginTransaction(); $result = parent::delete(); // TODO: Change the autogenerated stub $code = isset($result['success'])? $result['success'] : ''; if(!$code){ DB::rollBack(); return $result; } // 删除交易记录 if(TradeModel::whereIn('goods_id', $ids)->count() && !TradeModel::whereIn('goods_id', $ids)->update(['mark'=>0,'remark'=>'删除商品同步删除','update_time'=>time()])){ DB::rollBack(); return message('删除商品交易记录失败', false); } DB::commit(); return $result; } }