TradeService.php 47 KB

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