TradeService.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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\ShopModel;
  16. use App\Models\TradeModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 交易管理-服务类
  23. * @author laravel开发员
  24. * @since 2020/11/11
  25. * Class TradeService
  26. * @package App\Services\Common
  27. */
  28. class TradeService extends BaseService
  29. {
  30. // 静态对象
  31. protected static $instance = null;
  32. /**
  33. * 构造函数
  34. * @author laravel开发员
  35. * @since 2020/11/11
  36. * TradeService constructor.
  37. */
  38. public function __construct()
  39. {
  40. $this->model = new TradeModel();
  41. }
  42. /**
  43. * 静态入口
  44. * @return static|null
  45. */
  46. public static function make()
  47. {
  48. if (!self::$instance) {
  49. self::$instance = (new static());
  50. }
  51. return self::$instance;
  52. }
  53. /**
  54. * @param $params
  55. * @param int $pageSize
  56. * @return array
  57. */
  58. public function getDataList($params, $pageSize = 15)
  59. {
  60. $where = ['a.mark' => 1];
  61. $status = isset($params['status']) ? $params['status'] : 0;
  62. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  63. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  64. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  65. $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
  66. $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
  67. $type = isset($params['type']) ? $params['type'] : 0;
  68. if ($shopId > 0) {
  69. $where['a.shop_id'] = $shopId;
  70. }
  71. if ($parentId > 0) {
  72. $where['b.parent_id'] = $parentId;
  73. }
  74. if ($isAppeal > 0) {
  75. $where['a.is_appeal'] = $isAppeal;
  76. }
  77. if ($status > 0 && !$isAppeal) {
  78. $where['a.status'] = $status;
  79. }
  80. $model = $this->model->from('trade as a')
  81. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  82. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  83. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  84. ->leftJoin('shop as s', 's.id', '=', 'g.shop_id')
  85. ->where($where)
  86. ->where('a.status','>', 0)
  87. ->where(function($query) use($userId,$status){
  88. // if($status == 0){
  89. // $query->whereNotIn('a.sell_uid',[$userId]);
  90. // }
  91. // else if($status == 2){
  92. // $query->whereNotIn('a.user_id',[$userId]);
  93. // }
  94. })
  95. ->where(function($query) use($type){
  96. if($type){
  97. $query->where('a.create_time','>=', strtotime(date('Y-m-d')));
  98. }
  99. })
  100. ->where(function ($query) use ($params) {
  101. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  102. if ($keyword) {
  103. $query->where('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%")
  104. ->orWhere('s.name', 'like', "%{$keyword}%")
  105. ->orWhere('s.code', 'like', "%{$keyword}%")
  106. ->orWhere('g.goods_name', 'like', "%{$keyword}%");
  107. }
  108. })
  109. ->where(function ($query) use ($params) {
  110. $time = isset($params['time']) ? $params['time'] : 0;
  111. if ($time) {
  112. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  113. }
  114. })
  115. ->where(function ($query) use ($type, $userId, $sellUid, $status) {
  116. if($type == 1){
  117. if($status == 0){
  118. $query->where(['a.user_id'=>$userId,'a.is_out'=>0]);
  119. $query->whereNotIn('a.sell_uid', [$userId]);
  120. }else if($status == 2){
  121. $query->where('a.sell_uid', '=',$userId);
  122. $query->whereNotIn('a.user_id', [$userId]);
  123. }else{
  124. $query->where(['a.user_id'=>$userId,'a.is_out'=>0]);
  125. }
  126. }else{
  127. if($userId){
  128. $query->where(['a.user_id'=>$userId,'a.is_out'=>0]);
  129. }else if($sellUid){
  130. $query->where('a.sell_uid', '=', $sellUid);
  131. }
  132. }
  133. /*if ($sellUid && $userId) {
  134. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  135. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  136. });
  137. }else if($userId){
  138. $query->where('a.user_id', '=', $userId);
  139. }else if($sellUid){
  140. $query->where('a.sell_uid', '=', $sellUid);
  141. }*/
  142. })
  143. ->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'])
  144. ->orderBy('s.id', 'asc')
  145. ->orderBy('a.create_time', 'desc')
  146. ->orderBy('a.id', 'desc');
  147. // 统计
  148. $totalModdel = clone $model;
  149. $totalModdel = $totalModdel->whereIn('a.status',[3,4]);
  150. $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
  151. $total = $totalModdel->sum('a.real_price');
  152. $counts['total'] = intval($total);
  153. $bonus = $totalModdel->sum('a.bonus');
  154. $counts['bonus'] = intval($bonus);
  155. $serviceFee = $totalModdel->sum('a.service_fee');
  156. $counts['service_fee'] = intval($serviceFee);
  157. $fee = $totalModdel->sum('a.fee');
  158. $counts['fee'] = intval($fee);
  159. $list = $model->paginate($pageSize > 0 ? $pageSize : 9999999);
  160. $list = $list ? $list->toArray() : [];
  161. if ($list) {
  162. foreach ($list['data'] as &$item) {
  163. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H:i:s') : '';
  164. $item['pay_time'] = $item['pay_time'] ? datetime($item['pay_time'], 'Y-m-d H:i:s') : '';
  165. $item['confirm_time'] = $item['confirm_time'] ? datetime($item['confirm_time'], 'Y-m-d H:i:s') : '';
  166. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  167. $item['pay_img'] = isset($item['pay_img']) && $item['pay_img'] ? get_image_url($item['pay_img']) : '';
  168. $item['price'] = intval($item['price']);
  169. $item['real_price'] = intval($item['real_price']);
  170. $item['fee'] = intval($item['fee']);
  171. }
  172. }
  173. return [
  174. 'pageSize' => $pageSize,
  175. 'total' => isset($list['total']) ? $list['total'] : 0,
  176. 'counts' => $counts,
  177. 'list' => isset($list['data']) ? $list['data'] : []
  178. ];
  179. }
  180. /**
  181. * 转商品列表数据
  182. * @param $params
  183. * @param int $pageSize
  184. * @return array
  185. */
  186. public function getSwitchGoods($params, $pageSize = 15)
  187. {
  188. $where = ['a.mark' => 1,'a.status'=>1,'a.is_pay'=>2];
  189. $status = isset($params['status']) ? $params['status'] : 0;
  190. if ($status > 0) {
  191. $where['a.status'] = $status;
  192. }
  193. $isTrade = isset($params['is_trade']) ? $params['is_trade'] : 0;
  194. if ($isTrade > 0) {
  195. $where['a.is_trade'] = $isTrade;
  196. }
  197. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  198. if ($shopId > 0) {
  199. $where['a.shop_id'] = $shopId;
  200. }
  201. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  202. if ($userId > 0) {
  203. $where['a.user_id'] = $userId;
  204. }
  205. $list = $this->model->from('trade as a')
  206. ->leftJoin('member as b', 'b.id', '=', 'a.sell_uid')
  207. ->leftJoin('shop as c', 'c.id', '=', 'a.shop_id')
  208. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  209. ->where($where)
  210. ->where(function ($query) use ($params) {
  211. $keyword = isset($params['keyword']) ? $params['keyword'] : '';
  212. if ($keyword) {
  213. $query->where('g.goods_name', 'like', "%{$keyword}%")->orWhere('c.name', 'like', "%{$keyword}%")->orWhere('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%");
  214. }
  215. $username = isset($params['username']) ? $params['username'] : '';
  216. if ($username) {
  217. $query->orWhere('b.nickname', 'like', "%{$keyword}%")->orWhere('b.mobile', 'like', "%{$keyword}%");
  218. }
  219. })
  220. ->where(function ($query) use ($params) {
  221. $time = isset($params['time']) ? $params['time'] : '';
  222. if ($time) {
  223. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  224. }
  225. })
  226. ->select(['a.*', 'b.nickname', 'b.code as user_code', 'b.mobile as mobile', 'c.name as shop_name', 'c.code as shop_code','g.goods_name','g.thumb'])
  227. ->orderBy('a.create_time', 'desc')
  228. ->orderBy('a.id', 'desc')
  229. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  230. $list = $list ? $list->toArray() : [];
  231. if ($list) {
  232. foreach ($list['data'] as &$item) {
  233. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
  234. $item['thumb'] = isset($item['thumb']) && $item['thumb'] ? get_image_url($item['thumb']) : '';
  235. }
  236. }
  237. return [
  238. 'pageSize' => $pageSize,
  239. 'total' => isset($list['total']) ? $list['total'] : 0,
  240. 'list' => isset($list['data']) ? $list['data'] : []
  241. ];
  242. }
  243. /**
  244. * 详情
  245. * @param $id
  246. * @return mixed
  247. */
  248. public function getInfo($id)
  249. {
  250. $info = $this->model->from('trade as a')
  251. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  252. ->leftJoin('member as c', 'c.id', '=', 'a.sell_uid')
  253. ->leftJoin('goods as g', 'g.id', '=', 'a.goods_id')
  254. ->where(['a.id'=>$id,'a.mark'=>1])
  255. ->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'])
  256. ->first();
  257. if($info){
  258. $info['create_time_text'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
  259. $info['pay_time'] = $info['pay_time'] ? datetime($info['pay_time'], 'Y-m-d H:i:s') : '';
  260. $info['confirm_time'] = $info['confirm_time'] ? datetime($info['confirm_time'], 'Y-m-d H:i:s') : '';
  261. $info['thumb'] = isset($info['thumb']) && $info['thumb'] ? get_image_url($info['thumb']) : '';
  262. $info['pay_img'] = isset($info['pay_img']) && $info['pay_img'] ? get_image_url($info['pay_img']) : '';
  263. $info['price'] = intval($info['price']);
  264. $info['real_price'] = intval($info['real_price']);
  265. $info['fee'] = intval($info['fee']);
  266. $bankInfo = MemberBankService::make()->getBindInfo($info['sell_uid']);
  267. $info['bank_info'] = $bankInfo? $bankInfo : ['realname'=>'','bank_name'=>'','bank_num'=>''];
  268. }
  269. return $info;
  270. }
  271. /**
  272. * 统计
  273. * @param $params
  274. * @return int[]
  275. */
  276. public function getCounts($params)
  277. {
  278. $counts = ['total' => 0, 'service_fee' => 0, 'bonus' => 0, 'fee' => 0];
  279. $where = ['a.mark' => 1];
  280. $status = isset($params['status']) ? $params['status'] : 0;
  281. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  282. $shopId = isset($params['shop_id']) ? $params['shop_id'] : 0;
  283. $parentId = isset($params['parent_id']) ? $params['parent_id'] : 0;
  284. $isAppeal = isset($params['is_appeal']) ? $params['is_appeal'] : 0;
  285. $sellUid = isset($params['sell_uid']) ? $params['sell_uid'] : 0;
  286. if ($shopId > 0) {
  287. $where['a.shop_id'] = $shopId;
  288. }
  289. if ($parentId > 0) {
  290. $where['b.parent_id'] = $parentId;
  291. }
  292. if ($userId > 0) {
  293. $where['a.user_id'] = $userId;
  294. }
  295. if ($sellUid > 0) {
  296. $where['a.sell_uid'] = $sellUid;
  297. }
  298. if ($isAppeal > 0) {
  299. $where['a.is_appeal'] = $isAppeal;
  300. }
  301. if ($status > 0) {
  302. $where['a.status'] = $status;
  303. }
  304. $counts['total'] = intval($this->model->from('trade as a')
  305. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  306. ->where($where)
  307. ->where(function ($query) use ($params) {
  308. $time = isset($params['time']) ? $params['time'] : 0;
  309. if ($time) {
  310. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  311. }
  312. })
  313. ->where(function ($query) use ($userId,$sellUid, $status) {
  314. if ($sellUid && $userId) {
  315. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  316. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  317. });
  318. }else if($userId){
  319. $query->where('a.user_id', '=', $userId);
  320. }else if($sellUid){
  321. $query->where('a.sell_uid', '=', $sellUid);
  322. }
  323. })
  324. ->sum('real_price'));
  325. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  326. $bonusRate = $bonusRate ? $bonusRate : 5;
  327. $counts['bonus'] = intval($counts['total'] * $bonusRate / 100);
  328. $counts['fee'] = intval($this->model->from('trade as a')
  329. ->leftJoin('member as b', 'a.user_id', '=', 'b.id')
  330. ->where($where)
  331. ->where(function ($query) use ($params) {
  332. $time = isset($params['time']) ? $params['time'] : 0;
  333. if ($time) {
  334. $query->where('a.pay_time', '>=', $time)->orWhere('a.create_time', '>=', $time);
  335. }
  336. })
  337. ->where(function ($query) use ($userId,$sellUid, $status) {
  338. if ($sellUid && $userId) {
  339. $query->where('a.user_id', $userId)->orWhere(function($query) use($sellUid){
  340. $query->where('a.sell_uid', $sellUid)->whereIn('a.status', [1,2]);
  341. });
  342. }else if($userId){
  343. $query->where('a.user_id', '=', $userId);
  344. }else if($sellUid){
  345. $query->where('a.sell_uid', '=', $sellUid);
  346. }
  347. })
  348. ->sum('fee'));
  349. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  350. $serviceRate = $serviceRate ? $serviceRate : 8;
  351. $counts['service_fee'] = intval($counts['total'] * $serviceRate / 100);
  352. return $counts;
  353. }
  354. /**
  355. * 添加或编辑
  356. * @return array
  357. * @since 2020/11/11
  358. * @author laravel开发员
  359. */
  360. public function edit()
  361. {
  362. $data = request()->all();
  363. return parent::edit($data); // TODO: Change the autogenerated stub
  364. }
  365. /**
  366. * 获取用户交易统计
  367. * @param $userId
  368. * @param int $status
  369. * @param int $time
  370. * @return mixed
  371. */
  372. public function getUserTradeTotal($userId, $status = 0, $time = 0)
  373. {
  374. $cacheKey = "caches:trade:userTotal:{$userId}_".(is_array($status)? implode('-', $status):$status)."_{$time}";
  375. $data = RedisService::get($cacheKey);
  376. if($data){
  377. return $data;
  378. }
  379. $where = ['a.user_id' => $userId, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1];
  380. $data = $this->model->from('trade as a')
  381. ->leftJoin('member as b','b.id','=','a.user_id')
  382. ->where($where)
  383. ->where(function ($query) use ($status, $time) {
  384. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  385. // 本月
  386. if ($time == 1) {
  387. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  388. } // 今日
  389. else if ($time == 2) {
  390. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  391. }
  392. })
  393. ->sum('a.real_price');
  394. RedisService::set($cacheKey, $data, rand(3,5));
  395. return $data;
  396. }
  397. /**
  398. * 获取用户团队交易统计
  399. * @param $userId
  400. * @param int $status
  401. * @param int $time
  402. * @return mixed
  403. */
  404. public function getTeamTradeTotal($userId, $status = 0, $time = 0)
  405. {
  406. $cacheKey = "caches:trade:teamTotal:{$userId}_".(is_array($status)? implode('-', $status):$status)."_{$time}";
  407. $data = RedisService::get($cacheKey);
  408. if($data){
  409. return $data;
  410. }
  411. $where = ['b.parent_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  412. $data = $this->model->from('trade as a')
  413. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  414. ->where($where)
  415. ->where(function ($query) use ($status, $time) {
  416. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  417. // 本月
  418. if ($time == 1) {
  419. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  420. } // 今日
  421. else if ($time == 2) {
  422. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  423. }
  424. })
  425. ->sum('a.real_price');
  426. RedisService::set($cacheKey, $data, rand(3,5));
  427. return $data;
  428. }
  429. /**
  430. * 获取店铺交易统计
  431. * @param $userId
  432. * @param int $time
  433. * @return mixed
  434. */
  435. public function getShopTradeTotal($shopId, $type='real_price', $time = 2)
  436. {
  437. $cacheKey = "caches:trade:shop_{$type}:{$shopId}_".(is_array($time)? implode('-', $time):$time);
  438. $data = RedisService::get($cacheKey);
  439. if($data){
  440. return round($data, 2);
  441. }
  442. $where = ['a.shop_id' => $shopId,'a.mark'=>1];
  443. $data = $this->model->from('trade as a')
  444. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  445. ->where($where)
  446. ->whereIn('a.status',[3,4])
  447. ->where(function ($query) use ($time) {
  448. // 本月
  449. if ($time == 1) {
  450. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  451. } // 今日
  452. else if ($time == 2) {
  453. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  454. }
  455. })
  456. ->sum('a.'.$type);
  457. RedisService::set($cacheKey, $data, rand(3,5));
  458. return round($data, 2);
  459. }
  460. /**
  461. * 获取店铺佣金统计
  462. * @param $userId
  463. * @param int $time
  464. * @return mixed
  465. */
  466. public function getShopBonusTotal($shopId, $time = 2)
  467. {
  468. $cacheKey = "caches:trade:shop_bonus:{$shopId}_".(is_array($time)? implode('-', $time):$time);
  469. $data = RedisService::get($cacheKey);
  470. if($data){
  471. return round($data, 2);
  472. }
  473. $where = ['a.shop_id' => $shopId,'type'=> 2,'a.coin_type'=> 2,'a.status'=>1,'a.mark'=>1];
  474. $data = AccountModel::from('account_log as a')
  475. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  476. ->where($where)
  477. ->where(function ($query) use ($time) {
  478. // 本月
  479. if ($time == 1) {
  480. $query->where('a.create_time', '>=', strtotime(date('Y-m-01')));
  481. } // 今日
  482. else if ($time == 2) {
  483. $query->where('a.create_time', '>=', strtotime(date('Y-m-d')));
  484. }else if ($time){
  485. $month = date('Y-m', $time/1000);
  486. $lastDay = strtotime("{$month}-01 +1 month -1 day");
  487. if($month){
  488. $query->where('a.create_time', '>=', $time/1000);
  489. $query->where('a.create_time', '<', $lastDay+86400);
  490. }
  491. }
  492. })
  493. ->sum('a.money');
  494. RedisService::set($cacheKey, $data, rand(3,5));
  495. return round($data, 2);
  496. }
  497. /**
  498. * 获取用户佣金统计
  499. * @param $userId
  500. * @param int $time
  501. * @return mixed
  502. */
  503. public function getTradeBonusTotal($userId, $time = 0)
  504. {
  505. $cacheKey = "caches:trade:bonus:{$userId}_".(is_array($time)? implode('-', $time):$time);
  506. $data = RedisService::get($cacheKey);
  507. if($data){
  508. return round($data, 2);
  509. }
  510. $where = ['a.user_id' => $userId,'type'=> 2,'a.coin_type'=> 2,'a.status'=>1,'a.mark'=>1];
  511. $data = AccountModel::from('account_log as a')
  512. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  513. ->where($where)
  514. ->where(function ($query) use ($time) {
  515. // 本月
  516. if ($time == 1) {
  517. $query->where('a.create_time', '>=', strtotime(date('Y-m-01')));
  518. } // 今日
  519. else if ($time == 2) {
  520. $query->where('a.create_time', '>=', strtotime(date('Y-m-d')));
  521. }else if ($time){
  522. $month = date('Y-m', $time/1000);
  523. $lastDay = strtotime("{$month}-01 +1 month -1 day");
  524. if($month){
  525. $query->where('a.create_time', '>=', $time/1000);
  526. $query->where('a.create_time', '<', $lastDay+86400);
  527. }
  528. }
  529. })
  530. ->sum('a.money');
  531. RedisService::set($cacheKey, $data, rand(3,5));
  532. return round($data, 2);
  533. }
  534. /**
  535. * 获取用户收益统计
  536. * @param $userId
  537. * @param int $time
  538. * @return mixed
  539. */
  540. public function getTradeProfitTotal($userId, $time = 0)
  541. {
  542. $cacheKey = "caches:trade:profit:{$userId}_".(is_array($time)? implode('-', $time):$time);
  543. $data = RedisService::get($cacheKey);
  544. if($data){
  545. return round($data, 2);
  546. }
  547. $where = ['a.user_id' => $userId,'type'=> 2,'a.coin_type'=> 5,'a.status'=>1,'a.mark'=>1];
  548. $data = AccountModel::from('account_log as a')
  549. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  550. ->where($where)
  551. ->where(function ($query) use ($time) {
  552. // 本月
  553. if ($time == 1) {
  554. $query->where('a.create_time', '>=', strtotime(date('Y-m-01')));
  555. } // 今日
  556. else if ($time == 2) {
  557. $query->where('a.create_time', '>=', strtotime(date('Y-m-d')));
  558. }else if ($time){
  559. $month = date('Y-m', $time/1000);
  560. $lastDay = strtotime("{$month}-01 +1 month -1 day");
  561. if($month){
  562. $query->where('a.create_time', '>=', $time/1000);
  563. $query->where('a.create_time', '<', $lastDay+86400);
  564. }
  565. }
  566. })
  567. ->sum('a.money');
  568. RedisService::set($cacheKey, $data, rand(3,5));
  569. return round($data, 2);
  570. }
  571. /**
  572. * 获取用户佣金统计
  573. * @param $userId
  574. * @param int $status
  575. * @param int $time
  576. * @return mixed
  577. */
  578. public function getTradeBonusTotal1($userId, $status = 0, $time = 0)
  579. {
  580. $cacheKey = "caches:trade:bonus:{$userId}_".(is_array($status)? implode('-', $status):$status)."_".(is_array($time)? implode('-', $time):$time);
  581. $data = RedisService::get($cacheKey);
  582. if($data){
  583. return round($data, 2);
  584. }
  585. $where = ['b.parent_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  586. $data = $this->model->from('trade as a')
  587. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  588. ->where($where)
  589. ->where(function ($query) use ($status, $time) {
  590. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  591. // 本月
  592. if ($time == 1) {
  593. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  594. } // 今日
  595. else if ($time == 2) {
  596. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  597. }else if (is_array($time)){
  598. $start = isset($time[0])? $time[0] : '';
  599. $end = isset($time[1])? $time[1] : '';
  600. if($end && $end>$start){
  601. $query->where('a.pay_time', '<=', strtotime($end));
  602. if($start){
  603. $query->where('a.pay_time', '>=', strtotime($start));
  604. }
  605. }else if ($start){
  606. $query->where('a.pay_time', '=', strtotime($start));
  607. }
  608. }
  609. })
  610. ->sum('a.bonus');
  611. RedisService::set($cacheKey, $data, rand(3,5));
  612. return round($data, 2);
  613. }
  614. /**
  615. * 获取用户收益统计
  616. * @param $userId
  617. * @param int $status
  618. * @param int $time
  619. * @return mixed
  620. */
  621. public function getTradeProfitTotal1($userId, $status = 0, $time = 0)
  622. {
  623. $cacheKey = "caches:trade:profit:{$userId}_".(is_array($status)? implode('-', $status):$status)."_".(is_array($time)? implode('-', $time):$time);
  624. $data = RedisService::get($cacheKey);
  625. if($data){
  626. return round($data, 2);
  627. }
  628. $where = ['a.user_id' => $userId, 'a.mark' => 1,'b.status'=>1];
  629. $data = $this->model->from('trade as a')
  630. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  631. ->where($where)
  632. ->where(function ($query) use ($status, $time) {
  633. $query->whereIn('a.status', is_array($status) ? $status : [$status]);
  634. // 本月
  635. if ($time == 1) {
  636. $query->where('a.pay_time', '>=', strtotime(date('Y-m-01')));
  637. } // 今日
  638. else if ($time == 2) {
  639. $query->where('a.pay_time', '>=', strtotime(date('Y-m-d')));
  640. }else if (is_array($time)){
  641. $start = isset($time[0])? $time[0] : '';
  642. $end = isset($time[1])? $time[1] : '';
  643. if($end && $end>$start){
  644. $query->where('a.pay_time', '<=', strtotime($end));
  645. if($start){
  646. $query->where('a.pay_time', '>=', strtotime($start));
  647. }
  648. }else if ($start){
  649. $query->where('a.pay_time', '=', strtotime($start));
  650. }
  651. }
  652. })
  653. ->sum('a.profit');
  654. RedisService::set($cacheKey, $data, rand(3,5));
  655. return round($data, 2);
  656. }
  657. /**
  658. * 抢拍交易订单数
  659. * @param $userId 用户ID
  660. * @param int $status 状态
  661. * @return mixed
  662. */
  663. public function getNewTradeCountByStatus($userId, $status = 0)
  664. {
  665. $where = ['user_id' => $userId, 'mark' => 1, 'is_read' => 0,'is_out'=>0];
  666. $counts = $this->model->where($where)
  667. ->where(function ($query) use ($status) {
  668. $query->whereIn('status', is_array($status) ? $status : [$status]);
  669. })
  670. ->where('create_time','>=',strtotime(date('Y-m-d')))
  671. ->select(['status', DB::raw('count(*) as count')])
  672. ->groupBy('status')
  673. ->get();
  674. if ($counts) {
  675. $temps = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  676. foreach ($counts as $v) {
  677. $temps['status' . $v['status']] = $v['count'];
  678. }
  679. $counts = $temps;
  680. } else {
  681. $counts = ['status1' => 0, 'status2' => 0, 'status3' => 0];
  682. }
  683. return $counts;
  684. }
  685. /**
  686. * 抢购
  687. * @param $params
  688. * @param $userId
  689. * @param $shopId
  690. * @return bool
  691. */
  692. public function buy($params, $userId, $shopId)
  693. {
  694. $goodsId = isset($params['id']) ? $params['id'] : 0;
  695. $info = GoodsService::make()->getInfo(['id' => $goodsId, 'status' => 1, 'mark' => 1]);
  696. $goodsUserId = isset($info['user_id']) ? $info['user_id'] : 0;
  697. $isTrade = isset($info['is_trade']) ? $info['is_trade'] : 0;
  698. if (empty($info)) {
  699. $this->error = 2031;
  700. return false;
  701. }
  702. if ($isTrade == 1) {
  703. $this->error = 2032;
  704. return false;
  705. }
  706. // 验证用户是否可以抢拍
  707. $memberInfo = MemberService::make()->getCacheInfo($userId);
  708. $memberIsTrade = isset($memberInfo['is_trade'])? $memberInfo['is_trade'] : 0;
  709. if($memberIsTrade != 1){
  710. $this->error = 2032;
  711. return false;
  712. }
  713. if ($goodsUserId == $userId) {
  714. $this->error = 2036;
  715. return false;
  716. }
  717. $shopInfo = ShopService::make()->getInfo($shopId);
  718. if (empty($shopInfo)) {
  719. $this->error = 2033;
  720. return false;
  721. }
  722. // 营业时间
  723. $curTime = time();
  724. $startTime = isset($shopInfo['start_time']) ? $shopInfo['start_time'] : '';
  725. $endTime = isset($shopInfo['end_time']) ? $shopInfo['end_time'] : '';
  726. // 店长自己抢
  727. if ($shopInfo['user_id'] == $userId) {
  728. $snapTime = ConfigService::make()->getConfigByCode('snap_time');
  729. $snapTime = $snapTime ? $snapTime : 5;
  730. $curTime = strtotime(date('Y-m-d') . ' ' . $startTime) + 1;
  731. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime) - $snapTime * 60) {
  732. $this->error = 2034;
  733. return false;
  734. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  735. $this->error = 2035;
  736. return false;
  737. }
  738. } else {
  739. if (time() < strtotime(date('Y-m-d') . ' ' . $startTime)) {
  740. $this->error = 2034;
  741. return false;
  742. } else if (time() > strtotime(date('Y-m-d') . ' ' . $endTime)) {
  743. $this->error = 2035;
  744. return false;
  745. }
  746. }
  747. // 验证收款账号
  748. if (!MemberBankService::make()->getBindInfo($userId)) {
  749. $this->error = 2037;
  750. return false;
  751. }
  752. $feeRate = ConfigService::make()->getConfigByCode('sell_fee_rate');
  753. $feeRate = $feeRate ? $feeRate : '2.5';
  754. $realPrice = intval($info['price'] - $info['sell_price'] + $info['source_price']);
  755. $fee = round($realPrice * $feeRate / 100, 0);
  756. $lockCacheKey = "caches:trade:lock:{$goodsId}";
  757. if (RedisService::get($lockCacheKey)) {
  758. $this->error = 2032;
  759. return false;
  760. }
  761. RedisService::set($lockCacheKey, $info, rand(5,10));
  762. $data = [
  763. 'order_sn' => get_order_num('T'),
  764. 'goods_id' => $goodsId,
  765. 'user_id' => $userId,
  766. 'shop_id' => $shopId,
  767. 'sell_uid' => $info['user_id'],
  768. 'num' => 1,
  769. 'price' => $info['price'],
  770. 'source_price' => $info['source_price'],
  771. 'sell_price' => $info['sell_price'],
  772. 'real_price' => $realPrice,
  773. 'new_real_price' => $realPrice,
  774. 'fee' => $fee,
  775. 'new_price' => $info['price'],
  776. 'remark' => '抢拍交易',
  777. 'create_time' => $curTime,
  778. 'update_time' => $curTime,
  779. 'status' => 1
  780. ];
  781. DB::beginTransaction();
  782. if(TradeModel::where(['goods_id'=> $goodsId,'mark'=>1])->whereIn('status',[1,2])->value('id')){
  783. DB::rollBack();
  784. $this->error = 2032;
  785. return false;
  786. }
  787. if (!TradeModel::insertGetId($data)) {
  788. DB::rollBack();
  789. $this->error = 2040;
  790. RedisService::keyDel($lockCacheKey);
  791. return false;
  792. }
  793. if (!GoodsModel::where(['id' => $goodsId])->update(['is_trade' => 1, 'update_time' => time()])) {
  794. DB::rollBack();
  795. $this->error = 2040;
  796. RedisService::keyDel($lockCacheKey);
  797. return false;
  798. }
  799. DB::commit();
  800. $this->error = 2041;
  801. RedisService::keyDel($lockCacheKey);
  802. return true;
  803. }
  804. /**
  805. * 付款
  806. * @param $params
  807. * @param $userId
  808. * @param $shopId
  809. * @return bool
  810. */
  811. public function pay($params, $userId, $shopId)
  812. {
  813. $id = isset($params['id']) ? $params['id'] : 0;
  814. $payImg = isset($params['pay_img']) ? $params['pay_img'] : '';
  815. if (empty($payImg)) {
  816. $this->error = 2043;
  817. return false;
  818. }
  819. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  820. if (empty($id) || empty($info)) {
  821. $this->error = 2042;
  822. return false;
  823. }
  824. if ($info['status'] != 1) {
  825. $this->error = 2044;
  826. return false;
  827. }
  828. if ($info['user_id'] != $userId) {
  829. $this->error = 2045;
  830. return false;
  831. }
  832. if ($this->model->where(['id' => $id, 'mark' => 1])->update(['pay_time' => time(), 'pay_img' => $payImg, 'status' => 2, 'update_time' => time()])) {
  833. $this->error = 2046;
  834. return true;
  835. }
  836. $this->error = 2047;
  837. return false;
  838. }
  839. /**
  840. * 确认收款
  841. * @param $params
  842. * @param $userId
  843. * @return bool
  844. */
  845. public function confirm($params, $userId)
  846. {
  847. $id = isset($params['id']) ? $params['id'] : 0;
  848. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  849. if (empty($id) || empty($info)) {
  850. $this->error = 2042;
  851. return false;
  852. }
  853. if (!in_array($info['status'], [1, 2])) {
  854. $this->error = 2044;
  855. return false;
  856. }
  857. DB::beginTransaction();
  858. $data = ['confirm_time' => time(),'is_pay'=>1, 'status' => 3, 'update_time' => time()];
  859. if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
  860. $this->error = 2050;
  861. DB::rollBack();
  862. return false;
  863. }
  864. DB::commit();
  865. $this->error = 2049;
  866. return true;
  867. }
  868. /**
  869. * 确认收款
  870. * @param $params
  871. * @param $userId
  872. * @return bool
  873. */
  874. public function confirm1($params, $userId)
  875. {
  876. $id = isset($params['id']) ? $params['id'] : 0;
  877. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  878. if (empty($id) || empty($info)) {
  879. $this->error = 2042;
  880. return false;
  881. }
  882. if (!in_array($info['status'], [1, 2])) {
  883. $this->error = 2044;
  884. return false;
  885. }
  886. DB::beginTransaction();
  887. $data = ['confirm_time' => time(),'is_pay'=>1, 'status' => 3, 'update_time' => time()];
  888. if (!$this->model->where(['id' => $id, 'mark' => 1])->update($data)) {
  889. $this->error = 2050;
  890. DB::rollBack();
  891. return false;
  892. }
  893. $this->model->where(['goods_id' => $info['goods_id'],'status'=>4, 'mark' => 1])->update(['is_out'=>1,'update_time'=>time()]);
  894. // 更改商品归属人
  895. if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id'=> $info['user_id'],'update_time'=> time()])) {
  896. $this->error = 2050;
  897. DB::rollBack();
  898. return false;
  899. }
  900. $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
  901. $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
  902. $meritsCount = isset($memberInfo['merits_count']) ? $memberInfo['merits_count'] : 0;
  903. $updateData = ['merits_count'=> $meritsCount+$info['real_price'],'merits_time'=>date('Y-m-d H:i:s'),'member_level'=>1,'update_time'=>time()];
  904. if(!MemberModel::where(['id'=> $info['user_id']])->update($updateData)){
  905. $this->error = 2050;
  906. DB::rollBack();
  907. return false;
  908. }
  909. // 佣金结算
  910. $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
  911. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  912. $bonusRate = $bonusRate ? $bonusRate : 5;
  913. $bonus = $info['real_price'] * $bonusRate / 100;
  914. $profitRate = ConfigService::make()->getConfigByCode('profit_rate');
  915. $profitRate = $profitRate ? $profitRate : 0;
  916. $profit = $info['real_price'] * $profitRate / 100;
  917. if (!$this->model->where(['id' => $id, 'mark' => 1])->update(['bonus' => $bonus,'profit'=> $profit, 'update_time' => time()])) {
  918. $this->error = 2051;
  919. DB::rollBack();
  920. return true;
  921. }
  922. // 收益记录
  923. $data = [
  924. 'user_id' => $info['user_id'],
  925. 'shop_id' => $info['shop_id'],
  926. 'source_uid' => 0,
  927. 'source_order_sn' => $info['order_sn'],
  928. 'type' => 2,
  929. 'coin_type' => 5,
  930. 'money' => $profit,
  931. 'balance' => 0,
  932. 'create_time' => time(),
  933. 'update_time' => time(),
  934. 'remark' => '抢拍收益',
  935. 'status' => 1,
  936. 'mark' => 1
  937. ];
  938. if (!AccountModel::insertGetId($data)) {
  939. $this->error = 2051;
  940. DB::rollBack();
  941. return true;
  942. }
  943. // 佣金入账
  944. if ($memberInfo && $parentId && $parentInfo && $bonus>0) {
  945. $meritsTotal = isset($parentInfo['merits_total'])? $parentInfo['merits_total'] : 0;
  946. $updateData = ['merits_total'=> $meritsTotal + $info['real_price'],'bonus' => $parentInfo['bonus'] + $bonus,'bonus_total'=> $parentInfo['bonus_total']+$bonus, 'update_time' => time()];
  947. if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update($updateData)) {
  948. $this->error = 2051;
  949. DB::rollBack();
  950. return true;
  951. }
  952. // 佣金记录
  953. $data = [
  954. 'user_id' => $parentId,
  955. 'shop_id' => $info['shop_id'],
  956. 'source_uid' => $info['user_id'],
  957. 'source_order_sn' => $info['order_sn'],
  958. 'type' => 2,
  959. 'coin_type' => 2,
  960. 'money' => $bonus,
  961. 'balance' => $parentInfo['bonus'],
  962. 'create_time' => time(),
  963. 'update_time' => time(),
  964. 'remark' => '推广佣金',
  965. 'status' => 1,
  966. 'mark' => 1
  967. ];
  968. if (!AccountModel::insertGetId($data)) {
  969. $this->error = 2051;
  970. DB::rollBack();
  971. return true;
  972. }
  973. // 结算统计
  974. FinanceService::make()->settleBonus($bonus, 2);
  975. }
  976. DB::commit();
  977. // 店铺统计
  978. $shopId = isset($info['shop_id']) ? $info['shop_id'] : 0;
  979. $shopInfo = ShopModel::where(['id'=> $shopId])->first();
  980. if($shopInfo){
  981. ShopModel::where(['id'=> $shopId])->update(['trade_count'=> $shopInfo['trade_count'], 'trade_total'=> $info['real_price']+$shopInfo['trade_total']]);
  982. }
  983. $this->error = 2049;
  984. return true;
  985. }
  986. /**
  987. * 申请待售
  988. * @param $params
  989. * @param $userId
  990. * @return false
  991. */
  992. public function sell($params, $userId)
  993. {
  994. $id = isset($params['id']) ? $params['id'] : 0;
  995. $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
  996. if (empty($id) || empty($info)) {
  997. $this->error = 2042;
  998. return false;
  999. }
  1000. if ($info['status'] != 3) {
  1001. $this->error = 2053;
  1002. return false;
  1003. }
  1004. if ($info['user_id'] != $userId) {
  1005. $this->error = 2045;
  1006. return false;
  1007. }
  1008. DB::beginTransaction();
  1009. if (!$this->model->where(['id' => $id])->update(['status' => 4, 'is_sell' => 1, 'update_time' => time()])) {
  1010. DB::rollBack();
  1011. $this->error = 2054;
  1012. return false;
  1013. }
  1014. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['confirm_status' => 2, 'update_time' => time()])) {
  1015. DB::rollBack();
  1016. $this->error = 2054;
  1017. return false;
  1018. }
  1019. DB::commit();
  1020. $this->error = 2055;
  1021. return true;
  1022. }
  1023. /**
  1024. * 申请待售审核
  1025. * @param $params
  1026. * @param $userId
  1027. * @return false
  1028. */
  1029. public function sellConfirm($params)
  1030. {
  1031. $id = isset($params['id']) ? $params['id'] : 0;
  1032. $info = $this->model->from('trade as a')
  1033. ->leftJoin('goods as b','b.id','=','a.goods_id')
  1034. ->where(['a.id' => $id, 'a.mark' => 1])
  1035. ->select(['a.*','b.split_price','b.split_num'])
  1036. ->first();
  1037. if (empty($id) || empty($info)) {
  1038. $this->error = 2042;
  1039. return false;
  1040. }
  1041. if (!in_array($info['status'], [3,4])) {
  1042. $this->error = 2053;
  1043. return false;
  1044. }
  1045. DB::beginTransaction();
  1046. $priceRate = ConfigService::make()->getConfigByCode('price_rate');
  1047. $priceRate = $priceRate? $priceRate : 4;
  1048. $stopSplitPrice = ConfigService::make()->getConfigByCode('stop_split_price');
  1049. $stopSplitPrice = $stopSplitPrice? $stopSplitPrice : 500;
  1050. // 判断是否可以上架或拆分
  1051. if($info['split_price']<=0){
  1052. $splitPrice = ConfigService::make()->getConfigByCode('split_price');
  1053. $splitPrice = $splitPrice? $splitPrice : 10000;
  1054. $info['split_price'] = $splitPrice;
  1055. }
  1056. $realPrice = $info['real_price'];
  1057. $sellPrice = $info['sell_price']; // 特价
  1058. $price = $info['price']; // 买入价格
  1059. $price1 = $info['new_price']; // 买入价格
  1060. $addPrice = intval($realPrice*$priceRate/100,0);
  1061. // 满足涨价上架
  1062. if($price1+$addPrice < $info['split_price']){
  1063. // 平台服务费
  1064. $serviceRate = ConfigService::make()->getConfigByCode('service_fee_rate');
  1065. $serviceRate = $serviceRate ? $serviceRate : 8;
  1066. $serviceFee = round(($realPrice)*$serviceRate/100, 0);
  1067. 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()])) {
  1068. $this->error = 2056;
  1069. DB::rollBack();
  1070. return false;
  1071. }
  1072. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['last_sell_time'=>time(), 'price' => $price1+$addPrice,'real_price'=> $realPrice + $addPrice,'is_trade'=> 2,'confirm_status'=>1, 'update_time' => time()])) {
  1073. $this->error = 2056;
  1074. DB::rollBack();
  1075. return false;
  1076. }
  1077. // 服务费统计
  1078. FinanceService::make()->settleBonus($serviceFee, 1);
  1079. DB::commit();
  1080. // 佣金结算
  1081. if(!TradeService::make()->tradeSettle($id, $info)){
  1082. return false;
  1083. }
  1084. $this->error = 2057;
  1085. return true;
  1086. }
  1087. // 停止拆分
  1088. else if($info['sell_price'] == $stopSplitPrice){
  1089. if (!GoodsModel::where(['id' => $info['goods_id']])->update(['status' => 2,'confirm_status'=>1,'split_stop'=>1, 'update_time' => time()])) {
  1090. $this->error = 2054;
  1091. DB::rollBack();
  1092. return false;
  1093. }
  1094. // 佣金结算
  1095. DB::commit();
  1096. if(!TradeService::make()->tradeSettle($id, $info)){
  1097. return false;
  1098. }
  1099. $this->error = 2064;
  1100. return true;
  1101. }
  1102. // 满足拆分
  1103. else if($info['split_price']) {
  1104. if(!GoodsService::make()->split($info['goods_id'], $info)){
  1105. $this->error = 2058;
  1106. DB::rollBack();
  1107. return false;
  1108. }
  1109. // 佣金结算
  1110. DB::commit();
  1111. if(!TradeService::make()->tradeSettle($id, $info)){
  1112. return false;
  1113. }
  1114. $this->error = 2059;
  1115. return true;
  1116. }
  1117. $this->error = 2056;
  1118. return false;
  1119. }
  1120. /**
  1121. * 交易结算
  1122. * @param $tradeId
  1123. * @param $info
  1124. * @return bool
  1125. */
  1126. public function tradeSettle($tradeId, $info)
  1127. {
  1128. // 更新商品交易状态
  1129. DB::beginTransaction();
  1130. $this->model->where(['goods_id' => $info['goods_id'],'status'=>4, 'mark' => 1])->whereNotIn('id',[$tradeId])->update(['is_out'=>1,'update_time'=>time()]);
  1131. // 更改商品归属人
  1132. if (!GoodsModel::where(['id' => $info['goods_id'], 'mark' => 1])->update(['user_id'=> $info['user_id'],'update_time'=> time()])) {
  1133. $this->error = 2050;
  1134. DB::rollBack();
  1135. return false;
  1136. }
  1137. $memberInfo = MemberModel::where(['id' => $info['user_id'], 'mark' => 1])->first();
  1138. $parentId = isset($memberInfo['parent_id']) ? $memberInfo['parent_id'] : 0;
  1139. $meritsCount = isset($memberInfo['merits_count']) ? $memberInfo['merits_count'] : 0;
  1140. $updateData = ['merits_count'=> $meritsCount+$info['real_price'],'merits_time'=>date('Y-m-d H:i:s'),'member_level'=>1,'update_time'=>time()];
  1141. if(!MemberModel::where(['id'=> $info['user_id']])->update($updateData)){
  1142. $this->error = 2050;
  1143. DB::rollBack();
  1144. return false;
  1145. }
  1146. // 佣金结算
  1147. $parentInfo = MemberModel::where(['id' => $parentId, 'mark' => 1])->first();
  1148. $bonusRate = ConfigService::make()->getConfigByCode('bonus_rate');
  1149. $bonusRate = $bonusRate ? $bonusRate : 5;
  1150. $bonus = $info['real_price'] * $bonusRate / 100;
  1151. $profitRate = ConfigService::make()->getConfigByCode('profit_rate');
  1152. $profitRate = $profitRate ? $profitRate : 0;
  1153. $profit = $info['real_price'] * $profitRate / 100;
  1154. if (!$this->model->where(['id' => $tradeId, 'mark' => 1])->update(['bonus' => $bonus,'profit'=> $profit, 'update_time' => time()])) {
  1155. $this->error = 2051;
  1156. DB::rollBack();
  1157. return true;
  1158. }
  1159. // 收益记录
  1160. $data = [
  1161. 'user_id' => $info['user_id'],
  1162. 'shop_id' => $info['shop_id'],
  1163. 'source_uid' => 0,
  1164. 'source_order_sn' => $info['order_sn'],
  1165. 'type' => 2,
  1166. 'coin_type' => 5,
  1167. 'money' => $profit,
  1168. 'balance' => 0,
  1169. 'create_time' => time(),
  1170. 'update_time' => time(),
  1171. 'remark' => '抢拍收益',
  1172. 'status' => 1,
  1173. 'mark' => 1
  1174. ];
  1175. if (!AccountModel::insertGetId($data)) {
  1176. $this->error = 2051;
  1177. DB::rollBack();
  1178. return true;
  1179. }
  1180. // 佣金入账
  1181. if ($memberInfo && $parentId && $parentInfo && $bonus>0) {
  1182. $meritsTotal = isset($parentInfo['merits_total'])? $parentInfo['merits_total'] : 0;
  1183. $updateData = ['merits_total'=> $meritsTotal + $info['real_price'],'bonus' => $parentInfo['bonus'] + $bonus,'bonus_total'=> $parentInfo['bonus_total']+$bonus, 'update_time' => time()];
  1184. if (!MemberModel::where(['id' => $parentId, 'mark' => 1])->update($updateData)) {
  1185. $this->error = 2051;
  1186. DB::rollBack();
  1187. return true;
  1188. }
  1189. // 佣金记录
  1190. $data = [
  1191. 'user_id' => $parentId,
  1192. 'shop_id' => $info['shop_id'],
  1193. 'source_uid' => $info['user_id'],
  1194. 'source_order_sn' => $info['order_sn'],
  1195. 'type' => 2,
  1196. 'coin_type' => 2,
  1197. 'money' => $bonus,
  1198. 'balance' => $parentInfo['bonus'],
  1199. 'create_time' => time(),
  1200. 'update_time' => time(),
  1201. 'remark' => '推广佣金',
  1202. 'status' => 1,
  1203. 'mark' => 1
  1204. ];
  1205. if (!AccountModel::insertGetId($data)) {
  1206. $this->error = 2051;
  1207. DB::rollBack();
  1208. return true;
  1209. }
  1210. // 结算统计
  1211. FinanceService::make()->settleBonus($bonus, 2);
  1212. }
  1213. DB::commit();
  1214. // 店铺统计
  1215. $shopId = isset($info['shop_id']) ? $info['shop_id'] : 0;
  1216. $shopInfo = ShopModel::where(['id'=> $shopId])->first();
  1217. if($shopInfo){
  1218. ShopModel::where(['id'=> $shopId])->update(['trade_count'=> $shopInfo['trade_count'], 'trade_total'=> $info['real_price']+$shopInfo['trade_total']]);
  1219. }
  1220. return true;
  1221. }
  1222. /**
  1223. * 修改订单
  1224. * @param $params
  1225. * @return bool
  1226. */
  1227. public function modify($params)
  1228. {
  1229. $id = isset($params['id'])? $params['id'] : 0;
  1230. $status = isset($params['status'])? $params['status'] : 0;
  1231. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  1232. if(!$id || empty($info)){
  1233. $this->error = 2042;
  1234. return false;
  1235. }
  1236. if(!in_array($info['status'], [1,2,3,4])){
  1237. $this->error = 2082;
  1238. return false;
  1239. }
  1240. if(!in_array($status, [1,2,4])){
  1241. $this->error =2087;
  1242. return false;
  1243. }
  1244. // $safePassword = isset($params['password'])? trim($params['password']) : '';
  1245. // if(empty($safePassword)){
  1246. // $this->error = 2085;
  1247. // return false;
  1248. // }
  1249. $memberInfo = MemberModel::where(['id'=> $info['user_id']])->select(['id','safe_password'])->first();
  1250. $password = isset($memberInfo['safe_password'])? $memberInfo['safe_password'] : '';
  1251. if(empty($memberInfo)){
  1252. $this->error = 2019;
  1253. return false;
  1254. }
  1255. // $safePassword = get_password($safePassword);
  1256. // if($password != $safePassword){
  1257. // $this->error = 2086;
  1258. // return false;
  1259. // }
  1260. if($this->model->where(['id'=> $id])->update(['status'=> $status,'update_time'=> time()])){
  1261. $this->error = 1020;
  1262. return true;
  1263. }else{
  1264. $this->error = 1021;
  1265. return false;
  1266. }
  1267. }
  1268. /**
  1269. * 修改订单
  1270. * @param $params
  1271. * @return bool
  1272. */
  1273. public function cancel($params)
  1274. {
  1275. $id = isset($params['id'])? $params['id'] : 0;
  1276. $status = isset($params['status'])? $params['status'] : 0;
  1277. $info = $this->model->where(['id'=> $id,'mark'=>1])->first();
  1278. if(!$id || empty($info)){
  1279. $this->error = 2042;
  1280. return false;
  1281. }
  1282. if(!in_array($info['status'], [1,2,3,4])){
  1283. $this->error = 2082;
  1284. return false;
  1285. }
  1286. if($this->model->where(['id'=> $id])->update(['status'=> 5,'update_time'=> time()])){
  1287. $this->error = 2088;
  1288. return true;
  1289. }else{
  1290. $this->error = 2089;
  1291. return false;
  1292. }
  1293. }
  1294. /**
  1295. * 定期清除交易记录
  1296. * @return false
  1297. */
  1298. public function clearByDay()
  1299. {
  1300. $day = ConfigService::make()->getConfigByCode('clear_trade_time');
  1301. $day = $day? $day : 2;
  1302. $cacheKey = "caches:task:clearTrade:d{$day}_".date('Ymd');
  1303. if(RedisService::get($cacheKey)){
  1304. $this->error = '2301';
  1305. return false;
  1306. }
  1307. $clearDay = strtotime(date('Y-m-d')) - $day * 86400;
  1308. $count = $this->model->where('create_time','<', $clearDay)->count();
  1309. if($count<=0){
  1310. $this->error = '';
  1311. RedisService::set($cacheKey,['error'=>'没有记录可以清除','day'=> $day,'clearDay'=> date('Y-m-d', $clearDay)], 6 * 3600);
  1312. return false;
  1313. }
  1314. $this->model->where('create_time','<', $clearDay)->update(['mark'=>0,'remark'=>'到期清除','update_time'=>time()]);
  1315. $this->model->where('create_time','<', $clearDay-86400)->where(['mark'=>0])->delete();
  1316. $result = ['count'=> $count,'day'=> date('Y-m-d H:i:s', $clearDay)];
  1317. RedisService::set($cacheKey, $result, 7200);
  1318. return $result;
  1319. }
  1320. }