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.

75 lines
2.3 KiB

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. * @return mixed|void
  15. */
  16. public function isStageNewUser($user_id): bool
  17. {
  18. $exist = OrderMain::query()
  19. ->where(['user_id' => $user_id])
  20. ->where(function ($query){
  21. $query->whereIn('state', [OrderMain::ORDER_STATE_COMPLETE,OrderMain::ORDER_STATE_EVALUATED,OrderMain::ORDER_STATE_UNREFUND])
  22. ->orWhereIn('dm_state', [OrderMain::ORDER_STATE_UNTAKE,OrderMain::ORDER_STATE_DELIVERY]);
  23. })
  24. ->exists();
  25. return !$exist;
  26. }
  27. /**
  28. * 根据用户的openid更新unionid信息
  29. * 如果没有找到用户,则不做任何处理
  30. * @param $openid
  31. * @param $unionid
  32. * @return array
  33. */
  34. public function saveUserUnionid($openid,$unionid)
  35. {
  36. $result = [
  37. 'status' => false,
  38. 'msg' => '用户不存在或者已存在相同unionid'
  39. ];
  40. // 查询用户是否存在
  41. $userinfo = Users::select('id','unionid')->where('openid',$openid)->first();
  42. if($userinfo && $userinfo->unionid != $unionid){
  43. $userinfo->unionid = $unionid;
  44. if($res = $userinfo->save()){
  45. $result['status'] = true;
  46. $result['msg'] = '更改用户unionid信息成功';
  47. $result['res'] = $res;
  48. }else{
  49. $result['msg'] = '更改用户unionid信息失败';
  50. }
  51. }
  52. return $result;
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. public function isStoreFirstOrderToday($user_id, $store_id, $current_order_id)
  58. {
  59. return !Order::query()
  60. ->where(['user_id' => $user_id, 'store_id' => $store_id])
  61. ->whereIn('dm_state', [OrderMain::ORDER_STATE_UNTAKE,OrderMain::ORDER_STATE_DELIVERY])
  62. ->where('time_add', '>=', date('Y-m-d 00:00:00'))
  63. ->where('time_add', '<=', date('Y-m-d 23:59:59'))
  64. ->where('id', '!=', $current_order_id)
  65. ->exists();
  66. }
  67. }