h_exam.go 23 KB

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