TradeService.php 59 KB

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