TradeService.php 35 KB

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