基于ssm框架的出道选秀管理系统的功能模块代码观众(普通用户) (能看到艺人表演的视频,能投票,留言功能(能给自己喜欢的明星留言),可以对自己喜的艺人进行打赏,增加选手的人气))代码
时间: 2024-03-24 22:41:43 浏览: 82
一个基于SSM框架的用户投票系统.zip
以下是基于SSM框架的出道选秀管理系统的观众(普通用户)功能模块的Java代码示例:
1. 观众DAO接口及其实现类:
```
public interface AudienceDao {
// 根据观众ID获取观众信息
Audience getAudienceById(int audienceId);
// 观众投票
void vote(int audienceId, int artistId);
// 观众留言
void leaveMessage(int audienceId, int artistId, String message);
// 观众打赏
void reward(int audienceId, int artistId, int amount);
}
@Repository
public class AudienceDaoImpl implements AudienceDao {
@Autowired
private JdbcTemplate jdbcTemplate;
// 根据观众ID获取观众信息
@Override
public Audience getAudienceById(int audienceId) {
String sql = "SELECT * FROM audience WHERE audience_id=?";
return jdbcTemplate.queryForObject(sql, new Object[]{audienceId}, new AudienceRowMapper());
}
// 观众投票
@Override
public void vote(int audienceId, int artistId) {
String sql = "INSERT INTO vote(audience_id, artist_id) VALUES (?,?)";
jdbcTemplate.update(sql, audienceId, artistId);
}
// 观众留言
@Override
public void leaveMessage(int audienceId, int artistId, String message) {
String sql = "INSERT INTO message(audience_id, artist_id, content) VALUES (?,?,?)";
jdbcTemplate.update(sql, audienceId, artistId, message);
}
// 观众打赏
@Override
public void reward(int audienceId, int artistId, int amount) {
String sql = "UPDATE artist SET popularity=popularity+?, income=income+? WHERE artist_id=?";
jdbcTemplate.update(sql, amount, amount, artistId);
sql = "INSERT INTO reward(audience_id, artist_id, amount) VALUES (?,?,?)";
jdbcTemplate.update(sql, audienceId, artistId, amount);
}
}
class AudienceRowMapper implements RowMapper<Audience> {
@Override
public Audience mapRow(ResultSet rs, int rowNum) throws SQLException {
Audience audience = new Audience();
audience.setAudienceId(rs.getInt("audience_id"));
audience.setUsername(rs.getString("username"));
audience.setPassword(rs.getString("password"));
audience.setNickname(rs.getString("nickname"));
//...
return audience;
}
}
```
2. 观众Service接口及其实现类:
```
public interface AudienceService {
// 观众登录
Audience login(String username, String password);
// 观众投票
void vote(int audienceId, int artistId) throws VoteException;
// 观众留言
void leaveMessage(int audienceId, int artistId, String message);
// 观众打赏
void reward(int audienceId, int artistId, int amount) throws RewardException;
}
@Service
public class AudienceServiceImpl implements AudienceService {
@Autowired
private AudienceDao audienceDao;
// 观众登录
@Override
public Audience login(String username, String password) {
Audience audience = audienceDao.getAudienceByUsername(username);
if (audience == null || !audience.getPassword().equals(password)) {
return null;
}
return audience;
}
// 观众投票
@Override
public void vote(int audienceId, int artistId) throws VoteException {
// 判断是否已经投过票
if (hasVoted(audienceId, artistId)) {
throw new VoteException("已经投过票了");
}
// 更新选手的票数
updateVoteCount(artistId);
// 记录投票记录
audienceDao.vote(audienceId, artistId);
}
// 观众留言
@Override
public void leaveMessage(int audienceId, int artistId, String message) {
audienceDao.leaveMessage(audienceId, artistId, message);
}
// 观众打赏
@Override
public void reward(int audienceId, int artistId, int amount) throws RewardException {
// 判断余额是否足够
Audience audience = audienceDao.getAudienceById(audienceId);
if (audience.getBalance() < amount) {
throw new RewardException("余额不足");
}
// 更新选手的人气和收入
audienceDao.reward(audienceId, artistId, amount);
}
// 判断是否已经投过票
private boolean hasVoted(int audienceId, int artistId) {
String sql = "SELECT COUNT(*) FROM vote WHERE audience_id=? AND artist_id=?";
Integer count = audienceDao.queryForObject(sql, new Object[]{audienceId, artistId}, Integer.class);
return count != null && count > 0;
}
// 更新选手的票数
private void updateVoteCount(int artistId) {
String sql = "UPDATE artist SET vote_count=vote_count+1 WHERE artist_id=?";
audienceDao.update(sql, artistId);
}
}
```
3. 观众Controller:
```
@Controller
@RequestMapping("/audience")
public class AudienceController {
@Autowired
private AudienceService audienceService;
// 观众登录
@PostMapping("/login")
@ResponseBody
public Result login(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpSession session) {
Audience audience = audienceService.login(username, password);
if (audience == null) {
return Result.fail("用户名或密码错误");
}
session.setAttribute("audience", audience);
return Result.success();
}
// 观众投票
@PostMapping("/vote")
@ResponseBody
public Result vote(@RequestParam("artistId") int artistId, HttpSession session) {
Audience audience = (Audience) session.getAttribute("audience");
if (audience == null) {
return Result.fail("请先登录");
}
try {
audienceService.vote(audience.getAudienceId(), artistId);
return Result.success();
} catch (VoteException e) {
return Result.fail(e.getMessage());
}
}
// 观众留言
@PostMapping("/leaveMessage")
@ResponseBody
public Result leaveMessage(@RequestParam("artistId") int artistId,
@RequestParam("message") String message,
HttpSession session) {
Audience audience = (Audience) session.getAttribute("audience");
if (audience == null) {
return Result.fail("请先登录");
}
audienceService.leaveMessage(audience.getAudienceId(), artistId, message);
return Result.success();
}
// 观众打赏
@PostMapping("/reward")
@ResponseBody
public Result reward(@RequestParam("artistId") int artistId,
@RequestParam("amount") int amount,
HttpSession session) {
Audience audience = (Audience) session.getAttribute("audience");
if (audience == null) {
return Result.fail("请先登录");
}
try {
audienceService.reward(audience.getAudienceId(), artistId, amount);
return Result.success();
} catch (RewardException e) {
return Result.fail(e.getMessage());
}
}
}
```
阅读全文