TradeService.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\AccountModel;
  13. use App\Models\GoodsModel;
  14. use App\Models\MemberModel;
  15. use App\Models\ShopModel;
  16. use App\Models\TradeModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 交易管理-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. * Class TradeService
  26. * @package App\Services\Common
  27. */
  28. class TradeService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * TradeService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new TradeModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * @param $params
  55. * @param int $pageSize
  56. * @return array
  57. */
  58. public function getDataList($params, $pageSize = 15)
  59. {
  60. $where = ['a.mark' => 1];
  61. $status = isset($params['status']) ? $params['status'] : 0;
  62. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  63. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  64. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  65. $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
  66. $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
  67. $type = isset($params['type']) ? $params['type'] : 0;
  68. if ($shopId > 0) {
  69. $where['a.shop_id'] = $shopId;
  70. }
  71. if ($parentId > 0) {
  72. $where['b.parent_id'] = $parentId;
  73. }
  74. if ($isAppeal > 0) {
  75. $where['a.is_appeal'] = $isAppeal;
  76. }
  77. if ($status > 0 && !$isAppeal) {
  78. $where['a.status'] = $status;
  79. }
  80. $model = $this->model->from('trade as a')
  81. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  82. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  83. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  84. ->leftJoin('shop as s', 's.id', '=', 'g.shop_id')
  85. ->where($where)
  86. ->where('a.status','>', 0)
  87. ->where(function($query) use($userId,$status){
  88. // if($status == 0){
  89. // $query->whereNotIn('a.sell_uid',[$userId]);
  90. // }
  91. // else if($status == 2){
  92. // $query->whereNotIn('a.user_id',[$userId]);
  93. // }
  94. })
  95. ->where(function($query) use($type){
  96. if($type){
  97. $query->where('a.create_time','>=', strtotime(date('Y-m-d')));
  98. }
  99. })
  100. ->where(function ($query) use ($params) {
  101. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  102. if ($keyword) {
  103. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")->orWhere('g.goods_name', 'like', "%{$keyword}%");
  104. }
  105. })
  106. ->where(function ($query) use ($params) {
  107. $time = isset($params['time']) ? $params['time'] : 0;
  108. if ($time) {
  109. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  110. }
  111. })
  112. ->where(function ($query) use ($type, $userId, $sellUid, $status) {
  113. if($type == 1){
  114. if($status == 0){
  115. $query->where(['a.user_id'=>$userId,'a.is_out'=>0]);
  116. $query->whereNotIn('a.sell_uid', [$userId]);
  117. }else if($status == 2){
  118. $query->where('a.sell_uid', '=',$userId);
  119. $query->whereNotIn('a.user_id', [$userId]);
  120. }else{
  121. $query->where(['a.user_id'=>$userId,'a.is_out'=>0]);
  122. }
  123. }else{
  124. if($userId){
  125. $query->where(['a.user_id'=>$userId,'a.is_out'=>0]);
  126. }else if($sellUid){
  127. $query->where('a.sell_uid', '=', $sellUid);
  128. }
  129. }
  130. /*if ($sellUid && $userId) {
  131. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  132. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  133. });
  134. }else if($userId){
  135. $query->where('a.user_id', '=', $userId);
  136. }else if($sellUid){
  137. $query->where('a.sell_uid', '=', $sellUid);
  138. }*/
  139. })
  140. ->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'])
  141. ->orderBy('a.create_time', 'desc')
  142. ->orderBy('a.id', 'desc');
  143. $totalModdel = $model;
  144. $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
  145. $total = $totalModdel->sum('a.real_price');
  146. $counts['total'] = intval($total);
  147. $bonus = $totalModdel->sum('a.bonus');
  148. $counts['bonus'] = intval($bonus);
  149. $serviceFee = $totalModdel->sum('a.service_fee');
  150. $counts['service_fee'] = intval($serviceFee);
  151. $fee = $totalModdel->sum('a.fee');
  152. $counts['fee'] = intval($fee);
  153. $list = $model->paginate($pageSize > 0 ? $pageSize : 9999999);
  154. $list = $list ? $list->toArray() : [];
  155. if ($list) {
  156. foreach ($list['data'] as &$item) {
  157. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  158. $item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
  159. $item['confirm_time'] = $item['confirm_time'] ? datetime($item['confirm_time'], 'Y-m-d H:i:s') : '';
  160. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  161. $item['pay_img'] = isset($item['pay_img']) && $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  162. $item['price'] = intval($item['price']);
  163. $item['real_price'] = intval($item['real_price']);
  164. $item['fee'] = intval($item['fee']);
  165. }
  166. }
  167. return [
  168. 'pageSize' => $pageSize,
  169. 'total' => isset($list['total']) ? $list['total'] : 0,
  170. 'counts' => $counts,
  171. 'list' => isset($list['data']) ? $list['data'] : []
  172. ];
  173. }
  174. /**
  175. * 转商品列表数据
  176. * @param $params
  177. * @param int $pageSize
  178. * @return array
  179. */
  180. public function getSwitchGoods($params, $pageSize = 15)
  181. {
  182. $where = ['a.mark' => 1,'a.status'=>1,'a.is_pay'=>2];
  183. $status = isset($params['status']) ? $params['status'] : 0;
  184. if ($status > 0) {
  185. $where['a.status'] = $status;
  186. }
  187. $isTrade = isset($params['is_trade']) ? $params['is_trade'] : 0;
  188. if ($isTrade > 0) {
  189. $where['a.is_trade'] = $isTrade;
  190. }
  191. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  192. if ($shopId > 0) {
  193. $where['a.shop_id'] = $shopId;
  194. }
  195. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  196. if ($userId > 0) {
  197. $where['a.user_id'] = $userId;
  198. }
  199. $list = $this->model->from('trade as a')
  200. ->leftJoin('member as b', 'b.id', '=', 'a.sell_uid')
  201. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  202. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  203. ->where($where)
  204. ->where(function ($query) use ($params) {
  205. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  206. if ($keyword) {
  207. $query->where('g.goods_name', 'like', "%{$keyword}%")->orWhere('c.name', 'like', "%{$keyword}%")->orWhere('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%");
  208. }
  209. $username = isset($params['username']) ? $params['username'] : '';
  210. if ($username) {
  211. $query->orWhere('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%");
  212. }
  213. })
  214. ->where(function ($query) use ($params) {
  215. $time = isset($params['time']) ? $params['time'] : '';
  216. if ($time) {
  217. $query->where('g.last_sell_time', '<=', $time);
  218. }
  219. })
  220. ->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.thumb'])
  221. ->orderBy('a.create_time', 'desc')
  222. ->orderBy('a.id', 'desc')
  223. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  224. $list = $list ? $list->toArray() : [];
  225. if ($list) {
  226. foreach ($list['data'] as &$item) {
  227. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  228. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  229. }
  230. }
  231. return [
  232. 'pageSize' => $pageSize,
  233. 'total' => isset($list['total']) ? $list['total'] : 0,
  234. 'list' => isset($list['data']) ? $list['data'] : []
  235. ];
  236. }
  237. /**
  238. * 详情
  239. * @param $id
  240. * @return mixed
  241. */
  242. public function getInfo($id)
  243. {
  244. $info = $this->model->from('trade as a')
  245. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  246. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  247. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  248. ->where(['a.id'=>$id,'a.mark'=>1])
  249. ->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'])
  250. ->first();
  251. if($info){
  252. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  253. $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
  254. $info['confirm_time'] = $info['confirm_time'] ? datetime($info['confirm_time'], 'Y-m-d H:i:s') : '';
  255. $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
  256. $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  257. $info['price'] = intval($info['price']);
  258. $info['real_price'] = intval($info['real_price']);
  259. $info['fee'] = intval($info['fee']);
  260. $bankInfo = MemberBankService::make()->getBindInfo($info['sell_uid']);
  261. $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
  262. }
  263. return $info;
  264. }
  265. /**
  266. * 统计
  267. * @param $params
  268. * @return int[]
  269. */
  270. public function getCounts($params)
  271. {
  272. $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
  273. $where = ['a.mark' => 1];
  274. $status = isset($params['status']) ? $params['status'] : 0;
  275. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  276. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  277. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  278. $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
  279. $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
  280. if ($shopId > 0) {
  281. $where['a.shop_id'] = $shopId;
  282. }
  283. if ($parentId > 0) {
  284. $where['b.parent_id'] = $parentId;
  285. }
  286. if ($userId > 0) {
  287. $where['a.user_id'] = $userId;
  288. }
  289. if ($sellUid > 0) {
  290. $where['a.sell_uid'] = $sellUid;
  291. }
  292. if ($isAppeal > 0) {
  293. $where['a.is_appeal'] = $isAppeal;
  294. }
  295. if ($status > 0) {
  296. $where['a.status'] = $status;
  297. }
  298. $counts['total'] = intval($this->model->from('trade as a')
  299. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  300. ->where($where)
  301. ->where(function ($query) use ($params) {
  302. $time = isset($params['time']) ? $params['time'] : 0;
  303. if ($time) {
  304. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  305. }
  306. })
  307. ->where(function ($query) use ($userId,$sellUid, $status) {
  308. if ($sellUid && $userId) {
  309. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  310. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  311. });
  312. }else if($userId){
  313. $query->where('a.user_id', '=', $userId);
  314. }else if($sellUid){
  315. $query->where('a.sell_uid', '=', $sellUid);
  316. }
  317. })
  318. ->sum('real_price'));
  319. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  320. $bonusRate = $bonusRate ? $bonusRate : 5;
  321. $counts['bonus'] = intval($counts['total'] * $bonusRate / 100);
  322. $counts['fee'] = intval($this->model->from('trade as a')
  323. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  324. ->where($where)
  325. ->where(function ($query) use ($params) {
  326. $time = isset($params['time']) ? $params['time'] : 0;
  327. if ($time) {
  328. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  329. }
  330. })
  331. ->where(function ($query) use ($userId,$sellUid, $status) {
  332. if ($sellUid && $userId) {
  333. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  334. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  335. });
  336. }else if($userId){
  337. $query->where('a.user_id', '=', $userId);
  338. }else if($sellUid){
  339. $query->where('a.sell_uid', '=', $sellUid);
  340. }
  341. })
  342. ->sum('fee'));
  343. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  344. $serviceRate = $serviceRate ? $serviceRate : 8;
  345. $counts['service_fee'] = intval($counts['total'] * $serviceRate / 100);
  346. return $counts;
  347. }
  348. /**
  349. * 添加或编辑
  350. * @return array
  351. * @since 2020/11/11
  352. * @author laravel开发员
  353. */
  354. public function edit()
  355. {
  356. $data = request()->all();
  357. return parent::edit($data); // TODO: Change the autogenerated stub
  358. }
  359. /**
  360. * 获取用户交易统计
  361. * @param $userId
  362. * @param int $status
  363. * @param int $time
  364. * @return mixed
  365. */
  366. public function getUserTradeTotal($userId, $status = 0, $time = 0)
  367. {
  368. $cacheKey = "caches:trade:userTotal:{$userId}_".(is_array($status)? implode('-', $status):$status)."_{$time}";
  369. $data = RedisService::get($cacheKey);
  370. if($data){
  371. return $data;
  372. }
  373. $where = ['a.user_id' => $userId, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1];
  374. $data = $this->model->from('trade as a')
  375. ->leftJoin('member as b','b.id','=','a.user_id')
  376. ->where($where)
  377. ->where(function ($query) use ($status, $time) {
  378. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  379. // 本月
  380. if ($time == 1) {
  381. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  382. } // 今日
  383. else if ($time == 2) {
  384. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  385. }
  386. })
  387. ->sum('a.real_price');
  388. RedisService::set($cacheKey, $data, rand(3,5));
  389. return $data;
  390. }
  391. /**
  392. * 获取用户团队交易统计
  393. * @param $userId
  394. * @param int $status
  395. * @param int $time
  396. * @return mixed
  397. */
  398. public function getTeamTradeTotal($userId, $status = 0, $time = 0)
  399. {
  400. $cacheKey = "caches:trade:teamTotal:{$userId}_".(is_array($status)? implode('-', $status):$status)."_{$time}";
  401. $data = RedisService::get($cacheKey);
  402. if($data){
  403. return $data;
  404. }
  405. $where = ['b.parent_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  406. $data = $this->model->from('trade as a')
  407. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  408. ->where($where)
  409. ->where(function ($query) use ($status, $time) {
  410. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  411. // 本月
  412. if ($time == 1) {
  413. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  414. } // 今日
  415. else if ($time == 2) {
  416. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  417. }
  418. })
  419. ->sum('a.real_price');
  420. RedisService::set($cacheKey, $data, rand(3,5));
  421. return $data;
  422. }
  423. /**
  424. * 获取用户佣金统计
  425. * @param $userId
  426. * @param int $time
  427. * @return mixed
  428. */
  429. public function getTradeBonusTotal($userId, $time = 0)
  430. {
  431. $cacheKey = "caches:trade:bonus:{$userId}_".(is_array($time)? implode('-', $time):$time);
  432. $data = RedisService::get($cacheKey);
  433. if($data){
  434. return round($data, 2);
  435. }
  436. $where = ['a.user_id' => $userId,'type'=> 2,'a.coin_type'=> 2,'a.status'=>1,'a.mark'=>1];
  437. $data = AccountModel::from('account_log as a')
  438. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  439. ->where($where)
  440. ->where(function ($query) use ($time) {
  441. // 本月
  442. if ($time == 1) {
  443. $query->where('a.create_time', '>=', strtotime(date('Y-m-01')));
  444. } // 今日
  445. else if ($time == 2) {
  446. $query->where('a.create_time', '>=', strtotime(date('Y-m-d')));
  447. }else if ($time){
  448. $month = date('Y-m', $time/1000);
  449. $lastDay = strtotime("{$month}-01 +1 month -1 day");
  450. if($month){
  451. $query->where('a.create_time', '>=', $time/1000);
  452. $query->where('a.create_time', '<', $lastDay+86400);
  453. }
  454. }
  455. })
  456. ->sum('a.money');
  457. RedisService::set($cacheKey, $data, rand(3,5));
  458. return round($data, 2);
  459. }
  460. /**
  461. * 获取用户收益统计
  462. * @param $userId
  463. * @param int $time
  464. * @return mixed
  465. */
  466. public function getTradeProfitTotal($userId, $time = 0)
  467. {
  468. $cacheKey = "caches:trade:profit:{$userId}_".(is_array($time)? implode('-', $time):$time);
  469. $data = RedisService::get($cacheKey);
  470. if($data){
  471. return round($data, 2);
  472. }
  473. $where = ['a.user_id' => $userId,'type'=> 2,'a.coin_type'=> 5,'a.status'=>1,'a.mark'=>1];
  474. $data = AccountModel::from('account_log as a')
  475. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  476. ->where($where)
  477. ->where(function ($query) use ($time) {
  478. // 本月
  479. if ($time == 1) {
  480. $query->where('a.create_time', '>=', strtotime(date('Y-m-01')));
  481. } // 今日
  482. else if ($time == 2) {
  483. $query->where('a.create_time', '>=', strtotime(date('Y-m-d')));
  484. }else if ($time){
  485. $month = date('Y-m', $time/1000);
  486. $lastDay = strtotime("{$month}-01 +1 month -1 day");
  487. if($month){
  488. $query->where('a.create_time', '>=', $time/1000);
  489. $query->where('a.create_time', '<', $lastDay+86400);
  490. }
  491. }
  492. })
  493. ->sum('a.money');
  494. RedisService::set($cacheKey, $data, rand(3,5));
  495. return round($data, 2);
  496. }
  497. /**
  498. * 获取用户佣金统计
  499. * @param $userId
  500. * @param int $status
  501. * @param int $time
  502. * @return mixed
  503. */
  504. public function getTradeBonusTotal1($userId, $status = 0, $time = 0)
  505. {
  506. $cacheKey = "caches:trade:bonus:{$userId}_".(is_array($status)? implode('-', $status):$status)."_".(is_array($time)? implode('-', $time):$time);
  507. $data = RedisService::get($cacheKey);
  508. if($data){
  509. return round($data, 2);
  510. }
  511. $where = ['b.parent_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  512. $data = $this->model->from('trade as a')
  513. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  514. ->where($where)
  515. ->where(function ($query) use ($status, $time) {
  516. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  517. // 本月
  518. if ($time == 1) {
  519. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  520. } // 今日
  521. else if ($time == 2) {
  522. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  523. }else if (is_array($time)){
  524. $start = isset($time[0])? $time[0] : '';
  525. $end = isset($time[1])? $time[1] : '';
  526. if($end && $end>$start){
  527. $query->where('a.pay_time', '<=', strtotime($end));
  528. if($start){
  529. $query->where('a.pay_time', '>=', strtotime($start));
  530. }
  531. }else if ($start){
  532. $query->where('a.pay_time', '=', strtotime($start));
  533. }
  534. }
  535. })
  536. ->sum('a.bonus');
  537. RedisService::set($cacheKey, $data, rand(3,5));
  538. return round($data, 2);
  539. }
  540. /**
  541. * 获取用户收益统计
  542. * @param $userId
  543. * @param int $status
  544. * @param int $time
  545. * @return mixed
  546. */
  547. public function getTradeProfitTotal1($userId, $status = 0, $time = 0)
  548. {
  549. $cacheKey = "caches:trade:profit:{$userId}_".(is_array($status)? implode('-', $status):$status)."_".(is_array($time)? implode('-', $time):$time);
  550. $data = RedisService::get($cacheKey);
  551. if($data){
  552. return round($data, 2);
  553. }
  554. $where = ['a.user_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  555. $data = $this->model->from('trade as a')
  556. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  557. ->where($where)
  558. ->where(function ($query) use ($status, $time) {
  559. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  560. // 本月
  561. if ($time == 1) {
  562. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  563. } // 今日
  564. else if ($time == 2) {
  565. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  566. }else if (is_array($time)){
  567. $start = isset($time[0])? $time[0] : '';
  568. $end = isset($time[1])? $time[1] : '';
  569. if($end && $end>$start){
  570. $query->where('a.pay_time', '<=', strtotime($end));
  571. if($start){
  572. $query->where('a.pay_time', '>=', strtotime($start));
  573. }
  574. }else if ($start){
  575. $query->where('a.pay_time', '=', strtotime($start));
  576. }
  577. }
  578. })
  579. ->sum('a.profit');
  580. RedisService::set($cacheKey, $data, rand(3,5));
  581. return round($data, 2);
  582. }
  583. /**
  584. * 抢拍交易订单数
  585. * @param $userId 用户ID
  586. * @param int $status 状态
  587. * @return mixed
  588. */
  589. public function getNewTradeCountByStatus($userId, $status = 0)
  590. {
  591. $where = ['user_id' => $userId, 'mark' => 1, 'is_read' => 0];
  592. $counts = $this->model->where($where)
  593. ->where(function ($query) use ($status) {
  594. $query->whereIn('status', is_array($status) ? $status : [$status]);
  595. })
  596. ->where('create_time','>=',strtotime(date('Y-m-d')))
  597. ->select(['status', DB::raw('count(*) as count')])
  598. ->groupBy('status')
  599. ->get();
  600. if ($counts) {
  601. $temps = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  602. foreach ($counts as $v) {
  603. $temps['status' . $v['status']] = $v['count'];
  604. }
  605. $counts = $temps;
  606. } else {
  607. $counts = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  608. }
  609. return $counts;
  610. }
  611. /**
  612. * 抢购
  613. * @param $params
  614. * @param $userId
  615. * @param $shopId
  616. * @return bool
  617. */
  618. public function buy($params, $userId, $shopId)
  619. {
  620. $goodsId = isset($params['id']) ? $params['id'] : 0;
  621. $info = GoodsService::make()->getInfo(['id' => $goodsId, 'status' => 1, 'mark' => 1]);
  622. $goodsUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  623. $isTrade = isset($info['is_trade']) ? $info['is_trade'] : 0;
  624. if (empty($info)) {
  625. $this->error = 2031;
  626. return false;
  627. }
  628. if ($isTrade == 1) {
  629. $this->error = 2032;
  630. return false;
  631. }
  632. if ($goodsUserId == $userId) {
  633. $this->error = 2036;
  634. return false;
  635. }
  636. $shopInfo = ShopService::make()->getInfo($shopId);
  637. if (empty($shopInfo)) {
  638. $this->error = 2033;
  639. return false;
  640. }
  641. // 营业时间
  642. $curTime = time();
  643. $startTime = isset($shopInfo['start_time']) ? $shopInfo['start_time'] : '';
  644. $endTime = isset($shopInfo['end_time']) ? $shopInfo['end_time'] : '';
  645. // 店长自己抢
  646. if ($shopInfo['user_id'] == $userId) {
  647. $snapTime = ConfigService::make()->getConfigByCode('snap_time');
  648. $snapTime = $snapTime ? $snapTime : 5;
  649. $curTime = strtotime(date('Y-m-d') . ' ' . $startTime) + 1;
  650. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime) - $snapTime * 60) {
  651. $this->error = 2034;
  652. return false;
  653. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  654. $this->error = 2035;
  655. return false;
  656. }
  657. } else {
  658. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime)) {
  659. $this->error = 2034;
  660. return false;
  661. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  662. $this->error = 2035;
  663. return false;
  664. }
  665. }
  666. // 验证收款账号
  667. if (!MemberBankService::make()->getBindInfo($userId)) {
  668. $this->error = 2037;
  669. return false;
  670. }
  671. $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  672. $feeRate = $feeRate ? $feeRate : '2.5';
  673. $realPrice = intval($info['price'] - $info['sell_price'] + $info['source_price']);
  674. $fee = round($realPrice * $feeRate / 100, 0);
  675. $lockCacheKey = "caches:trade:lock:{$goodsId}";
  676. if (RedisService::get($lockCacheKey)) {
  677. $this->error = 2032;
  678. return false;
  679. }
  680. RedisService::set($lockCacheKey, $info, rand(1, 2));
  681. $data = [
  682. 'order_sn' => get_order_num('T'),
  683. 'goods_id' => $goodsId,
  684. 'user_id' => $userId,
  685. 'shop_id' => $shopId,
  686. 'sell_uid' => $info['user_id'],
  687. 'num' => 1,
  688. 'price' => $info['price'],
  689. 'source_price' => $info['source_price'],
  690. 'sell_price' => $info['sell_price'],
  691. 'real_price' => $realPrice,
  692. 'new_real_price' => $realPrice,
  693. 'fee' => $fee,
  694. 'new_price' => $info['price'],
  695. 'remark' => '抢拍交易',
  696. 'create_time' => $curTime,
  697. 'update_time' => $curTime,
  698. 'status' => 1
  699. ];
  700. DB::beginTransaction();
  701. if (!TradeModel::insertGetId($data)) {
  702. DB::rollBack();
  703. $this->error = 2040;
  704. return false;
  705. }
  706. if (!GoodsModel::where(['id' => $goodsId])->update(['is_trade' => 1, 'update_time' => time()])) {
  707. DB::rollBack();
  708. $this->error = 2040;
  709. return false;
  710. }
  711. DB::commit();
  712. RedisService::keyDel($lockCacheKey);
  713. $this->error = 2041;
  714. return true;
  715. }
  716. /**
  717. * 付款
  718. * @param $params
  719. * @param $userId
  720. * @param $shopId
  721. * @return bool
  722. */
  723. public function pay($params, $userId, $shopId)
  724. {
  725. $id = isset($params['id']) ? $params['id'] : 0;
  726. $payImg = isset($params['pay_img']) ? $params['pay_img'] : '';
  727. if (empty($payImg)) {
  728. $this->error = 2043;
  729. return false;
  730. }
  731. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  732. if (empty($id) || empty($info)) {
  733. $this->error = 2042;
  734. return false;
  735. }
  736. if ($info['status'] != 1) {
  737. $this->error = 2044;
  738. return false;
  739. }
  740. if ($info['user_id'] != $userId) {
  741. $this->error = 2045;
  742. return false;
  743. }
  744. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['pay_time' => time(), 'pay_img' => $payImg, 'status' => 2, 'update_time' => time()])) {
  745. $this->error = 2046;
  746. return true;
  747. }
  748. $this->error = 2047;
  749. return false;
  750. }
  751. /**
  752. * 确认收款
  753. * @param $params
  754. * @param $userId
  755. * @return bool
  756. */
  757. public function confirm($params, $userId)
  758. {
  759. $id = isset($params['id']) ? $params['id'] : 0;
  760. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  761. if (empty($id) || empty($info)) {
  762. $this->error = 2042;
  763. return false;
  764. }
  765. if (!in_array($info['status'], [1, 2])) {
  766. $this->error = 2044;
  767. return false;
  768. }
  769. // if($info['sell_uid'] != $userId){
  770. // $this->error = 2045;
  771. // return false;
  772. // }
  773. DB::beginTransaction();
  774. $data = ['confirm_time' => time(),'is_pay'=>1, 'status' => 3, 'update_time' => time()];
  775. if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
  776. $this->error = 2050;
  777. DB::rollBack();
  778. return false;
  779. }
  780. $this->model->where(['goods_id' => $info['goods_id'],'status'=>4, 'mark' => 1])->update(['is_out'=>1,'update_time'=>time()]);
  781. // 更改商品归属人
  782. if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id'=> $info['user_id'],'update_time'=> time()])) {
  783. $this->error = 2050;
  784. DB::rollBack();
  785. return false;
  786. }
  787. $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
  788. $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
  789. $meritsCount = isset($memberInfo['merits_count']) ? $memberInfo['merits_count'] : 0;
  790. $updateData = ['merits_count'=> $meritsCount+$info['real_price'],'merits_time'=>date('Y-m-d H:i:s'),'member_level'=>1,'update_time'=>time()];
  791. if(!MemberModel::where(['id'=> $userId])->update($updateData)){
  792. $this->error = 2050;
  793. DB::rollBack();
  794. return false;
  795. }
  796. // 佣金结算
  797. $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
  798. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  799. $bonusRate = $bonusRate ? $bonusRate : 5;
  800. $bonus = $info['real_price'] * $bonusRate / 100;
  801. $profitRate = ConfigService::make()->getConfigByCode('profit_rate');
  802. $profitRate = $profitRate ? $profitRate : 0;
  803. $profit = $info['real_price'] * $profitRate / 100;
  804. if (!$this->model->where(['id' => $id, 'mark' => 1])->update(['bonus' => $bonus,'profit'=> $profit, 'update_time' => time()])) {
  805. $this->error = 2051;
  806. DB::rollBack();
  807. return true;
  808. }
  809. // 收益记录
  810. $data = [
  811. 'user_id' => $userId,
  812. 'shop_id' => $info['shop_id'],
  813. 'source_uid' => $userId,
  814. 'source_order_sn' => $info['order_sn'],
  815. 'type' => 2,
  816. 'coin_type' => 5,
  817. 'money' => $profit,
  818. 'balance' => 0,
  819. 'create_time' => time(),
  820. 'update_time' => time(),
  821. 'remark' => '抢拍收益',
  822. 'status' => 1,
  823. 'mark' => 1
  824. ];
  825. if (!AccountModel::insertGetId($data)) {
  826. $this->error = 2051;
  827. DB::rollBack();
  828. return true;
  829. }
  830. // 佣金入账
  831. if ($memberInfo && $parentId && $parentInfo && $bonus>0) {
  832. $meritsTotal = isset($parentInfo['merits_total'])? $parentInfo['merits_total'] : 0;
  833. $updateData = ['merits_total'=> $meritsTotal + $info['real_price'],'bonus' => $parentInfo['bonus'] + $bonus,'bonus_total'=> $parentInfo['bonus_total']+$bonus, 'update_time' => time()];
  834. if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update($updateData)) {
  835. $this->error = 2051;
  836. DB::rollBack();
  837. return true;
  838. }
  839. // 佣金记录
  840. $data = [
  841. 'user_id' => $parentId,
  842. 'shop_id' => $info['shop_id'],
  843. 'source_uid' => $userId,
  844. 'source_order_sn' => $info['order_sn'],
  845. 'type' => 2,
  846. 'coin_type' => 2,
  847. 'money' => $bonus,
  848. 'balance' => $parentInfo['bonus'],
  849. 'create_time' => time(),
  850. 'update_time' => time(),
  851. 'remark' => '推广佣金',
  852. 'status' => 1,
  853. 'mark' => 1
  854. ];
  855. if (!AccountModel::insertGetId($data)) {
  856. $this->error = 2051;
  857. DB::rollBack();
  858. return true;
  859. }
  860. // 结算统计
  861. FinanceService::make()->settleBonus($bonus, 2);
  862. }
  863. DB::commit();
  864. // 店铺统计
  865. $shopId = isset($info['shop_id']) ? $info['shop_id'] : 0;
  866. $shopInfo = ShopModel::where(['id'=> $shopId])->first();
  867. if($shopInfo){
  868. ShopModel::where(['id'=> $shopId])->update(['trade_count'=> $shopInfo['trade_count'], 'trade_total'=> $info['real_price']+$shopInfo['trade_total']]);
  869. }
  870. $this->error = 2049;
  871. return true;
  872. }
  873. /**
  874. * 申请待售
  875. * @param $params
  876. * @param $userId
  877. * @return false
  878. */
  879. public function sell($params, $userId)
  880. {
  881. $id = isset($params['id']) ? $params['id'] : 0;
  882. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  883. if (empty($id) || empty($info)) {
  884. $this->error = 2042;
  885. return false;
  886. }
  887. if ($info['status'] != 3) {
  888. $this->error = 2053;
  889. return false;
  890. }
  891. if ($info['user_id'] != $userId) {
  892. $this->error = 2045;
  893. return false;
  894. }
  895. DB::beginTransaction();
  896. if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 1, 'update_time' => time()])) {
  897. DB::rollBack();
  898. $this->error = 2054;
  899. return false;
  900. }
  901. if (!GoodsModel::where(['goods_id' => $info['goods_id']])->update(['confirm_status' => 2, 'update_time' => time()])) {
  902. DB::rollBack();
  903. $this->error = 2054;
  904. return false;
  905. }
  906. DB::commit();
  907. $this->error = 2055;
  908. return true;
  909. }
  910. /**
  911. * 申请待售审核
  912. * @param $params
  913. * @param $userId
  914. * @return false
  915. */
  916. public function sellConfirm($params)
  917. {
  918. $id = isset($params['id']) ? $params['id'] : 0;
  919. $info = $this->model->from('trade as a')
  920. ->leftJoin('goods as b','b.id','=','a.goods_id')
  921. ->where(['a.id' => $id, 'a.mark' => 1])
  922. ->select(['a.*','b.split_price','b.split_num'])
  923. ->first();
  924. if (empty($id) || empty($info)) {
  925. $this->error = 2042;
  926. return false;
  927. }
  928. if (!in_array($info['status'], [3,4])) {
  929. $this->error = 2053;
  930. return false;
  931. }
  932. DB::beginTransaction();
  933. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  934. $priceRate = $priceRate? $priceRate : 4;
  935. $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
  936. $stopSplitPrice = $stopSplitPrice? $stopSplitPrice : 500;
  937. // 判断是否可以上架或拆分
  938. $realPrice = $info['real_price'];
  939. $sellPrice = $info['sell_price']; // 特价
  940. $price = $info['price']; // 买入价格
  941. $price1 = $info['new_price']; // 买入价格
  942. $addPrice = intval($realPrice*$priceRate/100,0);
  943. // 满足涨价上架
  944. if($price1+$addPrice < $info['split_price']){
  945. // 平台服务费
  946. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  947. $serviceRate = $serviceRate ? $serviceRate : 8;
  948. $serviceFee = round(($realPrice)*$serviceRate/100, 0);
  949. if (!$this->model->where(['id' => $id])->update(['status' => 4,'service_fee'=> $serviceFee,'new_price'=> $price1+$addPrice,'new_real_price'=> $realPrice + $addPrice, 'is_sell' => 2, 'update_time' => time()])) {
  950. $this->error = 2056;
  951. DB::rollBack();
  952. return false;
  953. }
  954. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['last_sell_time'=>time(), 'price' => $price1+$addPrice,'real_price'=> $realPrice + $addPrice,'is_trade'=> 2,'confirm_status'=>1, 'update_time' => time()])) {
  955. $this->error = 2056;
  956. DB::rollBack();
  957. return false;
  958. }
  959. // 服务费统计
  960. FinanceService::make()->settleBonus($serviceFee, 1);
  961. DB::commit();
  962. $this->error = 2057;
  963. return true;
  964. }
  965. // 停止拆分
  966. else if($info['sell_price'] == $stopSplitPrice){
  967. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['status' => 2,'confirm_status'=>1,'split_stop'=>1, 'update_time' => time()])) {
  968. $this->error = 2054;
  969. DB::rollBack();
  970. return false;
  971. }
  972. $this->error = 2064;
  973. DB::commit();
  974. return true;
  975. }
  976. // 满足拆分
  977. else if($info['split_price']) {
  978. if(!GoodsService::make()->split($info['goods_id'], $info)){
  979. $this->error = 2058;
  980. DB::rollBack();
  981. return false;
  982. }
  983. $this->error = 2059;
  984. DB::commit();
  985. return true;
  986. }
  987. $this->error = 2056;
  988. return false;
  989. }
  990. /**
  991. * 修改订单
  992. * @param $params
  993. * @return bool
  994. */
  995. public function modify($params)
  996. {
  997. $id = isset($params['id'])? $params['id'] : 0;
  998. $status = isset($params['status'])? $params['status'] : 0;
  999. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  1000. if(!$id || empty($info)){
  1001. $this->error = 2042;
  1002. return false;
  1003. }
  1004. if(!in_array($info['status'], [1,2,3,4])){
  1005. $this->error = 2082;
  1006. return false;
  1007. }
  1008. if(!in_array($status, [1,2,4])){
  1009. $this->error =2087;
  1010. return false;
  1011. }
  1012. $safePassword = isset($params['password'])? trim($params['password']) : '';
  1013. if(empty($safePassword)){
  1014. $this->error = 2085;
  1015. return false;
  1016. }
  1017. $memberInfo = MemberModel::where(['id'=> $info['user_id']])->select(['id','safe_password'])->first();
  1018. $password = isset($memberInfo['safe_password'])? $memberInfo['safe_password'] : '';
  1019. if(empty($memberInfo)){
  1020. $this->error = 2019;
  1021. return false;
  1022. }
  1023. $safePassword = get_password($safePassword);
  1024. if($password != $safePassword){
  1025. $this->error = 2086;
  1026. return false;
  1027. }
  1028. if($this->model->where(['id'=> $id])->update(['status'=> $status,'update_time'=> time()])){
  1029. $this->error = 1020;
  1030. return true;
  1031. }else{
  1032. $this->error = 1021;
  1033. return false;
  1034. }
  1035. }
  1036. /**
  1037. * 修改订单
  1038. * @param $params
  1039. * @return bool
  1040. */
  1041. public function cancel($params)
  1042. {
  1043. $id = isset($params['id'])? $params['id'] : 0;
  1044. $status = isset($params['status'])? $params['status'] : 0;
  1045. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  1046. if(!$id || empty($info)){
  1047. $this->error = 2042;
  1048. return false;
  1049. }
  1050. if(!in_array($info['status'], [1,2,3,4])){
  1051. $this->error = 2082;
  1052. return false;
  1053. }
  1054. if($this->model->where(['id'=> $id])->update(['status'=> 5,'update_time'=> time()])){
  1055. $this->error = 2088;
  1056. return true;
  1057. }else{
  1058. $this->error = 2089;
  1059. return false;
  1060. }
  1061. }
  1062. /**
  1063. * 定期清除交易记录
  1064. * @return false
  1065. */
  1066. public function clearByDay()
  1067. {
  1068. $day = ConfigService::make()->getConfigByCode('clear_trade_time');
  1069. $day = $day? $day : 2;
  1070. $cacheKey = "caches:task:clearTrade:d{$day}_".date('Ymd');
  1071. if(RedisService::get($cacheKey)){
  1072. $this->error = '2301';
  1073. return false;
  1074. }
  1075. $clearDay = strtotime(date('Y-m-d')) - $day * 86400;
  1076. $count = $this->model->where('create_time','<', $clearDay)->where(['mark'=>1])->count();
  1077. if($count<=0){
  1078. $this->error = '';
  1079. RedisService::set($cacheKey,['error'=>'没有记录可以清除','day'=> $day,'clearDay'=> date('Y-m-d', $clearDay)], 6 * 3600);
  1080. return false;
  1081. }
  1082. $this->model->where('create_time','<', $clearDay)->update(['mark'=>0,'到期清除','update_time'=>time()]);
  1083. $this->model->where('create_time','<', $clearDay-86400)->delete();
  1084. return ['count'=> $count,'day'=> date('Y-m-d H:i:s', $clearDay)];
  1085. }
  1086. }