You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.1 KiB
60 lines
2.1 KiB
package memory
|
|
|
|
import (
|
|
"yunyan/comm"
|
|
"yunyan/pb"
|
|
)
|
|
|
|
// Add 新增记忆项
|
|
// @Summary 新增记忆项
|
|
// @Description 新增一条拾忆条目;client_key 非空时做幂等,重复提交返回既有记录
|
|
// @Tags Memory
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param data body pb.MemoryAddReq true "请求数据"
|
|
// @Success 200 {object} comm.HttpResult{data=pb.MemoryAddResp} "响应数据"
|
|
// @Router /api/home/memory_add [post]
|
|
func (this *apiComp) Add(session comm.IUserSession, req *pb.MemoryAddReq) (resp *pb.MemoryAddResp, errdata *pb.ErrorData) {
|
|
uid, errdata := requireUID(session)
|
|
if errdata != nil {
|
|
return
|
|
}
|
|
item := req.Item
|
|
if errdata = normalizeItem(uid, item); errdata != nil {
|
|
return
|
|
}
|
|
// 手动新增的东西天然算「用户编辑过」,会议重新生成时不该被清掉
|
|
if item.Source == comm.MemorySrcManual {
|
|
item.UserEdited = true
|
|
}
|
|
// 这几个字段只由服务端决定,客户端传什么都不作数
|
|
item.Id = 0
|
|
item.GenRound = 0
|
|
item.FinishTime = 0
|
|
|
|
// 幂等:端侧指令会重试,客户端本地那套按指令 id 去重只在本进程内存里,
|
|
// 换机/重装/重启后全失效,服务端必须自己挡一道。
|
|
if exist, err := this.module.model.findItemByClientKey(uid, item.ClientKey); err != nil {
|
|
this.module.Errorf("memory_add uid:%s 幂等查询失败 err:%v", uid, err)
|
|
errdata = &pb.ErrorData{Code: pb.ErrorCode_DBError, Message: "新增失败"}
|
|
return
|
|
} else if exist != nil {
|
|
resp = &pb.MemoryAddResp{Item: exist, Duplicated: true}
|
|
return
|
|
}
|
|
|
|
this.module.recalcRemind(item)
|
|
if err := this.module.model.addItem(item); err != nil {
|
|
// 唯一索引冲突 = 并发下的重复提交,回查一次当成幂等命中,不要报错给用户
|
|
if exist, e := this.module.model.findItemByClientKey(uid, item.ClientKey); e == nil && exist != nil {
|
|
resp = &pb.MemoryAddResp{Item: exist, Duplicated: true}
|
|
return
|
|
}
|
|
this.module.Errorf("memory_add uid:%s err:%v", uid, err)
|
|
errdata = &pb.ErrorData{Code: pb.ErrorCode_DBError, Message: "新增失败"}
|
|
return
|
|
}
|
|
resp = &pb.MemoryAddResp{Item: item}
|
|
return
|
|
}
|
|
|