TradeService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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\Common\FinanceService;
  19. use App\Services\RedisService;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 交易管理-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. * Class TradeService
  26. * @package App\Services\Common
  27. */
  28. class TradeService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * TradeService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new TradeModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * @param $params
  55. * @param int $pageSize
  56. * @return array
  57. */
  58. public function getDataList($params, $pageSize = 15)
  59. {
  60. $where = ['a.mark' => 1];
  61. $status = isset($params['status']) ? $params['status'] : 0;
  62. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  63. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  64. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  65. $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
  66. $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
  67. 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. $list = $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. ->where($where)
  84. ->where(function ($query) use ($params) {
  85. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  86. if ($keyword) {
  87. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")->orWhere('g.goods_name', 'like', "%{$keyword}%");
  88. }
  89. })
  90. ->where(function ($query) use ($params) {
  91. $time = isset($params['time']) ? $params['time'] : 0;
  92. if ($time) {
  93. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  94. }
  95. })
  96. ->where(function ($query) use ($userId,$sellUid, $status) {
  97. if ($sellUid && $userId) {
  98. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  99. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  100. });
  101. }else if($userId){
  102. $query->where('a.user_id', '=', $userId);
  103. }else if($sellUid){
  104. $query->where('a.sell_uid', '=', $sellUid);
  105. }
  106. })
  107. ->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.price', 'g.thumb'])
  108. ->orderBy('a.pay_time', 'desc')
  109. ->orderBy('a.id', 'desc')
  110. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  111. $list = $list ? $list->toArray() : [];
  112. if ($list) {
  113. foreach ($list['data'] as &$item) {
  114. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  115. $item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
  116. $item['confirm_time'] = $item['confirm_time'] ? datetime($item['confirm_time'], 'Y-m-d H:i:s') : '';
  117. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  118. $item['price'] = intval($item['price']);
  119. $item['real_price'] = intval($item['real_price']);
  120. $item['fee'] = intval($item['fee']);
  121. }
  122. }
  123. return [
  124. 'pageSize' => $pageSize,
  125. 'total' => isset($list['total']) ? $list['total'] : 0,
  126. 'counts' => [
  127. ''
  128. ],
  129. 'list' => isset($list['data']) ? $list['data'] : []
  130. ];
  131. }
  132. /**
  133. * 详情
  134. * @param $id
  135. * @return mixed
  136. */
  137. public function getInfo($id)
  138. {
  139. $info = $this->model->from('trade as a')
  140. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  141. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  142. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  143. ->where(['a.id'=>$id,'a.mark'=>1])
  144. ->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'])
  145. ->first();
  146. if($info){
  147. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  148. $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
  149. $info['confirm_time'] = $info['confirm_time'] ? datetime($info['confirm_time'], 'Y-m-d H:i:s') : '';
  150. $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
  151. $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  152. $info['price'] = intval($info['price']);
  153. $info['real_price'] = intval($info['real_price']);
  154. $info['fee'] = intval($info['fee']);
  155. $bankInfo = MemberBankService::make()->getBindInfo($info['sell_uid']);
  156. $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
  157. }
  158. return $info;
  159. }
  160. /**
  161. * 统计
  162. * @param $params
  163. * @return int[]
  164. */
  165. public function getCounts($params)
  166. {
  167. $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
  168. $where = ['a.mark' => 1];
  169. $status = isset($params['status']) ? $params['status'] : 0;
  170. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  171. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  172. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  173. $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
  174. $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
  175. if ($shopId > 0) {
  176. $where['a.shop_id'] = $shopId;
  177. }
  178. if ($parentId > 0) {
  179. $where['b.parent_id'] = $parentId;
  180. }
  181. if ($userId > 0) {
  182. $where['a.user_id'] = $userId;
  183. }
  184. if ($sellUid > 0) {
  185. $where['a.sell_uid'] = $sellUid;
  186. }
  187. if ($isAppeal > 0) {
  188. $where['a.is_appeal'] = $isAppeal;
  189. }
  190. if ($status > 0) {
  191. $where['a.status'] = $status;
  192. }
  193. $counts['total'] = intval($this->model->from('trade as a')
  194. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  195. ->where($where)
  196. ->where(function ($query) use ($params) {
  197. $time = isset($params['time']) ? $params['time'] : 0;
  198. if ($time) {
  199. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  200. }
  201. })
  202. ->where(function ($query) use ($userId,$sellUid, $status) {
  203. if ($sellUid && $userId) {
  204. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  205. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  206. });
  207. }else if($userId){
  208. $query->where('a.user_id', '=', $userId);
  209. }else if($sellUid){
  210. $query->where('a.sell_uid', '=', $sellUid);
  211. }
  212. })
  213. ->sum('real_price'));
  214. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  215. $bonusRate = $bonusRate ? $bonusRate : 5;
  216. $counts['bonus'] = intval($counts['total'] * $bonusRate / 100);
  217. $counts['fee'] = intval($this->model->from('trade as a')
  218. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  219. ->where($where)
  220. ->where(function ($query) use ($params) {
  221. $time = isset($params['time']) ? $params['time'] : 0;
  222. if ($time) {
  223. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  224. }
  225. })
  226. ->where(function ($query) use ($userId,$sellUid, $status) {
  227. if ($sellUid && $userId) {
  228. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  229. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  230. });
  231. }else if($userId){
  232. $query->where('a.user_id', '=', $userId);
  233. }else if($sellUid){
  234. $query->where('a.sell_uid', '=', $sellUid);
  235. }
  236. })
  237. ->sum('fee'));
  238. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  239. $serviceRate = $serviceRate ? $serviceRate : 8;
  240. $counts['service_fee'] = intval($counts['total'] * $serviceRate / 100);
  241. return $counts;
  242. }
  243. /**
  244. * 添加或编辑
  245. * @return array
  246. * @since 2020/11/11
  247. * @author laravel开发员
  248. */
  249. public function edit()
  250. {
  251. $data = request()->all();
  252. return parent::edit($data); // TODO: Change the autogenerated stub
  253. }
  254. /**
  255. * 获取店铺交易统计
  256. * @param $shopId
  257. * @param int $status
  258. * @return mixed
  259. */
  260. public function getShopTradeTotal($shopId, $status = 0)
  261. {
  262. $where = ['shop_id' => $shopId, 'mark' => 1];
  263. if ($status) {
  264. $where['status'] = $status;
  265. }
  266. return $this->model->where($where)
  267. ->sum('real_price');
  268. }
  269. /**
  270. * 获取交易数
  271. * @param $shopId
  272. * @param int $status
  273. * @return mixed
  274. */
  275. public function getShopTradeCount($shopId, $status = 0)
  276. {
  277. $where = ['shop_id' => $shopId, 'mark' => 1];
  278. if ($status) {
  279. $where['status'] = $status;
  280. }
  281. return $this->model->where($where)
  282. ->count('id');
  283. }
  284. /**
  285. * 获取用户交易统计
  286. * @param $userId
  287. * @param int $status
  288. * @param int $time
  289. * @return mixed
  290. */
  291. public function getUserTradeTotal($userId, $status = 0, $time = 0)
  292. {
  293. $where = ['user_id' => $userId, 'mark' => 1];
  294. return $this->model->where($where)
  295. ->where(function ($query) use ($status, $time) {
  296. $query->whereIn('status', is_array($status) ? $status : [$status]);
  297. // 本月
  298. if ($time == 1) {
  299. $query->where('pay_time', '>=', strtotime(date('Y-m-01')));
  300. } // 今日
  301. else if ($time == 2) {
  302. $query->where('pay_time', '>=', strtotime(date('Y-m-d')));
  303. }
  304. })
  305. ->sum('real_price');
  306. }
  307. /**
  308. * 获取用户团队交易统计
  309. * @param $userId
  310. * @param int $status
  311. * @param int $time
  312. * @return mixed
  313. */
  314. public function getTeamTradeTotal($userId, $status = 0, $time = 0)
  315. {
  316. $where = ['b.parent_id' => $userId, 'a.mark' => 1];
  317. return $this->model->from('trade as a')
  318. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  319. ->where($where)
  320. ->where(function ($query) use ($status, $time) {
  321. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  322. // 本月
  323. if ($time == 1) {
  324. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  325. } // 今日
  326. else if ($time == 2) {
  327. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  328. }
  329. })
  330. ->sum('a.real_price');
  331. }
  332. /**
  333. * 抢拍交易订单数
  334. * @param $userId 用户ID
  335. * @param int $status 状态
  336. * @return mixed
  337. */
  338. public function getNewTradeCountByStatus($userId, $status = 0)
  339. {
  340. $where = ['user_id' => $userId, 'mark' => 1, 'is_read' => 0];
  341. $counts = $this->model->where($where)
  342. ->where(function ($query) use ($status) {
  343. $query->whereIn('status', is_array($status) ? $status : [$status]);
  344. })
  345. ->select(['status', DB::raw('count(*) as count')])
  346. ->groupBy('status')
  347. ->get();
  348. if ($counts) {
  349. $temps = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  350. foreach ($counts as $v) {
  351. $temps['status' . $v['status']] = $v['count'];
  352. }
  353. $counts = $temps;
  354. } else {
  355. $counts = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  356. }
  357. return $counts;
  358. }
  359. /**
  360. * 抢购
  361. * @param $params
  362. * @param $userId
  363. * @param $shopId
  364. * @return bool
  365. */
  366. public function buy($params, $userId, $shopId)
  367. {
  368. $goodsId = isset($params['id']) ? $params['id'] : 0;
  369. $info = GoodsService::make()->getInfo(['id' => $goodsId, 'status' => 1, 'mark' => 1]);
  370. $goodsUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  371. $isTrade = isset($info['is_trade']) ? $info['is_trade'] : 0;
  372. if (empty($info)) {
  373. $this->error = 2031;
  374. return false;
  375. }
  376. if ($isTrade == 1) {
  377. $this->error = 2032;
  378. return false;
  379. }
  380. if ($goodsUserId == $userId) {
  381. $this->error = 2036;
  382. return false;
  383. }
  384. $shopInfo = ShopService::make()->getInfo($shopId);
  385. if (empty($shopInfo)) {
  386. $this->error = 2033;
  387. return false;
  388. }
  389. // 营业时间
  390. $curTime = time();
  391. $startTime = isset($shopInfo['start_time']) ? $shopInfo['start_time'] : '';
  392. $endTime = isset($shopInfo['end_time']) ? $shopInfo['end_time'] : '';
  393. // 店长自己抢
  394. if ($shopInfo['user_id'] == $userId) {
  395. $snapTime = ConfigService::make()->getConfigByCode('snap_time');
  396. $snapTime = $snapTime ? $snapTime : 5;
  397. $curTime = strtotime(date('Y-m-d') . ' ' . $startTime) + 1;
  398. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime) - $snapTime * 60) {
  399. $this->error = 2034;
  400. return false;
  401. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  402. $this->error = 2035;
  403. return false;
  404. }
  405. } else {
  406. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime)) {
  407. $this->error = 2034;
  408. return false;
  409. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  410. $this->error = 2035;
  411. return false;
  412. }
  413. }
  414. // 验证收款账号
  415. if (!MemberBankService::make()->getBindInfo($userId)) {
  416. $this->error = 2037;
  417. return false;
  418. }
  419. $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  420. $feeRate = $feeRate ? $feeRate : '2.5';
  421. $realPrice = intval($info['price'] - $info['sell_price'] + $info['source_price']);
  422. $fee = round($realPrice * $feeRate / 100, 0);
  423. $lockCacheKey = "caches:trade:lock:{$goodsId}";
  424. if (RedisService::get($lockCacheKey)) {
  425. $this->error = 2032;
  426. return false;
  427. }
  428. RedisService::set($lockCacheKey, $info, rand(1, 2));
  429. $data = [
  430. 'order_sn' => get_order_num('T'),
  431. 'goods_id' => $goodsId,
  432. 'user_id' => $userId,
  433. 'shop_id' => $shopId,
  434. 'sell_uid' => $info['user_id'],
  435. 'num' => 1,
  436. 'price' => $info['price'],
  437. 'source_price' => $info['source_price'],
  438. 'sell_price' => $info['sell_price'],
  439. 'real_price' => $realPrice,
  440. 'fee' => $fee,
  441. 'new_price' => $info['price'],
  442. 'remark' => '抢拍交易',
  443. 'create_time' => $curTime,
  444. 'update_time' => $curTime,
  445. 'status' => 1
  446. ];
  447. DB::beginTransaction();
  448. if (!TradeModel::insertGetId($data)) {
  449. DB::rollBack();
  450. $this->error = 2040;
  451. return false;
  452. }
  453. if (!GoodsModel::where(['id' => $goodsId])->update(['is_trade' => 1, 'update_time' => time()])) {
  454. DB::rollBack();
  455. $this->error = 2040;
  456. return false;
  457. }
  458. DB::commit();
  459. RedisService::keyDel($lockCacheKey);
  460. $this->error = 2041;
  461. return true;
  462. }
  463. /**
  464. * 付款
  465. * @param $params
  466. * @param $userId
  467. * @param $shopId
  468. * @return bool
  469. */
  470. public function pay($params, $userId, $shopId)
  471. {
  472. $id = isset($params['id']) ? $params['id'] : 0;
  473. $payImg = isset($params['pay_img']) ? $params['pay_img'] : '';
  474. if (empty($payImg)) {
  475. $this->error = 2043;
  476. return false;
  477. }
  478. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  479. if (empty($id) || empty($info)) {
  480. $this->error = 2042;
  481. return false;
  482. }
  483. if ($info['status'] != 1) {
  484. $this->error = 2044;
  485. return false;
  486. }
  487. if ($info['user_id'] != $userId) {
  488. $this->error = 2045;
  489. return false;
  490. }
  491. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['pay_time' => time(), 'pay_img' => $payImg, 'status' => 2, 'update_time' => time()])) {
  492. $this->error = 2046;
  493. return true;
  494. }
  495. $this->error = 2047;
  496. return false;
  497. }
  498. /**
  499. * 确认收款
  500. * @param $params
  501. * @param $userId
  502. * @return bool
  503. */
  504. public function confirm($params, $userId)
  505. {
  506. $id = isset($params['id']) ? $params['id'] : 0;
  507. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  508. if (empty($id) || empty($info)) {
  509. $this->error = 2042;
  510. return false;
  511. }
  512. if (!in_array($info['status'], [1, 2])) {
  513. $this->error = 2044;
  514. return false;
  515. }
  516. // if($info['sell_uid'] != $userId){
  517. // $this->error = 2045;
  518. // return false;
  519. // }
  520. DB::beginTransaction();
  521. $data = ['confirm_time' => time(),'is_pay'=>1, 'status' => 3, 'update_time' => time()];
  522. if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
  523. $this->error = 2050;
  524. DB::rollBack();
  525. return false;
  526. }
  527. // 更改商品归属人
  528. if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id'=> $info['user_id'],'update_time'=> time()])) {
  529. $this->error = 2050;
  530. DB::rollBack();
  531. return false;
  532. }
  533. if(!MemberModel::where(['id'=> $userId])->update(['memvr_level'=>1,'update_time'=>time()])){
  534. $this->error = 2050;
  535. DB::rollBack();
  536. return false;
  537. }
  538. // 佣金结算
  539. $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
  540. $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
  541. $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
  542. if ($memberInfo && $parentId && $parentInfo) {
  543. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  544. $bonusRate = $bonusRate ? $bonusRate : 5;
  545. $bonus = $info['real_price'] * $bonusRate / 100;
  546. if ($bonus > 0) {
  547. if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update(['bonus' => $parentInfo['bonus'] + $bonus, 'update_time' => time()])) {
  548. $this->error = 2051;
  549. DB::rollBack();
  550. return true;
  551. }
  552. $data = [
  553. 'user_id' => $parentId,
  554. 'shop_id' => $info['shop_id'],
  555. 'source_uid' => $userId,
  556. 'source_order_sn' => $info['order_sn'],
  557. 'type' => 2,
  558. 'coin_type' => 2,
  559. 'money' => $bonus,
  560. 'balance' => $parentInfo['bonus'],
  561. 'create_time' => time(),
  562. 'update_time' => time(),
  563. 'remark' => '推广佣金',
  564. 'status' => 1,
  565. 'mark' => 1
  566. ];
  567. if (!AccountModel::insertGetId($data)) {
  568. $this->error = 2051;
  569. DB::rollBack();
  570. return true;
  571. }
  572. // 结算统计
  573. // FinanceService::make()->settle($bonus, 1);
  574. FinanceService::make()->settleBonus($bonus, 1);
  575. }
  576. }
  577. DB::commit();
  578. $this->error = 2049;
  579. return true;
  580. }
  581. /**
  582. * 申请待售
  583. * @param $params
  584. * @param $userId
  585. * @return false
  586. */
  587. public function sell($params, $userId)
  588. {
  589. $id = isset($params['id']) ? $params['id'] : 0;
  590. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  591. if (empty($id) || empty($info)) {
  592. $this->error = 2042;
  593. return false;
  594. }
  595. if ($info['status'] != 3) {
  596. $this->error = 2053;
  597. return false;
  598. }
  599. if ($info['user_id'] != $userId) {
  600. $this->error = 2045;
  601. return false;
  602. }
  603. if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 1, 'update_time' => time()])) {
  604. $this->error = 2054;
  605. return false;
  606. }
  607. $this->error = 2055;
  608. return true;
  609. }
  610. /**
  611. * 申请待售审核
  612. * @param $params
  613. * @param $userId
  614. * @return false
  615. */
  616. public function sellConfirm($params)
  617. {
  618. $id = isset($params['id']) ? $params['id'] : 0;
  619. $info = $this->model->from('trade as a')
  620. ->leftJoin('goods as b','b.id','=','a.goods_id')
  621. ->where(['a.id' => $id, 'a.mark' => 1])
  622. ->select(['a.*','b.split_price','b.split_num'])
  623. ->first();
  624. if (empty($id) || empty($info)) {
  625. $this->error = 2042;
  626. return false;
  627. }
  628. if (!in_array($info['status'], [3,4])) {
  629. $this->error = 2053;
  630. return false;
  631. }
  632. DB::beginTransaction();
  633. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  634. $priceRate = $priceRate? $priceRate : 4;
  635. $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
  636. $stopSplitPrice = $stopSplitPrice? $stopSplitPrice : 500;
  637. // 判断是否可以上架或拆分
  638. $realPrice = $info['real_price'];
  639. $sellPrice = $info['sell_price']; // 特价
  640. $price = $info['price']; // 买入价格
  641. $price1 = $info['new_price']; // 买入价格
  642. $addPrice = intval($realPrice*$priceRate/100,0);
  643. // 满足涨价上架
  644. if($price1+$addPrice < $info['split_price']){
  645. if (!$this->model->where(['id' => $id])->update(['status' => 4,'new_price'=> $price1+$addPrice,'real_price'=> $realPrice + $addPrice, 'is_sell' => 1, 'update_time' => time()])) {
  646. $this->error = 2056;
  647. DB::rollBack();
  648. return false;
  649. }
  650. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['last_sell_time'=>time(), 'price' => $price1+$addPrice,'is_trade'=> 2, 'update_time' => time()])) {
  651. $this->error = 2056;
  652. DB::rollBack();
  653. return false;
  654. }
  655. DB::commit();
  656. $this->error = 2057;
  657. return true;
  658. }
  659. // 停止拆分
  660. else if($info['sell_price'] == $stopSplitPrice){
  661. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['status' => 2,'split_stop'=>1, 'update_time' => time()])) {
  662. $this->error = 2054;
  663. DB::rollBack();
  664. return false;
  665. }
  666. $this->error = 2064;
  667. DB::commit();
  668. return true;
  669. }
  670. // 满足拆分
  671. else if($info['split_price']) {
  672. if(!GoodsService::make()->split($info['goods_id'], $info)){
  673. $this->error = 2058;
  674. DB::rollBack();
  675. return false;
  676. }
  677. $this->error = 2059;
  678. DB::commit();
  679. return true;
  680. }
  681. $this->error = 2056;
  682. return false;
  683. }
  684. }