123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace app\models\vote;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\Collection;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class Vote extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'vote';
- use ModelTrait;
- /**
- * @return Vote
- */
- public static function ing()
- {
- return self::where('status', 1);
- }
- /**
- * @param $id
- * @param int $uid
- * @return Collection
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function votingSub($id, $uid = 0, $status = '')
- {
- $model = VoteSub::ing()->where('vote_id', $id);
- if ($status == 1) $model = VoteSub::ing(false)->where('vote_id', $id)->where('start_time', '>', time());
- if ($status == 2) $model = VoteSub::where('vote_id', $id)->where('status', 'in', [2, 4]);
- $data = $model->select()->each(function ($item) use ($uid) {
- $item['now_voted'] = UserVote::where('sub_vote_id', $item['id'])->sum('vote_num');
- if ($uid) {
- $item['user_now_voted'] = UserVote::where('uid', $uid)->where('sub_vote_id', $item['id'])->sum('vote_num');
- }
- $item['commission_type'] = sys_config('vote_commission_type', '');
- $item['commission_ratio'] = sys_config('vote_commission_ratio', 0);
- });
- return $data;
- }
- /**
- * @param $where
- * @return array
- */
- public static function getList($where)
- {
- $model = new self();
- if (isset($where['title']) && $where['title'] != '') {
- $model->where('id|vote_name', 'like', "%{$where['title']}%");
- }
- if (isset($where['status']) && $where['status'] != '') {
- $model->where('status', $where['status']);
- }
- if (isset($where['money_type']) && $where['money_type'] != '') {
- $model->where('money_type', $where['money_type']);
- }
- $count = $model->count();
- $data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
- switch ($item['status']) {
- case 1:
- $item['_status'] = '进行中';
- break;
- case 2:
- $item['_status'] = '已结束';
- break;
- case 0:
- $item['_status'] = '未开始';
- break;
- default:
- $item['_status'] = '未知';
- break;
- }
- $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
- });
- return compact('count', 'data');
- }
- //获取某用户的详细信息
- public static function getInfo($id)
- {
- $Info = self::get($id);
- return [
- ['name' => '投票名', 'value' => $Info['vote_name']],
- ['name' => '投票目标', 'value' => $Info['money_type']],
- ['name' => '投票总额', 'value' => UserVote::where('vote_id', $id)->sum('vote_num')],
- ];
- }
- }
|