TradeService.php 47 KB

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