package com.edu.servlet;
import com.edu.bean.SongBean;
import com.edu.bean.SongtypeBean;
import com.edu.bean.UserBean;
import com.edu.bean.VipBean;
import com.edu.service.*;
import com.edu.service.impl.*;
import com.edu.util.R;
import com.google.gson.Gson;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.DecimalFormat;
import java.util.List;
/**
* @Author: 王仁洪
* @Date: 2019/1/16 11:07
*/
public class SongUtilServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private SongService songService = new SongServiceImpl();
private SongTypeService songTypeService = new SongTypeServiceImpl();
private ClicksService clicksService = new ClicksServiceIpml();
private UserService userService = new UserServiceImpl();
private DownloadService downloadService = new DownloadServiceImpl();
private VipService vipService = new VipServiceImpl();
private Gson gson = new Gson();
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String state = request.getParameter("state");
System.out.println(state);
if ("querySongType".equals(state)){
this.querySongType(request,response);
}else if ("querySongs".equals(state)){
this.querySongs(request,response);
}else if ("querySongByTypeId".equals(state)){
this.querySongByTypeId(request,response);
}else if("hotSearch".equals(state)){
this.hotSearch(request,response);
}else if ("hotDownload".equals(state)){
this.hotDownload(request,response);
}else if ("click".equals(state)){
this.click(request,response);
}else if ("query".equals(state)){
this.query(request,response);
}else if ("listen".equals(state)){
this.listen(request,response);
}else if ("download".equals(state)){
this.download(request,response);
}else if ("purchase".equals(state)){
this.purchase(request,response);
}else if ("selectVip".equals(state)){
this.selectVip(request,response);
}else if ("recharge".equals(state)){
this.recharge(request,response);
}
}
private void recharge(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setCharacterEncoding("utf-8");
PrintWriter printWriter = response.getWriter();
Integer user_id = Integer.parseInt(request.getParameter("user_id"));
Integer vip_id = Integer.parseInt(request.getParameter("vip_id"));
Boolean flag = userService.recharge(user_id, vip_id);
R r = R.modify(flag);
String json = gson.toJson(r);
printWriter.print(json);
}
private void selectVip(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer userId = Integer.parseInt(request.getParameter("userId"));
List<UserBean> userBeans = userService.selectById(userId);
Integer vip_id = userBeans.get(0).getVip_id();
List<VipBean> vipBeans = vipService.selectUpVip(vip_id);
Double rate = 0.8;
for (int i=0;i<vipBeans.size();i++){
VipBean vipBean = vipBeans.get(i);
//计算月数
Integer monthNum = (i+1)*6;
//得到vip类型
String vip = vipBean.getVip();
//计算金额
Double money = monthNum * 8 * rate;
//将金额格式化为小数点后保留两位数字
DecimalFormat decimalFormat = new DecimalFormat("#.##");
String formatMoney = decimalFormat.format(money);
vipBean.setVip(vip + monthNum + "个月共计" + formatMoney);
// vipBeans.get(i).setVip(vipBeans.get(i).getVip() + ((i+1)*6) + "个月共计"
// + ((i+1)*6)*8*rate);
}
request.setAttribute("userId",userId);
request.setAttribute("vipBeans",vipBeans);
request.getRequestDispatcher("/page/user/recharge_jsp").forward(request,response);
}
private void downLoad(List<SongBean> songBeans,HttpServletResponse response) throws UnsupportedEncodingException {
//获取所要下载的文件名称
String song_name = songBeans.get(0).getSong_name();
String song_format = songBeans.get(0).getSong_format();
//下载文件所在目录
String song_url = songBeans.get(0).getSong_url();
String fileName = new String((song_name + "." + song_format).getBytes("utf-8"),"ISO8859-1");
// System.out.println(fileName);
try {
// path是指欲下载的文件的路径。
File file = new File(song_url);
// 以流的形式下载文件。
InputStream inputStream = new BufferedInputStream(new FileInputStream(song_url));
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Content-Length", "" + file.length());
OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
outputStream.write(buffer);
outputStream.flush();
outputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private void purchase(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
Integer user_id = Integer.parseInt(request.getParameter("user_id"));
Integer song_id = Integer.parseInt(request.getParameter("song_id"));
downloadService.download(user_id,song_id);
List<SongBean> songBeans = songService.selectById(song_id);
this.downLoad(songBeans,response);
}
private void download(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setCharacterEncoding("utf-8");
//设置ContentType字段值
response.setContentType("text/html;charset=utf-8");
Integer song_id = Integer.parseInt(request.getParameter("song_id"));
List<SongBean> songBeans = songService.selectById(song_id);
Integer songVipId = songBeans.get(0).getVip_id();
HttpSession session = request.getSession();
Object loginId = session.getAttribute("userLoginId");
if (null==loginId){
request.getRequestDispatcher("/page/user/user_tips_jsp").forward(request,response);
}else {
List<UserBean> userBeans = userService.selectById((Integer) loginId);
Integer userVipId = userBeans.get(0).getVip_id();
if (userVipId >= songVipId){
//在下载(download)表插入一条下载记录,
//将歌曲(song)表中与song_id对应的记录的下载次数加一
downloadService.download((Integer)loginId,song_id);
this.downLoad(songBeans,response);
}else {
String purchaseInfo = "您需支付¥" + (songVipId - 1)*2 + "元以购买该歌曲!";
request.setAttribute("user_id",(Integer)loginId);
request.setAttribute("song_id",song_id);
request.setAttribute("purchaseInfo",purchaseInfo);
request.getRequestDispatcher("/page/user/purchase_jsp").forward(request,response);
}
}
}
pr
妄北y
- 粉丝: 2w+
- 资源: 1万+
最新资源
- 使用深度学习预测低压网络的电压分布 低电压(LV)电路的能量分布将发生变化,而以前的被动适应和-“忘记”的网络管理方法将是低效的,以确保其有效运行 需要一种自适应的方法,包括对电路风险的预测
- 小型六轴机器人样机,可二次开发,脉冲加方向,仪嘉控制器,
- 基于扩展卡尔曼滤波EKF和模型预测控制MPC,自动泊车场景建模开发,文复现 MATLAB(工程项目线上支持)
- (工程项目线上支持)横向控制,稳定性控制,直接横摆力矩控制 汽车电子稳定性控制建模仿真 包括模型和说明书 DYC, ESP
- 低成本dsp变频器方案,有C语言源码,编译码,PCB图纸,物料清单
- 三维重建程序~基于多视图几何的三维重建 代码特点: 1.使用osmbundler进行特征点提取与匹配; 2.使用cmvs进行稀疏点云的生成; 3.使用pmvs进行稠密点云的生成; 4.用户界面使用py
- 电动汽车动力系统整车仿真,具有双向DCDC变器实现能量反馈,可带异步电机,永磁同步电机仿真,应用最大转矩电流比控制加独特的弱磁控制策略
- 基于OpenCV的口罩识别系统 相关技术:python,opencv,pyqt (请自行安装向日葵远程软件,以便提供远程帮助) 软件说明:读取用户设备的摄像头,可实时检测画面中的人的口罩佩戴情况,并给
- matlab程序 研究方向:综合能源系统,微电网,主从博弈,合作,非合作博弈相关方向,多时间尺度
- 研究考虑综合需求响应和碳交易机制的冷、热、电、气4种能源形式的综合能源系统,系统内含能源设备主要包括光伏电源、风力机组、燃气轮机和燃气锅炉;储能系统主要包括储电设备蓄电池、储热设备蓄热槽;能量转设备包
- 研究考虑综合需求响应和碳交易机制的冷、热、电、气4种能源形式的综合能源系统,系统内含能源设备主要包括光伏电源、风力机组、燃气轮机和燃气锅炉;储能系统主要包括储电设备蓄电池、储热设备蓄热槽;能量转设备包
- labview自动化贴合设备程序 labview自动化贴合设备程序,包括上下CCD标定算法,9点标定,整机贴合精度0.1mm,边缘抓取全套源代码,标准架构,快速上手,其他项目快速适用
- system_ftnn_gbid.mp3
- stm32真实企业项目1,源码原理图PCB图,感谢已经拿后的多位客户,你们愿意为学习投资,节省时间 包含源码,原理图设计和pcb 项目要求与网上搜的那些开发板的例程完全不在一个级别,也不是那些凑合性质
- 提供基于comsol中相场方法模拟多孔介质两相驱替(水气、油水等等)的算例(也可以定做水平集驱替的算例),可在此基础上学会利用comsol软件进行两相流驱替的模拟,拓展研究,具体参考算例附后 附赠基
- C#,C#程序实例,C#与西门子,C#与西门子PLC通讯,C#与S7-200SMART以太网通讯,C#与西门子PLC通讯,原生Socket通讯,异步通讯,多线程操作,连接PLC数量没有限制,占用内存少
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈