TradeService.php 29 KB

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