User.php 2.6 KB

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