TradeService.php 35 KB

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