实现添加每周任务
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 {
@Autowired
private 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;
}
}
点击查看代码
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;
}
}
点击查看代码
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 {
@Override
public 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;
}
@Override
public 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);
}
@Override
public boolean updateGoalStatus(Integer goalId, Integer status) {
WeeklyGoal weeklyGoal = new WeeklyGoal();
weeklyGoal.setId(goalId);
weeklyGoal.setStatus(status);
return this.updateById(weeklyGoal);
}
}
点击查看代码
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);
}
点击查看代码
package com.qi.demo.adapter
import 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.content
holder.statusCheckBox.isChecked = goal.status == 1
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val dateRange = "${dateFormat.format(goal.weekStart)} 至 ${dateFormat.format(goal.weekEnd)}"
holder.dateText.text = dateRange
holder.statusCheckBox.setOnCheckedChangeListener { _, isChecked ->
onStatusChanged(goal, isChecked)
}
}
override fun getItemCount() = goals.size
fun updateGoals(newGoals: List<WeeklyGoal>) {
goals.clear()
goals.addAll(newGoals)
notifyDataSetChanged()
}
}
点击查看代码
package com.qi.demo.entity
import java.util.Date
data 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
)
浙公网安备 33010602011771号