You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Order;
  4. use App\Model\OrderMain;
  5. use App\Model\Users;
  6. class UserService implements UserServiceInterface
  7. {
  8. /**
  9. * 是否平台新用户
  10. * 在很多奖励的地方会需要用到这个查询
  11. * 判定条件:
  12. * 没有在平台下过单(包括线上和线下)
  13. * @param $user_id
  14. * @param $order_main_id
  15. * @return mixed|void
  16. */
  17. public function isPlatformNewUser($user_id, $order_main_id): bool
  18. {
  19. $exist = OrderMain::query()
  20. ->where(['user_id' => $user_id])
  21. ->where('id', '!=', $order_main_id)
  22. ->where(function ($query){
  23. $query->whereIn('state', [OrderMain::ORDER_STATE_COMPLETE,OrderMain::ORDER_STATE_EVALUATED,OrderMain::ORDER_STATE_UNREFUND])
  24. ->orWhereIn('dm_state', [OrderMain::ORDER_STATE_UNTAKE,OrderMain::ORDER_STATE_DELIVERY]);
  25. })
  26. ->exists();
  27. return !$exist;
  28. }
  29. /**
  30. * 根据用户的openid更新unionid信息
  31. * 如果没有找到用户,则不做任何处理
  32. * @param $openid
  33. * @param $unionid
  34. * @return array
  35. */
  36. public function saveUserUnionid($openid,$unionid)
  37. {
  38. $result = [
  39. 'status' => false,
  40. 'msg' => '用户不存在或者已存在相同unionid'
  41. ];
  42. // 查询用户是否存在
  43. $userinfo = Users::select('id','unionid')->where('openid',$openid)->first();
  44. if($userinfo && $userinfo->unionid != $unionid){
  45. $userinfo->unionid = $unionid;
  46. if($res = $userinfo->save()){
  47. $result['status'] = true;
  48. $result['msg'] = '更改用户unionid信息成功';
  49. $result['res'] = $res;
  50. }else{
  51. $result['msg'] = '更改用户unionid信息失败';
  52. }
  53. }
  54. return $result;
  55. }
  56. /**
  57. * @inheritDoc
  58. */
  59. public function isStoreFirstOrderToday($user_id, $store_id, $current_order_id, $limit_amount = 3)
  60. {
  61. return !Order::query()
  62. ->where(['user_id' => $user_id, 'store_id' => $store_id])
  63. ->whereIn('dm_state', [OrderMain::ORDER_STATE_UNTAKE,OrderMain::ORDER_STATE_DELIVERY])
  64. ->where('time_add', '>=', date('Y-m-d 00:00:00'))
  65. ->where('time_add', '<=', date('Y-m-d 23:59:59'))
  66. ->where('money', '>=', $limit_amount)
  67. ->where('id', '!=', $current_order_id)
  68. ->exists();
  69. }
  70. /**
  71. * 用户账户(钱包)
  72. * @inheritDoc
  73. * $type 1 2
  74. */
  75. public function userWallet($userId,$money,$type)
  76. {
  77. $user = Users::select('id','wallet')->where('id',$userId)->lockForUpdate()->first();
  78. $res = false;
  79. if(!empty($user) && is_numeric($money) && $money > 0){
  80. if($type == Users::WALLET_TYPE_INC){
  81. $res = $user->increment('wallet',$money);
  82. }else if($type == Users::WALLET_TYPE_DEC){
  83. $res = $user->decrement('wallet',$money);
  84. }
  85. }
  86. return $res;
  87. }
  88. }