TradeService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 = ['a.user_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  304. return $this->model->from('trade as a')
  305. ->leftJoin('member as b','b.id','=','a.user_id')
  306. ->where($where)
  307. ->where(function ($query) use ($status, $time) {
  308. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  309. // 本月
  310. if ($time == 1) {
  311. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  312. } // 今日
  313. else if ($time == 2) {
  314. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  315. }
  316. })
  317. ->sum('a.real_price');
  318. }
  319. /**
  320. * 获取用户团队交易统计
  321. * @param $userId
  322. * @param int $status
  323. * @param int $time
  324. * @return mixed
  325. */
  326. public function getTeamTradeTotal($userId, $status = 0, $time = 0)
  327. {
  328. $where = ['b.parent_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  329. return $this->model->from('trade as a')
  330. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  331. ->where($where)
  332. ->where(function ($query) use ($status, $time) {
  333. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  334. // 本月
  335. if ($time == 1) {
  336. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  337. } // 今日
  338. else if ($time == 2) {
  339. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  340. }
  341. })
  342. ->sum('a.real_price');
  343. }
  344. /**
  345. * 抢拍交易订单数
  346. * @param $userId 用户ID
  347. * @param int $status 状态
  348. * @return mixed
  349. */
  350. public function getNewTradeCountByStatus($userId, $status = 0)
  351. {
  352. $where = ['user_id' => $userId, 'mark' => 1, 'is_read' => 0];
  353. $counts = $this->model->where($where)
  354. ->where(function ($query) use ($status) {
  355. $query->whereIn('status', is_array($status) ? $status : [$status]);
  356. })
  357. ->select(['status', DB::raw('count(*) as count')])
  358. ->groupBy('status')
  359. ->get();
  360. if ($counts) {
  361. $temps = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  362. foreach ($counts as $v) {
  363. $temps['status' . $v['status']] = $v['count'];
  364. }
  365. $counts = $temps;
  366. } else {
  367. $counts = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  368. }
  369. return $counts;
  370. }
  371. /**
  372. * 抢购
  373. * @param $params
  374. * @param $userId
  375. * @param $shopId
  376. * @return bool
  377. */
  378. public function buy($params, $userId, $shopId)
  379. {
  380. $goodsId = isset($params['id']) ? $params['id'] : 0;
  381. $info = GoodsService::make()->getInfo(['id' => $goodsId, 'status' => 1, 'mark' => 1]);
  382. $goodsUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  383. $isTrade = isset($info['is_trade']) ? $info['is_trade'] : 0;
  384. if (empty($info)) {
  385. $this->error = 2031;
  386. return false;
  387. }
  388. if ($isTrade == 1) {
  389. $this->error = 2032;
  390. return false;
  391. }
  392. if ($goodsUserId == $userId) {
  393. $this->error = 2036;
  394. return false;
  395. }
  396. $shopInfo = ShopService::make()->getInfo($shopId);
  397. if (empty($shopInfo)) {
  398. $this->error = 2033;
  399. return false;
  400. }
  401. // 营业时间
  402. $curTime = time();
  403. $startTime = isset($shopInfo['start_time']) ? $shopInfo['start_time'] : '';
  404. $endTime = isset($shopInfo['end_time']) ? $shopInfo['end_time'] : '';
  405. // 店长自己抢
  406. if ($shopInfo['user_id'] == $userId) {
  407. $snapTime = ConfigService::make()->getConfigByCode('snap_time');
  408. $snapTime = $snapTime ? $snapTime : 5;
  409. $curTime = strtotime(date('Y-m-d') . ' ' . $startTime) + 1;
  410. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime) - $snapTime * 60) {
  411. $this->error = 2034;
  412. return false;
  413. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  414. $this->error = 2035;
  415. return false;
  416. }
  417. } else {
  418. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime)) {
  419. $this->error = 2034;
  420. return false;
  421. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  422. $this->error = 2035;
  423. return false;
  424. }
  425. }
  426. // 验证收款账号
  427. if (!MemberBankService::make()->getBindInfo($userId)) {
  428. $this->error = 2037;
  429. return false;
  430. }
  431. $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  432. $feeRate = $feeRate ? $feeRate : '2.5';
  433. $realPrice = intval($info['price'] - $info['sell_price'] + $info['source_price']);
  434. $fee = round($realPrice * $feeRate / 100, 0);
  435. $lockCacheKey = "caches:trade:lock:{$goodsId}";
  436. if (RedisService::get($lockCacheKey)) {
  437. $this->error = 2032;
  438. return false;
  439. }
  440. RedisService::set($lockCacheKey, $info, rand(1, 2));
  441. $data = [
  442. 'order_sn' => get_order_num('T'),
  443. 'goods_id' => $goodsId,
  444. 'user_id' => $userId,
  445. 'shop_id' => $shopId,
  446. 'sell_uid' => $info['user_id'],
  447. 'num' => 1,
  448. 'price' => $info['price'],
  449. 'source_price' => $info['source_price'],
  450. 'sell_price' => $info['sell_price'],
  451. 'real_price' => $realPrice,
  452. 'new_real_price' => $realPrice,
  453. 'fee' => $fee,
  454. 'new_price' => $info['price'],
  455. 'remark' => '抢拍交易',
  456. 'create_time' => $curTime,
  457. 'update_time' => $curTime,
  458. 'status' => 1
  459. ];
  460. DB::beginTransaction();
  461. if (!TradeModel::insertGetId($data)) {
  462. DB::rollBack();
  463. $this->error = 2040;
  464. return false;
  465. }
  466. if (!GoodsModel::where(['id' => $goodsId])->update(['is_trade' => 1, 'update_time' => time()])) {
  467. DB::rollBack();
  468. $this->error = 2040;
  469. return false;
  470. }
  471. DB::commit();
  472. RedisService::keyDel($lockCacheKey);
  473. $this->error = 2041;
  474. return true;
  475. }
  476. /**
  477. * 付款
  478. * @param $params
  479. * @param $userId
  480. * @param $shopId
  481. * @return bool
  482. */
  483. public function pay($params, $userId, $shopId)
  484. {
  485. $id = isset($params['id']) ? $params['id'] : 0;
  486. $payImg = isset($params['pay_img']) ? $params['pay_img'] : '';
  487. if (empty($payImg)) {
  488. $this->error = 2043;
  489. return false;
  490. }
  491. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  492. if (empty($id) || empty($info)) {
  493. $this->error = 2042;
  494. return false;
  495. }
  496. if ($info['status'] != 1) {
  497. $this->error = 2044;
  498. return false;
  499. }
  500. if ($info['user_id'] != $userId) {
  501. $this->error = 2045;
  502. return false;
  503. }
  504. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['pay_time' => time(), 'pay_img' => $payImg, 'status' => 2, 'update_time' => time()])) {
  505. $this->error = 2046;
  506. return true;
  507. }
  508. $this->error = 2047;
  509. return false;
  510. }
  511. /**
  512. * 确认收款
  513. * @param $params
  514. * @param $userId
  515. * @return bool
  516. */
  517. public function confirm($params, $userId)
  518. {
  519. $id = isset($params['id']) ? $params['id'] : 0;
  520. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  521. if (empty($id) || empty($info)) {
  522. $this->error = 2042;
  523. return false;
  524. }
  525. if (!in_array($info['status'], [1, 2])) {
  526. $this->error = 2044;
  527. return false;
  528. }
  529. // if($info['sell_uid'] != $userId){
  530. // $this->error = 2045;
  531. // return false;
  532. // }
  533. DB::beginTransaction();
  534. $data = ['confirm_time' => time(),'is_pay'=>1, 'status' => 3, 'update_time' => time()];
  535. if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
  536. $this->error = 2050;
  537. DB::rollBack();
  538. return false;
  539. }
  540. $this->model->where(['goods_id' => $info['goods_id'],'status'=>4, 'mark' => 1])->update(['is_out'=>1,'update_time'=>time()]);
  541. // 更改商品归属人
  542. if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id'=> $info['user_id'],'update_time'=> time()])) {
  543. $this->error = 2050;
  544. DB::rollBack();
  545. return false;
  546. }
  547. if(!MemberModel::where(['id'=> $userId])->update(['member_level'=>1,'update_time'=>time()])){
  548. $this->error = 2050;
  549. DB::rollBack();
  550. return false;
  551. }
  552. // 佣金结算
  553. $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
  554. $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
  555. $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
  556. if ($memberInfo && $parentId && $parentInfo) {
  557. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  558. $bonusRate = $bonusRate ? $bonusRate : 5;
  559. $bonus = $info['real_price'] * $bonusRate / 100;
  560. if ($bonus > 0) {
  561. if (!$this->model->where(['id' => $id, 'mark' => 1])->update(['bonus' => $bonus, 'update_time' => time()])) {
  562. $this->error = 2051;
  563. DB::rollBack();
  564. return true;
  565. }
  566. if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update(['bonus' => $parentInfo['bonus'] + $bonus,'bonus_total'=> $parentInfo['bonus_total']+$bonus, 'update_time' => time()])) {
  567. $this->error = 2051;
  568. DB::rollBack();
  569. return true;
  570. }
  571. $data = [
  572. 'user_id' => $parentId,
  573. 'shop_id' => $info['shop_id'],
  574. 'source_uid' => $userId,
  575. 'source_order_sn' => $info['order_sn'],
  576. 'type' => 2,
  577. 'coin_type' => 2,
  578. 'money' => $bonus,
  579. 'balance' => $parentInfo['bonus'],
  580. 'create_time' => time(),
  581. 'update_time' => time(),
  582. 'remark' => '推广佣金',
  583. 'status' => 1,
  584. 'mark' => 1
  585. ];
  586. if (!AccountModel::insertGetId($data)) {
  587. $this->error = 2051;
  588. DB::rollBack();
  589. return true;
  590. }
  591. // 结算统计
  592. FinanceService::make()->settleBonus($bonus, 2);
  593. }
  594. }
  595. DB::commit();
  596. $this->error = 2049;
  597. return true;
  598. }
  599. /**
  600. * 申请待售
  601. * @param $params
  602. * @param $userId
  603. * @return false
  604. */
  605. public function sell($params, $userId)
  606. {
  607. $id = isset($params['id']) ? $params['id'] : 0;
  608. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  609. if (empty($id) || empty($info)) {
  610. $this->error = 2042;
  611. return false;
  612. }
  613. if ($info['status'] != 3) {
  614. $this->error = 2053;
  615. return false;
  616. }
  617. if ($info['user_id'] != $userId) {
  618. $this->error = 2045;
  619. return false;
  620. }
  621. if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 1, 'update_time' => time()])) {
  622. $this->error = 2054;
  623. return false;
  624. }
  625. $this->error = 2055;
  626. return true;
  627. }
  628. /**
  629. * 申请待售审核
  630. * @param $params
  631. * @param $userId
  632. * @return false
  633. */
  634. public function sellConfirm($params)
  635. {
  636. $id = isset($params['id']) ? $params['id'] : 0;
  637. $info = $this->model->from('trade as a')
  638. ->leftJoin('goods as b','b.id','=','a.goods_id')
  639. ->where(['a.id' => $id, 'a.mark' => 1])
  640. ->select(['a.*','b.split_price','b.split_num'])
  641. ->first();
  642. if (empty($id) || empty($info)) {
  643. $this->error = 2042;
  644. return false;
  645. }
  646. if (!in_array($info['status'], [3,4])) {
  647. $this->error = 2053;
  648. return false;
  649. }
  650. DB::beginTransaction();
  651. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  652. $priceRate = $priceRate? $priceRate : 4;
  653. $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
  654. $stopSplitPrice = $stopSplitPrice? $stopSplitPrice : 500;
  655. // 判断是否可以上架或拆分
  656. $realPrice = $info['real_price'];
  657. $sellPrice = $info['sell_price']; // 特价
  658. $price = $info['price']; // 买入价格
  659. $price1 = $info['new_price']; // 买入价格
  660. $addPrice = intval($realPrice*$priceRate/100,0);
  661. // 满足涨价上架
  662. if($price1+$addPrice < $info['split_price']){
  663. // 平台服务费
  664. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  665. $serviceRate = $serviceRate ? $serviceRate : 8;
  666. $serviceFee = round(($price)*$serviceRate/100, 0);
  667. 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()])) {
  668. $this->error = 2056;
  669. DB::rollBack();
  670. return false;
  671. }
  672. 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()])) {
  673. $this->error = 2056;
  674. DB::rollBack();
  675. return false;
  676. }
  677. // 服务费统计
  678. FinanceService::make()->settleBonus($serviceFee, 1);
  679. DB::commit();
  680. $this->error = 2057;
  681. return true;
  682. }
  683. // 停止拆分
  684. else if($info['sell_price'] == $stopSplitPrice){
  685. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['status' => 2,'split_stop'=>1, 'update_time' => time()])) {
  686. $this->error = 2054;
  687. DB::rollBack();
  688. return false;
  689. }
  690. $this->error = 2064;
  691. DB::commit();
  692. return true;
  693. }
  694. // 满足拆分
  695. else if($info['split_price']) {
  696. if(!GoodsService::make()->split($info['goods_id'], $info)){
  697. $this->error = 2058;
  698. DB::rollBack();
  699. return false;
  700. }
  701. $this->error = 2059;
  702. DB::commit();
  703. return true;
  704. }
  705. $this->error = 2056;
  706. return false;
  707. }
  708. /**
  709. * 修改订单
  710. * @param $params
  711. * @return bool
  712. */
  713. public function modify($params)
  714. {
  715. $id = isset($params['id'])? $params['id'] : 0;
  716. $status = isset($params['status'])? $params['status'] : 0;
  717. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  718. if(!$id || empty($info)){
  719. $this->error = 2042;
  720. return false;
  721. }
  722. if(!in_array($info['status'], [1,2,3,4])){
  723. $this->error = 2082;
  724. return false;
  725. }
  726. if(!in_array($status, [1,2,4])){
  727. $this->error =2087;
  728. return false;
  729. }
  730. $safePassword = isset($params['password'])? trim($params['password']) : '';
  731. if(empty($safePassword)){
  732. $this->error = 2085;
  733. return false;
  734. }
  735. $memberInfo = MemberModel::where(['id'=> $info['user_id']])->select(['id','safe_password'])->first();
  736. $password = isset($memberInfo['safe_password'])? $memberInfo['safe_password'] : '';
  737. if(empty($memberInfo)){
  738. $this->error = 2019;
  739. return false;
  740. }
  741. $safePassword = get_password($safePassword);
  742. if($password != $safePassword){
  743. $this->error = 2086;
  744. return false;
  745. }
  746. if($this->model->where(['id'=> $id])->update(['status'=> $status,'update_time'=> time()])){
  747. $this->error = 1020;
  748. return true;
  749. }else{
  750. $this->error = 1021;
  751. return false;
  752. }
  753. }
  754. /**
  755. * 修改订单
  756. * @param $params
  757. * @return bool
  758. */
  759. public function cancel($params)
  760. {
  761. $id = isset($params['id'])? $params['id'] : 0;
  762. $status = isset($params['status'])? $params['status'] : 0;
  763. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  764. if(!$id || empty($info)){
  765. $this->error = 2042;
  766. return false;
  767. }
  768. if(!in_array($info['status'], [1,2,3,4])){
  769. $this->error = 2082;
  770. return false;
  771. }
  772. if($this->model->where(['id'=> $id])->update(['status'=> 5,'update_time'=> time()])){
  773. $this->error = 2088;
  774. return true;
  775. }else{
  776. $this->error = 2089;
  777. return false;
  778. }
  779. }
  780. }