package com.landtool.lanbase.modules.sys.controller; import java.io.File; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.landtool.lanbase.common.utils.*; import com.landtool.lanbase.config.SysTemPropertyConfig; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import com.landtool.lanbase.modules.org.entity.OrgUser; import com.landtool.lanbase.modules.sys.entity.PubNews; import com.landtool.lanbase.modules.sys.service.PubNewsService; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import com.landtool.lanbase.common.annotation.LogAction; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; /** * @author zimao.guo * @Description: TODO(${comments}) * @date 2018-04-17 15:04:58 */ @RestController @RequestMapping("/sys/pubnews") public class PubNewsController extends AbstractController{ @Autowired private PubNewsService pubNewsService; @Autowired private SysTemPropertyConfig sysProps; /** * 列表 */ @LogAction("平台管理,资讯发布,资讯发布查询,查询") @RequestMapping(value ="/list", method ={RequestMethod.POST, RequestMethod.GET}) // @RequiresPermissions("sys:pubnews:list") @RequiresPermissions(value = {"sys:pubnews:list","sys:pubnews:edit"}, logical = Logical.OR) @ApiOperation( value = "资讯发布列表", notes = "" ) public Result list(@ApiParam(name="params",value="",required=true)@RequestParam Map params){ //查询列表数据 Query query = new Query(params); List pubNewsList = pubNewsService.queryList(query); int total = pubNewsService.queryTotal(query); PageUtils pageUtil = new PageUtils(pubNewsList, total, query.getLimit(), query.getPage()); return Result.ok().put("page", pageUtil); } /** * 删除 */ @LogAction("平台管理,资讯发布,资讯发布删除,删除") @PostMapping("/delete") @RequiresPermissions("sys:pubnews:edit") @ApiOperation( value = "删除资讯发布信息", notes = "" ) public Result delete(@ApiParam(name="newsids",value="newsids",required=true)@RequestBody Integer[] newsids){ pubNewsService.deleteBatch(newsids); return Result.ok(); } /** * 信息 */ @GetMapping("/info/{newsid}") @RequiresPermissions("sys:pubnews:edit") @ApiOperation( value = "资讯发布信息", notes = "" ) public Result info(@ApiParam(name="newsid",value="newsid",required=true)@PathVariable("newsid") String newsid){ PubNews info = pubNewsService.queryObject(Integer.parseInt(newsid)); return Result.ok().put("info", info); } /** * 保存 */ @LogAction("平台管理,资讯发布,资讯发布新增,新增") @PostMapping("/save") @RequiresPermissions("sys:pubnews:edit") @ApiOperation( value = "保存资讯发布信息", notes = "" ) public Result save(@ApiParam(name="资讯发布对象",value="传入json格式",required=true)@RequestBody PubNews pubnews){ if(pubnews.getTitle() == null || pubnews.getTitle().length() == 0){ return Result.error("保存失败,存在特殊字符,请重新输入!"); } OrgUser orgUser = (OrgUser) SecurityUtils.getSubject().getPrincipal(); Timestamp time = new Timestamp(new Date().getTime()); pubnews.setCreateuser(Integer.parseInt(orgUser.getUserid().toString())); pubnews.setCreatedate(time); pubnews.setLasteditdate(time); pubNewsService.save(pubnews); //保存完后获取对应newsid int newsid = pubNewsService.queryPubNewsWithSEQ(); return Result.ok().put("newsid",newsid); } /** * 修改 */ @LogAction("平台管理,资讯发布,资讯发布修改,修改") @PostMapping("/update") @RequiresPermissions("sys:pubnews:edit") @ApiOperation( value = "修改资讯发布信息", notes = "" ) public Result update(@ApiParam(name="资讯发布对象",value="传入json格式",required=true)@RequestBody PubNews pubnews){ if(pubnews.getTitle() == null || pubnews.getTitle().length() == 0){ return Result.error("保存失败,存在特殊字符,请重新输入!"); } Timestamp time = new Timestamp(new Date().getTime()); pubnews.setLasteditdate(time); pubNewsService.update(pubnews); return Result.ok(); } @RequestMapping("/uploadueditorfile") public ResponseEntity> uploadueditorfile( @RequestParam(value = "upfile", required = false) MultipartFile file, HttpServletRequest request) {//value的名字一定要叫upfile,这是ueditor写好的参数名称 String fileName = file.getOriginalFilename(); String[] types = fileName.split("\\."); String type = types[types.length - 1]; String root = sysProps.getUploadPath().replace("/","\\"); String LinShiUrl=root+"\\ueditor"; String suffix= FileUtils.getSuffix(file.getOriginalFilename()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); String timePath = sdf.format(new Date().getTime()); timePath = timePath.replace("-", ""); String newFileName= AttachmentUtils.newFileName(suffix,LinShiUrl,timePath); File newFile=new File(newFileName); //临时文件处理 try { file.transferTo(newFile); }catch (Exception e){ e.printStackTrace(); } Map result = new HashMap(); result.put("state", "SUCCESS");// UEDITOR的规则:不为SUCCESS则显示state的内容 result.put("url",sysProps.getUploadUeditorPath() + FileUtils.removePrefix(newFile.getAbsolutePath(),root).replace("\\", "/")); result.put("title", file.getOriginalFilename()); result.put("original", file.getOriginalFilename()); result.put("size", file.getSize()); result.put("type", type); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); return new ResponseEntity>(result,headers, HttpStatus.OK); } }