package com.zy.crm.system.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.baomidou.mybatisplus.plugins.Page; import com.core.annotations.ManagerAuth; import com.core.common.BaseRes; import com.core.common.Cools; import com.core.common.DateUtils; import com.core.common.R; import com.zy.crm.common.service.OssService; import com.zy.crm.common.utils.FileSaveExampleUtil; import com.zy.crm.common.web.BaseController; import com.zy.crm.manager.entity.Plan; import com.zy.crm.manager.entity.PlanType; import com.zy.crm.manager.entity.PlanUrl; import com.zy.crm.manager.entity.PriQuote; import com.zy.crm.system.entity.AppVersion; import com.zy.crm.system.entity.User; import com.zy.crm.system.service.AppVersionService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.util.ClassUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.text.SimpleDateFormat; import java.util.*; @RestController public class AppVersionController extends BaseController { @Autowired private AppVersionService appVersionService; @Autowired private OssService ossService; @RequestMapping(value = "/appVersion/{id}/auth") @ManagerAuth public R get(@PathVariable("id") String id) { return R.ok(appVersionService.selectById(String.valueOf(id))); } @RequestMapping(value = "/appVersion/list/auth") @ManagerAuth public R list(@RequestParam(defaultValue = "1") Integer curr, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = false) String orderByField, @RequestParam(required = false) String orderByType, @RequestParam(required = false) String condition, @RequestParam Map param){ EntityWrapper wrapper = new EntityWrapper<>(); excludeTrash(param); convert(param, wrapper); allLike(AppVersion.class, param.keySet(), wrapper, condition); // if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));} wrapper.orderBy("id", false); return R.ok(appVersionService.selectPage(new Page<>(curr, limit), wrapper)); } @RequestMapping(value = "/appVersion/checkUpdate/{version}/{type}") public R checkUpdate(@PathVariable("version") String version, @PathVariable("type") Integer type) { AppVersion latestApp = appVersionService.getLatestApp(type); if (latestApp == null) { return R.error(); } if (latestApp.getVersion().equals(version)) { return R.error("已是最新版本"); } String download = ossService.download(latestApp.getPath()); latestApp.setUrl(download); return R.ok("有新版本,需要更新").add(latestApp); } private void convert(Map map, EntityWrapper wrapper){ for (Map.Entry entry : map.entrySet()){ String val = String.valueOf(entry.getValue()); if (val.contains(RANGE_TIME_LINK)){ String[] dates = val.split(RANGE_TIME_LINK); wrapper.ge(entry.getKey(), DateUtils.convert(dates[0])); wrapper.le(entry.getKey(), DateUtils.convert(dates[1])); } else { wrapper.like(entry.getKey(), val); } } } @RequestMapping(value = "/appVersion/add/auth") @ManagerAuth public R add(AppVersion appVersion) { if (appVersion.getLatest() == 1) { //修改最新版本时,需要将其他版本设置为非最新版 appVersionService.updateLatest(0, appVersion.getType()); } appVersionService.insert(appVersion); return R.ok(); } @RequestMapping(value = "/appVersion/update/auth") @ManagerAuth public R update(AppVersion appVersion){ if (Cools.isEmpty(appVersion) || null==appVersion.getId()){ return R.error(); } if (appVersion.getLatest() == 1) { //修改最新版本时,需要将其他版本设置为非最新版 appVersionService.updateLatest(0, appVersion.getType()); } appVersionService.updateById(appVersion); return R.ok(); } @RequestMapping(value = "/appVersion/delete/auth") @ManagerAuth public R delete(@RequestParam(value="ids[]") Long[] ids){ for (Long id : ids){ appVersionService.deleteById(id); } return R.ok(); } @RequestMapping(value = "/appVersion/export/auth") @ManagerAuth public R export(@RequestBody JSONObject param){ EntityWrapper wrapper = new EntityWrapper<>(); List fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); Map map = excludeTrash(param.getJSONObject("appVersion")); convert(map, wrapper); List list = appVersionService.selectList(wrapper); return R.ok(exportSupport(list, fields)); } @RequestMapping(value = "/appVersionQuery/auth") @ManagerAuth public R query(String condition) { EntityWrapper wrapper = new EntityWrapper<>(); wrapper.like("id", condition); Page page = appVersionService.selectPage(new Page<>(0, 10), wrapper); List> result = new ArrayList<>(); for (AppVersion appVersion : page.getRecords()){ Map map = new HashMap<>(); map.put("id", appVersion.getId()); map.put("value", appVersion.getId()); result.add(map); } return R.ok(result); } @RequestMapping(value = "/appVersion/check/column/auth") @ManagerAuth public R query(@RequestBody JSONObject param) { Wrapper wrapper = new EntityWrapper().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val")); if (null != appVersionService.selectOne(wrapper)){ return R.parse(BaseRes.REPEAT).add(getComment(AppVersion.class, String.valueOf(param.get("key")))); } return R.ok(); } @RequestMapping(value = "/appVersion/uploadApp/auth") @ManagerAuth public R uploadApp(@RequestParam("id") Integer id, @RequestParam("file") MultipartFile[] files){ AppVersion appVersion = appVersionService.selectById(id); MultipartFile file = files[0]; SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss"); String path = ClassUtils.getDefaultClassLoader().getResource("appVersion").getPath(); //文件后缀名 String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); //上传文件名 String filename = format.format(new Date()) + suffix; //最终文件路径 String filepath = path + "/" + filename; //服务器端保存的文件对象 File serverFile = new File(filepath); if(!serverFile.exists()) { try { //创建文件 serverFile.createNewFile(); //将上传的文件写入到服务器端文件内 file.transferTo(serverFile); } catch (IOException e) { e.printStackTrace(); } } //保存文件名 appVersion.setPath(filename); appVersionService.updateById(appVersion); return R.ok(); } @RequestMapping("/appVersion/downloadApp/{filename}") public void download(@PathVariable String filename, HttpServletResponse response) { try { ClassPathResource pathResource = new ClassPathResource("appVersion/" + filename); File file = pathResource.getFile(); InputStream inputStream = pathResource.getInputStream(); //输出文件 InputStream fis = new BufferedInputStream(inputStream); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); //获取文件的名字再浏览器下载页面 String name = file.getName(); response.addHeader("Content-Disposition", "attachment;filename=" + new String(name.getBytes(), "iso-8859-1")); response.addHeader("Content-Length", "" + file.length()); OutputStream out = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); out.write(buffer); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); response.setStatus(404); } } @RequestMapping(value = "/appVersion/downloadApp/url/view/file/auth") @ManagerAuth public R viewFile(@RequestParam("appId") String appId){ List fileDTOS = new ArrayList<>(); if (Cools.isEmpty(appId)){ return R.ok(fileDTOS); } Long id = Long.parseLong(appId); AppVersion appVersion = appVersionService.selectById(id); if (Cools.isEmpty(appVersion.getPath())){ return R.ok(fileDTOS); } try{ FileSaveExampleUtil.FileDTO fileDTO = new FileSaveExampleUtil.FileDTO(appVersion.getPath(),appVersion.getFileSize(),appVersion.getUrl()); fileDTO.setUserName(appVersion.getUserName()); fileDTOS.add(fileDTO); return R.ok(fileDTOS); }catch (Exception e){ return R.error(); } } @RequestMapping(value = "/appVersion/downloadApp/url/insert/file/auth") @ManagerAuth public R insertFilePriQuote(@RequestParam("appId") String appId, @RequestParam("filename") String filename, @RequestParam("filesize") Long filesize) { try { String[] split = appId.split("-"); Long id = Long.parseLong(split[1]); AppVersion appVersion = appVersionService.selectById(id); appVersion.setPath(filename); appVersion.setFileSize(filesize); appVersion.setUserName(getUser().getNickname()); String url = "http://zhongyang-ftpserver.oss-cn-hangzhou.aliyuncs.com/" + filename; appVersion.setUrl(url); appVersionService.updateById(appVersion); return R.ok(); } catch (Exception e) { System.out.println(e); return R.error(); } } }