TradeService.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. $type = isset($params['type']) ? $params['type'] : 0;
  67. if ($shopId > 0) {
  68. $where['a.shop_id'] = $shopId;
  69. }
  70. if ($parentId > 0) {
  71. $where['b.parent_id'] = $parentId;
  72. }
  73. if ($isAppeal > 0) {
  74. $where['a.is_appeal'] = $isAppeal;
  75. }
  76. if ($status > 0 && !$isAppeal) {
  77. $where['a.status'] = $status;
  78. }
  79. $model = $this->model->from('trade as a')
  80. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  81. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  82. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  83. ->leftJoin('shop as s', 's.id', '=', 'g.shop_id')
  84. ->where($where)
  85. ->where('a.status','>', 0)
  86. ->where(function($query) use($userId,$status){
  87. // if($status == 0){
  88. // $query->whereNotIn('a.sell_uid',[$userId]);
  89. // }
  90. // else if($status == 2){
  91. // $query->whereNotIn('a.user_id',[$userId]);
  92. // }
  93. })
  94. ->where(function($query) use($type){
  95. if($type){
  96. $query->where('a.create_time','>=', strtotime(date('Y-m-d')));
  97. }
  98. })
  99. ->where(function ($query) use ($params) {
  100. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  101. if ($keyword) {
  102. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")->orWhere('g.goods_name', 'like', "%{$keyword}%");
  103. }
  104. })
  105. ->where(function ($query) use ($params) {
  106. $time = isset($params['time']) ? $params['time'] : 0;
  107. if ($time) {
  108. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  109. }
  110. })
  111. ->where(function ($query) use ($type, $userId, $sellUid, $status) {
  112. if($type == 1){
  113. if($status == 0){
  114. $query->where('a.user_id', '=', $userId);
  115. $query->whereNotIn('a.sell_uid', [$userId]);
  116. }else if($status == 2){
  117. $query->whereNotIn('a.user_id', [$userId]);
  118. }else{
  119. $query->where('a.user_id', '=', $userId);
  120. }
  121. }else{
  122. if($userId){
  123. $query->where('a.user_id', '=', $userId);
  124. }else if($sellUid){
  125. $query->where('a.sell_uid', '=', $sellUid);
  126. }
  127. }
  128. /*if ($sellUid && $userId) {
  129. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  130. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  131. });
  132. }else if($userId){
  133. $query->where('a.user_id', '=', $userId);
  134. }else if($sellUid){
  135. $query->where('a.sell_uid', '=', $sellUid);
  136. }*/
  137. })
  138. ->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'])
  139. ->orderBy('a.create_time', 'desc')
  140. ->orderBy('a.id', 'desc');
  141. $totalModdel = $model;
  142. $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
  143. $total = $totalModdel->sum('a.real_price');
  144. $counts['total'] = intval($total);
  145. $bonus = $totalModdel->sum('a.bonus');
  146. $counts['bonus'] = intval($bonus);
  147. $serviceFee = $totalModdel->sum('a.service_fee');
  148. $counts['service_fee'] = intval($serviceFee);
  149. $fee = $totalModdel->sum('a.fee');
  150. $counts['fee'] = intval($fee);
  151. $list = $model->paginate($pageSize > 0 ? $pageSize : 9999999);
  152. $list = $list ? $list->toArray() : [];
  153. if ($list) {
  154. foreach ($list['data'] as &$item) {
  155. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  156. $item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
  157. $item['confirm_time'] = $item['confirm_time'] ? datetime($item['confirm_time'], 'Y-m-d H:i:s') : '';
  158. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  159. $item['pay_img'] = isset($item['pay_img']) && $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  160. $item['price'] = intval($item['price']);
  161. $item['real_price'] = intval($item['real_price']);
  162. $item['fee'] = intval($item['fee']);
  163. }
  164. }
  165. return [
  166. 'pageSize' => $pageSize,
  167. 'total' => isset($list['total']) ? $list['total'] : 0,
  168. 'counts' => $counts,
  169. 'list' => isset($list['data']) ? $list['data'] : []
  170. ];
  171. }
  172. /**
  173. * 详情
  174. * @param $id
  175. * @return mixed
  176. */
  177. public function getInfo($id)
  178. {
  179. $info = $this->model->from('trade as a')
  180. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  181. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  182. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  183. ->where(['a.id'=>$id,'a.mark'=>1])
  184. ->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'])
  185. ->first();
  186. if($info){
  187. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  188. $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
  189. $info['confirm_time'] = $info['confirm_time'] ? datetime($info['confirm_time'], 'Y-m-d H:i:s') : '';
  190. $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
  191. $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  192. $info['price'] = intval($info['price']);
  193. $info['real_price'] = intval($info['real_price']);
  194. $info['fee'] = intval($info['fee']);
  195. $bankInfo = MemberBankService::make()->getBindInfo($info['sell_uid']);
  196. $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
  197. }
  198. return $info;
  199. }
  200. /**
  201. * 统计
  202. * @param $params
  203. * @return int[]
  204. */
  205. public function getCounts($params)
  206. {
  207. $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
  208. $where = ['a.mark' => 1];
  209. $status = isset($params['status']) ? $params['status'] : 0;
  210. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  211. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  212. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  213. $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
  214. $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
  215. if ($shopId > 0) {
  216. $where['a.shop_id'] = $shopId;
  217. }
  218. if ($parentId > 0) {
  219. $where['b.parent_id'] = $parentId;
  220. }
  221. if ($userId > 0) {
  222. $where['a.user_id'] = $userId;
  223. }
  224. if ($sellUid > 0) {
  225. $where['a.sell_uid'] = $sellUid;
  226. }
  227. if ($isAppeal > 0) {
  228. $where['a.is_appeal'] = $isAppeal;
  229. }
  230. if ($status > 0) {
  231. $where['a.status'] = $status;
  232. }
  233. $counts['total'] = intval($this->model->from('trade as a')
  234. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  235. ->where($where)
  236. ->where(function ($query) use ($params) {
  237. $time = isset($params['time']) ? $params['time'] : 0;
  238. if ($time) {
  239. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  240. }
  241. })
  242. ->where(function ($query) use ($userId,$sellUid, $status) {
  243. if ($sellUid && $userId) {
  244. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  245. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  246. });
  247. }else if($userId){
  248. $query->where('a.user_id', '=', $userId);
  249. }else if($sellUid){
  250. $query->where('a.sell_uid', '=', $sellUid);
  251. }
  252. })
  253. ->sum('real_price'));
  254. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  255. $bonusRate = $bonusRate ? $bonusRate : 5;
  256. $counts['bonus'] = intval($counts['total'] * $bonusRate / 100);
  257. $counts['fee'] = intval($this->model->from('trade as a')
  258. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  259. ->where($where)
  260. ->where(function ($query) use ($params) {
  261. $time = isset($params['time']) ? $params['time'] : 0;
  262. if ($time) {
  263. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  264. }
  265. })
  266. ->where(function ($query) use ($userId,$sellUid, $status) {
  267. if ($sellUid && $userId) {
  268. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  269. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  270. });
  271. }else if($userId){
  272. $query->where('a.user_id', '=', $userId);
  273. }else if($sellUid){
  274. $query->where('a.sell_uid', '=', $sellUid);
  275. }
  276. })
  277. ->sum('fee'));
  278. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  279. $serviceRate = $serviceRate ? $serviceRate : 8;
  280. $counts['service_fee'] = intval($counts['total'] * $serviceRate / 100);
  281. return $counts;
  282. }
  283. /**
  284. * 添加或编辑
  285. * @return array
  286. * @since 2020/11/11
  287. * @author laravel开发员
  288. */
  289. public function edit()
  290. {
  291. $data = request()->all();
  292. return parent::edit($data); // TODO: Change the autogenerated stub
  293. }
  294. /**
  295. * 获取店铺交易统计
  296. * @param $shopId
  297. * @param int $status
  298. * @return mixed
  299. */
  300. public function getShopTradeTotal($shopId, $status = 0)
  301. {
  302. $where = ['shop_id' => $shopId, 'mark' => 1];
  303. if ($status) {
  304. $where['status'] = $status;
  305. }
  306. return $this->model->where($where)
  307. ->sum('real_price');
  308. }
  309. /**
  310. * 获取交易数
  311. * @param $shopId
  312. * @param int $status
  313. * @return mixed
  314. */
  315. public function getShopTradeCount($shopId, $status = 0)
  316. {
  317. $where = ['shop_id' => $shopId, 'mark' => 1];
  318. if ($status) {
  319. $where['status'] = $status;
  320. }
  321. return $this->model->where($where)
  322. ->count('id');
  323. }
  324. /**
  325. * 获取用户交易统计
  326. * @param $userId
  327. * @param int $status
  328. * @param int $time
  329. * @return mixed
  330. */
  331. public function getUserTradeTotal($userId, $status = 0, $time = 0)
  332. {
  333. $cacheKey = "caches:trade:userTotal:{$userId}_".(is_array($status)? implode('-', $status):$status)."_{$time}";
  334. $data = RedisService::get($cacheKey);
  335. if($data){
  336. return $data;
  337. }
  338. $where = ['a.user_id' => $userId, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1];
  339. $data = $this->model->from('trade as a')
  340. ->leftJoin('member as b','b.id','=','a.user_id')
  341. ->where($where)
  342. ->where(function ($query) use ($status, $time) {
  343. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  344. // 本月
  345. if ($time == 1) {
  346. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  347. } // 今日
  348. else if ($time == 2) {
  349. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  350. }
  351. })
  352. ->sum('a.real_price');
  353. RedisService::set($cacheKey, $data, rand(3,5));
  354. return $data;
  355. }
  356. /**
  357. * 获取用户团队交易统计
  358. * @param $userId
  359. * @param int $status
  360. * @param int $time
  361. * @return mixed
  362. */
  363. public function getTeamTradeTotal($userId, $status = 0, $time = 0)
  364. {
  365. $cacheKey = "caches:trade:teamTotal:{$userId}_".(is_array($status)? implode('-', $status):$status)."_{$time}";
  366. $data = RedisService::get($cacheKey);
  367. if($data){
  368. return $data;
  369. }
  370. $where = ['b.parent_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  371. $data = $this->model->from('trade as a')
  372. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  373. ->where($where)
  374. ->where(function ($query) use ($status, $time) {
  375. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  376. // 本月
  377. if ($time == 1) {
  378. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  379. } // 今日
  380. else if ($time == 2) {
  381. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  382. }
  383. })
  384. ->sum('a.real_price');
  385. RedisService::set($cacheKey, $data, rand(3,5));
  386. return $data;
  387. }
  388. /**
  389. * 获取用户佣金统计
  390. * @param $userId
  391. * @param int $status
  392. * @param int $time
  393. * @return mixed
  394. */
  395. public function getTradeBonusTotal($userId, $status = 0, $time = 0)
  396. {
  397. $cacheKey = "caches:trade:bonus:{$userId}_".(is_array($status)? implode('-', $status):$status)."_".(is_array($time)? implode('-', $time):$time);
  398. $data = RedisService::get($cacheKey);
  399. if($data){
  400. return round($data, 2);
  401. }
  402. $where = ['a.user_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  403. $data = $this->model->from('trade as a')
  404. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  405. ->where($where)
  406. ->where(function ($query) use ($status, $time) {
  407. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  408. // 本月
  409. if ($time == 1) {
  410. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  411. } // 今日
  412. else if ($time == 2) {
  413. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  414. }else if (is_array($time)){
  415. $start = isset($time[0])? $time[0] : '';
  416. $end = isset($time[1])? $time[1] : '';
  417. if($end && $end>$start){
  418. $query->where('a.pay_time', '<=', strtotime($end));
  419. if($start){
  420. $query->where('a.pay_time', '>=', strtotime($start));
  421. }
  422. }else if ($start){
  423. $query->where('a.pay_time', '=', strtotime($start));
  424. }
  425. }
  426. })
  427. ->sum('a.bonus');
  428. RedisService::set($cacheKey, $data, rand(3,5));
  429. return round($data, 2);
  430. }
  431. /**
  432. * 获取用户收益统计
  433. * @param $userId
  434. * @param int $status
  435. * @param int $time
  436. * @return mixed
  437. */
  438. public function getTradeProfitTotal($userId, $status = 0, $time = 0)
  439. {
  440. $cacheKey = "caches:trade:profit:{$userId}_".(is_array($status)? implode('-', $status):$status)."_".(is_array($time)? implode('-', $time):$time);
  441. $data = RedisService::get($cacheKey);
  442. if($data){
  443. return round($data, 2);
  444. }
  445. $where = ['a.user_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  446. $data = $this->model->from('trade as a')
  447. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  448. ->where($where)
  449. ->where(function ($query) use ($status, $time) {
  450. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  451. // 本月
  452. if ($time == 1) {
  453. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  454. } // 今日
  455. else if ($time == 2) {
  456. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  457. }else if (is_array($time)){
  458. $start = isset($time[0])? $time[0] : '';
  459. $end = isset($time[1])? $time[1] : '';
  460. if($end && $end>$start){
  461. $query->where('a.pay_time', '<=', strtotime($end));
  462. if($start){
  463. $query->where('a.pay_time', '>=', strtotime($start));
  464. }
  465. }else if ($start){
  466. $query->where('a.pay_time', '=', strtotime($start));
  467. }
  468. }
  469. })
  470. ->sum('a.profit');
  471. RedisService::set($cacheKey, $data, rand(3,5));
  472. return round($data, 2);
  473. }
  474. /**
  475. * 抢拍交易订单数
  476. * @param $userId 用户ID
  477. * @param int $status 状态
  478. * @return mixed
  479. */
  480. public function getNewTradeCountByStatus($userId, $status = 0)
  481. {
  482. $where = ['user_id' => $userId, 'mark' => 1, 'is_read' => 0];
  483. $counts = $this->model->where($where)
  484. ->where(function ($query) use ($status) {
  485. $query->whereIn('status', is_array($status) ? $status : [$status]);
  486. })
  487. ->where('create_time','>=',strtotime(date('Y-m-d')))
  488. ->select(['status', DB::raw('count(*) as count')])
  489. ->groupBy('status')
  490. ->get();
  491. if ($counts) {
  492. $temps = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  493. foreach ($counts as $v) {
  494. $temps['status' . $v['status']] = $v['count'];
  495. }
  496. $counts = $temps;
  497. } else {
  498. $counts = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  499. }
  500. return $counts;
  501. }
  502. /**
  503. * 抢购
  504. * @param $params
  505. * @param $userId
  506. * @param $shopId
  507. * @return bool
  508. */
  509. public function buy($params, $userId, $shopId)
  510. {
  511. $goodsId = isset($params['id']) ? $params['id'] : 0;
  512. $info = GoodsService::make()->getInfo(['id' => $goodsId, 'status' => 1, 'mark' => 1]);
  513. $goodsUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  514. $isTrade = isset($info['is_trade']) ? $info['is_trade'] : 0;
  515. if (empty($info)) {
  516. $this->error = 2031;
  517. return false;
  518. }
  519. if ($isTrade == 1) {
  520. $this->error = 2032;
  521. return false;
  522. }
  523. if ($goodsUserId == $userId) {
  524. $this->error = 2036;
  525. return false;
  526. }
  527. $shopInfo = ShopService::make()->getInfo($shopId);
  528. if (empty($shopInfo)) {
  529. $this->error = 2033;
  530. return false;
  531. }
  532. // 营业时间
  533. $curTime = time();
  534. $startTime = isset($shopInfo['start_time']) ? $shopInfo['start_time'] : '';
  535. $endTime = isset($shopInfo['end_time']) ? $shopInfo['end_time'] : '';
  536. // 店长自己抢
  537. if ($shopInfo['user_id'] == $userId) {
  538. $snapTime = ConfigService::make()->getConfigByCode('snap_time');
  539. $snapTime = $snapTime ? $snapTime : 5;
  540. $curTime = strtotime(date('Y-m-d') . ' ' . $startTime) + 1;
  541. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime) - $snapTime * 60) {
  542. $this->error = 2034;
  543. return false;
  544. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  545. $this->error = 2035;
  546. return false;
  547. }
  548. } else {
  549. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime)) {
  550. $this->error = 2034;
  551. return false;
  552. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  553. $this->error = 2035;
  554. return false;
  555. }
  556. }
  557. // 验证收款账号
  558. if (!MemberBankService::make()->getBindInfo($userId)) {
  559. $this->error = 2037;
  560. return false;
  561. }
  562. $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  563. $feeRate = $feeRate ? $feeRate : '2.5';
  564. $realPrice = intval($info['price'] - $info['sell_price'] + $info['source_price']);
  565. $fee = round($realPrice * $feeRate / 100, 0);
  566. $lockCacheKey = "caches:trade:lock:{$goodsId}";
  567. if (RedisService::get($lockCacheKey)) {
  568. $this->error = 2032;
  569. return false;
  570. }
  571. RedisService::set($lockCacheKey, $info, rand(1, 2));
  572. $data = [
  573. 'order_sn' => get_order_num('T'),
  574. 'goods_id' => $goodsId,
  575. 'user_id' => $userId,
  576. 'shop_id' => $shopId,
  577. 'sell_uid' => $info['user_id'],
  578. 'num' => 1,
  579. 'price' => $info['price'],
  580. 'source_price' => $info['source_price'],
  581. 'sell_price' => $info['sell_price'],
  582. 'real_price' => $realPrice,
  583. 'new_real_price' => $realPrice,
  584. 'fee' => $fee,
  585. 'new_price' => $info['price'],
  586. 'remark' => '抢拍交易',
  587. 'create_time' => $curTime,
  588. 'update_time' => $curTime,
  589. 'status' => 1
  590. ];
  591. DB::beginTransaction();
  592. if (!TradeModel::insertGetId($data)) {
  593. DB::rollBack();
  594. $this->error = 2040;
  595. return false;
  596. }
  597. if (!GoodsModel::where(['id' => $goodsId])->update(['is_trade' => 1, 'update_time' => time()])) {
  598. DB::rollBack();
  599. $this->error = 2040;
  600. return false;
  601. }
  602. DB::commit();
  603. RedisService::keyDel($lockCacheKey);
  604. $this->error = 2041;
  605. return true;
  606. }
  607. /**
  608. * 付款
  609. * @param $params
  610. * @param $userId
  611. * @param $shopId
  612. * @return bool
  613. */
  614. public function pay($params, $userId, $shopId)
  615. {
  616. $id = isset($params['id']) ? $params['id'] : 0;
  617. $payImg = isset($params['pay_img']) ? $params['pay_img'] : '';
  618. if (empty($payImg)) {
  619. $this->error = 2043;
  620. return false;
  621. }
  622. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  623. if (empty($id) || empty($info)) {
  624. $this->error = 2042;
  625. return false;
  626. }
  627. if ($info['status'] != 1) {
  628. $this->error = 2044;
  629. return false;
  630. }
  631. if ($info['user_id'] != $userId) {
  632. $this->error = 2045;
  633. return false;
  634. }
  635. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['pay_time' => time(), 'pay_img' => $payImg, 'status' => 2, 'update_time' => time()])) {
  636. $this->error = 2046;
  637. return true;
  638. }
  639. $this->error = 2047;
  640. return false;
  641. }
  642. /**
  643. * 确认收款
  644. * @param $params
  645. * @param $userId
  646. * @return bool
  647. */
  648. public function confirm($params, $userId)
  649. {
  650. $id = isset($params['id']) ? $params['id'] : 0;
  651. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  652. if (empty($id) || empty($info)) {
  653. $this->error = 2042;
  654. return false;
  655. }
  656. if (!in_array($info['status'], [1, 2])) {
  657. $this->error = 2044;
  658. return false;
  659. }
  660. // if($info['sell_uid'] != $userId){
  661. // $this->error = 2045;
  662. // return false;
  663. // }
  664. DB::beginTransaction();
  665. $data = ['confirm_time' => time(),'is_pay'=>1, 'status' => 3, 'update_time' => time()];
  666. if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
  667. $this->error = 2050;
  668. DB::rollBack();
  669. return false;
  670. }
  671. $this->model->where(['goods_id' => $info['goods_id'],'status'=>4, 'mark' => 1])->update(['is_out'=>1,'update_time'=>time()]);
  672. // 更改商品归属人
  673. if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id'=> $info['user_id'],'update_time'=> time()])) {
  674. $this->error = 2050;
  675. DB::rollBack();
  676. return false;
  677. }
  678. if(!MemberModel::where(['id'=> $userId])->update(['member_level'=>1,'update_time'=>time()])){
  679. $this->error = 2050;
  680. DB::rollBack();
  681. return false;
  682. }
  683. // 佣金结算
  684. $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
  685. $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
  686. $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
  687. if ($memberInfo && $parentId && $parentInfo) {
  688. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  689. $bonusRate = $bonusRate ? $bonusRate : 5;
  690. $bonus = $info['real_price'] * $bonusRate / 100;
  691. $profitRate = ConfigService::make()->getConfigByCode('profit_rate');
  692. $profitRate = $profitRate ? $profitRate : 0;
  693. $profit = $info['real_price'] * $profitRate / 100;
  694. if ($bonus > 0) {
  695. if (!$this->model->where(['id' => $id, 'mark' => 1])->update(['bonus' => $bonus,'profit'=> $profit, 'update_time' => time()])) {
  696. $this->error = 2051;
  697. DB::rollBack();
  698. return true;
  699. }
  700. if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update(['bonus' => $parentInfo['bonus'] + $bonus,'bonus_total'=> $parentInfo['bonus_total']+$bonus, 'update_time' => time()])) {
  701. $this->error = 2051;
  702. DB::rollBack();
  703. return true;
  704. }
  705. $data = [
  706. 'user_id' => $parentId,
  707. 'shop_id' => $info['shop_id'],
  708. 'source_uid' => $userId,
  709. 'source_order_sn' => $info['order_sn'],
  710. 'type' => 2,
  711. 'coin_type' => 2,
  712. 'money' => $bonus,
  713. 'balance' => $parentInfo['bonus'],
  714. 'create_time' => time(),
  715. 'update_time' => time(),
  716. 'remark' => '推广佣金',
  717. 'status' => 1,
  718. 'mark' => 1
  719. ];
  720. if (!AccountModel::insertGetId($data)) {
  721. $this->error = 2051;
  722. DB::rollBack();
  723. return true;
  724. }
  725. // 结算统计
  726. FinanceService::make()->settleBonus($bonus, 2);
  727. }
  728. }
  729. DB::commit();
  730. $this->error = 2049;
  731. return true;
  732. }
  733. /**
  734. * 申请待售
  735. * @param $params
  736. * @param $userId
  737. * @return false
  738. */
  739. public function sell($params, $userId)
  740. {
  741. $id = isset($params['id']) ? $params['id'] : 0;
  742. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  743. if (empty($id) || empty($info)) {
  744. $this->error = 2042;
  745. return false;
  746. }
  747. if ($info['status'] != 3) {
  748. $this->error = 2053;
  749. return false;
  750. }
  751. if ($info['user_id'] != $userId) {
  752. $this->error = 2045;
  753. return false;
  754. }
  755. if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 1, 'update_time' => time()])) {
  756. $this->error = 2054;
  757. return false;
  758. }
  759. $this->error = 2055;
  760. return true;
  761. }
  762. /**
  763. * 申请待售审核
  764. * @param $params
  765. * @param $userId
  766. * @return false
  767. */
  768. public function sellConfirm($params)
  769. {
  770. $id = isset($params['id']) ? $params['id'] : 0;
  771. $info = $this->model->from('trade as a')
  772. ->leftJoin('goods as b','b.id','=','a.goods_id')
  773. ->where(['a.id' => $id, 'a.mark' => 1])
  774. ->select(['a.*','b.split_price','b.split_num'])
  775. ->first();
  776. if (empty($id) || empty($info)) {
  777. $this->error = 2042;
  778. return false;
  779. }
  780. if (!in_array($info['status'], [3,4])) {
  781. $this->error = 2053;
  782. return false;
  783. }
  784. DB::beginTransaction();
  785. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  786. $priceRate = $priceRate? $priceRate : 4;
  787. $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
  788. $stopSplitPrice = $stopSplitPrice? $stopSplitPrice : 500;
  789. // 判断是否可以上架或拆分
  790. $realPrice = $info['real_price'];
  791. $sellPrice = $info['sell_price']; // 特价
  792. $price = $info['price']; // 买入价格
  793. $price1 = $info['new_price']; // 买入价格
  794. $addPrice = intval($realPrice*$priceRate/100,0);
  795. // 满足涨价上架
  796. if($price1+$addPrice < $info['split_price']){
  797. // 平台服务费
  798. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  799. $serviceRate = $serviceRate ? $serviceRate : 8;
  800. $serviceFee = round(($realPrice)*$serviceRate/100, 0);
  801. 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()])) {
  802. $this->error = 2056;
  803. DB::rollBack();
  804. return false;
  805. }
  806. 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()])) {
  807. $this->error = 2056;
  808. DB::rollBack();
  809. return false;
  810. }
  811. // 服务费统计
  812. FinanceService::make()->settleBonus($serviceFee, 1);
  813. DB::commit();
  814. $this->error = 2057;
  815. return true;
  816. }
  817. // 停止拆分
  818. else if($info['sell_price'] == $stopSplitPrice){
  819. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['status' => 2,'split_stop'=>1, 'update_time' => time()])) {
  820. $this->error = 2054;
  821. DB::rollBack();
  822. return false;
  823. }
  824. $this->error = 2064;
  825. DB::commit();
  826. return true;
  827. }
  828. // 满足拆分
  829. else if($info['split_price']) {
  830. if(!GoodsService::make()->split($info['goods_id'], $info)){
  831. $this->error = 2058;
  832. DB::rollBack();
  833. return false;
  834. }
  835. $this->error = 2059;
  836. DB::commit();
  837. return true;
  838. }
  839. $this->error = 2056;
  840. return false;
  841. }
  842. /**
  843. * 修改订单
  844. * @param $params
  845. * @return bool
  846. */
  847. public function modify($params)
  848. {
  849. $id = isset($params['id'])? $params['id'] : 0;
  850. $status = isset($params['status'])? $params['status'] : 0;
  851. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  852. if(!$id || empty($info)){
  853. $this->error = 2042;
  854. return false;
  855. }
  856. if(!in_array($info['status'], [1,2,3,4])){
  857. $this->error = 2082;
  858. return false;
  859. }
  860. if(!in_array($status, [1,2,4])){
  861. $this->error =2087;
  862. return false;
  863. }
  864. $safePassword = isset($params['password'])? trim($params['password']) : '';
  865. if(empty($safePassword)){
  866. $this->error = 2085;
  867. return false;
  868. }
  869. $memberInfo = MemberModel::where(['id'=> $info['user_id']])->select(['id','safe_password'])->first();
  870. $password = isset($memberInfo['safe_password'])? $memberInfo['safe_password'] : '';
  871. if(empty($memberInfo)){
  872. $this->error = 2019;
  873. return false;
  874. }
  875. $safePassword = get_password($safePassword);
  876. if($password != $safePassword){
  877. $this->error = 2086;
  878. return false;
  879. }
  880. if($this->model->where(['id'=> $id])->update(['status'=> $status,'update_time'=> time()])){
  881. $this->error = 1020;
  882. return true;
  883. }else{
  884. $this->error = 1021;
  885. return false;
  886. }
  887. }
  888. /**
  889. * 修改订单
  890. * @param $params
  891. * @return bool
  892. */
  893. public function cancel($params)
  894. {
  895. $id = isset($params['id'])? $params['id'] : 0;
  896. $status = isset($params['status'])? $params['status'] : 0;
  897. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  898. if(!$id || empty($info)){
  899. $this->error = 2042;
  900. return false;
  901. }
  902. if(!in_array($info['status'], [1,2,3,4])){
  903. $this->error = 2082;
  904. return false;
  905. }
  906. if($this->model->where(['id'=> $id])->update(['status'=> 5,'update_time'=> time()])){
  907. $this->error = 2088;
  908. return true;
  909. }else{
  910. $this->error = 2089;
  911. return false;
  912. }
  913. }
  914. public function clearByDay()
  915. {
  916. $day = ConfigService::make()->getConfigByCode('clear_trade_time');
  917. $day = $day? $day : 2;
  918. $cacheKey = "caches:task:clearTrade:d{$day}_".date('Ymd');
  919. if(RedisService::get($cacheKey)){
  920. $this->error = '2301';
  921. return false;
  922. }
  923. $clearDay = strtotime(date('Y-m-d')) - $day * 86400;
  924. $count = $this->model->where('create_time','<', $clearDay)->where(['mark'=>1])->count();
  925. if($count<=0){
  926. $this->error = '';
  927. RedisService::set($cacheKey,['error'=>'没有记录可以清除','day'=> $day,'clearDay'=> date('Y-m-d', $clearDay)], 6 * 3600);
  928. return false;
  929. }
  930. }
  931. }