h_exam.go 26 KB

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