需求:前后端分离项目,后端需要提供给前端两个下拉框接口。
一个查询所有月份的接口,另一个是查询所有大于等于当前时间的月份
查询所有月份的枚举类
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author huihui
* @description: 查询所有月
* @table
* @date 2022/03/21
*/
public enum MonthEnum {
MONTH_1("1", "1月"),
MONTH_2("2", "2月"),
MONTH_3("3", "3月"),
MONTH_4("4", "4月"),
MONTH_5("5", "5月"),
MONTH_6("6", "6月"),
MONTH_7("7", "7月"),
MONTH_8("8", "8月"),
MONTH_9("9", "9月"),
MONTH_10("10", "10月"),
MONTH_11("11", "11月"),
MONTH_12("12", "12月");
private String code;
private String name;
public static String getName(String code) {
MonthEnum[] enums = values();
MonthEnum[] var2 = enums;
int var3 = enums.length;
for(int var4 = 0; var4 < var3; ++var4) {
MonthEnum enum1 = var2[var4];
String nowTypeCode = enum1.getCode();
if(nowTypeCode.equals(code)) {
return enum1.name;
}
}
return null;
}
public static MonthEnum matchByCode(String code) {
MonthEnum[] enums = values();
MonthEnum[] var2 = enums;
int var3 = enums.length;
for(int var4 = 0; var4 < var3; ++var4) {
MonthEnum enum1 = var2[var4];
String nowTypeCode = enum1.getCode();
if(nowTypeCode.equals(code)) {
return enum1;
}
}
return null;
}
public static Boolean isContain(String code){
MonthEnum[] enums = values();
MonthEnum[] var2 = enums;
int var3 = enums.length;
for(int var4 = 0; var4 < var3; ++var4) {
MonthEnum enum1 = var2[var4];
String nowTypeCode = enum1.getCode();
if(nowTypeCode.equals(code)) {
return true;
}
}
return false;
}
/**
* 返回全部状态
*/
public static Map<String, String> initMap(){
Map<String, String> map = new LinkedHashMap<String, String>();//以牺牲时间为代价,使用LinkedHashMap保证按插入顺序排序
MonthEnum[] nums = MonthEnum.values();
if(nums!= null && nums.length>0){
for(MonthEnum num: nums){
map.put(num.getCode(), num.getName());
}
}
return map;
}
private MonthEnum(String code, String name) {
this.code = code;
this.name = name;
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
web接口1:查询所有月份
/**
* 查询所有月
*
* @param
*/
@PostMapping(value = "/queryMonths")
public ResultJson queryMonths() {
ResultJson resultJson = new ResultJson();
long time = System.currentTimeMillis();
log.info("【查询所有月】开始");
//1.初始化-活动类型
Map<String, String> map = MonthEnum.initMap();
//2.组装列表页
List<QueryDictResq> resqList = new ArrayList<>();
for (Map.Entry<String, String> entry : map.entrySet()) {
QueryDictResq resq = new QueryDictResq();
resq.setId(entry.getKey());
resq.setName(entry.getValue());
resqList.add(resq);
log.info("【查询所有月】结束,返回数量【" + resqList.size() + "】");
}
//3.返回
log.info("【查询所有月】结束,耗时【" + (System.currentTimeMillis() - time) + "】");
return resultJson.success(resqList);
}
web接口2:查询所有大于等于当前时间的月份
import lombok.Data;
/**
* @author huihui
* @description: 年份
* @date 2021/10/25 10:48
*/
@Data
public class QueryUseMonthsReq {
private static final long serialVersionUID=1L;
//主键
private String year;
}
/**
* 查询所有大于等于当前时间的月份
*
* @param
*/
@PostMapping(value = "/queryUseMonths")
public ResultJson queryUseMonths(@RequestBody QueryUseMonthsReq req) {
ResultJson resultJson = new ResultJson();
long time = System.currentTimeMillis();
log.info("【查询所有大于等于当前时间的月份】开始");
//1.初始化-活动类型
Map<String, String> map = MonthEnum.initMap();
//2.组装列表页
List<QueryDictResq> resqList = new ArrayList<>();
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
QueryDictResq resq = new QueryDictResq();
Map.Entry<String, String> entry = it.next();
//查询所有大于等于当前时间的月份
int result = Integer.parseInt(req.getYear()) - Integer.parseInt(DateUtils.date2Y(new Date()));
if ((result == 0 && (Integer.parseInt(entry.getKey()) >= Integer.parseInt(DateUtils.date2M(new Date())))) || result > 0) {
//获取今年大于等于当前月份的月份
resq.setId(entry.getKey());
resq.setName(entry.getValue());
resqList.add(resq);
}
log.info("【查询所有大于等于当前时间的月份】结束,返回数量【" + resqList.size() + "】");
}
//3.返回
log.info("【查询所有大于等于当前时间的月份】结束,耗时【" + (System.currentTimeMillis() - time) + "】");
return resultJson.success(resqList);
}
web接口1测试结果:
出参:
{
"code": "200",
"msg": "请求成功",
"data": [
{
"id": "1",
"name": "1月"
},
{
"id": "2",
"name": "2月"
},
{
"id": "3",
"name": "3月"
},
{
"id": "4",
"name": "4月"
},
{
"id": "5",
"name": "5月"
},
{
"id": "6",
"name": "6月"
},
{
"id": "7",
"name": "7月"
},
{
"id": "8",
"name": "8月"
},
{
"id": "9",
"name": "9月"
},
{
"id": "10",
"name": "10月"
},
{
"id": "11",
"name": "11月"
},
{
"id": "12",
"name": "12月"
}
]
}
web接口2测试结果:
postman入参:
{
"year": "2022"
}
出参:
{
"code": "200",
"msg": "请求成功",
"data": [
{
"id": "3",
"name": "3月"
},
{
"id": "4",
"name": "4月"
},
{
"id": "5",
"name": "5月"
},
{
"id": "6",
"name": "6月"
},
{
"id": "7",
"name": "7月"
},
{
"id": "8",
"name": "8月"
},
{
"id": "9",
"name": "9月"
},
{
"id": "10",
"name": "10月"
},
{
"id": "11",
"name": "11月"
},
{
"id": "12",
"name": "12月"
}
]
}