service
@Transactional(rollbackFor = Exception.class)
public Long updateSort(FirmBusinessSortReq req) {
if (req.getId().equals(req.getTargetId())) return 0L;
FirmBusiness source = this.getById(req.getId());
FirmBusiness target = this.getById(req.getTargetId());
firmBusinessMapper.updateSort(req.getId(), source.getSortOrder(), target.getSortOrder(), SecurityUtils.getOwnWith401());
return req.getId();
}
mapper
void updateSort(@Param("id") Long id, @Param("sourceOrder") Integer sourceOrder, @Param("targetOrder")Integer targetOrder,@Param("own") Integer own);
sql
<update id="updateSort">
<choose>
<when test="sourceOrder > targetOrder">
update ow_firm_business oc
set oc.sort_order = IF(oc.id=${id}, ${targetOrder},
IF(oc.sort_order between ${targetOrder} and ${sourceOrder}-1, oc.sort_order+1, oc.sort_order))
where oc.own = ${own}
<!--oc.del_flag = ${@com.common.constant.CommonConstants@NOT_DELETE}-->and
and oc.del_flag = '0'
</when>
<otherwise>
update ow_firm_business oc
set oc.sort_order = IF(oc.id=${id}, ${targetOrder},
IF(oc.sort_order between ${sourceOrder}+1 and ${targetOrder}, oc.sort_order-1, oc.sort_order))
where oc.own = ${own}
<!--oc.del_flag = ${@com.common.constant.CommonConstants@NOT_DELETE}-->and
and oc.del_flag = '0'
</otherwise>
</choose>
</update>