TradeService.php 34 KB

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