package com.taoke.autopay.service.impl; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.taoke.autopay.dto.ChannelOrderCountDTO; import com.taoke.autopay.dto.UserSubmitKeyLimitDto; import com.taoke.autopay.entity.OrderChannelEnum; import com.taoke.autopay.entity.SystemConfigKeyEnum; import com.taoke.autopay.service.SystemConfigService; import com.taoke.autopay.service.UserSettingService; import com.taoke.autopay.utils.StringUtil; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Comparator; import java.util.List; /** * @author hxh * @title: UserSettingServiceImpl * @description: TODO * @date 2024/8/1 22:42 */ @Service public class UserSettingServiceImpl implements UserSettingService { @Resource private SystemConfigService systemConfigService; @Override public void setUserSubmitKeyCount(List limit) { if (limit == null) { limit = new ArrayList<>(); } limit.sort((UserSubmitKeyLimitDto o1, UserSubmitKeyLimitDto o2) -> o1.getOrderType()==o2.getOrderType() ? o2.getBeforeCount() - o1.getBeforeCount():o1.getOrderType() - o2.getOrderType()); systemConfigService.setValue(SystemConfigKeyEnum.USER_SUBMIT_KEY_COUNT_LIMIT, new Gson().toJson(limit)); } @Override public List getUserSubmitKeyCountList(boolean fromCache) { String value = null; if (!fromCache) { value = systemConfigService.getValue(SystemConfigKeyEnum.USER_SUBMIT_KEY_COUNT_LIMIT); } else { value = systemConfigService.getValueCache(SystemConfigKeyEnum.USER_SUBMIT_KEY_COUNT_LIMIT); } if (StringUtil.isNullOrEmpty(value)) { return new ArrayList<>(); } return new Gson().fromJson(value, new TypeToken>() { }.getType()); } @Override public int getLimitCountByTotalCount(int orderType, long beforeCount, OrderChannelEnum orderChannel) { List list = getUserSubmitKeyCountList(true); if (list.isEmpty()) { return Integer.MAX_VALUE; } for (UserSubmitKeyLimitDto t : list) { if(t.getOrderType()!=orderType){ continue; } if (beforeCount >= t.getBeforeCount()) { for(ChannelOrderCountDTO tt:t.getTodayOrderCountList()){ if(tt.getOrderChannel() ==orderChannel){ return tt.getOrderCount(); } } return Integer.MAX_VALUE; } } return Integer.MAX_VALUE; } }