From cebb05f8d1ddbf5ac415c5471e17d2d94ea1a6de Mon Sep 17 00:00:00 2001 From: zjh <1084500556@qq.com> Date: 星期三, 15 五月 2024 18:14:07 +0800 Subject: [PATCH] zjh 2024/05/15-1 --- ltkj-system/src/main/java/com/ltkj/tduck/utils/SortUtils.java | 218 +++++++++++++++++++++++++++--------------------------- 1 files changed, 109 insertions(+), 109 deletions(-) diff --git a/ltkj-system/src/main/java/com/ltkj/tduck/utils/SortUtils.java b/ltkj-system/src/main/java/com/ltkj/tduck/utils/SortUtils.java index 56e135a..c59f3ce 100644 --- a/ltkj-system/src/main/java/com/ltkj/tduck/utils/SortUtils.java +++ b/ltkj-system/src/main/java/com/ltkj/tduck/utils/SortUtils.java @@ -1,109 +1,109 @@ -//package com.ltkj.tduck.utils; -// -// -//import cn.hutool.core.util.ObjectUtil; -//import cn.hutool.core.util.StrUtil; -//import com.baomidou.mybatisplus.core.toolkit.Wrappers; -//import com.ltkj.tduck.constant.FormRedisKeyConstants; -//import com.ltkj.tduck.domain.UserFormItemEntity; -//import com.ltkj.tduck.service.UserFormItemService; -//import lombok.RequiredArgsConstructor; -//import org.springframework.stereotype.Component; -// -//import java.util.Comparator; -//import java.util.List; -// -///** -// * 涓や釜鍓嶆彁:浣嶇疆鐨勪俊鎭褰曞湪浠诲姟鐨勬ā鍨嬩笂(pos瀛楁);涓�鑸袱涓换鍔$殑pos鍊肩浉宸緢澶�(榛樿鏄�65536);鐜板湪鍋囪鏈変换鍔� A, B, C. -// * 鍒椾妇鍑犱釜甯歌鐨勫満鏅�:濡傛灉C瑕佺Щ鍒癆鐨勫墠闈�, 閭d箞鍚庣浼氭妸 C.pos 鏀逛负 A.pos/2;鑻ユ灉C瑕佺Щ鍒癆鍜孊涔嬮棿, -// * 閭d箞鍚庣浼氭妸 C.pos 鏀逛负 (A.pos + B.pos)/2;濡傛灉B瑕佺Щ鍒癈鐨勫悗闈�, 閭d箞鍚庣浼氭妸 B.pos 鏀逛负 C.pos + 65536; -// * 褰撶劧杩欓噷浼氭湁涓瀬绔儏鍐甸渶瑕佸鐞�(鍋囪pos鏄暣鏁�):濡傛灉 A.pos 鏄� 1, B.pos 鏄� 2, C鎯崇Щ鍒癆鍜孊涔嬮棿, 鍏跺疄鏄病鏈変竴涓暣鏁板彲浠ョ敤鐨�, -// * 閽堝杩欑鎯呭喌, 鍚庣浼氶噸鏂扮粰鏁翠釜鍒楄〃鐨勪换鍔¢噸鏂板埛涓�閬峱os鍊�. -// * <p> -// * 鎷栧姩鎺掑簭宸ュ叿绫� -// * 绠楁硶鍙傝�� @link https://www.zhihu.com/question/55789722 -// * -// * @author smalljop -// */ -//@RequiredArgsConstructor -//@Component -//public class SortUtils { -// -// private final CacheUtils cacheUtils; -// -// /** -// * 鎺掑簭榛樿鑷鍥犲瓙 -// */ -// private final Long SORT_DEFAULT_INCR_FACT = 65536L; -// -// private final UserFormItemService formItemService; -// -// -// /** -// * 鑾峰彇鎺掑簭鏁板�� -// * -// * @param formKey 琛ㄥ崟key -// * @return 鍒濆鎺掑簭鏁板�� -// */ -// public Long getInitialSortPosition(String formKey) { -// String redisKey = StrUtil.format(FormRedisKeyConstants.FORM_ITEM_POS_DELTA, formKey); -// // 妯℃澘鍒涘缓鏃� 鍒濆鎺掑簭鏁板�� -// if (StrUtil.isBlank(cacheUtils.get(redisKey))) { -// Long sort = formItemService.getLastItemSort(formKey); -// cacheUtils.save(redisKey, String.valueOf(sort == null ? 1 : sort)); -// } -// return cacheUtils.incr(redisKey, SORT_DEFAULT_INCR_FACT.intValue()); -// } -// -// -// /** -// * * 涓や釜鍓嶆彁:浣嶇疆鐨勪俊鎭褰曞湪浠诲姟鐨勬ā鍨嬩笂(pos瀛楁);涓�鑸袱涓换鍔$殑pos鍊肩浉宸緢澶�(榛樿鏄�65536);鐜板湪鍋囪鏈変换鍔� A, B, C. -// * * 鍒椾妇鍑犱釜甯歌鐨勫満鏅�:濡傛灉C瑕佺Щ鍒癆鐨勫墠闈�, 閭d箞鍚庣浼氭妸 C.pos 鏀逛负 A.pos/2;鑻ユ灉C瑕佺Щ鍒癆鍜孊涔嬮棿, -// * * 閭d箞鍚庣浼氭妸 C.pos 鏀逛负 (A.pos + B.pos)/2;濡傛灉B瑕佺Щ鍒癈鐨勫悗闈�, 閭d箞鍚庣浼氭妸 B.pos 鏀逛负 C.pos + 65536; -// * * 褰撶劧杩欓噷浼氭湁涓瀬绔儏鍐甸渶瑕佸鐞�(鍋囪pos鏄暣鏁�):濡傛灉 A.pos 鏄� 1, B.pos 鏄� 2, C鎯崇Щ鍒癆鍜孊涔嬮棿, 鍏跺疄鏄病鏈変竴涓暣鏁板彲浠ョ敤鐨�, -// * * 閽堝杩欑鎯呭喌, 鍚庣浼氶噸鏂扮粰鏁翠釜鍒楄〃鐨勪换鍔¢噸鏂板埛涓�閬峱os鍊�. -// * 鎺掑簭涔嬪悗閲嶆柊璁$畻 -// * -// * @param beforePosition 涔嬪墠鐨勪綅缃� -// * @param afterPosition 涔嬪悗鐨勪綅缃� -// * @param formKey 琛ㄥ崟key -// * @return 鏈�缁堜綅缃� -// */ -// public Long calcSortPosition(Long beforePosition, Long afterPosition, String formKey) { -// String redisKey = StrUtil.format(FormRedisKeyConstants.FORM_ITEM_POS_DELTA, formKey); -// boolean isCenter = (ObjectUtil.isNotNull(beforePosition) && 0L != beforePosition) && (ObjectUtil.isNotNull(afterPosition) && 0L != afterPosition); -// if (isCenter) { // 鏀惧埌涓棿 -// return (beforePosition + afterPosition) / 2; -// } -// if (ObjectUtil.isNull(beforePosition) || 0L == beforePosition) { // 鏀惧埌鏈�鍓嶉潰 -// return afterPosition / 2; -// } else { // 鏀惧埌鏈�鍚庨潰 -// return cacheUtils.incr(redisKey, SORT_DEFAULT_INCR_FACT.intValue()); -// } -// } -// -// -// /** -// * 鏋佺鎯呭喌鍒锋柊鏁翠釜鍒楄〃 -// * -// * @param beforePosition 涔嬪墠鐨勪綅缃� -// * @param afterPosition 涔嬪悗鐨勪綅缃� -// * @param formKey 琛ㄥ崟key -// * @param sort 鎺掑簭鏁板�� -// */ -// public Boolean sortAllList(Long beforePosition, Long afterPosition, String formKey, Long sort) { -// // 鍗冲皢娌℃湁鏁存暟锛屽埛鏂板叏閮ㄥ垪琛ㄦ帓搴忓�� -// if ((ObjectUtil.isNotNull(beforePosition) && 0L != beforePosition && ObjectUtil.equal(sort, beforePosition + 1)) -// || (ObjectUtil.isNotNull(afterPosition) && 0L != afterPosition && ObjectUtil.equal(sort, 1L))) { -// List<UserFormItemEntity> itemEntityList = formItemService.list(Wrappers.<UserFormItemEntity>lambdaQuery().eq(UserFormItemEntity::getFormKey, formKey)); -// itemEntityList.sort(Comparator.comparing(UserFormItemEntity::getSort)); -// for (int i = 0; i < itemEntityList.size(); i++) { -// itemEntityList.get(i).setSort(SORT_DEFAULT_INCR_FACT * (i + 1)); -// } -// formItemService.updateBatchById(itemEntityList); -// return true; -// } -// return false; -// } -// -//} +package com.ltkj.tduck.utils; + + +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.ltkj.tduck.constant.FormRedisKeyConstants; +import com.ltkj.tduck.domain.UserFormItemEntity; +import com.ltkj.tduck.service.UserFormItemService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.Comparator; +import java.util.List; + +/** + * 涓や釜鍓嶆彁:浣嶇疆鐨勪俊鎭褰曞湪浠诲姟鐨勬ā鍨嬩笂(pos瀛楁);涓�鑸袱涓换鍔$殑pos鍊肩浉宸緢澶�(榛樿鏄�65536);鐜板湪鍋囪鏈変换鍔� A, B, C. + * 鍒椾妇鍑犱釜甯歌鐨勫満鏅�:濡傛灉C瑕佺Щ鍒癆鐨勫墠闈�, 閭d箞鍚庣浼氭妸 C.pos 鏀逛负 A.pos/2;鑻ユ灉C瑕佺Щ鍒癆鍜孊涔嬮棿, + * 閭d箞鍚庣浼氭妸 C.pos 鏀逛负 (A.pos + B.pos)/2;濡傛灉B瑕佺Щ鍒癈鐨勫悗闈�, 閭d箞鍚庣浼氭妸 B.pos 鏀逛负 C.pos + 65536; + * 褰撶劧杩欓噷浼氭湁涓瀬绔儏鍐甸渶瑕佸鐞�(鍋囪pos鏄暣鏁�):濡傛灉 A.pos 鏄� 1, B.pos 鏄� 2, C鎯崇Щ鍒癆鍜孊涔嬮棿, 鍏跺疄鏄病鏈変竴涓暣鏁板彲浠ョ敤鐨�, + * 閽堝杩欑鎯呭喌, 鍚庣浼氶噸鏂扮粰鏁翠釜鍒楄〃鐨勪换鍔¢噸鏂板埛涓�閬峱os鍊�. + * <p> + * 鎷栧姩鎺掑簭宸ュ叿绫� + * 绠楁硶鍙傝�� @link https://www.zhihu.com/question/55789722 + * + * @author smalljop + */ +@RequiredArgsConstructor +@Component +public class SortUtils { + + private final CacheUtils cacheUtils; + + /** + * 鎺掑簭榛樿鑷鍥犲瓙 + */ + private final Long SORT_DEFAULT_INCR_FACT = 65536L; + + private final UserFormItemService formItemService; + + + /** + * 鑾峰彇鎺掑簭鏁板�� + * + * @param formKey 琛ㄥ崟key + * @return 鍒濆鎺掑簭鏁板�� + */ + public Long getInitialSortPosition(String formKey) { + String redisKey = StrUtil.format(FormRedisKeyConstants.FORM_ITEM_POS_DELTA, formKey); + // 妯℃澘鍒涘缓鏃� 鍒濆鎺掑簭鏁板�� + if (StrUtil.isBlank(cacheUtils.get(redisKey))) { + Long sort = formItemService.getLastItemSort(formKey); + cacheUtils.save(redisKey, String.valueOf(sort == null ? 1 : sort)); + } + return cacheUtils.incr(redisKey, SORT_DEFAULT_INCR_FACT.intValue()); + } + + + /** + * * 涓や釜鍓嶆彁:浣嶇疆鐨勪俊鎭褰曞湪浠诲姟鐨勬ā鍨嬩笂(pos瀛楁);涓�鑸袱涓换鍔$殑pos鍊肩浉宸緢澶�(榛樿鏄�65536);鐜板湪鍋囪鏈変换鍔� A, B, C. + * * 鍒椾妇鍑犱釜甯歌鐨勫満鏅�:濡傛灉C瑕佺Щ鍒癆鐨勫墠闈�, 閭d箞鍚庣浼氭妸 C.pos 鏀逛负 A.pos/2;鑻ユ灉C瑕佺Щ鍒癆鍜孊涔嬮棿, + * * 閭d箞鍚庣浼氭妸 C.pos 鏀逛负 (A.pos + B.pos)/2;濡傛灉B瑕佺Щ鍒癈鐨勫悗闈�, 閭d箞鍚庣浼氭妸 B.pos 鏀逛负 C.pos + 65536; + * * 褰撶劧杩欓噷浼氭湁涓瀬绔儏鍐甸渶瑕佸鐞�(鍋囪pos鏄暣鏁�):濡傛灉 A.pos 鏄� 1, B.pos 鏄� 2, C鎯崇Щ鍒癆鍜孊涔嬮棿, 鍏跺疄鏄病鏈変竴涓暣鏁板彲浠ョ敤鐨�, + * * 閽堝杩欑鎯呭喌, 鍚庣浼氶噸鏂扮粰鏁翠釜鍒楄〃鐨勪换鍔¢噸鏂板埛涓�閬峱os鍊�. + * 鎺掑簭涔嬪悗閲嶆柊璁$畻 + * + * @param beforePosition 涔嬪墠鐨勪綅缃� + * @param afterPosition 涔嬪悗鐨勪綅缃� + * @param formKey 琛ㄥ崟key + * @return 鏈�缁堜綅缃� + */ + public Long calcSortPosition(Long beforePosition, Long afterPosition, String formKey) { + String redisKey = StrUtil.format(FormRedisKeyConstants.FORM_ITEM_POS_DELTA, formKey); + boolean isCenter = (ObjectUtil.isNotNull(beforePosition) && 0L != beforePosition) && (ObjectUtil.isNotNull(afterPosition) && 0L != afterPosition); + if (isCenter) { // 鏀惧埌涓棿 + return (beforePosition + afterPosition) / 2; + } + if (ObjectUtil.isNull(beforePosition) || 0L == beforePosition) { // 鏀惧埌鏈�鍓嶉潰 + return afterPosition / 2; + } else { // 鏀惧埌鏈�鍚庨潰 + return cacheUtils.incr(redisKey, SORT_DEFAULT_INCR_FACT.intValue()); + } + } + + + /** + * 鏋佺鎯呭喌鍒锋柊鏁翠釜鍒楄〃 + * + * @param beforePosition 涔嬪墠鐨勪綅缃� + * @param afterPosition 涔嬪悗鐨勪綅缃� + * @param formKey 琛ㄥ崟key + * @param sort 鎺掑簭鏁板�� + */ + public Boolean sortAllList(Long beforePosition, Long afterPosition, String formKey, Long sort) { + // 鍗冲皢娌℃湁鏁存暟锛屽埛鏂板叏閮ㄥ垪琛ㄦ帓搴忓�� + if ((ObjectUtil.isNotNull(beforePosition) && 0L != beforePosition && ObjectUtil.equal(sort, beforePosition + 1)) + || (ObjectUtil.isNotNull(afterPosition) && 0L != afterPosition && ObjectUtil.equal(sort, 1L))) { + List<UserFormItemEntity> itemEntityList = formItemService.list(Wrappers.<UserFormItemEntity>lambdaQuery().eq(UserFormItemEntity::getFormKey, formKey)); + itemEntityList.sort(Comparator.comparing(UserFormItemEntity::getSort)); + for (int i = 0; i < itemEntityList.size(); i++) { + itemEntityList.get(i).setSort(SORT_DEFAULT_INCR_FACT * (i + 1)); + } + formItemService.updateBatchById(itemEntityList); + return true; + } + return false; + } + +} -- Gitblit v1.8.0