TradeService.php 28 KB

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