TradeService.php 39 KB

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