后端开发文档
文章目录
1. 用户每日打卡
设计思路
用户每日打卡功能允许用户记录每日的心情、活动等信息。每条打卡记录包含用户ID、文本内容、时间、图片和分析字段。通过数据库插入操作,将用户的打卡信息存储到数据库中。
实现过程
-
数据模型定义:
@NotNull(message = "userId不能为空") private Integer userId; @NotNull(message = "text不能为空") @Size(max = 255, message = "text最大长度为255") private String text; @NotNull(message = "time不能为空") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private DateTime time; @NotNull(message = "images不能为空") private String images; private String analysis;
-
数据库插入操作:
<!-- 用户每日打卡 -->
<insert id="checkIn" parameterType="Map">
insert into check_in(user_id, text, time, images, analysis)
values (#{userId}, #{text}, #{time}, #{images}, #{analysis})
</insert>
这样实现的好处
- 数据完整性:通过注解确保所有必填字段不为空,保证数据的完整性。
- 灵活性:支持用户输入文本、图片和时间,满足多样化的打卡需求。
- 扩展性:可以轻松添加新的字段或功能,如分析字段。
POST 用户每日打卡
POST /user/checkIn
用户每日打卡
Body 请求参数
{
"userId": 5,
"text": "今天打卡测试",
"time": "2024-05-24 08:16:46",
"images": "https://lhplanet-1316168555.cos.ap-beijing.myqcloud.com/shanyi/development-documentation/development-documentation-v3.0.assets/202405241119574.png",
"analysis": ""
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
---|---|---|---|---|
token | header | string | 否 | none |
body | body | object | 否 | none |
» userId | body | integer | 是 | none |
» text | body | string | 是 | none |
» time | body | string | 是 | none |
» images | body | string | 是 | none |
返回示例
成功
{
"msg": "success",
"code": 200
}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
---|---|---|---|
200 | OK | 成功 | Inline |
返回数据结构
状态码 200
名称 | 类型 | 必选 | 约束 | 中文名 | 说明 |
---|---|---|---|---|---|
» code | integer | true | none | none | |
» msg | string | true | none | none |
2. 分页查询用户每日打卡
设计思路
分页查询功能允许用户按页查看打卡记录,并支持按用户ID、昵称、姓名、时间范围和排序方式进行筛选。
实现过程
-
数据模型定义:
@NotNull(message = "page不能为空") @Min(value = 1, message = "page最小值为1") private Integer page; @NotNull(message = "length不能为空") @Range(min = 1, max = 50, message = "length每页长度不正确,最小1,最大50") private Integer length; private Integer userId; private String nickName; private String name; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private DateTime startTime; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private DateTime endTime; @NotNull(message = "排序不能为空") private String order;
-
分页查询接口:
@PostMapping("/searchCheckInByPage") @SaCheckLogin public R searchCheckInByPage(@RequestBody @Valid searchCheckInByPageForm form) throws ParseException { Map param = BeanUtil.beanToMap(form); int page = form.getPage(); int length = form.getLength(); int start = (page - 1) * length; param.put("start", start); PageUtils pageUtils = userService.searchCheckInByPage(param); return R.ok().put("result", pageUtils); }
-
服务层实现:
@Override public PageUtils searchCheckInByPage(Map param) { ArrayList<HashMap> list = null; long count = userDao.countUserCheckIn(param); if (count > 0) { list = userDao.searchCheckInByPage(param); } else { list = new ArrayList<>(); } int page = MapUtil.getInt(param, "page"); int length = MapUtil.getInt(param, "length"); PageUtils pageUtils = new PageUtils(list, count, page, length); return pageUtils; }
-
数据库查询操作:
<!-- 查询用户打卡总数 -->
<select id="countUserCheckIn" parameterType="Map" resultType="long">
SELECT COUNT(*)
FROM check_in ci
JOIN (
SELECT u.id AS user_id
FROM user u
JOIN user_info ui ON u.id = ui.user_id
WHERE 1 = 1
<if test="userId != null and userId != ''">
AND u.id = #{userId}
</if>
<if test="nickName != null and nickName != ''">
AND u.nick_name LIKE CONCAT('%', #{nickName}, '%')
</if>
<if test="name != null and name != ''">
AND ui.name LIKE CONCAT('%', #{name}, '%')
</if>
) sub ON ci.user_id = sub.user_id
WHERE 1 = 1
<if test="startTime != null">
AND ci.time >= #{startTime}
</if>
<if test="endTime != null">
AND ci.time <= #{endTime}
</if>
</select>
<!-- 分页查询用户打卡记录 -->
<select id="searchCheckInByPage" parameterType="Map" resultType="HashMap">
SELECT u.nick_name AS nickName,
ui.name,
ci.id,
ci.user_id,
ci.time,
ci.text,
ci.images,
ci.analysis
FROM check_in ci
JOIN user u ON ci.user_id = u.id
JOIN user_info ui ON u.id = ui.user_id
WHERE 1 = 1
<if test="userId != null and userId != ''">
AND u.id = #{userId}
</if>
<if test="nickName != null and nickName != ''">
AND u.nick_name LIKE CONCAT('%', #{nickName}, '%')
</if>
<if test="name != null and name != ''">
AND ui.name LIKE CONCAT('%', #{name}, '%')
</if>
<if test="startTime != null">
AND ci.time >= #{startTime}
</if>
<if test="endTime != null">
AND ci.time <= #{endTime}
</if>
<if test="order == 'desc' ">
ORDER BY ci.time desc
</if>
<if test="order == 'asc' ">
ORDER BY ci.time asc
</if>
LIMIT #{length} OFFSET #{start}
</select>
这样实现的好处
- 高效查询:通过分页查询减少一次性查询的数据量,提高查询效率。
- 灵活筛选:支持多种筛选条件,满足用户的个性化需求。
- 排序功能:支持按时间升序或降序排序,方便用户查看。
POST 查询用户的打卡信息
POST /user/searchCheckInByPage
查询某一个关注的用户的打卡信息,可以按时间排序也可以默认由近到远,需要被关注的那个用户的id
Body 请求参数
{
"page": 1,
"length": 10,
"userId": 5,
"startTime": "",
"endTime": "",
"order": "desc"
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
---|---|---|---|---|
token | header | string | 否 | none |
body | body | object | 否 | none |
» page | body | integer | 是 | none |
» length | body | integer | 是 | 最小1,最大50 |
» userId | body | integer | 是 | 要查询的那个用户的id |
» startTime | body | string | 否 | none |
» endTime | body | string | 否 | none |
» order | body | string | 是 | 按照打卡时间排序,desc或者asc |
返回示例
成功
{
"msg": "success",
"result": {
"totalCount": 2,
"pageSize": 10,
"totalPage": 1,
"pageIndex": 1,
"list": [
{
"images": "https://thirdwx.qlogo.cn/mmopen/vi_32/6ibtnStKibXDUTibnu052w40ps0DKqcUZBYaAiberiasgSsTGXwCLAicmXng2gE48ZnibYrmnPIiaVlNWjpCYiby85ibgM8Q/132",
"user_id": 5,
"nickName": "绊象",
"name": "陈泽胜",
"time": "2024-07-11T18:40:02",
"text": "今天打卡",
"analysis": "暂无"
},
{
"images": "https://thirdwx.qlogo.cn/mmopen/vi_32/6ibtnStKibXDUTibnu052w40ps0DKqcUZBYaAiberiasgSsTGXwCLAicmXng2gE48ZnibYrmnPIiaVlNWjpCYiby85ibgM8Q/132",
"user_id": 5,
"nickName": "绊象",
"name": "陈泽胜",
"time": "2024-05-21T10:00:00",
"text": "今天打卡",
"analysis": "暂无"
}
]
},
"code": 200
}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
---|---|---|---|
200 | OK | 成功 | Inline |
返回数据结构
状态码 200
名称 | 类型 | 必选 | 约束 | 中文名 | 说明 |
---|---|---|---|---|---|
» msg | string | true | none | none | |
» result | object | true | none | none | |
»» totalCount | integer | true | none | none | |
»» pageSize | integer | true | none | none | |
»» totalPage | integer | true | none | none | |
»» pageIndex | integer | true | none | none | |
»» list | [object] | true | none | none | |
»»» images | string | true | none | none | |
»»» user_id | integer | true | none | none | |
»»» nickName | string | true | none | none | |
»»» name | string | true | none | none | |
»»» time | string | true | none | none | |
»»» text | string | true | none | none | |
»»» analysis | string | true | none | none | |
» code | integer | true | none | none |
3. 用户修改每日打卡信息
设计思路
用户可以修改已存在的打卡记录,包括文本内容、时间和图片。通过数据库更新操作,将修改后的信息保存到数据库中。
实现过程
-
数据模型定义:
@NotNull(message = "checkInId不能为空") private Integer checkInId; @NotNull(message = "text不能为空") @Size(max = 255, message = "text最大长度为255") private String text; @NotNull(message = "time不能为空") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private DateTime time; @NotNull(message = "images不能为空") private String images;
-
数据库更新操作:
<!-- 修改用户每日打卡信息 -->
<update id="updateCheckIn" parameterType="Map">
UPDATE check_in
SET text = #{text},
time = #{time},
images = #{images}
WHERE id = #{checkInId};
</update>
这样实现的好处
- 数据更新:允许用户修改已存在的打卡记录,保持数据的动态更新。
- 灵活性:支持修改文本、时间和图片,满足用户的多样化需求。
POST 用户修改每日打卡信息
POST /user/updateCheckIn
Body 请求参数
{
"checkInId": 1,
"text": "修改打卡信息",
"time": "2024-06-21 10:00:00",
"images": "http://dummyimage.com/300x300"
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
---|---|---|---|---|
token | header | string | 否 | none |
body | body | object | 否 | none |
» checkInId | body | integer | 是 | none |
» text | body | string | 是 | none |
» time | body | string | 是 | none |
» images | body | string | 是 | none |
返回示例
200 Response
{
"code": "string",
"msg": "string"
}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
---|---|---|---|
200 | OK | 成功 | Inline |
返回数据结构
状态码 200
名称 | 类型 | 必选 | 约束 | 中文名 | 说明 |
---|---|---|---|---|---|
» code | string | true | none | none | |
» msg | string | true | none | none |
4. 用户删除每日打卡信息
设计思路
用户可以删除已存在的打卡记录,通过数据库删除操作,将指定的打卡记录从数据库中移除。
实现过程
-
数据模型定义:
@NotNull(message = "checkInId不能为空") private Integer checkInId;
-
数据库删除操作:
<!-- 删除用户打卡信息 -->
<delete id="deleteCheckIn" parameterType="Map">
delete from check_in where id = #{checkInId}
</delete>
POST 用户删除每日打卡信息
POST /user/deleteCheckIn
Body 请求参数
{
"checkInId": 2
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
---|---|---|---|---|
token | header | string | 否 | none |
body | body | object | 否 | none |
» checkInId | body | integer | 是 | none |
返回示例
200 Response
{
"code": "string",
"msg": "string"
}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
---|---|---|---|
200 | OK | 成功 | Inline |
返回数据结构
状态码 200
名称 | 类型 | 必选 | 约束 | 中文名 | 说明 |
---|---|---|---|---|---|
» code | string | true | none | none | |
» msg | string | true | none | none |
五.查询用户总共打卡多少天
设计思路
该功能用于查询用户总共打卡的天数。通过统计用户在数据库中打卡记录的不同日期,计算出用户总共打卡了多少天。
实现过程
数据模型定义
@NotNull(message = "userId不能为空")
private Integer userId;
数据库查询操作
<select id="countMyCheckIn" parameterType="Map" resultType="java.lang.Long">
select count(distinct date(time)) from check_in
where user_id = #{userId}
</select>
这样实现的好处
- 准确统计:通过统计不同日期的打卡记录,准确计算用户总共打卡的天数。
- 高效查询:使用
distinct
和count
组合,确保查询效率和准确性。 - 简单易用:只需提供用户ID即可获取打卡天数,接口设计简洁明了。
POST 查询总共打卡多少天
POST /user/countMyCheckIn
查询自己总共打卡多少天
Body 请求参数
{
"userId": 5
}
请求参数
名称 | 位置 | 类型 | 必选 | 说明 |
---|---|---|---|---|
token | header | string | 否 | none |
body | body | object | 否 | none |
» userId | body | integer | 是 | none |
返回示例
成功
{
"msg": "success",
"result": 2,
"code": 200
}
返回结果
状态码 | 状态码含义 | 说明 | 数据模型 |
---|---|---|---|
200 | OK | 成功 | Inline |
返回数据结构
状态码 200
名称 | 类型 | 必选 | 约束 | 中文名 | 说明 |
---|---|---|---|---|---|
» code | integer | true | none | none | |
» msg | string | true | none | none | |
» result | integer | true | none | none |