TradeService.php 35 KB

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