h_exam.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. package handler
  2. import (
  3. webServerEntity "cicv-data-closedloop/amd64/web_server/entity"
  4. "cicv-data-closedloop/common/config/c_db"
  5. "cicv-data-closedloop/common/config/c_log"
  6. commonEntity "cicv-data-closedloop/common/entity"
  7. "cicv-data-closedloop/common/util"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "github.com/signintech/gopdf"
  11. "io"
  12. "log"
  13. "math"
  14. "net/http"
  15. "os"
  16. "strconv"
  17. "sync"
  18. "time"
  19. )
  20. var (
  21. defaultTime = time.Date(2006, time.January, 2, 15, 4, 5, 0, time.Local)
  22. cacheMutex sync.Mutex
  23. cacheTeamName = make(map[string]time.Time)
  24. heartBeatTimeThreshold = 5 * time.Second // 心跳时间
  25. InitialPositionX = 456292.00 // todo 需要比赛确认起点
  26. InitialPositionY = 4397957.00 // todo 需要比赛确认起点
  27. // todo 比赛阶段
  28. trialBegin = time.Date(2024, time.June, 16, 13, 00, 00, 0, time.Local)
  29. trialEnd = time.Date(2024, time.June, 19, 23, 59, 59, 0, time.Local)
  30. )
  31. // 考试心跳
  32. func Tick(c *gin.Context) {
  33. param := new(webServerEntity.ExamPao)
  34. // 映射到结构体
  35. if err := c.ShouldBindJSON(&param); err != nil {
  36. c_log.GlobalLogger.Error("请求体解析失败,错误信息为:", err)
  37. c.JSON(http.StatusBadRequest, commonEntity.Response{
  38. Code: 500,
  39. Msg: "请求体解析失败。",
  40. })
  41. return
  42. }
  43. teamName := param.TeamName
  44. positionX, _ := strconv.ParseFloat(param.PositionX, 64)
  45. var positionY float64
  46. numStr := param.PositionY
  47. _, _ = fmt.Sscanf(numStr, "%e", &positionY)
  48. if !util.ContainsKey(cacheTeamName, teamName) && math.Abs(positionX-InitialPositionX) < 5.00 && math.Abs(positionY-InitialPositionY) < 5.00 { // (在起点开始)
  49. sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["exam-insert-begin_time-and-topic-by-team_name.sql"])
  50. stage := ""
  51. if time.Now().Before(trialBegin) {
  52. stage = "表演赛"
  53. } else if time.Now().After(trialBegin) && time.Now().Before(trialEnd) {
  54. stage = "预赛"
  55. } else {
  56. stage = "决赛"
  57. }
  58. competitionBegin := time.Now()
  59. cacheTeamName[teamName] = competitionBegin
  60. c_log.GlobalLogger.Infof("当前比赛阶段为 %v ,队伍 %v 在起始点范围内第一次启动,保存新一条比赛记录的开始时间 %v", stage, teamName, competitionBegin)
  61. if err := c_db.DoTx(sqlTemplate, []any{param.TeamName, stage, competitionBegin}); err != nil {
  62. c_log.GlobalLogger.Error("保存比赛开始时间报错:", err)
  63. c.JSON(http.StatusBadRequest, commonEntity.Response{Code: 500, Msg: "保存比赛开始时间报错。"})
  64. return
  65. }
  66. } else if !util.ContainsKey(cacheTeamName, teamName) && (math.Abs(positionX-InitialPositionX) > 5.00 || math.Abs(positionY-InitialPositionY) > 5.00) { // 不在起点(开始)
  67. // 车辆不是在起点启动的自动驾驶模式
  68. selectSql, err := util.ReadFile(c_db.SqlFilesMap["exam-select-latest-by-team_name.sql"])
  69. if err != nil {
  70. c_log.GlobalLogger.Error("读取sql文件报错:", err)
  71. return
  72. }
  73. // 可以传参数
  74. var result []webServerEntity.ExamPo
  75. if err = c_db.MysqlDb.Select(&result, selectSql, teamName); err != nil {
  76. c_log.GlobalLogger.Error("数据库查询报错:", err)
  77. return
  78. }
  79. if len(result) == 1 {
  80. c_log.GlobalLogger.Info("上一条数据记录为:", result[0])
  81. }
  82. // 添加队伍名到缓存中
  83. cacheTeamName[teamName] = time.Now()
  84. // 更新记录结束时间为默认时间
  85. sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["exam-update-end_time-by-team_name.sql"])
  86. if err = c_db.DoTx(sqlTemplate, []any{
  87. defaultTime,
  88. teamName,
  89. }); err != nil {
  90. c_log.GlobalLogger.Error("插入数据报错:", err)
  91. return
  92. }
  93. c_log.GlobalLogger.Infof("队伍 %v 的考试在中途中断后重新开始。", teamName)
  94. } else if util.ContainsKey(cacheTeamName, teamName) { // 进行中
  95. cacheTeamName[teamName] = time.Now()
  96. c_log.GlobalLogger.Errorf("接收到心跳信息,队伍名字为 %s,x坐标为 %.f,y坐标为 %.f ", teamName, positionX, positionY)
  97. } else {
  98. c_log.GlobalLogger.Errorf("接收到心跳信息但考试并未开始,队伍名字为 %s,x坐标为 %.f,y坐标为 %.f ", teamName, positionX, positionY)
  99. }
  100. c.JSON(http.StatusOK, commonEntity.Response{
  101. Code: 200,
  102. Msg: "心跳接收成功。",
  103. })
  104. }
  105. func ExamEndTicker() {
  106. // 创建一个定时器,每隔一秒触发一次
  107. ticker := time.NewTicker(1 * time.Second)
  108. for {
  109. select {
  110. // 定时器触发时执行的代码
  111. case <-ticker.C:
  112. cacheMutex.Lock()
  113. {
  114. var keysToDelete []string
  115. for teamName, heartBeatTime := range cacheTeamName {
  116. if time.Since(heartBeatTime) > heartBeatTimeThreshold { // 检查缓存中的队名,如果超过心跳时间,则代表考试结束,删除缓存中的队名
  117. c_log.GlobalLogger.Infof("队伍 %v 心跳超时,比赛结束。", teamName)
  118. keysToDelete = append(keysToDelete, teamName)
  119. }
  120. }
  121. for _, teamName := range keysToDelete { // 检查缓存中的队名,如果超过心跳时间,则代表考试结束,删除缓存中的队名
  122. delete(cacheTeamName, teamName)
  123. // 1 查询指定队伍的开始时间最新的考试是否有结束时间,如果有则不在处理,如果没有则更新
  124. var result []webServerEntity.ExamPo
  125. selectSql, err := util.ReadFile(c_db.SqlFilesMap["exam-select-latest-by-team_name.sql"])
  126. if err != nil {
  127. c_log.GlobalLogger.Error("读取sql文件报错:", err)
  128. return
  129. }
  130. // 可以传参数
  131. if err = c_db.MysqlDb.Select(&result, selectSql, teamName); err != nil {
  132. c_log.GlobalLogger.Error("数据库查询报错:", err)
  133. return
  134. }
  135. if !result[0].EndTime.Equal(defaultTime) {
  136. c_log.GlobalLogger.Error("赛队", teamName, "考试已结束!")
  137. return
  138. }
  139. // 更新到数据库(只更新最新一条)
  140. stage := ""
  141. if time.Now().Before(trialBegin) {
  142. stage = "表演赛"
  143. } else if time.Now().After(trialBegin) && time.Now().Before(trialEnd) {
  144. stage = "预赛"
  145. } else {
  146. stage = "决赛"
  147. }
  148. // 查询最新一条的id
  149. selectSql, err = util.ReadFile(c_db.SqlFilesMap["exam-select-max-id-by-team_name-and-topic.sql"])
  150. if err != nil {
  151. c_log.GlobalLogger.Error("读取sql文件报错:", err)
  152. return
  153. }
  154. // 可以传参数
  155. var resultId []int
  156. if err = c_db.MysqlDb.Select(&result, selectSql, teamName, stage); err != nil {
  157. c_log.GlobalLogger.Error("数据库查询报错:", err)
  158. return
  159. }
  160. c_log.GlobalLogger.Info("更新数据结束时间,id为:", resultId[0])
  161. sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["exam-update-end_time-by-id.sql"])
  162. if err := c_db.DoTx(sqlTemplate, []any{
  163. time.Now(),
  164. resultId[0],
  165. }); err != nil {
  166. c_log.GlobalLogger.Error("插入数据报错:", err)
  167. return
  168. }
  169. c_log.GlobalLogger.Infof("队伍 %v 的考试结束。", teamName)
  170. }
  171. }
  172. cacheMutex.Unlock()
  173. }
  174. }
  175. }
  176. // 考试开始时间
  177. func Begin(c *gin.Context) {
  178. param := new(webServerEntity.ExamPao)
  179. // 映射到结构体
  180. if err := c.ShouldBindJSON(&param); err != nil {
  181. c_log.GlobalLogger.Error("项目启动接收请求参数报错:", err)
  182. c.JSON(http.StatusBadRequest, commonEntity.Response{
  183. Code: 500,
  184. Msg: "请求体解析失败。",
  185. })
  186. return
  187. }
  188. // 插入到数据库
  189. sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["exam-insert-begin_time-by-team_name.sql"])
  190. c_log.GlobalLogger.Info("插入比赛开始时间", sqlTemplate)
  191. if err := c_db.DoTx(sqlTemplate, []any{
  192. param.TeamName,
  193. time.Now(),
  194. }); err != nil {
  195. c_log.GlobalLogger.Error("插入数据报错:", err)
  196. c.JSON(http.StatusBadRequest, commonEntity.Response{
  197. Code: 500,
  198. Msg: "插入数据报错。",
  199. })
  200. return
  201. }
  202. c.JSON(http.StatusOK, commonEntity.Response{
  203. Code: 200,
  204. Msg: "插入数据成功。",
  205. })
  206. }
  207. // 考试结束时间
  208. func End(c *gin.Context) {
  209. param := new(webServerEntity.ExamPao)
  210. // 映射到结构体
  211. if err := c.ShouldBindJSON(&param); err != nil {
  212. c_log.GlobalLogger.Error("项目启动接收请求参数报错:", err)
  213. c.JSON(http.StatusBadRequest, commonEntity.Response{
  214. Code: 500,
  215. Msg: "请求体解析失败。",
  216. })
  217. return
  218. }
  219. // 1 查询指定队伍的开始时间最新的考试是否有结束时间,如果有则不在处理,如果没有则更新
  220. var result []webServerEntity.ExamPo
  221. selectSql, err := util.ReadFile(c_db.SqlFilesMap["exam-select-latest-by-team_name.sql"])
  222. if err != nil {
  223. c_log.GlobalLogger.Error("读取sql文件报错:", err)
  224. c.JSON(http.StatusBadRequest, commonEntity.Response{
  225. Code: 500,
  226. Msg: "读取sql文件报错。",
  227. })
  228. return
  229. }
  230. // 可以传参数
  231. if err = c_db.MysqlDb.Select(&result, selectSql, param.TeamName); err != nil {
  232. c_log.GlobalLogger.Error("数据库查询报错:", err)
  233. c.JSON(http.StatusBadRequest, commonEntity.Response{
  234. Code: 500,
  235. Msg: "数据库查询报错。",
  236. })
  237. return
  238. }
  239. c_log.GlobalLogger.Info("数据库查询成功:", result)
  240. if !result[0].EndTime.Equal(defaultTime) {
  241. c_log.GlobalLogger.Error("赛队", param.TeamName, "重复请求考试结束接口!")
  242. c.JSON(http.StatusBadRequest, commonEntity.Response{
  243. Code: 500,
  244. Msg: "重复请求。",
  245. })
  246. return
  247. }
  248. // 更新到数据库
  249. sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["exam-update-end_time-by-team_name.sql"])
  250. if err := c_db.DoTx(sqlTemplate, []any{
  251. time.Now(),
  252. param.TeamName,
  253. }); err != nil {
  254. c_log.GlobalLogger.Error("插入数据报错:", err)
  255. c.JSON(http.StatusBadRequest, commonEntity.Response{
  256. Code: 500,
  257. Msg: "插入数据报错。",
  258. })
  259. return
  260. }
  261. c.JSON(http.StatusOK, commonEntity.Response{
  262. Code: 200,
  263. Msg: "插入数据成功。",
  264. })
  265. }
  266. // 分页查询
  267. // todo 如果日期为默认值,则返回空""
  268. func Page(c *gin.Context) {
  269. param := new(webServerEntity.ExamPagePao)
  270. _ = c.ShouldBindJSON(&param)
  271. var resultPos []webServerEntity.ExamPo
  272. var resultPosTotal []int
  273. var pageSql string
  274. var totalSql string
  275. offset := (param.CurrentPage - 1) * param.PageSize
  276. size := param.PageSize
  277. if param.TeamName == "" && param.Topic == "" {
  278. pageSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-page.sql"])
  279. totalSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-total.sql"])
  280. err := c_db.MysqlDb.Select(&resultPos, pageSql, offset, size)
  281. if err != nil {
  282. c_log.GlobalLogger.Error(err)
  283. }
  284. err = c_db.MysqlDb.Select(&resultPosTotal, totalSql)
  285. if err != nil {
  286. c_log.GlobalLogger.Error(err)
  287. }
  288. }
  289. if param.TeamName != "" && param.Topic == "" {
  290. pageSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-page-by-team_name.sql"])
  291. totalSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-total-by-team_name.sql"])
  292. _ = c_db.MysqlDb.Select(&resultPos, pageSql, "%"+param.TeamName+"%", offset, size)
  293. _ = c_db.MysqlDb.Select(&resultPosTotal, totalSql, "%"+param.TeamName+"%")
  294. }
  295. if param.TeamName == "" && param.Topic != "" {
  296. pageSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-page-by-topic.sql"])
  297. totalSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-total-by-topic.sql"])
  298. _ = c_db.MysqlDb.Select(&resultPos, pageSql, "%"+param.Topic+"%", offset, size)
  299. _ = c_db.MysqlDb.Select(&resultPosTotal, totalSql, "%"+param.Topic+"%")
  300. }
  301. if param.TeamName != "" && param.Topic != "" {
  302. pageSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-page-by-team_name-and-topic.sql"])
  303. totalSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-total-by-team_name-and-topic.sql"])
  304. _ = c_db.MysqlDb.Select(&resultPos, pageSql, "%"+param.TeamName+"%", "%"+param.Topic+"%", offset, size)
  305. _ = c_db.MysqlDb.Select(&resultPosTotal, totalSql, "%"+param.TeamName+"%", "%"+param.Topic+"%")
  306. }
  307. var resultVos []webServerEntity.ExamVo
  308. for _, po := range resultPos {
  309. resultVos = append(resultVos, webServerEntity.ExamVo{
  310. Id: po.Id,
  311. TeamName: po.TeamName,
  312. Topic: po.Topic,
  313. BeginTime: util.GetTimeString(po.BeginTime),
  314. EndTime: util.GetTimeString(po.EndTime),
  315. ScoreOnline: po.ScoreOnline,
  316. ScoreOffline: po.ScoreOffline,
  317. ScoreFinal: po.ScoreFinal,
  318. Details: po.Details,
  319. ScoreReportPath: po.ScoreReportPath,
  320. })
  321. }
  322. c.JSON(http.StatusOK, commonEntity.Response{
  323. Code: 200,
  324. Msg: "分页查询成功!",
  325. Data: resultVos,
  326. Total: resultPosTotal[0],
  327. })
  328. }
  329. // 评分报告pdf下载
  330. func Report(c *gin.Context) {
  331. param := new(webServerEntity.ExamReportPao)
  332. // 映射到结构体
  333. if err := c.ShouldBindJSON(&param); err != nil {
  334. c_log.GlobalLogger.Error("接收请求参数报错:", err)
  335. c.JSON(http.StatusBadRequest, commonEntity.Response{
  336. Code: 500,
  337. Msg: "请求体解析失败。",
  338. })
  339. return
  340. }
  341. layout := "2006-01-02 15:04:05"
  342. beginTime, _ := time.Parse(layout, param.BeginTime)
  343. endTime, _ := time.Parse(layout, param.EndTime)
  344. // 1 根据ID查询数据
  345. // 2 根据数据生成pdf
  346. // 3 创建pdf文件
  347. {
  348. // 初始化 pdf 对象
  349. pdf := gopdf.GoPdf{}
  350. pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
  351. // 添加字体文件到pdf
  352. err := pdf.AddTTFFont("simfang", "D:\\code\\cicv-data-closedloop\\amd64\\web_server\\simfang.ttf")
  353. if err != nil {
  354. log.Print(err.Error())
  355. return
  356. }
  357. leftMargin := 70.0
  358. topMargin := 100.0
  359. lineHeight := 25.0
  360. tableWidth := 450.0
  361. pdf.SetLeftMargin(leftMargin) // 设置左边距
  362. alignOptionText := gopdf.CellOption{Align: gopdf.Left | gopdf.Middle}
  363. //alignOptionText2 := gopdf.CellOption{Align: gopdf.Center | gopdf.Middle}
  364. alignOptionTable := gopdf.CellOption{Align: gopdf.Center | gopdf.Middle, Border: gopdf.Left | gopdf.Right | gopdf.Bottom | gopdf.Top}
  365. alignOptionTable2 := gopdf.CellOption{Align: gopdf.Left | gopdf.Middle}
  366. // ------- 封面页 -------
  367. pdf.AddPage()
  368. {
  369. err = pdf.SetFont("simfang", "", 36)
  370. err = pdf.Image("D:\\code\\cicv-data-closedloop\\amd64\\web_server\\background.png", 0, 0, nil) // 背景图片
  371. err = pdf.Image("D:\\code\\cicv-data-closedloop\\amd64\\web_server\\logo.png", 20, 20, &gopdf.Rect{W: 320, H: 100})
  372. {
  373. pdf.SetXY(100, 250)
  374. _ = pdf.Text("车路云一体化算法挑战赛")
  375. }
  376. {
  377. pdf.SetXY(220, 315) // 200
  378. _ = pdf.Text("评分报告")
  379. }
  380. _ = pdf.SetFont("simfang", "", 14)
  381. currentLine := 0.0
  382. tableCellWidth := 100.0
  383. tableLeftMartin := 190.0
  384. tableTopMartin := 500.0
  385. {
  386. {
  387. pdf.SetXY(tableLeftMartin, tableTopMartin+currentLine*lineHeight)
  388. _ = pdf.CellWithOption(&gopdf.Rect{W: 100, H: lineHeight}, "赛队编号:", alignOptionTable2)
  389. }
  390. {
  391. pdf.SetXY(tableLeftMartin+1.0*tableCellWidth, tableTopMartin+currentLine*lineHeight)
  392. _ = pdf.CellWithOption(&gopdf.Rect{W: 100, H: lineHeight}, param.TeamName, alignOptionTable2)
  393. }
  394. currentLine++
  395. }
  396. {
  397. {
  398. pdf.SetXY(tableLeftMartin, tableTopMartin+currentLine*lineHeight)
  399. _ = pdf.CellWithOption(&gopdf.Rect{W: 100, H: lineHeight}, "评测车型:", alignOptionTable2)
  400. }
  401. {
  402. pdf.SetXY(tableLeftMartin+1.0*tableCellWidth, tableTopMartin+currentLine*lineHeight)
  403. _ = pdf.CellWithOption(&gopdf.Rect{W: 100, H: lineHeight}, "多功能车平台", alignOptionTable2)
  404. }
  405. currentLine++
  406. }
  407. {
  408. {
  409. pdf.SetXY(tableLeftMartin, tableTopMartin+currentLine*lineHeight)
  410. _ = pdf.CellWithOption(&gopdf.Rect{W: 100, H: lineHeight}, "评测地点:", alignOptionTable2)
  411. }
  412. {
  413. pdf.SetXY(tableLeftMartin+1.0*tableCellWidth, tableTopMartin+currentLine*lineHeight)
  414. _ = pdf.CellWithOption(&gopdf.Rect{W: 100, H: lineHeight}, "天津生态城", alignOptionTable2)
  415. }
  416. currentLine++
  417. }
  418. {
  419. {
  420. pdf.SetXY(tableLeftMartin, tableTopMartin+currentLine*lineHeight)
  421. _ = pdf.CellWithOption(&gopdf.Rect{W: 100, H: lineHeight}, "报告时间:", alignOptionTable2)
  422. }
  423. {
  424. pdf.SetXY(tableLeftMartin+1.0*tableCellWidth, tableTopMartin+currentLine*lineHeight)
  425. _ = pdf.CellWithOption(&gopdf.Rect{W: 100, H: lineHeight}, time.Now().Format("2006年01月02日"), alignOptionTable2)
  426. }
  427. currentLine++
  428. }
  429. {
  430. pdf.SetXY(165, 775)
  431. _ = pdf.Cell(nil, "国汽(北京)智能网联汽车研究院有限公司")
  432. }
  433. }
  434. // todo 先不要目录页
  435. //// ------- 目录页 -------
  436. //{
  437. // pdf.AddPage()
  438. // currentLine := 0.0
  439. // {
  440. // err = pdf.SetFont("simfang", "", 18)
  441. // pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  442. // _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "目录", alignOptionText2)
  443. // currentLine++
  444. // currentLine++
  445. // }
  446. // {
  447. // err = pdf.SetFont("simfang", "", 14)
  448. // pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  449. // currentLine++
  450. // _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "1. 总体情况 ............................... 1", alignOptionText)
  451. // }
  452. //}
  453. // ------- 详情页 第一页 -------
  454. {
  455. pdf.AddPage()
  456. currentLine := 0.0
  457. // 1. 总体分数情况
  458. {
  459. {
  460. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  461. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "1. 总体分数情况", alignOptionText)
  462. currentLine++
  463. }
  464. {
  465. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  466. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "整体得分:"+util.ToString(param.ScoreFinal), alignOptionText)
  467. currentLine++
  468. }
  469. {
  470. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  471. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "线上得分:"+util.ToString(param.ScoreFinal), alignOptionText)
  472. currentLine++
  473. }
  474. {
  475. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  476. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "线下得分:", alignOptionText)
  477. currentLine++
  478. }
  479. {
  480. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  481. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "整体耗时:"+endTime.Sub(beginTime).String(), alignOptionText)
  482. currentLine++
  483. }
  484. }
  485. // 2. 分数详情
  486. {
  487. columnNumber := 4.0 // 列数
  488. tableCellWidth := tableWidth / columnNumber // 单元格宽度
  489. {
  490. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  491. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "2. 分数详情", alignOptionText)
  492. currentLine++
  493. }
  494. // 分数详情表格
  495. {
  496. {
  497. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  498. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "序号", alignOptionTable)
  499. }
  500. {
  501. pdf.SetXY(leftMargin+1.0*tableCellWidth, topMargin+currentLine*lineHeight)
  502. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "场景名称", alignOptionTable)
  503. }
  504. {
  505. pdf.SetXY(leftMargin+2.0*tableCellWidth, topMargin+currentLine*lineHeight)
  506. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "线上得分", alignOptionTable)
  507. }
  508. {
  509. pdf.SetXY(leftMargin+3.0*tableCellWidth, topMargin+currentLine*lineHeight)
  510. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "线下得分", alignOptionTable)
  511. }
  512. currentLine++
  513. }
  514. for i := 0; i < 10; i++ {
  515. {
  516. {
  517. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  518. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, util.ToString(i+1), alignOptionTable)
  519. }
  520. {
  521. pdf.SetXY(leftMargin+1.0*tableCellWidth, topMargin+currentLine*lineHeight)
  522. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "xxx", alignOptionTable)
  523. }
  524. {
  525. pdf.SetXY(leftMargin+2.0*tableCellWidth, topMargin+currentLine*lineHeight)
  526. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "xxx", alignOptionTable)
  527. }
  528. {
  529. pdf.SetXY(leftMargin+3.0*tableCellWidth, topMargin+currentLine*lineHeight)
  530. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "xxx", alignOptionTable)
  531. }
  532. currentLine++
  533. }
  534. }
  535. }
  536. }
  537. // ------- 详情页 第二页 -------
  538. {
  539. pdf.AddPage()
  540. currentLine := 0.0
  541. // 3. 线上扣分详情
  542. {
  543. columnNumber := 3.0 // 列数
  544. tableCellWidth := tableWidth / columnNumber // 单元格宽度
  545. {
  546. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  547. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "3. 线上扣分详情", alignOptionText)
  548. currentLine++
  549. }
  550. for i := 0; i < 5; i++ {
  551. // 场景1
  552. {
  553. {
  554. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  555. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "("+util.ToString(i+1)+")xxxx场景", alignOptionText)
  556. currentLine++
  557. }
  558. // 场景1 表格
  559. {
  560. {
  561. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  562. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "序号", alignOptionTable)
  563. }
  564. {
  565. pdf.SetXY(leftMargin+1.0*tableCellWidth, topMargin+currentLine*lineHeight)
  566. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "扣分点", alignOptionTable)
  567. }
  568. {
  569. pdf.SetXY(leftMargin+2.0*tableCellWidth, topMargin+currentLine*lineHeight)
  570. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "扣分说明", alignOptionTable)
  571. }
  572. currentLine++
  573. }
  574. for j := 0; j < 2; j++ {
  575. {
  576. {
  577. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  578. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, util.ToString(j+1), alignOptionTable)
  579. }
  580. {
  581. pdf.SetXY(leftMargin+1.0*tableCellWidth, topMargin+currentLine*lineHeight)
  582. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "xxx", alignOptionTable)
  583. }
  584. {
  585. pdf.SetXY(leftMargin+2.0*tableCellWidth, topMargin+currentLine*lineHeight)
  586. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "xxx", alignOptionTable)
  587. }
  588. currentLine++
  589. }
  590. }
  591. }
  592. }
  593. }
  594. }
  595. // ------- 详情页 第三页 -------
  596. size := 10
  597. if size > 5 {
  598. pdf.AddPage()
  599. currentLine := 0.0
  600. // 3. 线上扣分详情
  601. {
  602. columnNumber := 3.0 // 列数
  603. tableCellWidth := tableWidth / columnNumber // 单元格宽度
  604. for i := 5; i < 10; i++ {
  605. // 场景1
  606. {
  607. {
  608. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  609. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "("+util.ToString(i+1)+")xxxx场景", alignOptionText)
  610. currentLine++
  611. }
  612. // 场景1 表格
  613. {
  614. {
  615. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  616. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "序号", alignOptionTable)
  617. }
  618. {
  619. pdf.SetXY(leftMargin+1.0*tableCellWidth, topMargin+currentLine*lineHeight)
  620. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "扣分点", alignOptionTable)
  621. }
  622. {
  623. pdf.SetXY(leftMargin+2.0*tableCellWidth, topMargin+currentLine*lineHeight)
  624. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "扣分说明", alignOptionTable)
  625. }
  626. currentLine++
  627. }
  628. for j := 0; j < 2; j++ {
  629. {
  630. {
  631. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  632. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, util.ToString(j+1), alignOptionTable)
  633. }
  634. {
  635. pdf.SetXY(leftMargin+1.0*tableCellWidth, topMargin+currentLine*lineHeight)
  636. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "xxx", alignOptionTable)
  637. }
  638. {
  639. pdf.SetXY(leftMargin+2.0*tableCellWidth, topMargin+currentLine*lineHeight)
  640. _ = pdf.CellWithOption(&gopdf.Rect{W: tableCellWidth, H: lineHeight}, "xxx", alignOptionTable)
  641. }
  642. currentLine++
  643. }
  644. }
  645. }
  646. }
  647. }
  648. // 4 作品优化建议
  649. {
  650. pdf.SetXY(leftMargin, topMargin+currentLine*lineHeight)
  651. _ = pdf.CellWithOption(&gopdf.Rect{W: tableWidth, H: lineHeight}, "4. 作品优化建议", alignOptionText)
  652. currentLine++
  653. }
  654. }
  655. // ------- 附录页 -------
  656. pdf.AddPage()
  657. {
  658. pdf.SetXY(leftMargin, topMargin)
  659. _ = pdf.Text("附件:评分规则")
  660. pdf.AddExternalLink("https://www.baidu.com/", leftMargin, topMargin-lineHeight, 6*14, lineHeight)
  661. }
  662. // 写入本地
  663. pdf.WritePdf("D:\\hello.pdf")
  664. }
  665. // 打开要发送的文件
  666. file, err := os.Open("D:\\hello.pdf")
  667. if err != nil {
  668. c.String(http.StatusNotFound, "文件未找到")
  669. return
  670. }
  671. defer file.Close()
  672. // 将文件复制到响应主体
  673. _, err = io.Copy(c.Writer, file)
  674. if err != nil {
  675. c.String(http.StatusInternalServerError, "无法复制文件到响应体")
  676. return
  677. }
  678. }