package com.izouma.nineth.web; import com.alibaba.fastjson.JSONArray; import com.izouma.nineth.config.MetaConstants; import com.izouma.nineth.domain.RaexSnapshot; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.dto.SnapshotDTO; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.RaexSnapshotRepo; import com.izouma.nineth.service.RaexSnapshotService; import com.izouma.nineth.utils.ObjUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; import org.apache.commons.collections.CollectionUtils; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.util.Map; @RestController @RequestMapping("/raexSnapshot") @AllArgsConstructor public class RaexSnapshotController extends BaseController { private RaexSnapshotService raexSnapshotService; private RaexSnapshotRepo raexSnapshotRepo; //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public RaexSnapshot save(@RequestBody RaexSnapshot record) { if (record.getId() != null) { RaexSnapshot orig = raexSnapshotRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); return raexSnapshotRepo.save(orig); } return raexSnapshotRepo.save(record); } //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { return raexSnapshotService.all(pageQuery); } @GetMapping("/get/{id}") public RaexSnapshot get(@PathVariable Long id) { return raexSnapshotRepo.findById(id).orElseThrow(new BusinessException("无记录")); } @PostMapping("/del/{id}") public void del(@PathVariable Long id) { raexSnapshotRepo.softDelete(id); } @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } @GetMapping("/shot") @ResponseBody public void shot(HttpServletResponse response, String name, String time) throws IOException { List> map = raexSnapshotRepo.shot(MetaConstants.LIKE.concat(name).concat(MetaConstants.LIKE), time); List snapshotDTOS = mapToList(map); ExcelUtils.export(response, snapshotDTOS); } private List mapToList(List> map) { if (CollectionUtils.isEmpty(map)) { throw new BusinessException("无数据"); } JSONArray jsonArray = new JSONArray(); jsonArray.addAll(map); return jsonArray.toJavaList(SnapshotDTO.class); } }