h_exam.go 26 KB

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