TradeService.php 46 KB

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