TradeService.php 28 KB

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