User.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\task\model;
  10. use app\common\model\User as UserModel;
  11. use app\task\model\user\GradeLog as GradeLogModel;
  12. use app\common\enum\user\grade\log\ChangeType as ChangeTypeEnum;
  13. /**
  14. * 用户模型
  15. * Class User
  16. * @package app\task\model
  17. */
  18. class User extends UserModel
  19. {
  20. /**
  21. * 获取用户信息
  22. * @param $where
  23. * @param array $with
  24. * @return static|UserModel|null
  25. * @throws \think\exception\DbException
  26. */
  27. public static function detail($where, $with = [])
  28. {
  29. return parent::detail($where, $with);
  30. }
  31. /**
  32. * 查询满足会员等级升级条件的用户列表
  33. * @param $upgradeGrade
  34. * @param array $excludedUserIds
  35. * @return false|\PDOStatement|string|\think\Collection
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. */
  40. public function getUpgradeUserList($upgradeGrade, $excludedUserIds = [])
  41. {
  42. if (!empty($excludedUserIds)) {
  43. $this->where('user.user_id', 'not in', $excludedUserIds);
  44. }
  45. return $this->alias('user')
  46. ->field(['user.user_id', 'user.grade_id'])
  47. ->join('user_grade grade', 'grade.grade_id = user.grade_id', 'LEFT')
  48. ->where(function ($query) use ($upgradeGrade) {
  49. $query->where('user.grade_id', '=', 0);
  50. $query->whereOr('grade.weight', '<', $upgradeGrade['weight']);
  51. })
  52. ->where('user.expend_money', '>=', $upgradeGrade['upgrade']['expend_money'])
  53. ->where('user.is_delete', '=', 0)
  54. ->select();
  55. }
  56. /**
  57. * 批量设置会员等级
  58. * @param $data
  59. * @return array|false
  60. * @throws \Exception
  61. */
  62. public function setBatchGrade($data)
  63. {
  64. // 批量更新会员等级的数据
  65. $userData = [];
  66. // 批量更新会员等级变更记录的数据
  67. $logData = [];
  68. foreach ($data as $item) {
  69. $userData[] = [
  70. 'user_id' => $item['user_id'],
  71. 'grade_id' => $item['new_grade_id'],
  72. ];
  73. $logData[] = [
  74. 'user_id' => $item['user_id'],
  75. 'old_grade_id' => $item['old_grade_id'],
  76. 'new_grade_id' => $item['new_grade_id'],
  77. 'change_type' => ChangeTypeEnum::AUTO_UPGRADE,
  78. ];
  79. }
  80. // 批量更新会员等级
  81. $status = $this->isUpdate()->saveAll($userData);
  82. // 批量更新会员等级变更记录
  83. (new GradeLogModel)->records($logData);
  84. return $status;
  85. }
  86. }