package com.hmit.kernes.controller; import com.hmit.kernes.entity.JgSubjectEntity; import com.hmit.kernes.service.JgSubjectService; import com.hmit.kernes.utils.*; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.Date; import java.util.List; import java.util.Map; @Controller @RequestMapping("/jk/jgsubject") public class JgSubjectController extends AbstractController { @Autowired private JgSubjectService jgSubjectService; /** * 列表 */ @ResponseBody @RequestMapping("/list") public R list(@RequestParam Map<String, Object> params,String id){ //查询列表数据 params.put("menuId", id); String title=(String)params.get("title"); if(!StringUtils.isBlank(title)){ params.put("title",stringToUtfByFancy(title)); } String author=(String)params.get("author"); if(!StringUtils.isBlank(author)){ params.put("author",stringToUtfByFancy(author)); } //System.out.println(System.currentTimeMillis()); Query query = new Query(params); List<JgSubjectEntity> userList = jgSubjectService.queryList(query); //System.out.println(System.currentTimeMillis()); int total = jgSubjectService.queryTotal(query); //System.out.println(System.currentTimeMillis()); PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage()); return R.ok().put("page", pageUtil); } @ResponseBody @RequestMapping("/listdir") public R listDir(@RequestParam Map<String, Object> params,String id){ //查询列表数据 params.put("menuId", id); String title=(String)params.get("title"); if(!StringUtils.isBlank(title)){ params.put("title",stringToUtfByFancy(title)); } String author=(String)params.get("author"); if(!StringUtils.isBlank(author)){ params.put("author",stringToUtfByFancy(author)); } //System.out.println(System.currentTimeMillis()); Query query = new Query(params); List<JgSubjectEntity> userList = jgSubjectService.queryListDir(query); //System.out.println(System.currentTimeMillis()); int total = jgSubjectService.queryTotalDir(query); //System.out.println(System.currentTimeMillis()); PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage()); return R.ok().put("page", pageUtil); } public String stringToUtfByFancy(String title){ String titlenew=""; try { if(title.equals(new String(title.getBytes(),"ISO-8859-1"))){ titlenew=new String(title.trim().getBytes("ISO-8859-1"), "UTF-8"); } else{ titlenew=title; } }catch (UnsupportedEncodingException e){ e.printStackTrace(); } return titlenew; } @ResponseBody @RequestMapping("/all") public R AllList(@RequestParam Map<String, Object> params){ //查询所有文章 String title=(String) params.get("title"); String titlenew=""; try { titlenew=new String(title.trim().getBytes("ISO-8859-1"), "UTF-8"); }catch (UnsupportedEncodingException e){ e.printStackTrace(); } params.put("title",titlenew); Query query = new Query(params); List<JgSubjectEntity> userList = jgSubjectService.queryList(query); int total = jgSubjectService.queryTotal(query); PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage()); return R.ok().put("page", pageUtil); } @ResponseBody @RequestMapping("/addclick") public R addClick(@RequestParam String id){ jgSubjectService.addClick(id); return R.ok(); } @ResponseBody @RequestMapping("/articlelist") public R ArticleList(@RequestParam Map<String, Object> params){ String title=(String)params.get("title"); if(!StringUtils.isBlank(title)){ params.put("title",stringToUtfByFancy(title)); } String author=(String)params.get("author"); if(!StringUtils.isBlank(author)){ params.put("author",stringToUtfByFancy(author)); } //查询列表数据,所有允许轮播的 Query query = new Query(params); List<JgSubjectEntity> userList = jgSubjectService.getAllarticleCast(query); //int total = jgSubjectService.queryTotal(query); int total=jgSubjectService.getCountOfAllarticleCast(); PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage()); return R.ok().put("page", pageUtil); } /** * 信息 */ @ResponseBody @RequestMapping("/info/{id}") public R info(@PathVariable("id") Integer id){ JgSubjectEntity jgSubject = jgSubjectService.queryObject(id); return R.ok().put("jgSubject", jgSubject); } /** * 保存 */ @ResponseBody @RequestMapping("/save") public R save(@RequestBody JgSubjectEntity jgSubject){ verifyForm(jgSubject); jgSubject.setRegDate(DateUtils.format(new Date())); if (jgSubject.getMenuId().equals("130")) { jgSubject.setUrl("zhaoping/zhaoping2.html"); }else if (jgSubject.getMenuId().equals("129")) { jgSubject.setUrl("interact/gongshi_jg.html"); } jgSubject.setClickCount(0); jgSubjectService.save(jgSubject); return R.ok(); } /** * 修改 */ @ResponseBody @RequestMapping("/update") public R update(@RequestBody JgSubjectEntity jgSubject){ verifyForm(jgSubject); jgSubjectService.update(jgSubject); return R.ok(); } /** * 删除 */ @ResponseBody @RequestMapping("/delete") public R delete(@RequestBody Integer[] ids){ jgSubjectService.deleteBatch(ids); return R.ok(); } /** * 上传图片 */ @ResponseBody @RequestMapping("/uploadpho") public R uploadpho(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception { String url = request.getSession().getServletContext().getRealPath("doc/"); if (file.isEmpty()) { throw new RRException("上传文件不能为空"); } try{ url = url.replaceAll("/", "\\\\"); String fileName = file.getOriginalFilename(); File file3 = new File(url); if ((!file3.exists()) && (!file3.isDirectory())) { file3.mkdir(); } File file2 = new File(file3, fileName); InputStream in = file.getInputStream(); FileOutputStream fos = new FileOutputStream(file2); BufferedOutputStream bos = new BufferedOutputStream(fos); int b = -1; byte[] buffer = new byte[10485760]; while ((b = in.read(buffer)) != -1) { bos.write(buffer, 0, b); } bos.flush(); bos.close(); fos.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } return R.ok(); } /** * 上传附件 */ @ResponseBody @RequestMapping("/uploadfile") public R uploadfile(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception { String url = request.getSession().getServletContext().getRealPath("file/"); if (file.isEmpty()) { throw new RRException("上传文件不能为空"); } try{ url = url.replaceAll("/", "\\\\"); String fileName = file.getOriginalFilename(); File file3 = new File(url); if ((!file3.exists()) && (!file3.isDirectory())) { file3.mkdir(); } File file2 = new File(file3, fileName); InputStream in = file.getInputStream(); FileOutputStream fos = new FileOutputStream(file2); BufferedOutputStream bos = new BufferedOutputStream(fos); int b = -1; byte[] buffer = new byte[10485760]; while ((b = in.read(buffer)) != -1) { bos.write(buffer, 0, b); } bos.flush(); bos.close(); fos.close(); in.close(); } catch (Exception e) { e.printStackTrace(); } return R.ok(); } private void verifyForm(JgSubjectEntity jgSubjectEntity){ if (StringUtils.isBlank(jgSubjectEntity.getAuthor()) ){ throw new RRException("作者姓名不能为空"); } if(StringUtils.isBlank(jgSubjectEntity.getTitle())){ throw new RRException("标题不能为空"); } if(StringUtils.isBlank(jgSubjectEntity.getContent())){ throw new RRException("文章不能为空"); } if(jgSubjectEntity.getPubDate()==null||jgSubjectEntity.getPubDate()==""||jgSubjectEntity.getPubDate().isEmpty()){ throw new RRException("发布日期不能为空"); } } }