SystemAwardLevel.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\models\system;
  3. use app\admin\model\system\SystemUserLevel;
  4. use app\models\store\StoreCart;
  5. use app\models\store\StoreOrder;
  6. use app\models\user\User;
  7. use app\models\user\UserBill;
  8. use crmeb\basic\BaseModel;
  9. use crmeb\traits\ModelTrait;
  10. /**
  11. * TODO 会员等级Model
  12. * Class UserLevel
  13. * @package app\models\user
  14. */
  15. class SystemAwardLevel extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'system_award_level';
  27. use ModelTrait;
  28. /**
  29. * 获取查询条件
  30. * @param $where
  31. * @param string $alert
  32. * @param null $model
  33. * @return SystemUserLevel|null
  34. */
  35. public static function setWhere($where, $alert = '', $model = null)
  36. {
  37. $model = $model === null ? new self() : $model;
  38. if ($alert) $model = $model->alias($alert);
  39. $alert = $alert ? $alert . '.' : '';
  40. $model = $model->where("{$alert}is_del", 0);
  41. if (isset($where['is_show']) && $where['is_show'] !== '') $model = $model->where("{$alert}is_show", $where['is_show']);
  42. if (isset($where['title']) && $where['title']) $model = $model->where("{$alert}name", 'LIKE', "%$where[title]%");
  43. return $model;
  44. }
  45. public static function getSystemList($where)
  46. {
  47. $data = self::setWhere($where)->order('id asc')->page((int)$where['page'], (int)$where['limit'])->select();
  48. $data = count($data) ? $data->toArray() : [];
  49. $count = self::setWhere($where)->count();
  50. return compact('data', 'count');
  51. }
  52. public static function checkLevel($uid)
  53. {
  54. $spread = User::where('uid', $uid)->find();
  55. // $spread = User::where('uid', $user['spread_uid'])->find();
  56. while ($spread) {
  57. //推荐全员
  58. // $uids = User::where('spread_uid', $spread['uid'])->column('uid');
  59. // $orders = StoreOrder::where('paid', 1)
  60. // ->where('status', 'in', [2, 3])
  61. // ->where('uid', 'in', $uids)
  62. // ->where('refund_status', 0)
  63. // ->where('is_del', 0)
  64. // ->where('is_system_del', 0)->select();
  65. // $num = 0;
  66. // foreach ($orders as $v) {
  67. // $num += StoreCart::where('id', 'in', $v['cart_id'])->where('is_suit', 1)->sum('cart_num');
  68. // }
  69. $level_info = self::where('id', $spread['award_level'])->find();
  70. $level = self::where('level_num', '>', $level_info['level_num'] ?? 0)->order('level_num', 'desc')->select();
  71. foreach ($level as $v) {
  72. if ($spread['recommend_suit_num'] >= $v['level_num']) {
  73. User::where('uid', $spread['uid'])->update(['award_level' => $v['id']]);
  74. self::sendBackOrderBrokerage($spread['uid'], $spread['group_level'] > $v['id'] ? $v['id'] : $spread['group_level']);
  75. break;
  76. }
  77. }
  78. $spread = User::where('uid', $spread['spread_uid'])->find();
  79. }
  80. return true;
  81. }
  82. public static function sendBackOrderBrokerage($uid, $level)
  83. {
  84. $list = UserBill::where('award_level', '<=', $level)->where(['type' => 'brokerage', 'category' => 'now_money', 'pm' => 1, 'status' => 0, 'uid' => $uid, 'title' => '津贴绿积分|极差奖'])->where('title','in',['津贴绿积分','极差奖'])->select();
  85. if ($list) {
  86. self::beginTrans();
  87. try {
  88. foreach ($list as $v) {
  89. User::bcInc($v['uid'], 'brokerage_price', $v['number'], 'uid');
  90. $balance = User::getUserInfo($v['uid'])['brokerage_price'];
  91. UserBill::where('id', $v['id'])->update(['add_time' => date('Y-m-d H:i:s'), 'status' => 1, 'balance' => $balance]);
  92. }
  93. self::commitTrans();
  94. return true;
  95. } catch (\Exception $e) {
  96. self::rollbackTrans();
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. }