UserGrade.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\service;
  13. use app\common\library\helper;
  14. use app\console\library\Tools;
  15. use app\common\service\BaseService;
  16. use app\console\model\User as UserModel;
  17. use app\console\model\user\Grade as UserGradeModel;
  18. /**
  19. * 服务类:会员等级
  20. * Class UserGrade
  21. * @package app\console\service
  22. */
  23. class UserGrade extends BaseService
  24. {
  25. /**
  26. * 设置用户的会员等级
  27. * @param int $storeId
  28. * @return array|bool
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function setUserGrade(int $storeId)
  34. {
  35. // 获取所有会员等级
  36. $list = $this->getUsableList($storeId);
  37. // 遍历等级,根据升级条件 查询满足消费金额的用户列表,并且他的等级小于该等级
  38. $data = [];
  39. foreach ($list as $grade) {
  40. // 查询满足会员等级升级条件的用户列表
  41. $userList = UserModel::getUpgradeUserList($storeId, $grade, array_keys($data));
  42. // 遍历整理数据
  43. foreach ($userList as $user) {
  44. if (!isset($data[$user['user_id']])) {
  45. $data[$user['user_id']] = [
  46. 'user_id' => $user['user_id'],
  47. 'old_grade_id' => $user['grade_id'],
  48. 'new_grade_id' => $grade['grade_id'],
  49. ];
  50. }
  51. }
  52. }
  53. // 记录日志
  54. Tools::taskLogs('UserGrade', 'setUserGrade', [
  55. 'storeId' => $storeId,
  56. 'data' => $data
  57. ]);
  58. // 批量修改会员的等级
  59. return (new UserModel)->setBatchGrade($storeId, $data);
  60. }
  61. /**
  62. * 获取所有会员等级
  63. * @param int $storeId 商城ID
  64. * @return false|\think\Collection
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. private function getUsableList(int $storeId)
  70. {
  71. return UserGradeModel::getUsableList($storeId);
  72. }
  73. }