| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AccountModel;
- 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 TradeService
- * @package App\Services\Common
- */
- class TradeService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * TradeService constructor.
- */
- public function __construct()
- {
- $this->model = new TradeModel();
- }
- /**
- * 静态入口
- * @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;
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
- $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
- $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
- $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
- $type = isset($params['type']) ? $params['type'] : 0;
- $goodsStatus = isset($params['goods_status']) ? $params['goods_status'] : 0;
- if ($shopId > 0) {
- $where['a.shop_id'] = $shopId;
- }
- if ($parentId > 0) {
- $where['b.parent_id'] = $parentId;
- }
- if ($isAppeal > 0) {
- $where['a.is_appeal'] = $isAppeal;
- }
- if ($goodsStatus > 0) {
- $where['g.status'] = $goodsStatus;
- }
- $model = $this->model->from('trade as a')
- ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
- ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
- ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
- ->leftJoin('shop as s', 's.id', '=', 'g.shop_id')
- ->where($where)
- ->where('a.status', '>', 0)
- ->where(function ($query) use ($type, $status, $isAppeal) {
- if($type == 1 && $status == 2){
- $query->whereIn('a.status',[1,2,3])->orWhere(function($query) {
- $query->where(['a.status'=>4])->where('a.is_sell','<=', 1);
- });
- }else if($status>0 && !$isAppeal){
- $query->where(['a.status'=> $status]);
- }
- })
- ->where(function ($query) use ($params) {
- $keyword = isset($params['keyword']) ? $params['keyword'] : '';
- if ($keyword) {
- $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")
- ->orWhere('s.name', 'like', "%{$keyword}%")
- ->orWhere('s.code', 'like', "%{$keyword}%")
- ->orWhere('g.goods_name', 'like', "%{$keyword}%");
- }
- })
- ->where(function ($query) use ($params) {
- $time = isset($params['time']) ? $params['time'] : 0;
- if ($time) {
- $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
- }
- })
- ->where(function ($query) use ($type, $userId, $sellUid, $status) {
- if ($type == 1) {
- $query->whereIn('a.status',[1,2,3,4]);
- if ($status == 0) {
- $query->where(['a.user_id' => $userId, 'a.is_out' => 0]);
- $query->whereNotIn('a.sell_uid', [$userId]);
- } else if ($status == 2) {
- $query->where('a.sell_uid', '=', $userId);
- $query->whereNotIn('a.user_id', [$userId]);
- } else {
- $query->where(['a.user_id' => $userId, 'a.is_out' => 0]);
- }
- } else if($type == 3){
- $query->whereIn('a.status',[1,2,3,4]);
- } else {
- if ($userId) {
- $query->where(['a.user_id' => $userId, 'a.is_out' => 0]);
- } else if ($sellUid) {
- $query->where('a.sell_uid', '=', $sellUid);
- }
- }
- })
- ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 's.name as shop_name', 's.code as shop_code', 'c.nickname as sell_nickname', 'c.mobile as sell_mobile', 'g.goods_name', 'g.code', 'g.thumb']);
- // 店长排序
- if ($type == 1) {
- $model = $model->orderBy('a.status', 'asc');
- } else if ($type == 3) {
- $model = $model->orderBy('a.status', 'asc')->orderBy('a.is_sell', 'asc');
- }
- $model = $model->orderBy('s.id', 'asc')
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc');
- // 统计
- $totalModdel = clone $model;
- $totalModdel = $totalModdel->whereIn('a.status', [2, 3, 4]);
- $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
- $total = $totalModdel->sum('a.real_price');
- $fee = $totalModdel->sum('a.fee');
- $counts['fee'] = intval($fee);
- $countModel = clone $model;
- $countModel = $countModel->whereIn('a.status', [3, 4]);
- $counts['total'] = intval($total);
- $bonus = $countModel->sum('a.bonus');
- $counts['bonus'] = intval($bonus);
- $serviceFee = $countModel->sum('a.service_fee');
- $counts['service_fee'] = intval($serviceFee);
- $profit = $countModel->sum('a.profit');
- $counts['profit'] = intval($profit);
- $list = $model->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['pay_time'] = $item['pay_time'] ? datetime($item['pay_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']) : '';
- $item['pay_img'] = isset($item['pay_img']) && $item['pay_img'] ? get_image_url($item['pay_img']) : '';
- $item['price'] = intval($item['price']);
- $item['real_price'] = intval($item['real_price']);
- $item['fee'] = intval($item['fee']);
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'counts' => $counts,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 转商品列表数据
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getSwitchGoods($params, $pageSize = 15)
- {
- $where = ['a.mark' => 1, 'a.status' => 1, 'a.is_pay' => 2];
- $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;
- }
- $list = $this->model->from('trade as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.sell_uid')
- ->leftJoin('shop as c', 'c.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->where('g.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.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
- }
- })
- ->select(['a.*', 'b.nickname', 'b.code as user_code', 'b.mobile as mobile', 'c.name as shop_name', 'c.code as shop_code', 'g.goods_name','g.code', 'g.thumb'])
- ->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']) : '';
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 详情
- * @param $id
- * @return mixed
- */
- public function getInfo($id)
- {
- $info = $this->model->from('trade as a')
- ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
- ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
- ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
- ->where(['a.id' => $id, 'a.mark' => 1])
- ->select(['a.*', 'b.nickname', 'b.mobile as buy_mobile', 'c.nickname as sell_nickname', 'c.mobile as sell_mobile', 'g.goods_name', 'g.code', 'g.thumb'])
- ->first();
- if ($info) {
- $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
- $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
- $info['confirm_time'] = $info['confirm_time'] ? datetime($info['confirm_time'], 'Y-m-d H:i:s') : '';
- $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
- $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
- $info['price'] = intval($info['price']);
- $info['real_price'] = intval($info['real_price']);
- $info['fee'] = intval($info['fee']);
- $bankInfo = MemberBankService::make()->getBindInfo($info['sell_uid']);
- $info['bank_info'] = $bankInfo ? $bankInfo : ['realname' => '', 'bank_name' => '', 'bank_num' => ''];
- }
- return $info;
- }
- /**
- * 统计
- * @param $params
- * @return int[]
- */
- public function getCounts($params)
- {
- $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
- $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;
- $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
- $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
- $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
- if ($shopId > 0) {
- $where['a.shop_id'] = $shopId;
- }
- if ($parentId > 0) {
- $where['b.parent_id'] = $parentId;
- }
- if ($userId > 0) {
- $where['a.user_id'] = $userId;
- }
- if ($sellUid > 0) {
- $where['a.sell_uid'] = $sellUid;
- }
- if ($isAppeal > 0) {
- $where['a.is_appeal'] = $isAppeal;
- }
- if ($status > 0) {
- $where['a.status'] = $status;
- }
- $counts['total'] = intval($this->model->from('trade as a')
- ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
- ->where($where)
- ->where(function ($query) use ($params) {
- $time = isset($params['time']) ? $params['time'] : 0;
- if ($time) {
- $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
- }
- })
- ->where(function ($query) use ($userId, $sellUid, $status) {
- if ($sellUid && $userId) {
- $query->where('a.user_id', $userId)->orWhere(function ($query) use ($sellUid) {
- $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1, 2]);
- });
- } else if ($userId) {
- $query->where('a.user_id', '=', $userId);
- } else if ($sellUid) {
- $query->where('a.sell_uid', '=', $sellUid);
- }
- })
- ->sum('real_price'));
- $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
- $bonusRate = $bonusRate ? $bonusRate : 5;
- $counts['bonus'] = intval($counts['total'] * $bonusRate / 100);
- $counts['fee'] = intval($this->model->from('trade as a')
- ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
- ->where($where)
- ->where(function ($query) use ($params) {
- $time = isset($params['time']) ? $params['time'] : 0;
- if ($time) {
- $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
- }
- })
- ->where(function ($query) use ($userId, $sellUid, $status) {
- if ($sellUid && $userId) {
- $query->where('a.user_id', $userId)->orWhere(function ($query) use ($sellUid) {
- $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1, 2]);
- });
- } else if ($userId) {
- $query->where('a.user_id', '=', $userId);
- } else if ($sellUid) {
- $query->where('a.sell_uid', '=', $sellUid);
- }
- })
- ->sum('fee'));
- $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
- $serviceRate = $serviceRate ? $serviceRate : 8;
- $counts['service_fee'] = intval($counts['total'] * $serviceRate / 100);
- return $counts;
- }
- /**
- * 获取用户当天
- * @param $userId
- * @return array|mixed
- */
- public function getCountByDay($userId)
- {
- $cacheKey = "caches:trade:count:{$userId}";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where(['user_id'=> $userId, 'status'=> 4,'is_sell'=> 2,'mark'=>1])
- ->where('create_time','>=',strtotime(date('Y-m-d')))
- ->count('id');
- if($data){
- RedisService::set($cacheKey, $data, rand(3,5));
- }
- return $data;
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 获取用户交易统计
- * @param $userId
- * @param int $status
- * @param int $time
- * @return mixed
- */
- public function getUserTradeTotal($userId, $status = 0, $time = 0)
- {
- $cacheKey = "caches:trade:userTotal:{$userId}_" . (is_array($status) ? implode('-', $status) : $status) . "_{$time}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $where = ['a.user_id' => $userId, 'a.mark' => 1, 'b.status' => 1, 'b.mark' => 1];
- $data = $this->model->from('trade as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($status, $time) {
- $query->whereIn('a.status', is_array($status) ? $status : [$status]);
- // 本月
- if ($time == 1) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
- } // 今日
- else if ($time == 2) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
- }
- })
- ->sum('a.real_price');
- RedisService::set($cacheKey, $data, rand(3, 5));
- return $data;
- }
- /**
- * 获取用户团队交易统计
- * @param $userId
- * @param int $status
- * @param int $time
- * @return mixed
- */
- public function getTeamTradeTotal($userId, $status = 0, $time = 0)
- {
- $cacheKey = "caches:trade:teamTotal:{$userId}_" . (is_array($status) ? implode('-', $status) : $status) . "_{$time}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $where = ['b.parent_id' => $userId, 'a.mark' => 1, 'b.status' => 1];
- $data = $this->model->from('trade as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($status, $time) {
- $query->whereIn('a.status', is_array($status) ? $status : [$status]);
- // 本月
- if ($time == 1) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
- } // 今日
- else if ($time == 2) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
- }
- })
- ->sum('a.real_price');
- RedisService::set($cacheKey, $data, rand(3, 5));
- return $data;
- }
- /**
- * 获取店铺交易统计
- * @param $userId
- * @param int $time
- * @return mixed
- */
- public function getShopTradeTotal($shopId, $type = 'real_price', $time = 2)
- {
- $cacheKey = "caches:trade:shop_{$type}:{$shopId}_" . (is_array($time) ? implode('-', $time) : $time);
- $data = RedisService::get($cacheKey);
- if ($data) {
- return round($data, 2);
- }
- $where = ['a.shop_id' => $shopId, 'a.mark' => 1];
- $data = $this->model->from('trade as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->whereIn('a.status', [3, 4])
- ->where(function ($query) use ($time) {
- // 本月
- if ($time == 1) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
- } // 今日
- else if ($time == 2) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
- }
- })
- ->sum('a.' . $type);
- RedisService::set($cacheKey, $data, rand(3, 5));
- return round($data, 2);
- }
- /**
- * 获取店铺佣金统计
- * @param $userId
- * @param int $time
- * @return mixed
- */
- public function getShopBonusTotal($shopId, $time = 2)
- {
- $cacheKey = "caches:trade:shop_bonus:{$shopId}_" . (is_array($time) ? implode('-', $time) : $time);
- $data = RedisService::get($cacheKey);
- if ($data) {
- return round($data, 2);
- }
- $where = ['a.shop_id' => $shopId, 'type' => 2, 'a.coin_type' => 2, 'a.status' => 1, 'a.mark' => 1];
- $data = AccountModel::from('account_log as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($time) {
- // 本月
- if ($time == 1) {
- $query->where('a.create_time', '>=', strtotime(date('Y-m-01')));
- } // 今日
- else if ($time == 2) {
- $query->where('a.create_time', '>=', strtotime(date('Y-m-d')));
- } else if ($time) {
- $month = date('Y-m', $time / 1000);
- $lastDay = strtotime("{$month}-01 +1 month -1 day");
- if ($month) {
- $query->where('a.create_time', '>=', $time / 1000);
- $query->where('a.create_time', '<', $lastDay + 86400);
- }
- }
- })
- ->sum('a.money');
- RedisService::set($cacheKey, $data, rand(3, 5));
- return round($data, 2);
- }
- /**
- * 获取用户佣金统计
- * @param $userId
- * @param int $time
- * @return mixed
- */
- public function getTradeBonusTotal($userId, $time = 0)
- {
- $cacheKey = "caches:trade:bonus:{$userId}_" . (is_array($time) ? implode('-', $time) : $time);
- $data = RedisService::get($cacheKey);
- if ($data) {
- return round($data, 2);
- }
- $where = ['a.user_id' => $userId, 'type' => 2, 'a.coin_type' => 2, 'a.status' => 1, 'a.mark' => 1];
- $data = AccountModel::from('account_log as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($time) {
- // 本月
- if ($time == 1) {
- $query->where('a.create_time', '>=', strtotime(date('Y-m-01')));
- } // 今日
- else if ($time == 2) {
- $query->where('a.create_time', '>=', strtotime(date('Y-m-d')));
- } else if ($time) {
- $month = date('Y-m', $time / 1000);
- $lastDay = strtotime("{$month}-01 +1 month -1 day");
- if ($month) {
- $query->where('a.create_time', '>=', $time / 1000);
- $query->where('a.create_time', '<', $lastDay + 86400);
- }
- }
- })
- ->sum('a.money');
- RedisService::set($cacheKey, $data, rand(3, 5));
- return round($data, 2);
- }
- /**
- * 获取用户收益统计
- * @param $userId
- * @param int $time
- * @return mixed
- */
- public function getTradeProfitTotal($userId, $time = 0)
- {
- $cacheKey = "caches:trade:profit:{$userId}_" . (is_array($time) ? implode('-', $time) : $time);
- $data = RedisService::get($cacheKey);
- if ($data) {
- return round($data, 2);
- }
- $where = ['a.user_id' => $userId, 'type' => 2, 'a.coin_type' => 5, 'a.status' => 1, 'a.mark' => 1];
- $data = AccountModel::from('account_log as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($time) {
- // 本月
- if ($time == 1) {
- $query->where('a.create_time', '>=', strtotime(date('Y-m-01')));
- } // 今日
- else if ($time == 2) {
- $query->where('a.create_time', '>=', strtotime(date('Y-m-d')));
- } else if ($time) {
- $month = date('Y-m', $time / 1000);
- $lastDay = strtotime("{$month}-01 +1 month -1 day");
- if ($month) {
- $query->where('a.create_time', '>=', $time / 1000);
- $query->where('a.create_time', '<', $lastDay + 86400);
- }
- }
- })
- ->sum('a.money');
- RedisService::set($cacheKey, $data, rand(3, 5));
- return round($data, 2);
- }
- /**
- * 获取用户佣金统计
- * @param $userId
- * @param int $status
- * @param int $time
- * @return mixed
- */
- public function getTradeBonusTotal1($userId, $status = 0, $time = 0)
- {
- $cacheKey = "caches:trade:bonus:{$userId}_" . (is_array($status) ? implode('-', $status) : $status) . "_" . (is_array($time) ? implode('-', $time) : $time);
- $data = RedisService::get($cacheKey);
- if ($data) {
- return round($data, 2);
- }
- $where = ['b.parent_id' => $userId, 'a.mark' => 1, 'b.status' => 1];
- $data = $this->model->from('trade as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($status, $time) {
- $query->whereIn('a.status', is_array($status) ? $status : [$status]);
- // 本月
- if ($time == 1) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
- } // 今日
- else if ($time == 2) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
- } else if (is_array($time)) {
- $start = isset($time[0]) ? $time[0] : '';
- $end = isset($time[1]) ? $time[1] : '';
- if ($end && $end > $start) {
- $query->where('a.pay_time', '<=', strtotime($end));
- if ($start) {
- $query->where('a.pay_time', '>=', strtotime($start));
- }
- } else if ($start) {
- $query->where('a.pay_time', '=', strtotime($start));
- }
- }
- })
- ->sum('a.bonus');
- RedisService::set($cacheKey, $data, rand(3, 5));
- return round($data, 2);
- }
- /**
- * 获取用户收益统计
- * @param $userId
- * @param int $status
- * @param int $time
- * @return mixed
- */
- public function getTradeProfitTotal1($userId, $status = 0, $time = 0)
- {
- $cacheKey = "caches:trade:profit:{$userId}_" . (is_array($status) ? implode('-', $status) : $status) . "_" . (is_array($time) ? implode('-', $time) : $time);
- $data = RedisService::get($cacheKey);
- if ($data) {
- return round($data, 2);
- }
- $where = ['a.user_id' => $userId, 'a.mark' => 1, 'b.status' => 1];
- $data = $this->model->from('trade as a')
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($status, $time) {
- $query->whereIn('a.status', is_array($status) ? $status : [$status]);
- // 本月
- if ($time == 1) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
- } // 今日
- else if ($time == 2) {
- $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
- } else if (is_array($time)) {
- $start = isset($time[0]) ? $time[0] : '';
- $end = isset($time[1]) ? $time[1] : '';
- if ($end && $end > $start) {
- $query->where('a.pay_time', '<=', strtotime($end));
- if ($start) {
- $query->where('a.pay_time', '>=', strtotime($start));
- }
- } else if ($start) {
- $query->where('a.pay_time', '=', strtotime($start));
- }
- }
- })
- ->sum('a.profit');
- RedisService::set($cacheKey, $data, rand(3, 5));
- return round($data, 2);
- }
- /**
- * 抢拍交易订单数
- * @param $userId 用户ID
- * @param int $status 状态
- * @return mixed
- */
- public function getNewTradeCountByStatus($userId, $shopId = 0, $status = 0)
- {
- $where = ['user_id' => $userId, 'mark' => 1, 'is_read' => 0, 'is_out' => 0];
- if ($shopId) {
- $where['shop_id'] = $shopId;
- }
- $counts = $this->model->where($where)
- ->where(function ($query) use ($status) {
- $query->whereIn('status', is_array($status) ? $status : [$status]);
- })
- ->where('create_time', '>=', strtotime(date('Y-m-d')))
- ->select(['status', DB::raw('count(*) as count')])
- ->groupBy('status')
- ->get();
- if ($counts) {
- $temps = ['status1' => 0, 'status2' => 0, 'status3' => 0];
- foreach ($counts as $v) {
- $temps['status' . $v['status']] = $v['count'];
- }
- $counts = $temps;
- } else {
- $counts = ['status1' => 0, 'status2' => 0, 'status3' => 0];
- }
- return $counts;
- }
- /**
- * 获取用户抢拍数量
- * @param $userId
- * @param string $time
- * @return array|mixed
- */
- public function getTradeByDay($userId, $time=''){
- $cacheKey = "caches:trade:snapNum:{$userId}";
- $data = RedisService::get($cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->where(['user_id'=> $userId,'mark'=>1])
- ->whereIn('status',[1,2,3,4])
- ->where(function($query) use($time){
- if($time){
- $query->where('create_time','>=', $time);
- }
- })
- ->count('id');
- if($data){
- RedisService::set($cacheKey, $data, rand(3,5));
- }
- return $data;
- }
- /**
- * 抢购
- * @param $params
- * @param $userId
- * @param $shopId
- * @return bool
- */
- public function buy($params, $userId, $shopId)
- {
- $goodsId = isset($params['id']) ? $params['id'] : 0;
- $info = GoodsService::make()->getInfo(['id' => $goodsId, 'status' => 1, 'mark' => 1]);
- $goodsUserId = isset($info['user_id']) ? $info['user_id'] : 0;
- $isTrade = isset($info['is_trade']) ? $info['is_trade'] : 0;
- if (empty($info)) {
- $this->error = 2031;
- return false;
- }
- if ($isTrade == 1) {
- $this->error = 2032;
- return false;
- }
- // 验证用户是否可以抢拍
- $memberInfo = MemberService::make()->getCacheInfo($userId);
- $memberIsTrade = isset($memberInfo['is_trade']) ? $memberInfo['is_trade'] : 0;
- if ($memberIsTrade != 1) {
- $this->error = 2032;
- return false;
- }
- $shopInfo = ShopService::make()->getInfo($shopId);
- $shopUid = isset($shopInfo['user_id'])? $shopInfo['user_id'] : 0;
- if (empty($shopInfo)) {
- $this->error = 2033;
- return false;
- }
- // 非店长不可拍自己商品
- if ($goodsUserId == $userId && $shopUid != $userId) {
- $this->error = 2036;
- return false;
- }
- // 营业时间
- $curTime = time();
- $startTime = isset($shopInfo['start_time']) ? $shopInfo['start_time'] : '';
- $endTime = isset($shopInfo['end_time']) ? $shopInfo['end_time'] : '';
- // 抢拍数量限制
- $snapNum = ConfigService::make()->getConfigByCode('member_snap_num');
- $snapNum = $snapNum>0? $snapNum : 5;
- // 店长自己抢
- if ($shopUid == $userId) {
- $shopownerSnapNum = ConfigService::make()->getConfigByCode('shopowner_snap_num');
- $snapNum = $shopownerSnapNum>0? $shopownerSnapNum : 5;
- $snapTime = ConfigService::make()->getConfigByCode('snap_time');
- $snapTime = $snapTime ? $snapTime : 5;
- $curTime = strtotime(date('Y-m-d') . ' ' . $startTime) + 1;
- if (time() < strtotime(date('Y-m-d') . ' ' . $startTime) - $snapTime * 60) {
- $this->error = 2034;
- return false;
- } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
- $this->error = 2035;
- return false;
- }
- } else {
- if (time() < strtotime(date('Y-m-d') . ' ' . $startTime)) {
- $this->error = 2034;
- return false;
- } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
- $this->error = 2035;
- return false;
- }
- }
- // 抢拍数量验证
- $curSnapNum = TradeService::make()->getTradeByDay($userId, strtotime(date('Y-m-d')));
- if($curSnapNum>=$snapNum){
- $this->error = lang(2096,['num'=> $snapNum]);
- return false;
- }
- // 如果是店长拍自身商品,直接转为隔天待售
- if($shopUid == $goodsUserId && $shopUid == $userId){
- GoodsModel::where(['id' => $goodsId])->update(['last_sell_time' => time(),'remark' => '店长自拍重新上架 ' . date('Y-m-d H:i:s'), 'update_time' => time()]);
- $this->error = 2041;
- // 抢拍成功,清除当天已抢拍数量统计
- RedisService::keyDel("caches:trade:snapNum:{$userId}");
- return true;
- }
- // 验证收款账号
- if (!MemberBankService::make()->getBindInfo($userId)) {
- $this->error = 2037;
- return false;
- }
- $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
- $feeRate = $feeRate ? $feeRate : '2.5';
- $realPrice = intval($info['price'] - $info['sell_price'] + $info['source_price']);
- $fee = round($realPrice * $feeRate / 100, 0);
- $lockCacheKey = "caches:trade:lock:{$goodsId}";
- if (RedisService::get($lockCacheKey)) {
- $this->error = 2032;
- return false;
- }
- RedisService::set($lockCacheKey, $info, rand(5, 10));
- $data = [
- 'order_sn' => get_order_num('T'),
- 'goods_id' => $goodsId,
- 'user_id' => $userId,
- 'shop_id' => $shopId,
- 'sell_uid' => $info['user_id'],
- 'num' => 1,
- 'price' => $info['price'],
- 'source_price' => $info['source_price'],
- 'sell_price' => $info['sell_price'],
- 'real_price' => $realPrice,
- 'new_real_price' => $realPrice,
- 'fee' => $fee,
- 'new_price' => $info['price'],
- 'remark' => '抢拍交易',
- 'create_time' => $curTime,
- 'update_time' => $curTime,
- 'status' => 1
- ];
- DB::beginTransaction();
- if (TradeModel::where(['goods_id' => $goodsId, 'mark' => 1])->whereIn('status', [1, 2])->value('id')) {
- DB::rollBack();
- $this->error = 2032;
- return false;
- }
- if (!TradeModel::insertGetId($data)) {
- DB::rollBack();
- $this->error = 2040;
- RedisService::keyDel($lockCacheKey);
- return false;
- }
- if (!GoodsModel::where(['id' => $goodsId])->update(['is_trade' => 1, 'update_time' => time()])) {
- DB::rollBack();
- $this->error = 2040;
- RedisService::keyDel($lockCacheKey);
- return false;
- }
- DB::commit();
- $this->error = 2041;
- RedisService::keyDel($lockCacheKey);
- // 抢拍成功,清除当天已抢拍数量统计
- RedisService::keyDel("caches:trade:snapNum:{$userId}");
- return true;
- }
- /**
- * 付款
- * @param $params
- * @param $userId
- * @param $shopId
- * @return bool
- */
- public function pay($params, $userId, $shopId)
- {
- $id = isset($params['id']) ? $params['id'] : 0;
- $payImg = isset($params['pay_img']) ? $params['pay_img'] : '';
- if (empty($payImg)) {
- $this->error = 2043;
- return false;
- }
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (empty($id) || empty($info)) {
- $this->error = 2042;
- return false;
- }
- if ($info['status'] != 1) {
- $this->error = 2044;
- return false;
- }
- if ($info['user_id'] != $userId) {
- $this->error = 2045;
- return false;
- }
- // 卖家收款账号
- // $bankInfo = MemberBankService::make()->getBindInfo($info['user_id']);
- // if (empty($bankInfo)) {
- // $this->error = 2060;
- // return false;
- // }
- if ($this->model->where(['id' => $id, 'mark' => 1])->update(['pay_time' => time(), 'pay_img' => $payImg, 'status' => 2, 'update_time' => time()])) {
- $this->error = 2046;
- return true;
- }
- $this->error = 2047;
- return false;
- }
- /**
- * 确认收款
- * @param $params
- * @param $userId
- * @return bool
- */
- public function confirm($params, $userId)
- {
- $id = isset($params['id']) ? $params['id'] : 0;
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (empty($id) || empty($info)) {
- $this->error = 2042;
- return false;
- }
- if (!in_array($info['status'], [1, 2])) {
- $this->error = 2044;
- return false;
- }
- DB::beginTransaction();
- $data = ['confirm_time' => time(), 'is_pay' => 1, 'status' => 3, 'update_time' => time()];
- if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
- $this->error = 2050;
- DB::rollBack();
- return false;
- }
- DB::commit();
- $this->error = 2049;
- return true;
- }
- /**
- * 确认收款
- * @param $params
- * @param $userId
- * @return bool
- */
- public function confirm1($params, $userId)
- {
- $id = isset($params['id']) ? $params['id'] : 0;
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (empty($id) || empty($info)) {
- $this->error = 2042;
- return false;
- }
- if (!in_array($info['status'], [1, 2])) {
- $this->error = 2044;
- return false;
- }
- DB::beginTransaction();
- $data = ['confirm_time' => time(), 'is_pay' => 1, 'status' => 3, 'update_time' => time()];
- if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
- $this->error = 2050;
- DB::rollBack();
- return false;
- }
- $this->model->where(['goods_id' => $info['goods_id'], 'status' => 4, 'mark' => 1])->update(['is_out' => 1, 'update_time' => time()]);
- // 更改商品归属人
- if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id' => $info['user_id'], 'update_time' => time()])) {
- $this->error = 2050;
- DB::rollBack();
- return false;
- }
- $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
- $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
- $meritsCount = isset($memberInfo['merits_count']) ? $memberInfo['merits_count'] : 0;
- $updateData = ['merits_count' => $meritsCount + $info['real_price'], 'merits_time' => date('Y-m-d H:i:s'), 'member_level' => 1, 'update_time' => time()];
- if (!MemberModel::where(['id' => $info['user_id']])->update($updateData)) {
- $this->error = 2050;
- DB::rollBack();
- return false;
- }
- // 佣金结算
- $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
- $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
- $bonusRate = $bonusRate ? $bonusRate : 5;
- $bonus = $info['real_price'] * $bonusRate / 100;
- $profitRate = ConfigService::make()->getConfigByCode('profit_rate');
- $profitRate = $profitRate ? $profitRate : 0;
- $profit = $info['real_price'] * $profitRate / 100;
- if (!$this->model->where(['id' => $id, 'mark' => 1])->update(['bonus' => $bonus, 'profit' => $profit, 'update_time' => time()])) {
- $this->error = 2051;
- DB::rollBack();
- return true;
- }
- // 收益记录
- $data = [
- 'user_id' => $info['user_id'],
- 'shop_id' => $info['shop_id'],
- 'source_uid' => 0,
- 'source_order_sn' => $info['order_sn'],
- 'type' => 2,
- 'coin_type' => 5,
- 'money' => $profit,
- 'balance' => 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '抢拍收益',
- 'status' => 1,
- 'mark' => 1
- ];
- if (!AccountModel::insertGetId($data)) {
- $this->error = 2051;
- DB::rollBack();
- return true;
- }
- // 佣金入账
- if ($memberInfo && $parentId && $parentInfo && $bonus > 0) {
- $meritsTotal = isset($parentInfo['merits_total']) ? $parentInfo['merits_total'] : 0;
- $updateData = ['merits_total' => $meritsTotal + $info['real_price'], 'bonus' => $parentInfo['bonus'] + $bonus, 'bonus_total' => $parentInfo['bonus_total'] + $bonus, 'update_time' => time()];
- if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update($updateData)) {
- $this->error = 2051;
- DB::rollBack();
- return true;
- }
- // 佣金记录
- $data = [
- 'user_id' => $parentId,
- 'shop_id' => $info['shop_id'],
- 'source_uid' => $info['user_id'],
- 'source_order_sn' => $info['order_sn'],
- 'type' => 2,
- 'coin_type' => 2,
- 'money' => $bonus,
- 'balance' => $parentInfo['bonus'],
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '推广佣金',
- 'status' => 1,
- 'mark' => 1
- ];
- if (!AccountModel::insertGetId($data)) {
- $this->error = 2051;
- DB::rollBack();
- return true;
- }
- // 结算统计
- FinanceService::make()->settleBonus($bonus, 2);
- }
- DB::commit();
- // 店铺统计
- $shopId = isset($info['shop_id']) ? $info['shop_id'] : 0;
- $shopInfo = ShopModel::where(['id' => $shopId])->first();
- if ($shopInfo) {
- ShopModel::where(['id' => $shopId])->update(['trade_count' => $shopInfo['trade_count'], 'trade_total' => $info['real_price'] + $shopInfo['trade_total']]);
- }
- $this->error = 2049;
- return true;
- }
- /**
- * 申请待售
- * @param $params
- * @param $userId
- * @return false
- */
- public function sell($params, $userId)
- {
- $id = isset($params['id']) ? $params['id'] : 0;
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (empty($id) || empty($info)) {
- $this->error = 2042;
- return false;
- }
- if ($info['status'] != 3) {
- $this->error = 2053;
- return false;
- }
- if ($info['user_id'] != $userId) {
- $this->error = 2045;
- return false;
- }
- DB::beginTransaction();
- if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 1, 'update_time' => time()])) {
- DB::rollBack();
- $this->error = 2054;
- return false;
- }
- if (!GoodsModel::where(['id' => $info['goods_id']])->update(['confirm_status' => 2, 'update_time' => time()])) {
- DB::rollBack();
- $this->error = 2054;
- return false;
- }
- DB::commit();
- $this->error = 2055;
- return true;
- }
- /**
- * 申请待售审核
- * @param $params
- * @param $userId
- * @return false
- */
- public function sellConfirm($params)
- {
- $id = isset($params['id']) ? $params['id'] : 0;
- $info = $this->model->from('trade as a')
- ->leftJoin('goods as b', 'b.id', '=', 'a.goods_id')
- ->where(['a.id' => $id, 'a.mark' => 1])
- ->select(['a.*', 'b.split_price', 'b.split_num'])
- ->first();
- if (empty($id) || empty($info)) {
- $this->error = 2042;
- return false;
- }
- if (!in_array($info['status'], [3, 4])) {
- $this->error = 2053;
- return false;
- }
- DB::beginTransaction();
- $priceRate = ConfigService::make()->getConfigByCode('price_rate');
- $priceRate = $priceRate ? $priceRate : 4;
- $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
- $stopSplitPrice = $stopSplitPrice ? $stopSplitPrice : 500;
- // 判断是否可以上架或拆分
- if ($info['split_price'] <= 0) {
- $splitPrice = ConfigService::make()->getConfigByCode('split_price');
- $splitPrice = $splitPrice ? $splitPrice : 10000;
- $info['split_price'] = $splitPrice;
- }
- $realPrice = $info['real_price'];
- $sellPrice = $info['sell_price']; // 特价
- $price = $info['price']; // 买入价格
- $price1 = $info['new_price']; // 买入价格
- $addPrice = intval($realPrice * $priceRate / 100, 0);
- // 满足涨价上架
- if ($price1 + $addPrice < $info['split_price']) {
- // 更新价格
- if (!$this->model->where(['id' => $id])->update(['status' => 4, 'new_price' => $price1 + $addPrice, 'new_real_price' => $realPrice + $addPrice, 'is_sell' => 2, 'update_time' => time()])) {
- $this->error = 2056;
- DB::rollBack();
- return false;
- }
- // 更新其他交易状态
- $this->model->where(['goods_id' => $info['goods_id'], 'status' => 4, 'mark' => 1])->whereNotIn('id', [$id])->update(['is_out' => 1, 'update_time' => time()]);
- if (!GoodsModel::where(['id' => $info['goods_id']])->update(['user_id'=>$info['user_id'],'last_sell_time' => time(), 'price' => $price1 + $addPrice, 'real_price' => $realPrice + $addPrice, 'is_trade' => 2, 'confirm_status' => 1, 'remark' => '上架审核 ' . date('Y-m-d H:i:s'), 'update_time' => time()])) {
- $this->error = 2056;
- DB::rollBack();
- return false;
- }
- DB::commit();
- // 佣金结算
- if (!TradeService::make()->tradeSettle($id, $info)) {
- return false;
- }
- $this->error = 2057;
- return true;
- } // 停止拆分
- else if ($info['sell_price'] == $stopSplitPrice) {
- // 更新审核状态
- if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 2, 'update_time' => time()])) {
- $this->error = 2056;
- DB::rollBack();
- return false;
- }
- // 更改商品归属人
- if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id' => $info['user_id'], 'update_time' => time()])) {
- $this->error = 2050;
- DB::rollBack();
- return false;
- }
- if (!GoodsModel::where(['id' => $info['goods_id']])->update(['status' => 1, 'confirm_status' => 2, 'split_stop' => 1, 'update_time' => time()])) {
- $this->error = 2054;
- DB::rollBack();
- return false;
- }
- // 佣金结算
- DB::commit();
- if (!TradeService::make()->tradeSettle($id, $info)) {
- return false;
- }
- $this->error = 2064;
- return true;
- } // 满足拆分
- else if ($info['split_price']) {
- // 更新审核状态
- if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 2, 'update_time' => time()])) {
- $this->error = 2056;
- DB::rollBack();
- return false;
- }
- // 更新其他交易状态
- $this->model->where(['goods_id' => $info['goods_id'], 'status' => 4, 'mark' => 1])->whereNotIn('id', [$id])->update(['is_out' => 1, 'update_time' => time()]);
- // 更改商品归属人
- if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id' => $info['user_id'], 'update_time' => time()])) {
- $this->error = 2050;
- DB::rollBack();
- return false;
- }
- if (!GoodsService::make()->split($info['goods_id'], $info)) {
- $this->error = 2058;
- DB::rollBack();
- return false;
- }
- // 佣金结算
- DB::commit();
- if (!TradeService::make()->tradeSettle($id, $info)) {
- return false;
- }
- $this->error = 2059;
- return true;
- }
- $this->error = 2056;
- return false;
- }
- /**
- * 交易结算
- * @param $tradeId
- * @param $info
- * @return bool
- */
- public function tradeSettle($tradeId, $info)
- {
- // 更新商品交易状态
- DB::beginTransaction();
- // 交易次数
- GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->increment('trade_count', 1);
- $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
- $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
- $meritsCount = isset($memberInfo['merits_count']) ? $memberInfo['merits_count'] : 0;
- $updateData = ['merits_count' => $meritsCount + $info['real_price'], 'merits_time' => date('Y-m-d H:i:s'), 'member_level' => 1, 'update_time' => time()];
- if (!MemberModel::where(['id' => $info['user_id']])->update($updateData)) {
- $this->error = 2050;
- DB::rollBack();
- return false;
- }
- // 佣金结算
- $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
- $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
- $bonusRate = $bonusRate ? $bonusRate : 5;
- $bonus = $info['real_price'] * $bonusRate / 100;
- $profitRate = ConfigService::make()->getConfigByCode('profit_rate');
- $profitRate = $profitRate ? $profitRate : 0;
- $profit = $info['real_price'] * $profitRate / 100;
- // 平台服务费
- $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
- $serviceRate = $serviceRate ? $serviceRate : 8;
- $serviceFee = round($info['real_price'] * $serviceRate / 100, 0);
- if (!$this->model->where(['id' => $tradeId, 'mark' => 1])->update(['bonus' => $bonus,'service_fee'=> $serviceFee, 'profit' => $profit, 'update_time' => time()])) {
- $this->error = 2051;
- DB::rollBack();
- return true;
- }
- // 服务费统计
- if($serviceFee>0){
- FinanceService::make()->settleBonus($serviceFee, 1);
- }
- // 收益记录
- $data = [
- 'user_id' => $info['user_id'],
- 'shop_id' => $info['shop_id'],
- 'source_uid' => 0,
- 'source_order_sn' => $info['order_sn'],
- 'type' => 2,
- 'coin_type' => 5,
- 'money' => $profit,
- 'balance' => 0,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '抢拍收益',
- 'status' => 1,
- 'mark' => 1
- ];
- if (!AccountModel::insertGetId($data)) {
- $this->error = 2051;
- DB::rollBack();
- return true;
- }
- // 佣金入账
- if ($memberInfo && $parentId && $parentInfo && $bonus > 0) {
- $meritsTotal = isset($parentInfo['merits_total']) ? $parentInfo['merits_total'] : 0;
- $updateData = ['merits_total' => $meritsTotal + $info['real_price'], 'bonus' => $parentInfo['bonus'] + $bonus, 'bonus_total' => $parentInfo['bonus_total'] + $bonus, 'update_time' => time()];
- if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update($updateData)) {
- $this->error = 2051;
- DB::rollBack();
- return true;
- }
- // 佣金记录
- $data = [
- 'user_id' => $parentId,
- 'shop_id' => $info['shop_id'],
- 'source_uid' => $info['user_id'],
- 'source_order_sn' => $info['order_sn'],
- 'type' => 2,
- 'coin_type' => 2,
- 'money' => $bonus,
- 'balance' => $parentInfo['bonus'],
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '推广佣金',
- 'status' => 1,
- 'mark' => 1
- ];
- if (!AccountModel::insertGetId($data)) {
- $this->error = 2051;
- DB::rollBack();
- return true;
- }
- // 结算统计
- FinanceService::make()->settleBonus($bonus, 2);
- }
- DB::commit();
- // 店铺统计
- $shopId = isset($info['shop_id']) ? $info['shop_id'] : 0;
- $shopInfo = ShopModel::where(['id' => $shopId])->first();
- if ($shopInfo) {
- ShopModel::where(['id' => $shopId])->update(['trade_count' => $shopInfo['trade_count'] + 1, 'trade_total' => $info['real_price'] + $shopInfo['trade_total']]);
- }
- return true;
- }
- /**
- * 修改订单
- * @param $params
- * @return bool
- */
- public function modify($params)
- {
- $id = isset($params['id']) ? $params['id'] : 0;
- $status = isset($params['status']) ? $params['status'] : 0;
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (!$id || empty($info)) {
- $this->error = 2042;
- return false;
- }
- if (!in_array($info['status'], [1, 2, 3, 4])) {
- $this->error = 2082;
- return false;
- }
- if (!in_array($status, [1, 2, 4])) {
- $this->error = 2087;
- return false;
- }
- // $safePassword = isset($params['password'])? trim($params['password']) : '';
- // if(empty($safePassword)){
- // $this->error = 2085;
- // return false;
- // }
- $memberInfo = MemberModel::where(['id' => $info['user_id']])->select(['id', 'safe_password'])->first();
- $password = isset($memberInfo['safe_password']) ? $memberInfo['safe_password'] : '';
- if (empty($memberInfo)) {
- $this->error = 2019;
- return false;
- }
- // $safePassword = get_password($safePassword);
- // if($password != $safePassword){
- // $this->error = 2086;
- // return false;
- // }
- if ($this->model->where(['id' => $id])->update(['status' => $status, 'update_time' => time()])) {
- $this->error = 1020;
- return true;
- } else {
- $this->error = 1021;
- return false;
- }
- }
- /**
- * 修改订单
- * @param $params
- * @return bool
- */
- public function cancel($params)
- {
- $id = isset($params['id']) ? $params['id'] : 0;
- $status = isset($params['status']) ? $params['status'] : 0;
- $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
- if (!$id || empty($info)) {
- $this->error = 2042;
- return false;
- }
- if (!in_array($info['status'], [1, 2, 3, 4])) {
- $this->error = 2082;
- return false;
- }
- DB::beginTransaction();
- if (!$this->model->where(['id' => $id])->update(['status' => 5,'remark'=>'店长取消订单:'.date('Y-m-d H:i:s'), 'update_time' => time()])) {
- DB::rollBack();
- $this->error = 2089;
- return false;
- }
- if(!GoodsModel::where(['id'=> $info['goods_id'],'mark'=>1])->update(['status'=>1,'is_trade'=>2,'confirm_status'=>1,'remark'=>'店长取消订单'])){
- DB::rollBack();
- $this->error = 2089;
- return false;
- }
- DB::commit();
- $this->error = 2088;
- return true;
- }
- /**
- * 定期清除交易记录
- * @return false
- */
- public function clearByDay()
- {
- $day = ConfigService::make()->getConfigByCode('clear_trade_time');
- $day = $day ? $day : 2;
- $cacheKey = "caches:task:clearTrade:d{$day}_" . date('Ymd');
- if (RedisService::get($cacheKey)) {
- $this->error = '2301';
- return false;
- }
- // 取消订单
- $this->model->where('create_time', '<', strtotime(date('Y-m-d')))
- ->where('create_time', '<', time() - 7200)->where(['mark' => 1])
- ->where('status','<',4)
- ->update(['mark'=>0,'remark'=>'订单未付款取消']);
- // 清除订单
- $clearDay = strtotime(date('Y-m-d')) - $day * 86400;
- $count = $this->model->where('create_time', '<', $clearDay)->count();
- if ($count <= 0) {
- $this->error = '';
- RedisService::set($cacheKey, ['error' => '没有记录可以清除', 'day' => $day, 'clearDay' => date('Y-m-d', $clearDay)], 6 * 3600);
- return false;
- }
- $this->model->where('create_time', '<', $clearDay)->update(['mark' => 0, 'remark' => '到期清除', 'update_time' => time()]);
- $lists = $this->model->where('create_time', '<', $clearDay - 86400)->where(['mark' => 0])->select(['id','pay_img'])->get();
- if($lists){
- $lists = $lists->toArray();
- foreach ($lists as $v){
- $payImg = isset($v['pay_img'])? $v['pay_img'] : '';
- if($payImg && file_exists(ATTACHMENT_PATH.$payImg)){
- unlink(ATTACHMENT_PATH.$payImg);
- }
- }
- }
- $this->model->where('create_time', '<', $clearDay - 86400)->where(['mark' => 0])->delete();
- $result = ['count' => $count, 'day' => date('Y-m-d H:i:s', $clearDay)];
- RedisService::set($cacheKey, $result, 7200);
- return $result;
- }
- }
|