package com.izouma.nineth.web; import com.izouma.nineth.domain.MetaTaskBind; import com.izouma.nineth.domain.MetaTaskNew; import com.izouma.nineth.dto.PageQuery; import com.izouma.nineth.enums.MetaTaskType; import com.izouma.nineth.exception.BusinessException; import com.izouma.nineth.repo.MetaTaskBindRepo; import com.izouma.nineth.repo.MetaTaskNewRepo; import com.izouma.nineth.service.MetaTaskNewService; 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; @RestController @RequestMapping("/metaTaskNew") @AllArgsConstructor public class MetaTaskNewController extends BaseController { private MetaTaskNewService metaTaskNewService; private MetaTaskNewRepo metaTaskNewRepo; private MetaTaskBindRepo metaTaskBindRepo; //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/save") public MetaTaskNew save(@RequestBody MetaTaskNew record) { if (record.getId() != null) { MetaTaskNew orig = metaTaskNewRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录")); ObjUtils.merge(orig, record); MetaTaskNew save = metaTaskNewRepo.save(orig); saveMetaTaskBind(save.getId(), record); return save; } MetaTaskNew save = metaTaskNewRepo.save(record); saveMetaTaskBind(save.getId(), record); return save; } private void saveMetaTaskBind(Long taskId, MetaTaskNew record) { // 节点任务 if (CollectionUtils.isNotEmpty(record.getMetaNodeTask())) { record.getMetaNodeTask().forEach(metaTaskBind -> { metaTaskBind.setTaskId(taskId); metaTaskBind.setType(MetaTaskType.NODE); }); metaTaskBindRepo.saveAll(record.getMetaNodeTask()); } // 支线任务 if (CollectionUtils.isNotEmpty(record.getMetaBranchLineTask())) { record.getMetaBranchLineTask().forEach(metaTaskBind -> { metaTaskBind.setTaskId(taskId); metaTaskBind.setType(MetaTaskType.BRANCH_LINE); }); metaTaskBindRepo.saveAll(record.getMetaBranchLineTask()); } // 日常任务 if (CollectionUtils.isNotEmpty(record.getMetaDailyTask())) { record.getMetaDailyTask().forEach(metaTaskBind -> { metaTaskBind.setTaskId(taskId); metaTaskBind.setType(MetaTaskType.DAILY); }); metaTaskBindRepo.saveAll(record.getMetaDailyTask()); } } //@PreAuthorize("hasRole('ADMIN')") @PostMapping("/all") public Page all(@RequestBody PageQuery pageQuery) { return metaTaskNewService.all(pageQuery); } @GetMapping("/get/{id}") public MetaTaskNew get(@PathVariable Long id) { MetaTaskNew metaTaskNew = metaTaskNewRepo.findById(id).orElseThrow(new BusinessException("无记录")); List metaNodeTask = metaTaskBindRepo.findByTaskIdAndTypeAndDel(metaTaskNew.getId(), MetaTaskType.NODE, false); if (CollectionUtils.isNotEmpty(metaNodeTask)) { metaTaskNew.setMetaNodeTask(metaNodeTask); } List metaBranchLineTask = metaTaskBindRepo.findByTaskIdAndTypeAndDel(metaTaskNew.getId(), MetaTaskType.BRANCH_LINE, false); if (CollectionUtils.isNotEmpty(metaBranchLineTask)) { metaTaskNew.setMetaBranchLineTask(metaBranchLineTask); } List metaDailyTask = metaTaskBindRepo.findByTaskIdAndTypeAndDel(metaTaskNew.getId(), MetaTaskType.DAILY, false); if (CollectionUtils.isNotEmpty(metaDailyTask)) { metaTaskNew.setMetaDailyTask(metaDailyTask); } return metaTaskNew; } @PostMapping("/del/{id}") public void del(@PathVariable Long id) { metaTaskNewRepo.softDelete(id); } @GetMapping("/excel") @ResponseBody public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException { List data = all(pageQuery).getContent(); ExcelUtils.export(response, data); } }