TradeService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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) {
  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(function($query) use($userId, $sellUid){
  99. $query->where('a.user_id', $userId)->orWhere('a.sell_uid', $sellUid);
  100. })->where('a.status','<=', 2);
  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.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. $info['bank_info'] = MemberBankService::make()->getBindInfo($info['sell_uid']);
  156. }
  157. return $info;
  158. }
  159. /**
  160. * 统计
  161. * @param $params
  162. * @return int[]
  163. */
  164. public function getCounts($params)
  165. {
  166. $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
  167. $where = ['a.mark' => 1];
  168. $status = isset($params['status']) ? $params['status'] : 0;
  169. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  170. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  171. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  172. $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
  173. $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
  174. if ($shopId > 0) {
  175. $where['a.shop_id'] = $shopId;
  176. }
  177. if ($parentId > 0) {
  178. $where['b.parent_id'] = $parentId;
  179. }
  180. if ($userId > 0) {
  181. $where['a.user_id'] = $userId;
  182. }
  183. if ($sellUid > 0) {
  184. $where['a.sell_uid'] = $sellUid;
  185. }
  186. if ($isAppeal > 0) {
  187. $where['a.is_appeal'] = $isAppeal;
  188. }
  189. if ($status > 0) {
  190. $where['a.status'] = $status;
  191. }
  192. $type = isset($params['type']) ? $params['type'] : 0;
  193. $counts['total'] = intval($this->model->from('trade as a')
  194. ->where($where)
  195. ->sum('real_price'));
  196. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  197. $bonusRate = $bonusRate ? $bonusRate : 5;
  198. $counts['bonus'] = intval($counts['total'] * $bonusRate / 100);
  199. $counts['fee'] = intval($this->model->from('trade as a')
  200. ->where($where)
  201. ->sum('fee'));
  202. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  203. $serviceRate = $serviceRate ? $serviceRate : 8;
  204. $counts['service_fee'] = intval($counts['total'] * $serviceRate / 100);
  205. return $counts;
  206. }
  207. /**
  208. * 添加或编辑
  209. * @return array
  210. * @since 2020/11/11
  211. * @author laravel开发员
  212. */
  213. public function edit()
  214. {
  215. $data = request()->all();
  216. return parent::edit($data); // TODO: Change the autogenerated stub
  217. }
  218. /**
  219. * 获取店铺交易统计
  220. * @param $shopId
  221. * @param int $status
  222. * @return mixed
  223. */
  224. public function getShopTradeTotal($shopId, $status = 0)
  225. {
  226. $where = ['shop_id' => $shopId, 'mark' => 1];
  227. if ($status) {
  228. $where['status'] = $status;
  229. }
  230. return $this->model->where($where)
  231. ->sum('price');
  232. }
  233. /**
  234. * 获取交易数
  235. * @param $shopId
  236. * @param int $status
  237. * @return mixed
  238. */
  239. public function getShopTradeCount($shopId, $status = 0)
  240. {
  241. $where = ['shop_id' => $shopId, 'mark' => 1];
  242. if ($status) {
  243. $where['status'] = $status;
  244. }
  245. return $this->model->where($where)
  246. ->count('id');
  247. }
  248. /**
  249. * 获取用户交易统计
  250. * @param $userId
  251. * @param int $status
  252. * @param int $time
  253. * @return mixed
  254. */
  255. public function getUserTradeTotal($userId, $status = 0, $time = 0)
  256. {
  257. $where = ['user_id' => $userId, 'mark' => 1];
  258. return $this->model->where($where)
  259. ->where(function ($query) use ($status, $time) {
  260. $query->whereIn('status', is_array($status) ? $status : [$status]);
  261. // 本月
  262. if ($time == 1) {
  263. $query->where('pay_time', '>=', strtotime(date('Y-m-01')));
  264. } // 今日
  265. else if ($time == 2) {
  266. $query->where('pay_time', '>=', strtotime(date('Y-m-d')));
  267. }
  268. })
  269. ->sum('price');
  270. }
  271. /**
  272. * 获取用户团队交易统计
  273. * @param $userId
  274. * @param int $status
  275. * @param int $time
  276. * @return mixed
  277. */
  278. public function getTeamTradeTotal($userId, $status = 0, $time = 0)
  279. {
  280. $where = ['b.parent_id' => $userId, 'a.mark' => 1];
  281. return $this->model->from('trade as a')
  282. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  283. ->where($where)
  284. ->where(function ($query) use ($status, $time) {
  285. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  286. // 本月
  287. if ($time == 1) {
  288. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  289. } // 今日
  290. else if ($time == 2) {
  291. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  292. }
  293. })
  294. ->sum('a.price');
  295. }
  296. /**
  297. * 抢拍交易订单数
  298. * @param $userId 用户ID
  299. * @param int $status 状态
  300. * @return mixed
  301. */
  302. public function getNewTradeCountByStatus($userId, $status = 0)
  303. {
  304. $where = ['user_id' => $userId, 'mark' => 1, 'is_read' => 0];
  305. $counts = $this->model->where($where)
  306. ->where(function ($query) use ($status) {
  307. $query->whereIn('status', is_array($status) ? $status : [$status]);
  308. })
  309. ->select(['status', DB::raw('count(*) as count')])
  310. ->groupBy('status')
  311. ->get();
  312. if ($counts) {
  313. $temps = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  314. foreach ($counts as $v) {
  315. $temps['status' . $v['status']] = $v['count'];
  316. }
  317. $counts = $temps;
  318. } else {
  319. $counts = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  320. }
  321. return $counts;
  322. }
  323. /**
  324. * 抢购
  325. * @param $params
  326. * @param $userId
  327. * @param $shopId
  328. * @return bool
  329. */
  330. public function buy($params, $userId, $shopId)
  331. {
  332. $goodsId = isset($params['id']) ? $params['id'] : 0;
  333. $info = GoodsService::make()->getInfo(['id' => $goodsId, 'status' => 1, 'mark' => 1]);
  334. $goodsUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  335. $isTrade = isset($info['is_trade']) ? $info['is_trade'] : 0;
  336. if (empty($info)) {
  337. $this->error = 2031;
  338. return false;
  339. }
  340. if ($isTrade == 1) {
  341. $this->error = 2032;
  342. return false;
  343. }
  344. if ($goodsUserId == $userId) {
  345. $this->error = 2036;
  346. return false;
  347. }
  348. $shopInfo = ShopService::make()->getInfo($shopId);
  349. if (empty($shopInfo)) {
  350. $this->error = 2033;
  351. return false;
  352. }
  353. // 营业时间
  354. $curTime = time();
  355. $startTime = isset($shopInfo['start_time']) ? $shopInfo['start_time'] : '';
  356. $endTime = isset($shopInfo['end_time']) ? $shopInfo['end_time'] : '';
  357. // 店长自己抢
  358. if ($shopInfo['user_id'] == $userId) {
  359. $snapTime = ConfigService::make()->getConfigByCode('snap_time');
  360. $snapTime = $snapTime ? $snapTime : 5;
  361. $curTime = strtotime(date('Y-m-d') . ' ' . $startTime) + 1;
  362. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime) - $snapTime * 60) {
  363. $this->error = 2034;
  364. return false;
  365. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  366. $this->error = 2035;
  367. return false;
  368. }
  369. } else {
  370. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime)) {
  371. $this->error = 2034;
  372. return false;
  373. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  374. $this->error = 2035;
  375. return false;
  376. }
  377. }
  378. // 验证收款账号
  379. if (!MemberBankService::make()->getBindInfo($userId)) {
  380. $this->error = 2037;
  381. return false;
  382. }
  383. $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  384. $feeRate = $feeRate ? $feeRate : '2.5';
  385. $realPrice = intval($info['price'] - $info['sell_price'] + $info['source_price']);
  386. $fee = round($realPrice * $feeRate / 100, 0);
  387. $lockCacheKey = "caches:trade:lock:{$goodsId}";
  388. if (RedisService::get($lockCacheKey)) {
  389. $this->error = 2032;
  390. return false;
  391. }
  392. RedisService::set($lockCacheKey, $info, rand(1, 2));
  393. $data = [
  394. 'order_sn' => get_order_num('T'),
  395. 'goods_id' => $goodsId,
  396. 'user_id' => $userId,
  397. 'shop_id' => $shopId,
  398. 'sell_uid' => $info['user_id'],
  399. 'num' => 1,
  400. 'price' => $info['price'],
  401. 'source_price' => $info['source_price'],
  402. 'sell_price' => $info['sell_price'],
  403. 'real_price' => $realPrice,
  404. 'fee' => $fee,
  405. 'new_price' => $info['price'],
  406. 'remark' => '抢拍交易',
  407. 'create_time' => $curTime,
  408. 'update_time' => $curTime,
  409. 'status' => 1
  410. ];
  411. DB::beginTransaction();
  412. if (!TradeModel::insertGetId($data)) {
  413. DB::rollBack();
  414. $this->error = 2040;
  415. return false;
  416. }
  417. if (!GoodsModel::where(['id' => $goodsId])->update(['is_trade' => 1, 'update_time' => time()])) {
  418. DB::rollBack();
  419. $this->error = 2040;
  420. return false;
  421. }
  422. DB::commit();
  423. RedisService::keyDel($lockCacheKey);
  424. $this->error = 2041;
  425. return true;
  426. }
  427. /**
  428. * 付款
  429. * @param $params
  430. * @param $userId
  431. * @param $shopId
  432. * @return bool
  433. */
  434. public function pay($params, $userId, $shopId)
  435. {
  436. $id = isset($params['id']) ? $params['id'] : 0;
  437. $payImg = isset($params['pay_img']) ? $params['pay_img'] : '';
  438. if (empty($payImg)) {
  439. $this->error = 2043;
  440. return false;
  441. }
  442. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  443. if (empty($id) || empty($info)) {
  444. $this->error = 2042;
  445. return false;
  446. }
  447. if ($info['status'] != 1) {
  448. $this->error = 2044;
  449. return false;
  450. }
  451. if ($info['user_id'] != $userId) {
  452. $this->error = 2045;
  453. return false;
  454. }
  455. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['pay_time' => time(), 'pay_img' => $payImg, 'status' => 2, 'update_time' => time()])) {
  456. $this->error = 2046;
  457. return true;
  458. }
  459. $this->error = 2047;
  460. return false;
  461. }
  462. /**
  463. * 确认收款
  464. * @param $params
  465. * @param $userId
  466. * @return bool
  467. */
  468. public function confirm($params, $userId)
  469. {
  470. $id = isset($params['id']) ? $params['id'] : 0;
  471. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  472. if (empty($id) || empty($info)) {
  473. $this->error = 2042;
  474. return false;
  475. }
  476. if (!in_array($info['status'], [1, 2])) {
  477. $this->error = 2044;
  478. return false;
  479. }
  480. // if($info['sell_uid'] != $userId){
  481. // $this->error = 2045;
  482. // return false;
  483. // }
  484. DB::beginTransaction();
  485. $data = ['confirm_time' => time(), 'status' => 3, 'update_time' => time()];
  486. if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
  487. $this->error = 2050;
  488. DB::rollBack();
  489. return false;
  490. }
  491. // 更改商品归属人
  492. if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id'=> $info['user_id'],'update_time'=> time()])) {
  493. $this->error = 2050;
  494. DB::rollBack();
  495. return false;
  496. }
  497. // 佣金结算
  498. $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
  499. $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
  500. $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
  501. if ($memberInfo && $parentId && $parentInfo) {
  502. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  503. $bonusRate = $bonusRate ? $bonusRate : 5;
  504. $bonus = $info['real_price'] * $bonusRate / 100;
  505. if ($bonus > 0) {
  506. if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update(['bonus' => $parentInfo['bonus'] + $bonus, 'update_time' => time()])) {
  507. $this->error = 2051;
  508. DB::rollBack();
  509. return true;
  510. }
  511. $data = [
  512. 'user_id' => $parentId,
  513. 'shop_id' => $info['shop_id'],
  514. 'source_uid' => $userId,
  515. 'source_order_sn' => $info['order_sn'],
  516. 'type' => 2,
  517. 'coin_type' => 2,
  518. 'money' => $bonus,
  519. 'balance' => $parentInfo['bonus'],
  520. 'create_time' => time(),
  521. 'update_time' => time(),
  522. 'remark' => '交易成功佣金结算',
  523. 'status' => 1,
  524. 'mark' => 1
  525. ];
  526. if (!AccountModel::insertGetId($data)) {
  527. $this->error = 2051;
  528. DB::rollBack();
  529. return true;
  530. }
  531. // 结算统计
  532. // FinanceService::make()->settle($bonus, 1);
  533. FinanceService::make()->settleBonus($bonus, 1);
  534. }
  535. }
  536. DB::commit();
  537. $this->error = 2049;
  538. return true;
  539. }
  540. /**
  541. * 申请待售
  542. * @param $params
  543. * @param $userId
  544. * @return false
  545. */
  546. public function sell($params, $userId)
  547. {
  548. $id = isset($params['id']) ? $params['id'] : 0;
  549. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  550. if (empty($id) || empty($info)) {
  551. $this->error = 2042;
  552. return false;
  553. }
  554. if ($info['status'] != 3) {
  555. $this->error = 2053;
  556. return false;
  557. }
  558. if ($info['user_id'] != $userId) {
  559. $this->error = 2045;
  560. return false;
  561. }
  562. if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 1, 'update_time' => time()])) {
  563. $this->error = 2054;
  564. return false;
  565. }
  566. $this->error = 2055;
  567. return false;
  568. }
  569. /**
  570. * 申请待售审核
  571. * @param $params
  572. * @param $userId
  573. * @return false
  574. */
  575. public function sellConfirm($params)
  576. {
  577. $id = isset($params['id']) ? $params['id'] : 0;
  578. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  579. if (empty($id) || empty($info)) {
  580. $this->error = 2042;
  581. return false;
  582. }
  583. if ($info['status'] != 3) {
  584. $this->error = 2053;
  585. return false;
  586. }
  587. DB::beginTransaction();
  588. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  589. $priceRate = $priceRate? $priceRate : 4;
  590. $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
  591. $stopSplitPrice = $stopSplitPrice? $stopSplitPrice : 500;
  592. // 判断是否可以上架或拆分
  593. $realPrice = $info['real_price'];
  594. $price = $info['price']; // 特价
  595. $addPrice = $realPrice*$priceRate/100;
  596. // 满足涨价上架
  597. if($price+$addPrice < $info['split_price']){
  598. if (!$this->model->where(['id' => $id])->update(['status' => 4,'new_price'=> $price+$addPrice,'real_price'=> $realPrice + $addPrice, 'is_sell' => 2, 'update_time' => time()])) {
  599. $this->error = 2056;
  600. DB::rollBack();
  601. return false;
  602. }
  603. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['last_sell_time'=>time(),'real_price' => $realPrice + $addPrice, 'price' => $price+$addPrice,'is_trade'=> 2, 'update_time' => time()])) {
  604. $this->error = 2056;
  605. DB::rollBack();
  606. return false;
  607. }
  608. DB::commit();
  609. $this->error = 2057;
  610. return true;
  611. }
  612. // 停止拆分
  613. else if($info['sell_price'] == $stopSplitPrice){
  614. if (!GoodsModel::where(['id' => $id])->update(['status' => 2,'stop_split'=>1, 'update_time' => time()])) {
  615. $this->error = 2054;
  616. DB::rollBack();
  617. return false;
  618. }
  619. }
  620. // 满足拆分
  621. else {
  622. if(!GoodsService::make()->split($info['goods_id'])){
  623. $this->error = 2058;
  624. DB::rollBack();
  625. return false;
  626. }
  627. $this->error = 2059;
  628. DB::commit();
  629. return false;
  630. }
  631. $this->error = 2056;
  632. return false;
  633. }
  634. }