package handler import ( "cicv-data-closedloop/amd64/web_server/entity" "cicv-data-closedloop/common/config/c_db" "cicv-data-closedloop/common/config/c_log" commonEntity "cicv-data-closedloop/common/entity" "cicv-data-closedloop/common/util" "github.com/gin-gonic/gin" "net/http" "time" ) // 考试开始时间 func Begin(c *gin.Context) { param := new(entity.ExamPAO) // 映射到结构体 if err := c.ShouldBindJSON(¶m); err != nil { c_log.GlobalLogger.Error("项目启动接收请求参数报错:", err) c.JSON(http.StatusBadRequest, commonEntity.Response{ Code: 500, Msg: "请求体解析失败。", }) return } // 插入到数据库 sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["exam-insert-begin_time-by-team_name.sql"]) if err := c_db.DoTx(sqlTemplate, []any{ param.TeamName, time.Now(), }); err != nil { c_log.GlobalLogger.Error("插入数据报错:", err) c.JSON(http.StatusBadRequest, commonEntity.Response{ Code: 500, Msg: "插入数据报错。", }) return } c.JSON(http.StatusOK, commonEntity.Response{ Code: 200, Msg: "插入数据成功。", }) } // 考试结束时间 func End(c *gin.Context) { param := new(entity.ExamPAO) // 映射到结构体 if err := c.ShouldBindJSON(¶m); err != nil { c_log.GlobalLogger.Error("项目启动接收请求参数报错:", err) c.JSON(http.StatusBadRequest, commonEntity.Response{ Code: 500, Msg: "请求体解析失败。", }) return } // 1 查询指定队伍的开始时间最新的考试是否有结束时间,如果有则不在处理,如果没有则更新 var result entity.ExamPO selectSql, err := util.ReadFile(c_db.SqlFilesMap["exam-select-latest-by-team_name.sql"]) if err != nil { c_log.GlobalLogger.Error("读取sql文件报错:", err) c.JSON(http.StatusBadRequest, commonEntity.Response{ Code: 500, Msg: "读取sql文件报错。", }) return } // 可以传参数 if err = c_db.MysqlDb.Select(&result, selectSql, param.TeamName); err != nil { c_log.GlobalLogger.Error("数据库查询报错:", err) c.JSON(http.StatusBadRequest, commonEntity.Response{ Code: 500, Msg: "数据库查询报错。", }) return } c_log.GlobalLogger.Info("数据库查询成功:", result) if !result.EndTime.IsZero() { c_log.GlobalLogger.Error("赛队", param.TeamName, "重复请求考试结束接口!") c.JSON(http.StatusBadRequest, commonEntity.Response{ Code: 500, Msg: "重复请求。", }) return } // 更新到数据库 sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["exam-update-end_time-by-team_name.sql"]) if err := c_db.DoTx(sqlTemplate, []any{ time.Now(), param.TeamName, }); err != nil { c_log.GlobalLogger.Error("插入数据报错:", err) c.JSON(http.StatusBadRequest, commonEntity.Response{ Code: 500, Msg: "插入数据报错。", }) return } c.JSON(http.StatusOK, commonEntity.Response{ Code: 200, Msg: "插入数据成功。", }) }