list中存放实体类,Date日期是实体类一个属性值。将存放实体类的list进行排序。
实体类代码:
public class WorksheetStatistics extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 用户ID */
private Long userId;
/** 工序ID */
private Long procedureId;
/** 工序名 */
private String procedureName;
/** 日期 */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "日期", width = 30, dateFormat = "yyyy-MM-dd")
private Date date;
private Integer workNum;
/** 合格数量 */
private Integer passNum;
/** 工废数量 */
private Integer timeWasteNum;
/** 料废数量 */
private Integer materialWasteNum;
/** 返工数量 */
private Integer reworkNum;
/** 生产数量 */
private String workNumRate;
/** 合格数量 */
private String passNumRate;
/** 工废数量 */
private String timeWasteNumRate;
/** 料废数量 */
private String materialWasteNumRate;
/** 返工数量 */
private String reworkNumRate;
public void setUserId(Long userId)
{
this.userId = userId;
}
public Long getUserId()
{
return userId;
}
public void setProcedureId(Long procedureId)
{
this.procedureId = procedureId;
}
public Long getProcedureId()
{
return procedureId;
}
public void setProcedureName(String procedureName)
{
this.procedureName = procedureName;
}
public String getProcedureName()
{
return procedureName;
}
public void setDate(Date date)
{
this.date = date;
}
public Date getDate()
{
return date;
}
public void setWorkNum(Integer workNum)
{
this.workNum = workNum;
}
public Integer getWorkNum()
{
return workNum;
}
public void setPassNum(Integer passNum)
{
this.passNum = passNum;
}
public Integer getPassNum()
{
return passNum;
}
public void setTimeWasteNum(Integer timeWasteNum)
{
this.timeWasteNum = timeWasteNum;
}
public Integer getTimeWasteNum()
{
return timeWasteNum;
}
public void setMaterialWasteNum(Integer materialWasteNum)
{
this.materialWasteNum = materialWasteNum;
}
public Integer getMaterialWasteNum()
{
return materialWasteNum;
}
public void setReworkNum(Integer reworkNum)
{
this.reworkNum = reworkNum;
}
public Integer getReworkNum()
{
return reworkNum;
}
public void setWorkNumRate(String workNumRate)
{
this.workNumRate = workNumRate;
}
public String getWorkNumRate()
{
return workNumRate;
}
public void setPassNumRate(String passNumRate)
{
this.passNumRate = passNumRate;
}
public String getPassNumRate()
{
return passNumRate;
}
public void setTimeWasteNumRate(String timeWasteNumRate)
{
this.timeWasteNumRate = timeWasteNumRate;
}
public String getTimeWasteNumRate()
{
return timeWasteNumRate;
}
public void setMaterialWasteNumRate(String materialWasteNumRate)
{
this.materialWasteNumRate = materialWasteNumRate;
}
public String getMaterialWasteNumRate()
{
return materialWasteNumRate;
}
public void setReworkNumRate(String reworkNumRate)
{
this.reworkNumRate = reworkNumRate;
}
public String getReworkNumRate()
{
return reworkNumRate;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("userId", getUserId())
.append("procedureId", getProcedureId())
.append("procedureName", getProcedureName())
.append("date", getDate())
.append("workNum", getWorkNum())
.append("passNum", getPassNum())
.append("timeWasteNum", getTimeWasteNum())
.append("materialWasteNum", getMaterialWasteNum())
.append("reworkNum", getReworkNum())
.append("workNumRate", getWorkNumRate())
.append("passNumRate", getPassNumRate())
.append("timeWasteNumRate", getTimeWasteNumRate())
.append("materialWasteNumRate", getMaterialWasteNumRate())
.append("reworkNum", getReworkNum())
.append("remark", getRemark())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.toString();
}
}
将数据库取出的部分日期list将日期作为主键存入map
Map<String, String> dateMap = new HashMap<>(); // dateMap key:日期(如:2021-11-12) value: 日期(如:2021-11-12)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (WorksheetStatistics statistics : list) //存入map方便下面循环判断该日期数据是否存在
{
String date = sdf.format(statistics.getDate());
dateMap.put(date, date);
}
循环list查看哪天数据缺失,补充哪天日期
String[] startDates = startDate.split("-"); //startDate格式 2021-11-12
String[] endDates = endDate.split("-"); //endDate格式 2021-11-12
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDateTemp = LocalDate.of(Integer.parseInt(startDates[0]), Integer.parseInt(startDates[1]), Integer.parseInt(startDates[2]));
LocalDate endDateTemp = LocalDate.of(Integer.parseInt(endDates[0]), Integer.parseInt(endDates[1]), Integer.parseInt(endDates[2]));
while(startDateTemp.isBefore(endDateTemp)||startDateTemp.isEqual(endDateTemp))
{ //按照指定的时间段,按照日期划分
String dateKey = startDateTemp.format(fmt);
if(StringUtils.isEmpty(dateMap.get(dateKey))) //数据库查询出的list日期没有的则添加上,添加数量都为0
{
WorksheetStatistics statistics = new WorksheetStatistics();
try{
statistics.setDate(sdf.parse(dateKey));
statistics.setWorkNum(0);
statistics.setPassNum(0);
statistics.setTimeWasteNum(0);
statistics.setMaterialWasteNum(0);
statistics.setReworkNum(0);
statistics.setWorkNumRate("0.00%");
statistics.setPassNumRate("0.00%");
statistics.setTimeWasteNumRate("0.00%");
statistics.setMaterialWasteNumRate("0.00%");
statistics.setReworkNumRate("0.00%");
list.add(statistics);
}catch (Exception e){
e.printStackTrace();
}
}
startDateTemp = startDateTemp.plusDays(1);
}
最最最关键的一步,将整个list按照实体类date日期排序:
//按照 WorksheetStatistics 中 date日期排序
Collections.sort(list, (a, b) -> a.getDate().compareTo(b.getDate()));
查看排序后的结果:
if (list.size() > 0) {
for (WorksheetStatistics statistics : list) //存入map方便下面循环判断该日期数据是否存在
{
System.out.println("date: " + sdf.format(statistics.getDate()) + " workNum:" + statistics.getWorkNum());
}
}