5-27 fix
parent
9dfb999323
commit
ed9a44911b
|
|
@ -1,5 +1,6 @@
|
|||
package com.zcloud.safetyDutyList.web.tasklist;
|
||||
|
||||
import com.alibaba.cola.dto.MultiResponse;
|
||||
import com.alibaba.cola.dto.PageResponse;
|
||||
import com.alibaba.cola.dto.Response;
|
||||
import com.alibaba.cola.dto.SingleResponse;
|
||||
|
|
@ -42,17 +43,17 @@ public class TaskDetailController {
|
|||
return taskListService.addTaskDetail(cmd);
|
||||
}
|
||||
|
||||
@ApiOperation("新增任务")
|
||||
@ApiOperation("批量保存任务")
|
||||
@PostMapping("/saveBatch")
|
||||
public SingleResponse add(@Validated @RequestBody List<TaskDetailSaveCmd> cmds) {
|
||||
public MultiResponse<TaskDetailCO> addBatch(@Validated @RequestBody List<TaskDetailSaveBatchCmd> cmds) {
|
||||
return taskListService.addTaskDetailBatch(cmds);
|
||||
}
|
||||
|
||||
@ApiOperation("编辑任务")
|
||||
@PostMapping("/edit")
|
||||
public SingleResponse<TaskDetailCO> edit(@Validated @RequestBody TaskDetailEditCmd cmd) {
|
||||
return taskListService.editTaskDetail(cmd);
|
||||
}
|
||||
// @ApiOperation("编辑任务")
|
||||
// @PostMapping("/edit")
|
||||
// public SingleResponse<TaskDetailCO> edit(@Validated @RequestBody TaskDetailEditCmd cmd) {
|
||||
// return taskListService.editTaskDetail(cmd);
|
||||
// }
|
||||
|
||||
@ApiOperation("获取任务详情")
|
||||
@GetMapping("/{detailId}")
|
||||
|
|
|
|||
|
|
@ -3,81 +3,105 @@ package com.zcloud.safetyDutyList.command.tasklist;
|
|||
import com.alibaba.cola.exception.BizException;
|
||||
import com.zcloud.safetyDutyList.domain.enums.TaskStatusEnum;
|
||||
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskDetailGateway;
|
||||
import com.zcloud.safetyDutyList.domain.gateway.tasklist.TaskListGateway;
|
||||
import com.zcloud.safetyDutyList.domain.model.tasklist.TaskDetailE;
|
||||
import com.zcloud.safetyDutyList.dto.clientobject.tasklist.TaskDetailCO;
|
||||
import com.zcloud.safetyDutyList.dto.tasklist.TaskDetailSaveCmd;
|
||||
import com.zcloud.safetyDutyList.dto.tasklist.TaskDetailSaveBatchCmd;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 任务详情保存执行器
|
||||
* <p>
|
||||
* 处理新增任务详情的业务逻辑:
|
||||
* 1. 查询该清单下已有任务详情,计算总分
|
||||
* 2. 校验新增后清单总分不超过100分
|
||||
* 3. 创建任务详情实体并保存
|
||||
* 4. 返回新增后的任务详情信息
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class TaskDetailSaveBatchExe {
|
||||
private final TaskDetailGateway taskDetailGateway;
|
||||
private final TaskListGateway taskListGateway;
|
||||
|
||||
public Boolean execute(List<TaskDetailSaveCmd> cmds) {
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public List<TaskDetailCO> execute(List<TaskDetailSaveBatchCmd> cmds) {
|
||||
if (cmds == null || cmds.isEmpty()) {
|
||||
throw new BizException("参数不能为空");
|
||||
throw new BizException("参数不能为空");
|
||||
}
|
||||
|
||||
// 获取当前已有列表
|
||||
List<TaskDetailE> existDetails = taskDetailGateway.listByTaskListId(cmds.get(0).getTaskListId());
|
||||
if (existDetails == null || existDetails.isEmpty()) {
|
||||
throw new BizException("清单不存在");
|
||||
// 获取已存在的任务详情列表
|
||||
String taskListId = cmds.get(0).getTaskListId();
|
||||
List<TaskDetailE> existDetails = taskDetailGateway.listByTaskListId(taskListId);
|
||||
|
||||
Map<Long, TaskDetailE> existDetailMap = existDetails.stream()
|
||||
.collect(Collectors.toMap(TaskDetailE::getId, Function.identity(), (o, n) -> o));
|
||||
|
||||
// 获取要删除的ID
|
||||
List<Long> cmdIds = cmds.stream()
|
||||
.map(TaskDetailSaveBatchCmd::getId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
List<Long> toDeleteIds = existDetails.stream()
|
||||
.map(TaskDetailE::getId)
|
||||
.filter(id -> !cmdIds.contains(id))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
BigDecimal totalScore = BigDecimal.ZERO;
|
||||
List<TaskDetailE> toAdd = new ArrayList<>();
|
||||
List<TaskDetailE> toUpdate = new ArrayList<>();
|
||||
|
||||
for (TaskDetailSaveBatchCmd cmd : cmds) {
|
||||
if (cmd.getId() == null) {
|
||||
TaskDetailE detailE = new TaskDetailE();
|
||||
BeanUtils.copyProperties(cmd, detailE);
|
||||
detailE.init();
|
||||
detailE.setTaskStatus(TaskStatusEnum.IN_PROGRESS.getCode());
|
||||
detailE.setFeedbackStatus(1);
|
||||
toAdd.add(detailE);
|
||||
} else {
|
||||
TaskDetailE existDetail = existDetailMap.get(cmd.getId());
|
||||
if (existDetail == null) {
|
||||
throw new BizException("任务详情不存在,id=" + cmd.getId());
|
||||
}
|
||||
existDetail.setExecuteContent(cmd.getExecuteContent());
|
||||
existDetail.setFeedbackCycleType(cmd.getFeedbackCycleType());
|
||||
existDetail.setTaskScore(cmd.getTaskScore());
|
||||
toUpdate.add(existDetail);
|
||||
}
|
||||
if (cmd.getTaskScore() != null) {
|
||||
totalScore = totalScore.add(cmd.getTaskScore());
|
||||
}
|
||||
}
|
||||
|
||||
// 当前的分值
|
||||
// BigDecimal totalScore = existDetails.stream()
|
||||
// .map(TaskDetailE::getTaskScore)
|
||||
// .filter(s -> s != null)
|
||||
// .reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
//
|
||||
// BigDecimal cmdScore = cmds.stream()
|
||||
// .map(TaskDetailSaveCmd::getTaskScore)
|
||||
// .filter(s -> s != null)
|
||||
// .reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
//
|
||||
// BigDecimal diff = new BigDecimal("100").subtract(totalScore);
|
||||
//
|
||||
// if (cmdScore != null) {
|
||||
// totalScore = totalScore.add(cmdScore);
|
||||
// }
|
||||
// if (totalScore.compareTo(new BigDecimal("100")) > 0) {
|
||||
// throw new BizException("当前清单总分不能超过100,任务可设最大分值:" + diff);
|
||||
// }
|
||||
BigDecimal remainScore = existDetails.stream()
|
||||
.filter(d -> !cmdIds.contains(d.getId()) && !toDeleteIds.contains(d.getId()))
|
||||
.map(TaskDetailE::getTaskScore)
|
||||
.filter(Objects::nonNull)
|
||||
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
||||
totalScore = totalScore.add(remainScore);
|
||||
|
||||
List<TaskDetailE> detailEList = new ArrayList <>();
|
||||
for (TaskDetailSaveCmd cmd : cmds) {
|
||||
TaskDetailE detailE = new TaskDetailE();
|
||||
BeanUtils.copyProperties(cmd, detailE);
|
||||
detailE.init();
|
||||
detailE.setTaskStatus(TaskStatusEnum.IN_PROGRESS.getCode());
|
||||
detailE.setFeedbackStatus(1);
|
||||
|
||||
detailEList.add(detailE);
|
||||
if (totalScore.compareTo(new BigDecimal("100")) > 0) {
|
||||
throw new BizException("当前清单总分不能超过100");
|
||||
}
|
||||
|
||||
boolean res = taskDetailGateway.batchAdd(detailEList);
|
||||
if (!res) {
|
||||
throw new BizException("保存失败");
|
||||
for (Long deleteId : toDeleteIds) {
|
||||
taskDetailGateway.deleteById(deleteId);
|
||||
}
|
||||
|
||||
return true;
|
||||
if (!toAdd.isEmpty()) {
|
||||
taskDetailGateway.batchAdd(toAdd);
|
||||
}
|
||||
|
||||
for (TaskDetailE detailE : toUpdate) {
|
||||
taskDetailGateway.update(detailE);
|
||||
}
|
||||
|
||||
List<TaskDetailE> latestDetails = taskDetailGateway.listByTaskListId(taskListId);
|
||||
return latestDetails.stream().map(detailE -> {
|
||||
TaskDetailCO co = new TaskDetailCO();
|
||||
BeanUtils.copyProperties(detailE, co);
|
||||
return co;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -125,9 +125,9 @@ public class TaskListServiceImpl implements TaskListServiceI {
|
|||
}
|
||||
|
||||
@Override
|
||||
public SingleResponse addTaskDetailBatch(List <TaskDetailSaveCmd> cmds) {
|
||||
taskDetailSaveBatchExe.execute(cmds);
|
||||
return SingleResponse.buildSuccess();
|
||||
public MultiResponse<TaskDetailCO> addTaskDetailBatch(List<TaskDetailSaveBatchCmd> cmds) {
|
||||
List<TaskDetailCO> coList = taskDetailSaveBatchExe.execute(cmds);
|
||||
return MultiResponse.of(coList);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -100,12 +100,12 @@ public interface TaskListServiceI {
|
|||
SingleResponse<TaskDetailCO> addTaskDetail(TaskDetailSaveCmd cmd);
|
||||
|
||||
/**
|
||||
* 新增任务详情(校验清单总分不超过100)
|
||||
* 批量保存任务详情(id为空新增,id非空更新,数据库有但传参无则删除)
|
||||
*
|
||||
* @param cmds 任务保存命令
|
||||
* @return 新增后的任务详情
|
||||
* @param cmds 任务保存命令列表
|
||||
* @return 保存后的任务详情列表
|
||||
*/
|
||||
SingleResponse<TaskDetailCO> addTaskDetailBatch(List<TaskDetailSaveCmd> cmds);
|
||||
MultiResponse<TaskDetailCO> addTaskDetailBatch(List<TaskDetailSaveBatchCmd> cmds);
|
||||
|
||||
/**
|
||||
* 编辑任务详情(校验清单总分不超过100)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package com.zcloud.safetyDutyList.dto.tasklist;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* 任务详情保存命令
|
||||
* <p>
|
||||
* 用于新增任务详情,包含所属清单ID、执行内容、反馈周期类型、任务分值等字段
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class TaskDetailSaveBatchCmd implements Serializable {
|
||||
@ApiModelProperty(value = "主键ID", required = true)
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty(value = "清单表主键ID", required = true)
|
||||
@NotEmpty(message = "清单ID不能为空")
|
||||
private String taskListId;
|
||||
|
||||
@ApiModelProperty(value = "执行内容")
|
||||
private String executeContent;
|
||||
|
||||
@ApiModelProperty(value = "反馈周期类型:1-每月 2-季度 3-半年 4-年", required = true)
|
||||
@NotNull(message = "反馈周期不能为空")
|
||||
private Integer feedbackCycleType;
|
||||
|
||||
@ApiModelProperty(value = "任务分值")
|
||||
private BigDecimal taskScore;
|
||||
}
|
||||
Loading…
Reference in New Issue