程序人生
忠告,项目经验
万事开头难,一开始做一件事很痛苦的话,这是很正常的,冷静下来,慢慢来就是了。慢就是快,现在耐心做了,搞懂了,以后基本就是重复现在的。我现在在写毕业设计,年前可能做不完了,但我坚信过几天会越来越快。
效率最高的做事方法:平静,冷静,用心,用心思考。由简入繁。不要一上来就假设那种最难的情况,从最简单,最常见的情况入手,搞清楚后,控制变量,提升难度。
先照猫画虎。最后,你总能发现真理,原理,不一样的东西,然后感叹:原来如此。
//管理员首页数据统计
@RequestMapping("/admin/indexData")
@ResponseBody
public Message getIndexCount() {
Integer userCount = userService.getGeneralUserCount(2);
Integer petCount = petService.findAllPets().size();
Integer cmmdCount = commodityService.findAllCmmdCount();
Integer actCount = activityService.findAllActivities().size();
List<AdoptCount> allCountResult = applyAdoptService.getAllAdoptCounts(); //总领养申请列表
List<AdoptCount> agreeCountsResult = applyAdoptService.getAgreeCounts(); //当前领养列表
List<Long> allCounts = new ArrayList<>(); //领养申请总数列表
List<Long> agreeCounts = new ArrayList<>(); //当前正领养数量列表
List<String> labels = new ArrayList<>();
for (int i = 0; i < allCountResult.size(); i++) { //遍历所有的adoptCount对象
AdoptCount allCount = allCountResult.get(i); //获取每一个adoptCount对象
AdoptCount agreeCount = null;
allCounts.add(allCount.getCount()); //将每一个adoptCount对象的count属性添加到allCounts列表中
if (i < agreeCountsResult.size()) //agreeCount数量必定≤allCount数量
agreeCount = agreeCountsResult.get(i);
labels.add(allCount.getYear() + "-" + allCount.getMonth());
if (agreeCount == null //如果当前agreeCount为空,或者年月与allCount中的不相同,则:
|| allCount.getYear().intValue() != agreeCount.getYear().intValue()
|| allCount.getMonth().intValue() != agreeCount.getMonth().intValue()
) {
agreeCounts.add(0L); //agreeCounts追加0,确保allCount与agreeCount一一对应
agreeCountsResult.add(i, null); //agreeCountsResult追加null,确保allCountResult与agreeCountResult一一对应
} else {
agreeCounts.add(agreeCount.getCount()); //将每一个agreeCount对象的count属性添加到agreeCounts列表中
}
}
return Message.success()
.add("userCount", userCount)
.add("petCount", petCount)
.add("cmmdCount", cmmdCount)
.add("actCount", actCount)
.add("allCounts", allCounts)
.add("agreeCounts", agreeCounts)
.add("labels", labels);
}
<resultMap id="AdoptCountResultMap" type="cn.edu.zisu.entity.AdoptCount">
<result column="cnt" jdbcType="BIGINT" property="count"/>
<result column="y" jdbcType="INTEGER" property="year"/>
<result column="m" jdbcType="INTEGER" property="month"/>
</resultMap>
<!-- year(),month()是MySQL的函数,用于提取日期字段的年,月。不是随便写的-->
<select id="selectAllAdoptCount" resultMap="AdoptCountResultMap">
select count(*) cnt,year(taa.apply_time) y, month(taa.apply_time) m
from tb_apply_adopt taa
group by year(taa.apply_time), month(taa.apply_time)
order by year(taa.apply_time) desc, month(taa.apply_time) desc
limit 0,5
</select>
<select id="selectAgreeAdoptCount" resultMap="AdoptCountResultMap">
select count(*) cnt,year(taa.apply_time) y, month(taa.apply_time) m
from tb_apply_adopt taa
where taa.check_state = 1
group by year(taa.apply_time), month(taa.apply_time)
order by year(taa.apply_time) desc, month(taa.apply_time) desc
limit 0,5
</select>
package cn.edu.zisu.entity;
public class AdoptCount {
private Long count;
private Integer year;
private Integer month;
@Override
public String toString() {
return "MsgCount{" +
"count=" + count +
", year=" + year +
", month=" + month +
'}';
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
public Integer getMonth() {
return month;
}
public void setMonth(Integer month) {
this.month = month;
}
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
}