TradeService.php 36 KB

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