@ApiOperation(“工序-交换顺序(将当前记录与目标记录交换顺序) @author tarzan”)
@GetMapping(“/working/procedure/swap/{sourceId}/{targetId}”)
public CommonResponseDTO swap(@ApiParam(name = “sourceId”, value = “当前记录ID”, required = true) @PathVariable(“sourceId”) String sourceId,
@ApiParam(name = “targetId”, value = “目标记录ID”, required = true) @PathVariable(“targetId”) String targetId) {
return templateDetailService.swap(sourceId, targetId);
}
服务层方法代码
/**
-
方法描述: 交换顺序
-
@param sourceId
-
@param targetId
-
@Return {@link CommonResponseDTO< String>}
-
@throws
-
@author tarzan
-
@date 2020年04月01日 17:15:43
*/
public CommonResponseDTO swap(String sourceId, String targetId) {
WorkingProcedureTemplateDetail entity = detailMapper.selectByPrimaryKey(sourceId);
String templateId = entity.getTemplateId();
WorkingProcedureTemplate template = templateMapper.selectByPrimaryKey(templateId);
if(Objects.isNull(template) || template.getDeleteFlag().equals(CommonConst.DELETED) || !template.getStatus().equals(WorkingProcedureTemplateStatusEnum.DRAFT.getStatus())){
return CommonResponseDTO.forFail(“关联模板不可操作”);
}
List list=detailMapper.findByTemplateId(templateId);
WorkingProcedureTemplateDetail source = findOne(list,sourceId);
WorkingProcedureTemplateDetail target = findOne(list,targetId);
if (Objects.isNull(source) || Objects.isNull(target)) {
return CommonResponseDTO.recordNotExists();
}
//重新排序
List updateList=swapOrder(list,source,target);
if(CollectionUtils.isNotEmpty(updateList)){
detailMapper.updateBatchOrder(updateList);
}
return CommonResponseDTO.forSuc
cess();
}
/**
-
方法描述: 根据id查询list中对象
-
@param list
-
@param id
-
@Return {@link WorkingProcedureTemplateDetail}
-
@throws
-
@author tarzan Liu
-
@date 2020年08月11日 16:42:34
*/
public WorkingProcedureTemplateDetail findOne(List list,String id){
if (CollectionUtils.isNotEmpty(list)){
Map<String,WorkingProcedureTemplateDetail> map=list.stream().collect(Collectors.toMap(WorkingProcedureTemplateDetail::getId,e->e));
return map.get(id);
}
return null;
}
/**
-
方法描述: 拖动排序
-
@param list
-
@param source
-
@param target
-
@Return
-
@throws
-
@author tarzan Liu
-
@date 2020年08月10日 18:13:56
*/
public List swapOrder(List list,WorkingProcedureTemplateDetail source,WorkingProcedureTemplateDetail target){
if(source.getSort()<target.getSort()){
return downSwap(list,source,target);
}
if(source.getSort()>target.getSort()){
return upSwap(list,source,target);
}
return null;
}
/**
-
方法描述: 向下拖动排序
-
@param list
-
@param source
-
@param target
-
@Return {@link List< WorkingProcedureTemplateDetail>}
-
@throws
-
@author tarzan Liu
-
@date 2020年08月10日 18:14:14
*/
public List downSwap(List list,WorkingProcedureTemplateDetail source,WorkingProcedureTemplateDetail target){
int sourceSort = source.getSort();
int targetSort =target.getSort();
list.sort(Comparator.comparing(WorkingProcedureTemplateDetail::getSort));
list.forEach(e->{
if(sourceSort<e.getSort()&&e.getSort()<=targetSort){
exchange(source,e);
}
});
return list.subList(sourceSort-1,targetSort);
}
/**
-
方法描述: 向上拖动排序
-
@param list
-
@param source