package com.izouma.nineth.web; import com.izouma.nineth.domain.MetaSwitch; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.MetaSwitchRepo; import com.izouma.nineth.service.MetaSwitchService; import com.izouma.nineth.utils.ObjUtils; import com.izouma.nineth.utils.excel.ExcelUtils; import lombok.AllArgsConstructor; 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.Objects; @RestController @RequestMapping("/metaSwitch") @AllArgsConstructor public class MetaSwitchController extends BaseController { private MetaSwitchService metaSwitchService; private MetaSwitchRepo metaSwitchRepo; //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public MetaSwitch save(@RequestBody MetaSwitch record) { MetaSwitch metaSwitch = metaSwitchRepo.findByNameAndDel(record.getName(), false); if (Objects.nonNull(metaSwitch) && !Objects.equals(metaSwitch.getId(), record.getId())) { throw new BusinessException("该配置名称已经存在!"); } if (record.getId() != null) { MetaSwitch orig = metaSwitchRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); return metaSwitchRepo.save(orig); } return metaSwitchRepo.save(record); } //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { return metaSwitchService.all(pageQuery); } @GetMapping("/get/{id}") public MetaSwitch get(@PathVariable Long id) { return metaSwitchRepo.findById(id).orElseThrow(new BusinessException("无记录")); } @PostMapping("/del/{id}") public void del(@PathVariable Long id) { metaSwitchRepo.softDelete(id); } @PostMapping("/updateStatus/{id}/{status}") public void updateStatus(@PathVariable Long id, @PathVariable boolean status) { metaSwitchRepo.updateStatus(id, status); } @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } }