User.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\console\model;
  13. use app\common\model\User as UserModel;
  14. use app\common\model\user\Grade as UserGradeModel;
  15. use app\console\model\user\GradeLog as GradeLogModel;
  16. use app\common\enum\user\grade\log\ChangeType as ChangeTypeEnum;
  17. /**
  18. * 用户模型
  19. * Class User
  20. * @package app\console\model
  21. */
  22. class User extends UserModel
  23. {
  24. /**
  25. * 查询满足会员等级升级条件的用户列表
  26. * @param int $storeId 商城ID
  27. * @param UserGradeModel $upgradeGrade 会员等级
  28. * @param array $excludedUserIds 排除的会员ID集
  29. * @return mixed
  30. */
  31. public static function getUpgradeUserList(int $storeId, UserGradeModel $upgradeGrade, $excludedUserIds = [])
  32. {
  33. // 实例化查询对象
  34. $query = (new static)->getNewQuery();
  35. // 检索查询条件
  36. if (!empty($excludedUserIds)) {
  37. $query->where('user.user_id', 'not in', $excludedUserIds);
  38. }
  39. // 查询列表记录
  40. return $query->alias('user')
  41. ->field(['user.user_id', 'user.grade_id'])
  42. ->join('user_grade grade', 'grade.grade_id = user.grade_id', 'LEFT')
  43. ->where(function ($query) use ($upgradeGrade) {
  44. $query->where('user.grade_id', '=', 0);
  45. $query->whereOr('grade.weight', '<', $upgradeGrade['weight']);
  46. })
  47. ->where('user.expend_money', '>=', $upgradeGrade['upgrade']['expend_money'])
  48. ->where('user.store_id', '=', $storeId)
  49. ->where('user.is_delete', '=', 0)
  50. ->select();
  51. }
  52. /**
  53. * 批量设置会员等级
  54. * @param int $storeId 商城ID
  55. * @param array $data 会员等级更新数据
  56. * @return bool
  57. */
  58. public function setBatchGrade(int $storeId, array $data)
  59. {
  60. // 批量更新会员等级的数据
  61. $userData = [];
  62. // 批量新增会员等级变更记录的数据
  63. $logData = [];
  64. foreach ($data as $item) {
  65. $userData[] = [
  66. 'where' => ['user_id' => $item['user_id']],
  67. 'data' => ['grade_id' => $item['new_grade_id']]
  68. ];
  69. $logData[] = [
  70. 'user_id' => $item['user_id'],
  71. 'old_grade_id' => $item['old_grade_id'],
  72. 'new_grade_id' => $item['new_grade_id'],
  73. 'change_type' => ChangeTypeEnum::AUTO_UPGRADE,
  74. 'store_id' => $storeId,
  75. ];
  76. }
  77. // 批量更新会员等级
  78. $this->updateAll($userData);
  79. // 批量新增会员等级变更记录
  80. (new GradeLogModel)->records($logData);
  81. return true;
  82. }
  83. /**
  84. * 获取所有的店内职员列表
  85. * @return array
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. public static function getAllSellers(): array
  91. {
  92. /* where('role','>',User::NORMAL_USER)
  93. ->where('role','<=',User::COMMISSION_USER)
  94. ->*/
  95. $data = self::where('is_delete',0)
  96. ->field('user_id,shop_id,role')
  97. ->select();
  98. return $data?$data->toArray():[];
  99. }
  100. }