实现添加每周任务

news/2025/3/20 23:16:57/文章来源:https://www.cnblogs.com/qiixunlu/p/18784174

1.后端Spring
WeeklyGoalController

点击查看代码
package com.example.demo.controller;import com.example.demo.entity.WeeklyGoal;
import com.example.demo.service.WeeklyGoalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.List;
import java.util.Map;@Api(tags = "每周学习目标")
@RestController
@RequestMapping("/weekly-goal")
public class WeeklyGoalController {@Autowiredprivate WeeklyGoalService weeklyGoalService;@ApiOperation("创建每周目标")@PostMapping("/create")public Map<String, Object> createGoal(@RequestBody WeeklyGoal weeklyGoal) {Map<String, Object> result = new HashMap<>();try {WeeklyGoal created = weeklyGoalService.createWeeklyGoal(weeklyGoal);result.put("success", true);result.put("message", "创建成功");result.put("data", created);} catch (Exception e) {result.put("success", false);result.put("message", "创建失败:" + e.getMessage());}return result;}@ApiOperation("获取本周目标")@GetMapping("/current/{userId}")public Map<String, Object> getCurrentWeekGoals(@PathVariable String userId) {Map<String, Object> result = new HashMap<>();try {List<WeeklyGoal> goals = weeklyGoalService.getCurrentWeekGoals(userId);result.put("success", true);result.put("data", goals);} catch (Exception e) {result.put("success", false);result.put("message", "获取失败:" + e.getMessage());}return result;}@ApiOperation("更新目标状态")@PutMapping("/status")public Map<String, Object> updateStatus(@RequestParam Integer goalId,@RequestParam Integer status) {Map<String, Object> result = new HashMap<>();try {boolean updated = weeklyGoalService.updateGoalStatus(goalId, status);result.put("success", updated);result.put("message", updated ? "更新成功" : "更新失败");} catch (Exception e) {result.put("success", false);result.put("message", "更新失败:" + e.getMessage());}return result;}
}
WeeklyGoal
点击查看代码
package com.example.demo.entity;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import java.util.Date;@Data
@ApiModel(value="WeeklyGoal对象", description="每周学习目标")
public class WeeklyGoal {@TableId(type = IdType.AUTO)@ApiModelProperty(value = "目标ID")private Integer id;@ApiModelProperty(value = "用户学号")private String userId;@ApiModelProperty(value = "目标内容")private String content;@JsonFormat(pattern = "yyyy-MM-dd")@ApiModelProperty(value = "周开始日期")private Date weekStart;@JsonFormat(pattern = "yyyy-MM-dd")@ApiModelProperty(value = "周结束日期")private Date weekEnd;@ApiModelProperty(value = "完成状态:0-未完成,1-已完成")private Integer status;@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")@ApiModelProperty(value = "创建时间")private Date createTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Date getWeekStart() {return weekStart;}public void setWeekStart(Date weekStart) {this.weekStart = weekStart;}public Date getWeekEnd() {return weekEnd;}public void setWeekEnd(Date weekEnd) {this.weekEnd = weekEnd;}public Integer getStatus() {return status;}public void setStatus(Integer status) {this.status = status;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}
}
WeeklyGoalMapper
点击查看代码
package com.example.demo.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.WeeklyGoal;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface WeeklyGoalMapper extends BaseMapper<WeeklyGoal> {
}

WeeklyGoalServiceImpl

点击查看代码
package com.example.demo.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.WeeklyGoal;
import com.example.demo.mapper.WeeklyGoalMapper;
import com.example.demo.service.WeeklyGoalService;
import org.springframework.stereotype.Service;import java.util.Calendar;
import java.util.Date;
import java.util.List;@Service
public class WeeklyGoalServiceImpl extends ServiceImpl<WeeklyGoalMapper, WeeklyGoal> implements WeeklyGoalService {@Overridepublic WeeklyGoal createWeeklyGoal(WeeklyGoal weeklyGoal) {// 设置本周的开始和结束日期Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);weeklyGoal.setWeekStart(calendar.getTime());calendar.add(Calendar.DATE, 6);weeklyGoal.setWeekEnd(calendar.getTime());weeklyGoal.setStatus(0);this.save(weeklyGoal);return weeklyGoal;}@Overridepublic List<WeeklyGoal> getCurrentWeekGoals(String userId) {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);Date weekStart = calendar.getTime();calendar.add(Calendar.DATE, 6);Date weekEnd = calendar.getTime();QueryWrapper<WeeklyGoal> queryWrapper = new QueryWrapper<>();queryWrapper.eq("user_id", userId).ge("week_start", weekStart).le("week_end", weekEnd).orderByDesc("create_time");return this.list(queryWrapper);}@Overridepublic boolean updateGoalStatus(Integer goalId, Integer status) {WeeklyGoal weeklyGoal = new WeeklyGoal();weeklyGoal.setId(goalId);weeklyGoal.setStatus(status);return this.updateById(weeklyGoal);}
}
WeeklyGoalService
点击查看代码
package com.example.demo.service;import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.WeeklyGoal;import java.util.List;public interface WeeklyGoalService extends IService<WeeklyGoal> {WeeklyGoal createWeeklyGoal(WeeklyGoal weeklyGoal);List<WeeklyGoal> getCurrentWeekGoals(String userId);boolean updateGoalStatus(Integer goalId, Integer status);
}
2.安卓前端 WeeklyGoalAdapter
点击查看代码
package com.qi.demo.adapterimport android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.qi.demo.R
import com.qi.demo.entity.WeeklyGoal
import java.text.SimpleDateFormat
import java.util.*class WeeklyGoalAdapter(private val goals: MutableList<WeeklyGoal>,private val onStatusChanged: (WeeklyGoal, Boolean) -> Unit
) : RecyclerView.Adapter<WeeklyGoalAdapter.ViewHolder>() {class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {val contentText: TextView = view.findViewById(R.id.contentText)val statusCheckBox: CheckBox = view.findViewById(R.id.statusCheckBox)val dateText: TextView = view.findViewById(R.id.dateText)}override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {val view = LayoutInflater.from(parent.context).inflate(R.layout.item_weekly_goal, parent, false)return ViewHolder(view)}override fun onBindViewHolder(holder: ViewHolder, position: Int) {val goal = goals[position]holder.contentText.text = goal.contentholder.statusCheckBox.isChecked = goal.status == 1val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())val dateRange = "${dateFormat.format(goal.weekStart)} 至 ${dateFormat.format(goal.weekEnd)}"holder.dateText.text = dateRangeholder.statusCheckBox.setOnCheckedChangeListener { _, isChecked ->onStatusChanged(goal, isChecked)}}override fun getItemCount() = goals.sizefun updateGoals(newGoals: List<WeeklyGoal>) {goals.clear()goals.addAll(newGoals)notifyDataSetChanged()}
}
WeeklyGoal
点击查看代码
package com.qi.demo.entityimport java.util.Datedata class WeeklyGoal(val id: Int? = null,val userId: String,val content: String,val weekStart: Date? = null,val weekEnd: Date? = null,var status: Int = 0,val createTime: Date? = null
)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/902268.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ollama系列05:Ollama API 使用指南

本文是Ollama系列教程的第5篇,在前面的4篇内容中,给大家分享了如何再本地通过Ollama运行DeepSeek等大模型,演示了chatbox、CherryStudio等UI界面中集成Ollama的服务,并介绍了如何通过cherryStudio构建私有知识库。 在今天的分享中,我将分享如何通过API来调用ollama服务,通…

前端HTML+CSS+JS速成笔记

HTML 超文本标记语言。 单标签与双标签的区别 单标签用于没有内容的元素,双标签用于有内容的元素。 HTML文件结构 告诉浏览器这还是一个 Html 文件: <!DOCTYPE html>Html文件的范围: <html>...</html>Html 文件的头: <head>...</head>实际显…

12. ADC

一、ADC简介生活中接触到的大多数信息是醉着时间连续变化的物理量,如声音、温度、压力等。表达这些信息的电信号,称为 模拟信号(Analog Signal)。为了方便存储、处理,在计算机系统中,都是数字 0 和 1 信号,将模拟信号(连续信号)转换为数字信号(离散信号)的器件就叫模…

【刷题笔记】力扣 40. 组合总和 II——回溯算法中的去重

40. 组合总和 II 中等 给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用 一次 。 注意:解集不能包含重复的组合。 示例 1: 输入: candidates = [10,1,2,7,6,1,…

Spring AI 搭建AI原生应用 [clone]

作者 | 文心智能体平台导读 本文以快速开发一个 AI 原生应用为目的,介绍了 Spring AI 的包括对话模型、提示词模板、Function Calling、结构化输出、图片生成、向量化、向量数据库等全部核心功能,并介绍了检索增强生成的技术。依赖 Spring AI 提供的功能,我们可以轻松开发出…

mybatis逆向工程插件配置(mybatis-generator-maven-plugin)

MyBatis逆向工程是一种自动化工具,可以将数据库表结构转换为MyBatis的Mapper XML文件和相应的Java接口和对应的实体类。 1.生成maven项目 2.pom.xml文件中导入逆向工程插件相关配置<!--mybatis逆向工程--><build><plugins><!--其中的一个插件,逆向工程插…

Day01-Java项目学习

Day01 后端环境搭建 lombok插件 通过lombok插件。@Data 可以使用@Data、@Getter、@Setter等注解的形式,来对一个Java Bean(具有Getter,Setter方法的对象)实现快速的初始化。 @Slf4j 可以以注解的形式,自动化日志变量,通过添加@Slf4j(simple logging Facade for Java)生成…

20241105 实验一 《Python程序设计》

课程:《Python程序设计》 班级: 2411 姓名: 王梓墨 学号:20241105 实验教师:王志强 实验日期:2025年3月12日 必修/选修: 公选课 一.实验内容 1.熟悉Python开发环境; 2.练习Python运行、调试技能;(编写书中的程序,并进行调试分析) 3.编写程序,练习变量和类型、字…

英语四级备考第二天

第二天 今天是开始英语备考的第二天,当迈出第二步的时候,就意味着正走在通过考试的路上。到时当你以425分毋庸置疑地通过考试时,过去的90天都不曾虚度。 单词 今天新学的单词加上昨天应复习的单词,在50~60个之间。阅读 今天的阅读还是用扇贝单词推荐的包含学习的单词的文章…

投资日记_道氏理论技术分析

主要用于我自己参考,我感觉我做事情的时候容易上头,忘掉很多事情。技术分析有很多方法,但是我个人相信并实践的还是以道氏理论为根本的方法。方法千千万万只有适合自己价值观,习惯,情绪,性格的方法才是好的方法。 趋势 趋势是技术分析的根本,要是连当前趋势都看不懂,最…

asp.net core webapi 完整Swagger配置

在当前项目下新建Utility文件夹,Utility文件夹下面在创建SwaggerExt文件夹,文档结果如下 CustomSwaggerExt.cs文件如下using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models;namespace xxxxxxxxxx {/// <summary>/// 扩展Swagger/// </summary>pub…