TradeService.php 29 KB

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