| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672 |
- <?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\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){
- $query->whereIn('a.id', TradeModel::where(['is_pay'=>2,'status'=>1,'is_out'=>0,'mark'=>1])->pluck('goods_id'));
- }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, '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'];
- }
- 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' => $goods['goods_name'],
- '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(['status' => 2, 'mark' => 0, 'remark' => '已被拆分', 'update_time' => time()])) {
- DB::rollBack();
- return false;
- }
- if ($info && !TradeModel::where(['id' => $info['id'], 'mark' => 1])->update(['is_split' => 1, 'mark' => 0, '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' => $goods['goods_name'],
- '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(['status' => 2, '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, 'mark' => 0, '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'] : '';
- var_dump($ids);
- var_dump($result);
- 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;
- }
- }
|