h_exam.go 24 KB

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