package cn.controller;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import cn.pojo.Category;
import cn.pojo.Entry;
import cn.service.EbookService;
import cn.tools.PageSupport;
@Controller
public class EbookController extends BaseController{
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(EbookController.class);
@Resource
private EbookService ebookService;
//查询数据,支持模糊查询,分页功能
@RequestMapping(value = "/list")
public String getBillList(
Model model,HttpSession session,
@RequestParam(value = "category", required = false) String category,
@RequestParam(value = "pageIndex", required = false) String pageIndex) {
List<Entry> entryList = null;
int categoryId =0;
if (category != null) {
categoryId = Integer.valueOf(category);
}
// 设置页面容量
int pageSize = 3;
// 当前页码
int currentPageNo = 1;
// 设置页码数
if (pageIndex != null) {
currentPageNo = Integer.valueOf(pageIndex);
}
// 总数量(表)
int totalCount = 0;
try {
totalCount = ebookService.getEntryCount();
} catch (Exception e) {
e.printStackTrace();
}
// 总页数
PageSupport pages = new PageSupport();
pages.setCurrentPageNo(currentPageNo);
pages.setPageSize(pageSize);
pages.setTotalCount(totalCount);
int totalPageCount = pages.getTotalPageCount();
// 控制首页和尾页
if (currentPageNo < 1) {
currentPageNo = 1;
} else if (currentPageNo > totalPageCount) {
currentPageNo = totalPageCount;
}
try {
if (categoryId !=1 && categoryId !=2 && categoryId !=3&& categoryId !=4) {
categoryId =0;
}
entryList = ebookService.getEntryList(categoryId, currentPageNo, pageSize);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("entryList",entryList);
model.addAttribute("totalPageCount", totalPageCount);
model.addAttribute("totalCount", totalCount);
model.addAttribute("currentPageNo", currentPageNo);
model.addAttribute("pages", pages);
return "projectinfosList";
}
@RequestMapping(value = "/del", method = RequestMethod.GET)
@ResponseBody
public Object deluser(@RequestParam String id) throws NumberFormatException, Exception {
int count = ebookService.delete(Integer.parseInt(id));
return JSONArray.toJSONString(count);
}
@RequestMapping(value = "/add")
public String addBill() {
return "add";
}
@RequestMapping(value = "/addsave", method = RequestMethod.POST)
public String addPro(HttpSession session,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "summary", required = false) String summary,
@RequestParam(value = "uploaduser", required = false) String uploaduser,
@RequestParam(value = "categoryId", required = false) String categoryId,
@RequestParam(value = "createdate", required = false) String createdate) throws Exception{
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
try {
Entry projectinfo=new Entry();
projectinfo.setTitle(title);
projectinfo.setSummary(summary);
projectinfo.setUploaduser(uploaduser);
projectinfo.setCreatedate(format1.parse(createdate));
projectinfo.setCategoryId(Integer.parseInt(categoryId));
int num=ebookService.add(projectinfo);
if(num>0){
return "redirect:/list";
}else{
return "add";
}
} catch (ParseException e) {
e.printStackTrace();
}
return "redirect:/list";
}
/**
* 按照id查询信息并跳转到修改页面
* @param id
* @param model
* @param request
* @return
* @throws Exception
* @throws NumberFormatException
*/
@RequestMapping(value = "/upd/{id}", method = RequestMethod.GET)
public String getProById(@PathVariable String id, Model model,
HttpServletRequest request) throws NumberFormatException, Exception {
Entry entry=new Entry();
entry=ebookService.getEntryById(Integer.valueOf(id));
model.addAttribute("entry",entry);
return "updBook";
}
/**
* 修改
* @param id
* @param statu
* @param session
* @return
*/
@RequestMapping(value ="/updsave", method = RequestMethod.POST)
public String modifySave(
@RequestParam(value = "id", required = false) String id,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "summary", required = false) String summary,
@RequestParam(value = "uploaduser", required = false) String uploaduser,
@RequestParam(value = "userRole", required = false) String userRole,
@RequestParam(value = "createdate", required = false) String createdate,
HttpSession session) {
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
try {
Entry projectinfo=new Entry();
projectinfo.setId(Integer.valueOf(id));
projectinfo.setTitle(title);
projectinfo.setSummary(summary);
projectinfo.setUploaduser(uploaduser);
projectinfo.setCreatedate(format1.parse(createdate));
projectinfo.setCategoryId(Integer.parseInt(userRole));
if (ebookService.modify(projectinfo)>0) {
session.setAttribute("tishi", "修改成功!");
return "redirect:/list";
}
} catch (Exception e) {
e.printStackTrace();
}
session.setAttribute("tishi", "修改失败!");
return "redirect:/list";
}
/**
* 异步获取图书分类
* @return
*/
@RequestMapping(value="/rolelist.json",method=RequestMethod.GET)
@ResponseBody
public List<Category> getCategoryList(){
List<Category>categoryList = null;
try {
categoryList = ebookService.getCategory();
} catch (Exception e) {
e.printStackTrace();
}
logger.info("categoryList 数量:"+categoryList.size());
return categoryList;
}
}
Rokie-770880
- 粉丝: 9
- 资源: 15
最新资源
- anaconda和pycharm2020版本安装包
- 316L超低碳不锈钢埋弧焊焊接工艺探讨.pdf
- 316L不锈钢扩散焊接头高温蠕变性能.pdf
- 316L壳体的焊接.pdf
- 316N不锈钢管的焊接工艺探讨 - .pdf
- k8s网络通信组件cni插件cni-plugins-linux-arm64-v0.8.7.tgz
- 600MW超临界机组SA335-P91钢免根透焊接工艺控制 - .pdf
- 600MW机组管道焊接、热处理及无损检验施工.pdf
- 620℃超超临界机组用9% Cr-Mo-Co-B钢(CB2)焊接接头性能退化机制研究-学兔兔 www.xuetutu.com.pdf
- 700 MPa级低合金高强钢低匹配焊接接头组织和性能研究.pdf
- 800MPa及以上低合金高强钢焊接现状 - .pdf
- 904L材料的焊接 - .pdf
- 1000m^3LPG卧式储罐用07MnNiCrMoVDR钢的焊接与热处理.pdf
- 980MPa级高强钢焊接性的研究现状 - .pdf
- 2000 kN·m大型行星减速器焊接机体应力分析.pdf
- 机械设计奶酪生产包装线stp非常好的设计图纸100%好用.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈