h_exam.go 23 KB

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