Vote.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\models\vote;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\traits\ModelTrait;
  5. use think\Collection;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. class Vote extends BaseModel
  10. {
  11. /**
  12. * 数据表主键
  13. * @var string
  14. */
  15. protected $pk = 'id';
  16. /**
  17. * 模型名称
  18. * @var string
  19. */
  20. protected $name = 'vote';
  21. use ModelTrait;
  22. /**
  23. * @return Vote
  24. */
  25. public static function ing()
  26. {
  27. return self::where('status', 1);
  28. }
  29. /**
  30. * @param $id
  31. * @param int $uid
  32. * @return Collection
  33. * @throws DataNotFoundException
  34. * @throws DbException
  35. * @throws ModelNotFoundException
  36. */
  37. public static function votingSub($id, $uid = 0, $status = '')
  38. {
  39. $model = VoteSub::ing()->where('vote_id', $id);
  40. if ($status == 1) $model = VoteSub::ing(false)->where('vote_id', $id)->where('start_time', '>', time());
  41. if ($status == 2) $model = VoteSub::where('vote_id', $id)->where('status', 'in', [2, 4]);
  42. $data = $model->select()->each(function ($item) use ($uid) {
  43. $item['now_voted'] = UserVote::where('sub_vote_id', $item['id'])->sum('vote_num');
  44. if ($uid) {
  45. $item['user_now_voted'] = UserVote::where('uid', $uid)->where('sub_vote_id', $item['id'])->sum('vote_num');
  46. }
  47. $item['commission_type'] = sys_config('vote_commission_type', '');
  48. $item['commission_ratio'] = sys_config('vote_commission_ratio', 0);
  49. });
  50. return $data;
  51. }
  52. /**
  53. * @param $where
  54. * @return array
  55. */
  56. public static function getList($where)
  57. {
  58. $model = new self();
  59. if (isset($where['title']) && $where['title'] != '') {
  60. $model->where('id|vote_name', 'like', "%{$where['title']}%");
  61. }
  62. if (isset($where['status']) && $where['status'] != '') {
  63. $model->where('status', $where['status']);
  64. }
  65. if (isset($where['money_type']) && $where['money_type'] != '') {
  66. $model->where('money_type', $where['money_type']);
  67. }
  68. $count = $model->count();
  69. $data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
  70. switch ($item['status']) {
  71. case 1:
  72. $item['_status'] = '进行中';
  73. break;
  74. case 2:
  75. $item['_status'] = '已结束';
  76. break;
  77. case 0:
  78. $item['_status'] = '未开始';
  79. break;
  80. default:
  81. $item['_status'] = '未知';
  82. break;
  83. }
  84. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  85. });
  86. return compact('count', 'data');
  87. }
  88. //获取某用户的详细信息
  89. public static function getInfo($id)
  90. {
  91. $Info = self::get($id);
  92. return [
  93. ['name' => '投票名', 'value' => $Info['vote_name']],
  94. ['name' => '投票目标', 'value' => $Info['money_type']],
  95. ['name' => '投票总额', 'value' => UserVote::where('vote_id', $id)->sum('vote_num')],
  96. ];
  97. }
  98. }