TradeService.php 53 KB

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