UserService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2017/11/21
  6. * Time: 10:42
  7. */
  8. namespace App\Modules\User\Services;
  9. use App\Consts\QuickConst;
  10. use App\Modules\Book\Models\BookGiftsSend;
  11. use App\Modules\Book\Models\UserShelfBooks;
  12. use App\Modules\Subscribe\Models\BookOrder;
  13. use App\Modules\Subscribe\Models\ChapterOrder;
  14. use App\Modules\Subscribe\Services\ChapterOrderService;
  15. use App\Modules\User\Models\User;
  16. use App\Modules\User\Models\UserEnv;
  17. use DB;
  18. use App\Jobs\ActionTrigger;
  19. use App\Jobs\UserAddDeskJob;
  20. use App\Modules\OfficialAccount\Models\DistributionSelfDefineConfig;
  21. use App\Modules\Channel\Models\Channel;
  22. use App\Modules\Trade\Models\Order;
  23. use App\Modules\User\Models\QappUserAddDestop;
  24. use App\Modules\UserTask\Services\BaseTask;
  25. use App\Modules\UserTask\Services\UserTaskService;
  26. use Illuminate\Support\Facades\Cache;
  27. use Redis;
  28. class UserService
  29. {
  30. public static function setUserSendOrder(int $uid, int $send_order_id)
  31. {
  32. if (empty($uid) || empty($send_order_id)) {
  33. return false;
  34. }
  35. Redis::hmset('book_read:' . $uid,'send_order_id',$send_order_id,'soid_time',time());
  36. //return Redis::hset('book_read:' . $uid, 'send_order_id', $send_order_id);
  37. }
  38. public static function getUserSendOrderTime($uid)
  39. {
  40. $send_order_id_time = Redis::hGet('book_read:' . $uid, 'soid_time');
  41. $send_order_id_time = (int)($send_order_id_time);
  42. return $send_order_id_time;
  43. }
  44. public static function getUserSendOrder(int $uid)
  45. {
  46. $send_order_id = Redis::hGet('book_read:' . $uid, 'send_order_id');
  47. $send_order_id = (int)($send_order_id ? $send_order_id : 0);
  48. return $send_order_id;
  49. }
  50. public static function qappAddDesktop(int $uid, int $status)
  51. {
  52. $key = "last_add_desktop_status";
  53. $last_status = Redis::hget($key,$uid);
  54. if ($last_status != NULL && $last_status == $status){
  55. return false;
  56. }
  57. Redis::hset($key,$uid,$status);
  58. // myLog('incrAddDeskTop')->info('qappAddDesktop', compact('uid', 'status'));
  59. $log = QappUserAddDestop::where('uid', $uid)->orderBy('id', 'desc')->first();
  60. if($status == 1){
  61. // myLog('incrAddDeskTop')->info('qappAddDesktopTask', compact('uid', 'status'));
  62. $start = time();
  63. UserTaskService::addUserTaskQueue($uid, BaseTask::add_desk, UserTaskService::add_trigger);
  64. $time = time() - $start;
  65. if ($time >= 1 ){
  66. myLog('mq-put-timeout')->info("加桌任务队列-mq-投递超过1耗时".$time."s;路径:app/Modules/User/Services/UserService.php-qappAddDesktop方法 74行");
  67. }
  68. }
  69. if (!$log) {
  70. //第一次加桌 没有任何记录,加桌状态为1
  71. if($status == 1){
  72. // 加桌统计
  73. // QappAddDeskTopService::incrAddDeskTop($uid, QuickConst::FIELD_ADD_DESKTOP);
  74. $start = time();
  75. $job = new UserAddDeskJob($uid);
  76. dispatch($job)->onConnection('redis_queue')->onQueue('{qapp_user_add_desk}')->delay(now()->addMinutes(3));
  77. $time = time() - $start;
  78. if ($time >= 1 ){
  79. myLog('mq-put-timeout')->info("加桌队列-mq-投递超过1耗时".$time."s;路径:app/Modules/User/Services/UserService.php-qappAddDesktop方法 88行");
  80. }
  81. }
  82. //第一次加桌 没有任何记录,加桌状态为0
  83. if($status == 0){
  84. QappUserAddDestop::create([
  85. 'uid' => $uid,
  86. 'status' => $status,
  87. ]);
  88. }
  89. }else{
  90. //如果有记录,但是都是0 没有1的记录
  91. $oneexists = QappUserAddDestop::where('uid', $uid)->where('status', 1)->exists();
  92. if(!$oneexists && $status == 1){
  93. // 加桌统计
  94. // QappAddDeskTopService::incrAddDeskTop($uid, QuickConst::FIELD_ADD_DESKTOP);
  95. $job = new UserAddDeskJob($uid);
  96. $start = time();
  97. dispatch($job)->onConnection('redis_queue')->onQueue('{qapp_user_add_desk}')->delay(now()->addMinutes(3));
  98. $time = time() - $start;
  99. if ($time >= 1 ){
  100. myLog('mq-put-timeout')->info("加桌队列-mq-投递超过1耗时".$time."s;路径:app/Modules/User/Services/UserService.php-qappAddDesktop方法 111行");
  101. }
  102. return false;
  103. }
  104. //有加桌记录 最近一次加桌状态和和此次状态是否相等
  105. if($log->status != $status){
  106. //不相等 插入记录
  107. QappUserAddDestop::create([
  108. 'uid' => $uid,
  109. 'status' => $status,
  110. ]);
  111. }
  112. //相等 不做处理
  113. return false;
  114. }
  115. }
  116. /**
  117. * 创建用户
  118. * $params
  119. * 必穿参数 openid、unionid、distribution_channel_id
  120. * 可传 province、city、country、headimgurl、send_order_id、sex、register_ip
  121. */
  122. static function addUser($params)
  123. {
  124. return User::addUser($params);
  125. }
  126. static function getUserList($limit, $offset)
  127. {
  128. return User::getUserList($limit, $offset);
  129. }
  130. static function getUserByNickAndChannelId($distribution_channel_id, $nickname)
  131. {
  132. return User::getUserByNickAndChannelId($distribution_channel_id, $nickname);
  133. }
  134. /**
  135. * 获取用户信息列表
  136. * @param $pageSize 每页显示的条数
  137. */
  138. static function getPaginationList($pageSize = 20)
  139. {
  140. return User::getPaginationList($pageSize);
  141. }
  142. /**
  143. * 获取用户信息列表
  144. * @param $pageSize 每页显示的条数
  145. */
  146. static function getUsers($pageSize = 20, $careStatus)
  147. {
  148. return User::getPaginationList($pageSize, $careStatus);
  149. }
  150. /**
  151. * 根据id获取用户信息
  152. * @return user object
  153. */
  154. static function getById($id)
  155. {
  156. return User::getById($id);
  157. }
  158. /**
  159. * 根据id获取用户信息
  160. * @return user object
  161. */
  162. static function getUserDataById($id)
  163. {
  164. return User::getUserDataById($id);
  165. }
  166. /**
  167. * 根据id获取用户信息
  168. * @return user object
  169. */
  170. static function getUserDataDetailById($id)
  171. {
  172. return User::getUserDataDetailById($id);
  173. }
  174. /**
  175. * 根据id获取用户名称
  176. * @return string [用户昵称]
  177. */
  178. static function getNicknameById($id)
  179. {
  180. return User::getById($id)->nickname;
  181. }
  182. /**
  183. * 更新用户信息
  184. * @param int $id 用户ID
  185. * @param array $params 可传 province、city、country、headimgurl、sex
  186. * @return boolen
  187. */
  188. static function updateInfo($id, $params)
  189. {
  190. \Log::info('~~~~~~~~~~~~~~~~~====update User====~~~~~~~~~~~~~~~~' . "\n");
  191. \Log::info($id);
  192. \Log::info($params);
  193. return User::updateInfo($id, $params);
  194. }
  195. /**
  196. * 根据union和分销id获取用户信息
  197. * @param $union
  198. * @param $channel_id
  199. * @return mixed
  200. */
  201. static function getUserByUnionAndChannelId($openid, $channel_id)
  202. {
  203. return User::where('openid', $openid)->where('distribution_channel_id', $channel_id)->select('id', 'openid', 'unionid', 'balance', 'charge_balance', 'reward_balance')->first();
  204. }
  205. static function getUserByOpenidAndChannelId($openid, $channel_id)
  206. {
  207. return User::where('openid', $openid)->where('distribution_channel_id', $channel_id)->first();
  208. }
  209. /**
  210. * 用户余额充值
  211. * @param $uid
  212. * @param $fee
  213. * @return mixed
  214. */
  215. static function addBalance($uid, $fee, $charge, $given)
  216. {
  217. return User::addBalance($uid, $fee, $charge, $given);
  218. }
  219. /**
  220. * 查询推广注册用户总数
  221. * @param $send_order_id
  222. * @return mixed
  223. */
  224. static function getPromotionTotal($send_order_id)
  225. {
  226. return User::getPromotionTotal($send_order_id);
  227. }
  228. /**
  229. * 查询渠道某天注册用户总数
  230. * @param $channel_id
  231. * @param $date
  232. * @return mixed
  233. */
  234. static function getChannelDayTotal($channel_id, $date)
  235. {
  236. return User::getChannelDayTotal($channel_id, $date);
  237. }
  238. /**
  239. * 查询渠道注册用户总数
  240. * @param $channel_id
  241. * @return mixed
  242. */
  243. static function getChannelTotal($channel_id)
  244. {
  245. return User::getChannelTotal($channel_id);
  246. }
  247. /**
  248. * 查询渠道某段时间注册用户总数
  249. * @param $channel_id
  250. * @param $startDate
  251. * @param $endDate
  252. * @return mixed
  253. */
  254. static function getChannelDayToDayTotal($channel_id, $startDate = '', $endDate = '')
  255. {
  256. return User::getChannelDayToDayTotal($channel_id, $startDate, $endDate);
  257. }
  258. /**
  259. * 查询渠道某段时间注册用户总数
  260. * @param array $channelIds
  261. * @param $startDate
  262. * @param $endDate
  263. * @return mixed
  264. */
  265. static function getChannelsDayToDayTotal($channelIds, $startDate = '', $endDate = '')
  266. {
  267. return User::getChannelsDayToDayTotal($channelIds, $startDate, $endDate);
  268. }
  269. /**
  270. * 查询注册用户总数
  271. * @param $params
  272. */
  273. static function getTotalCount($params)
  274. {
  275. return User::getTotalCount($params);
  276. }
  277. /**
  278. * 查询渠道是否有登录
  279. * @param $channelId
  280. * @param $start_time
  281. * @param $end_time
  282. * @return int
  283. */
  284. static function judgeUserYesterdayLogin($channelId, $start_time, $end_time)
  285. {
  286. $user_behavior = DB::table('channel_operate_record')
  287. ->where([
  288. ['distribution_channel_id', '=', $channelId],
  289. ['created_at', '>=', $start_time],
  290. ['created_at', '<=', $end_time]
  291. ])
  292. ->first();
  293. if ($user_behavior) return 1;
  294. return 0;
  295. }
  296. /**
  297. * @param $date
  298. * @param $company_id
  299. * @return mixed
  300. */
  301. static function getCompanyDayTotal($date, $company_id)
  302. {
  303. $end_date = date('Y-m-d', strtotime($date) + 86400);
  304. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  305. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  306. ->where([
  307. ['users.created_at', '>=', $date],
  308. ['users.created_at', '<', $end_date],
  309. ['channel_users.company_id', '=', $company_id]
  310. ])
  311. ->groupBy('channel_users.company_id')
  312. ->select(DB::raw("count(users.id) as register_sum"))
  313. ->first();
  314. if ($info) return $info->register_sum;
  315. //\Log::info('getCompanyDayTotal error:' . $company_id);
  316. return $info;
  317. }
  318. static function getCompanyTotal($company_id)
  319. {
  320. $info = User::leftjoin('distribution_channels', 'distribution_channels.id', '=', 'users.distribution_channel_id')
  321. ->leftjoin('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  322. ->where('channel_users.company_id', $company_id)
  323. ->groupBy('channel_users.company_id')
  324. ->select(DB::raw("count(users.id) as register_sum"))
  325. ->first();
  326. if ($info) return $info->register_sum;
  327. //\Log::error('getCompanyTotal error:' . $company_id);
  328. return $info;
  329. }
  330. static function transfromBalanceByUser($from_uid, $to_uid)
  331. {
  332. $add_balance_sql = "update users,(select * from users where id =" . $from_uid . ") as u2
  333. set users.balance = users.balance + u2.balance,
  334. users.reward_balance = users.reward_balance +u2.reward_balance,
  335. users.charge_balance = users.charge_balance +u2.charge_balance where users.id =" . $to_uid;
  336. $res = DB::update($add_balance_sql);
  337. return User::updateInfo($from_uid, ['balance' => 0, 'reward_balance' => 0, 'charge_balance' => 0]);
  338. }
  339. // 扔用户一些动作到队列,异步处理一些额外逻辑
  340. static function PushUserActionToQueue($action_type, $distribution_channel_id, $param)
  341. {
  342. try {
  343. // \Log::info('PushUserActionToQueue_param_start:'.$distribution_channel_id.' action_type:'.$action_type. ' param:'.json_encode($param));
  344. // 判断站点是否需要额外逻辑
  345. $distribution_self_define_config = DistributionSelfDefineConfig::getDistributionSelfDefineConfig($distribution_channel_id, 'action_call_ycsd');
  346. if (empty($distribution_self_define_config)) {
  347. // \Log::info('not_action_call_ycsd:'.$distribution_channel_id);
  348. return '';
  349. } else {
  350. \Log::info('PushUserActionToQueue_param:' . $distribution_channel_id . ' action_type:' . $action_type . ' param:' . json_encode($param));
  351. // 如果支付通道已经切换回去,则不推送了
  352. // $pay_merchant_id = env('YCSD_CALL_PAY_MERCHANT_ID');
  353. // 当前默认
  354. $pay_merchant_id = Redis::get('YCSD_CALL_PAY_MERCHANT_ID');
  355. $channel = Channel::getById($distribution_channel_id);
  356. $current_pay_merchant_id = isset($channel->pay_merchant_id) ? $channel->pay_merchant_id : '';
  357. \Log::info('action_call_ycsd:' . $distribution_channel_id . ' $pay_merchant_id:' . $pay_merchant_id . ' $current_pay_merchant_id:' . $current_pay_merchant_id);
  358. if ($current_pay_merchant_id != $pay_merchant_id) {
  359. // 判断订单是否是默认支付,放行,切换期间的bug
  360. if ($action_type == 'CallBackOrder') {
  361. $order = Order::getByTradeNo($distribution_channel_id, $param['order_sn']);
  362. if (isset($order->pay_merchant_id) && $order->pay_merchant_id == $pay_merchant_id) {
  363. \Log::info('action_call_ycsd_old_order_continue:' . $param['order_sn']);
  364. } else {
  365. \Log::info('action_call_ycsd_has_change_pay_return:' . $distribution_channel_id);
  366. return '';
  367. }
  368. } else {
  369. \Log::info('action_call_ycsd_has_change_pay_return:' . $distribution_channel_id);
  370. return '';
  371. }
  372. }
  373. \Log::info('action_call_ycsd:' . $distribution_channel_id);
  374. }
  375. $data = [];
  376. $data['send_time'] = date("Y-m-d H:i:s");
  377. $data['action_type'] = $action_type;
  378. if ($action_type == 'Register') {
  379. $data['openid'] = $param['openid'];
  380. $data['uid'] = $param['uid'];
  381. } elseif ($action_type == 'CreateOrder') {
  382. $data['orderSn'] = $param['order_sn'];
  383. $data['amount'] = $param['amount'];
  384. $data['openid'] = $param['openid'];
  385. $data['uid'] = $param['uid'];
  386. } elseif ($action_type == 'CallBackOrder') {
  387. $data['orderSn'] = $param['order_sn'];
  388. $data['openid'] = $param['openid'];
  389. }
  390. $send_data = array(
  391. 'send_time' => date("Y-m-d H:i:s"),
  392. 'data' => $data
  393. );
  394. \Log::info('PushUserActionToQueue_openid:' . $data['openid'] . ' action_type:' . $action_type);
  395. \Log::info('$send_data');
  396. \Log::info($send_data);
  397. $delay = 0;
  398. $job = (new ActionTrigger($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('action_trigger_list');
  399. dispatch($job);
  400. } catch (\Exception $e) {
  401. \Log::info('ept:' . $e->getMessage());
  402. }
  403. }
  404. public static function isCpcUser($uid)
  405. {
  406. //openid下所有强关超过三天的uid都无充值,对强关后的uid进行曝光
  407. //openid下有一个强关超过三天的uid,都无充值,对强关后的uid进行曝光
  408. $sql_format = "SELECT u.id,f.subscribe_time,(SELECT id from orders where uid = u.id and `status` = 'PAID' LIMIT 1)as oid FROM users u
  409. JOIN force_subscribe_users f on u.id = f.uid and f.is_subscribed= 1
  410. WHERE u.openid in (SELECT openid from users WHERE id = %s)";
  411. $result = DB::select(sprintf($sql_format, $uid));
  412. if (!$result) return false;
  413. $is_has_force_subscribe_three_days = false;
  414. foreach ($result as $item) {
  415. //有充值
  416. if ($item->oid) return false;
  417. //强关没超过超过三天
  418. //if(time()-strtotime($item->subscribe_time) < 86400*3) return false;
  419. if (time() - strtotime($item->subscribe_time) > 86400 * 3) $is_has_force_subscribe_three_days = true;
  420. }
  421. return $is_has_force_subscribe_three_days;
  422. }
  423. public static function recordUA($ua, $uid)
  424. {
  425. return UserEnv::recordUA($ua, $uid);
  426. }
  427. public static function transfer(int $from, int $to, int $distribution_channel_id)
  428. {
  429. $transfer_record = DB::table('crm_transfer')->where('from_uid', $from)->first();
  430. if ($transfer_record) {
  431. return;
  432. }
  433. $from_user = self::getById($from);
  434. $to_user = self::getById($to);
  435. if (!$from_user) return;
  436. if (!$to_user) return;
  437. //阅读记录迁移***************************************
  438. $record = Redis::hgetall('book_read:' . $from);
  439. $not_uid_key = ['last_read', 'sign_counts', 'sign_info', 'sign_day'];
  440. $data = [];
  441. foreach ($record as $k => $item) {
  442. if (is_numeric($k) || in_array($k, $not_uid_key)) {
  443. $data[$k] = $item;
  444. }
  445. }
  446. //\Log::info('read record is');
  447. //\Log::info($data);
  448. if ($data) Redis::hmset('book_read:' . $to, $data);
  449. //签到记录
  450. /*$user_sign_model = new UserSign();
  451. $user_sign_model->setCurrentTable(date('Ym'));
  452. $sign_record = $user_sign_model->where('uid',$from)->select('price','sign_time','day','created_at')->orderBy('sign_time','desc')->get();
  453. $temp = [];
  454. if($sign_record){
  455. foreach ($sign_record as $item){
  456. $temp[] = [
  457. 'uid'=>$to,
  458. 'price'=>$item->price,
  459. 'day'=>$item->day,
  460. 'sign_time'=>$item->sign_time,
  461. 'created_at'=>$item->created_at,
  462. 'updated_at'=>date('Y-m-d H:i:s'),
  463. ];
  464. }
  465. //\Log::info('user_sign is');
  466. //\Log::info($temp);
  467. DB::table('user_sign'.date('Ym'))->insert($temp);
  468. }*/
  469. //订阅记录(按本)
  470. $book_order = BookOrder::where('uid', $from)->where('bid', '>', 0)->get();
  471. if ($book_order) {
  472. $temp = [];
  473. foreach ($book_order as $book) {
  474. $temp[] = [
  475. 'distribution_channel_id' => $distribution_channel_id,
  476. 'bid' => $book->bid,
  477. 'book_name' => $book->book_name,
  478. 'uid' => $to,
  479. 'u' => $book->u,
  480. 'fee' => 0,
  481. 'created_at' => $book->created_at,
  482. 'updated_at' => date('Y-m-d H:i:s'),
  483. ];
  484. }
  485. //\Log::info('book_order is');
  486. //\Log::info($temp);
  487. DB::table('book_orders')->insert($temp);
  488. }
  489. //订阅记录(按章)
  490. $chapter_order_record = ChapterOrderService::getRecordByUid($from, '', '', true);
  491. if ($chapter_order_record) {
  492. $temp = [];
  493. $i = 1;
  494. $chapter_model = new ChapterOrder();
  495. $chapter_model->setCurrentTable($to);
  496. foreach ($chapter_order_record as $chapter_order) {
  497. $temp[] = [
  498. 'distribution_channel_id' => $distribution_channel_id,
  499. 'bid' => $chapter_order->bid,
  500. 'cid' => $chapter_order->cid,
  501. 'chapter_name' => $chapter_order->chapter_name,
  502. 'book_name' => $chapter_order->book_name,
  503. 'uid' => $to,
  504. 'send_order_id' => 0,
  505. 'fee' => 0,
  506. 'created_at' => $chapter_order->created_at,
  507. 'updated_at' => date('Y-m-d H:i:s')
  508. ];
  509. if ($i % 100 == 0) {
  510. $chapter_model->insert($temp);
  511. $temp = [];
  512. }
  513. $i++;
  514. }
  515. //\Log::info('chapter order is');
  516. //\Log::info($temp);
  517. $chapter_model->insert($temp);
  518. }
  519. //打赏记录
  520. $gift_result = BookGiftsSend::where('uid', $from)->get();
  521. if ($gift_result) {
  522. $tmp = [];
  523. foreach ($gift_result as $g) {
  524. $tmp[] = [
  525. 'uid' => $to,
  526. 'gift_id' => $g->gift_id,
  527. 'bid' => $g->bid,
  528. 'icon' => $g->icon,
  529. 'name_desc' => $g->name_desc,
  530. 'cost' => 0,
  531. 'cost_reward' => $g->cost_reward,
  532. 'cost_recharge' => $g->cost_recharge,
  533. 'created_at' => $g->created_at,
  534. 'updated_at' => date('Y-m-d H:i:s')
  535. ];
  536. }
  537. //\Log::info('book gift is');
  538. //\Log::info($temp);
  539. DB::table('book_gifts_send')->insert($tmp);
  540. }
  541. //书架
  542. $result = UserShelfBooks::where('uid', $from)->get();
  543. if ($result) {
  544. $tmp = [];
  545. foreach ($result as $s) {
  546. $tmp[] = [
  547. 'uid' => $to,
  548. 'distribution_channel_id' => $distribution_channel_id,
  549. 'bid' => $s->bid,
  550. 'created_at' => $s->created_at,
  551. 'updated_at' => date('Y-m-d H:i:s')
  552. ];
  553. }
  554. //\Log::info('user book shelf is');
  555. //\Log::info($temp);
  556. DB::table('user_shelf_books')->insert($tmp);
  557. }
  558. //包年
  559. $result = DB::table('year_orders')->where('uid', $from)->where('end_time', '>=', date('Y-m-d H:i:s'))->first();
  560. if ($result) {
  561. DB::table('year_orders')->insert([
  562. 'uid' => $to,
  563. 'begin_time' => $result->begin_time,
  564. 'end_time' => $result->end_time,
  565. 'distribution_channel_id' => $distribution_channel_id,
  566. 'send_order_id' => $result->send_order_id,
  567. 'fee' => $result->fee,
  568. 'created_at' => date('Y-m-d H:i:s'),
  569. 'updated_at' => $result->updated_at,
  570. ]);
  571. //\Log::info('year_orders is');
  572. //print_r($result);
  573. }
  574. //书币
  575. $to_user->balance += $from_user->balance;
  576. $to_user->reward_balance += $from_user->balance;
  577. $to_user->nickname = $from_user->nickname;
  578. $to_user->sex = $from_user->sex;
  579. $to_user->head_img = $from_user->head_img;
  580. $to_user->country = $from_user->country;
  581. $to_user->city = $from_user->city;
  582. $to_user->province = $from_user->province;
  583. $to_user->save();
  584. DB::table('crm_transfer')->insert([
  585. 'from_uid' => $from,
  586. 'to_uid' => $to,
  587. 'is_enable' => 1,
  588. 'created_at' => date('Y-m-d H:i:s'),
  589. 'updated_at' => date('Y-m-d H:i:s')
  590. ]);
  591. }
  592. public static function getUserByOpenid($openid)
  593. {
  594. return User::where('openid', $openid)->get();
  595. }
  596. /**
  597. * 实时绑定用户和最新派单的关系,目前在获取章节详情调用;只能在章节详情时调用
  598. * @param [type] $uid [用户id]
  599. * @param [type] $send_order_id [派单id]
  600. * @return [type] [description]
  601. */
  602. public static function bindSendOrderId($uid, $send_order_id)
  603. {
  604. if ($send_order_id) {
  605. $res = Redis::hset('book_read_chapter:' . $uid,'send_order_id', $send_order_id);
  606. }
  607. }
  608. /**
  609. * 实时绑定用户和最新派单的关系后,其它逻辑获取用户绑定的最新派单id
  610. * @param [type] $uid [用户id]
  611. * @param [type] $send_order_id [派单id]
  612. * @return [int] [返回用户的派单id值]
  613. */
  614. public static function GetBindSendOrderId($uid)
  615. {
  616. try {
  617. $send_order_id = Redis::hget('book_read_chapter:' . $uid, 'send_order_id');
  618. if ($send_order_id)
  619. return (int)$send_order_id;
  620. } catch (\Exception $e) {
  621. }
  622. }
  623. }