package com.terra.system.controller.data; import com.terra.system.annotation.SysLog; import com.terra.system.controller.all.BaseController; import com.terra.system.entity.all.HttpStatus; import com.terra.system.entity.all.ResponseMsg; import com.terra.system.entity.ctrl.DownloadReqEntity; import com.terra.system.entity.data.DownloadEntity; import com.terra.system.entity.data.StyleEntity; import com.terra.system.entity.sys.UserEntity; import com.terra.system.helper.WebHelper; import com.terra.system.service.all.UploadAttachService; import com.terra.system.service.data.DownloadService; import com.terra.system.service.data.StyleService; import com.terra.system.service.sys.DownlogService; import com.terra.system.service.sys.TokenService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; import javax.annotation.Resource; import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; /** * 样式管理 * @author sws * @date 2022-09.26 */ @Tag(name = "数据管理\\样式管理") @RestController @RequestMapping("/style") public class StyleController extends BaseController { @Resource StyleService styleService; @Resource TokenService tokenService; @Resource DownloadService downloadService; @Resource DownlogService downlogService; @Resource UploadAttachService uploadAttachService; private final static String TAB_NAME = "lf.sys_style"; @SysLog() @Operation(summary = "查询记录数") @Parameters({ @Parameter(name = "name", description = "名称", in = ParameterIn.QUERY, required = false, example = "a") }) @GetMapping({"/selectCount"}) public ResponseMsg selectCount(String name) { try { int count = styleService.selectCount(name); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "分页查询") @Parameters({ @Parameter(name = "name", description = "名称", in = ParameterIn.QUERY, required = false, example = "a"), @Parameter(name = "pageSize", description = "每页条数", in = ParameterIn.QUERY, example = "10"), @Parameter(name = "pageIndex", description = "分页数(从1开始)", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/selectByPage") public ResponseMsg> selectByPage(String name, Integer pageSize, Integer pageIndex) { try { if (pageSize < 1 || pageIndex < 0) { return fail("分页数小于1或分页索引小于0", null); } List rs = styleService.selectByPage(name, pageSize, pageSize * (pageIndex - 1)); return success(rs); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "分页查询并返回记录数") @Parameters({ @Parameter(name = "name", description = "名称", in = ParameterIn.QUERY, example = "a"), @Parameter(name = "pageSize", description = "每页条数", in = ParameterIn.QUERY, example = "10"), @Parameter(name = "pageIndex", description = "分页数(从1开始)", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/selectByPageAndCount") public ResponseMsg> selectByPageAndCount(String name, Integer pageSize, Integer pageIndex) { try { if (pageSize < 1 || pageIndex < 0) { return fail("分页数小于1或分页索引小于0", null); } int count = styleService.selectCount(name); if (count == 0) { return success(0, null); } List rs = styleService.selectByPage(name, pageSize, pageSize * (pageIndex - 1)); return success(count, rs); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "插入一条") @Parameters({ @Parameter(name = "entity", description = "实体类", example = "") }) @PostMapping(value = "/insertStyle", produces = "application/json; charset=UTF-8") public ResponseMsg insertStyle(@RequestBody StyleEntity entity, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { entity.setCreateUser(ue.getId()); } int count = styleService.insertStyle(entity); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "插入多条") @Parameters({ @Parameter(name = "list", description = "实体类集合", example = "") }) @PostMapping(value = "/insertStyles", produces = "application/json; charset=UTF-8") public ResponseMsg insertStyles(@RequestBody List list, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { for (StyleEntity entity : list) { entity.setCreateUser(ue.getId()); } } int count = styleService.insertStyles(list); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "删除一条") @Parameters({ @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/deleteStyle") public ResponseMsg deleteStyle(int id) { try { int count = styleService.deleteStyle(id); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "删除多条") @Parameters({ @Parameter(name = "ids", description = "ID数组", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/deleteStyles") public ResponseMsg deleteStyles(@RequestParam List ids) { try { if (ids == null || ids.isEmpty()) { return fail("id数组不能为空", -1); } int count = styleService.deleteStyles(ids); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "更新一条") @Parameters({ @Parameter(name = "entity", description = "实体类", example = "") }) @ResponseBody @PostMapping(value = "/updateStyle", produces = "application/json; charset=UTF-8") public ResponseMsg updateStyle(@RequestBody StyleEntity entity, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { entity.setUpdateUser(ue.getId()); } int count = styleService.updateStyle(entity); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "根据ID查询") @Parameters({ @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/selectStyle") public ResponseMsg selectStyle(int id) { try { StyleEntity styleEntity = styleService.selectStyle(id); return success(styleEntity); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "查询所有") @GetMapping(value = "/selectStyleAll") public ResponseMsg> selectStyleAll() { try { List list = styleService.selectStyleAll(); return success(list); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "上传文件") @ResponseBody @PostMapping(value = "/upload") public ResponseMsg upload(@RequestParam("file") MultipartFile file, HttpServletRequest req) { UserEntity ue = tokenService.getCurrentUser(req); return uploadAttachService.upload(ue, TAB_NAME, file, this); } @SysLog() @Operation(summary = "下载文件") @Parameters({ @Parameter(name = "guid", description = "附件Guid") }) @GetMapping(value = "/download") public void download(String guid, HttpServletResponse res) { uploadAttachService.download(guid, res); } @SysLog() @Operation(summary = "下载文件") @Parameters({ @Parameter(name = "guid", description = "文件GUID", in = ParameterIn.QUERY) }) @RequestMapping(value = "/downloadFile", method = RequestMethod.GET) public void downloadFile(String guid, HttpServletRequest req, HttpServletResponse res) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue == null) { WebHelper.writeStr2Page(res, WebHelper.getErrJson(HttpStatus.UNAUTHORIZED, "用户未登录")); } DownloadEntity de = downloadService.selectByGuid(guid); if (de == null) { WebHelper.writeStr2Page(res, WebHelper.getErrJson(HttpStatus.NOT_FOUND, "文件不存在")); } downlogService.updateInfos(ue, de, req); String filePath = downloadService.getDownloadFilePath(de); WebHelper.download(filePath, de.getName(), res); } catch (Exception ex) { WebHelper.writeStr2Page(res, WebHelper.getErrJson(HttpStatus.UNAUTHORIZED, ex.getMessage())); log.error(ex.getMessage(), ex); } } @SysLog() @Operation(summary = "请求下载") @Parameters({ @Parameter(name = "guids", description = "附件Guid数组", schema = @Schema(type = "array"), example = "e5b6ae0889b88111f13a4b6e048348db,fa4f299e901a0c46e634f8fcc8185c0c") }) @ResponseBody @PostMapping(value = "/downloadReqForGuids") public ResponseMsg downloadReqForGuids(@RequestBody DownloadReqEntity dre, HttpServletRequest req, HttpServletResponse res) { try { if (null == dre || null == dre.getGuids() || dre.getGuids().isEmpty()) { return fail("Guid数组为空"); } UserEntity ue = tokenService.getCurrentUser(req); String guid = uploadAttachService.downloadReqForGuids(ue, dre.getGuids()); return success(guid); } catch (Exception ex) { return fail(ex, null); } } }