| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687 |
- package controllers
- import (
- // 根据实际项目结构调整导入路径
- "Tech/common"
- "Tech/models"
- "bytes"
- "encoding/json"
- "fmt"
- "io/ioutil"
- "mime/multipart"
- "net/http"
- "os"
- "os/exec"
- "time"
- "github.com/beego/beego/v2/client/orm"
- beego "github.com/beego/beego/v2/server/web"
- // 新增导入
- )
- type EvaLogController struct {
- beego.Controller
- }
- // /eva/record/list 获取课程评价记录列表
- func (c *EvaLogController) EvaRecordList() {
- type Request struct {
- RunLogID int `json:"runLogID"`
- }
- var req Request
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if len(requestBody) > 0 {
- if err := json.Unmarshal(requestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- }
- o := orm.NewOrm()
- // 查询所有角色信息用于关联角色名称
- var roleInfos []*models.RoleInfo
- _, err := o.QueryTable(new(models.RoleInfo)).All(&roleInfos)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询角色信息失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 创建角色ID到角色名称的映射
- roleMap := make(map[int]string)
- for _, role := range roleInfos {
- roleMap[role.RoleID] = role.RoleName
- }
- // 定义联合查询的结果结构体
- type EvaRecord struct {
- // CourseEvaLog 字段
- RunLogID int `orm:"column(运行记录编号)" json:"runLogID"`
- RunCourseID int `orm:"column(运行课程ID)" json:"runCourseID"`
- RunOperID int `orm:"column(运行操作ID)" json:"runOperID"`
- OperTime int `orm:"column(操作时间)" json:"operTime"`
- OperScore float64 `orm:"column(操作得分)" json:"operScore"`
- SeqNo int `orm:"column(序号)" json:"seqNo"`
- EvaTargetRole int `orm:"column(评价目标角色)" json:"evaTargetRole"`
- EvaName string `orm:"column(评价名称)" json:"evaName"`
- EvaMapID int `orm:"column(评价映射ID)" json:"evaMapID"`
- EvaType int `orm:"column(评价类型)" json:"evaType"`
- EvaSource int `orm:"column(评价来源)" json:"evaSource"`
- DisqualificationItem int `orm:"column(失格项)" json:"disqualificationItem"`
- IsOperated int `orm:"column(是否操作)" json:"isOperated"` // 节点是否操作 0 未操作 1 已操作
- EvaScore int `orm:"column(评价赋分)" json:"evaScore"`
- EvaDeduction int `orm:"column(评价失分)" json:"evaDeduction"`
- }
- // 定义带有角色名称的扩展结果结构体
- type EvaRecordWithRoleName struct {
- // CourseEvaLog 字段
- RunLogID int `json:"runLogID"`
- RunCourseID int `json:"runCourseID"`
- RunOperID int `json:"runOperID"`
- OperTime int `json:"operTime"`
- OperScore float64 `json:"operScore"`
- SeqNo int `json:"seqNo"`
- EvaTargetRole int `json:"evaTargetRole"`
- EvaName string `json:"evaName"`
- EvaMapID int `json:"evaMapID"`
- EvaType int `json:"evaType"`
- EvaSource int `json:"evaSource"`
- DisqualificationItem int `json:"disqualificationItem"`
- IsOperated int `json:"isOperated"` // 节点是否操作 0 未操作 1 已操作
- EvaScore int `json:"evaScore"`
- EvaDeduction int `json:"evaDeduction"`
- //课程名称
- CourseName string `json:"courseName"`
- TargetRoleName string `json:"targetRoleName"`
- }
- var evaRecords []EvaRecord
- var count int64
- // 如果RunLogID > 0,则查询对应的课程运行评价列表
- // 否则查询最新的课程运行评价列表
- var runLogID int
- if req.RunLogID > 0 {
- runLogID = req.RunLogID
- // 根据运行记录编号查询联合评价记录列表
- count, err = o.Raw(`
- SELECT
- l.运行记录编号,
- l.运行课程ID,
- l.运行操作ID,
- l.操作时间,
- l.操作得分,
- l.序号,
- l.评价目标角色,
- l.评价名称,
- l.评价映射ID,
- l.评价类型,
- l.评价来源,
- l.失格项,
- l.是否操作,
- l.评价赋分,
- l.评价失分
- FROM CourseEvaLog l
- WHERE l.运行记录编号 = ?
- `, req.RunLogID).QueryRows(&evaRecords)
- } else {
- // 查询最新的课程运行记录ID
- var latestRunLog models.CourseRunLog
- err = o.QueryTable(new(models.CourseRunLog)).
- OrderBy("-RunLogID").
- One(&latestRunLog)
- if err != nil {
- if err == orm.ErrNoRows {
- // 没有运行记录
- c.Data["json"] = map[string]interface{}{
- "code": 404,
- "msg": "未找到任何课程运行记录",
- }
- c.ServeJSON()
- return
- } else {
- // 查询出错
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询课程运行记录失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- }
- runLogID = latestRunLog.RunLogID
- // 根据最新运行记录ID查询联合评价记录列表
- count, err = o.Raw(`
- SELECT
- l.运行记录编号,
- l.运行课程ID,
- l.运行操作ID,
- l.操作时间,
- l.操作得分,
- l.序号,
- l.评价目标角色,
- l.评价名称,
- l.评价映射ID,
- l.评价类型,
- l.评价来源,
- l.失格项,
- l.是否操作,
- l.评价赋分,
- l.评价失分
- FROM CourseEvaLog l
- WHERE l.运行记录编号 = ?
- `, latestRunLog.RunLogID).QueryRows(&evaRecords)
- }
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询评价记录列表失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 查询课程运行记录以获取课程名称
- var courseRunLog models.CourseRunLog
- err = o.QueryTable(new(models.CourseRunLog)).Filter("RunLogID", runLogID).One(&courseRunLog)
- if err != nil && err != orm.ErrNoRows {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询课程运行记录失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 为每个评价项添加角色名称和课程名称(适配 evaRecords 为值切片)
- evaRecordsWithRoles := make([]*EvaRecordWithRoleName, len(evaRecords))
- for i, record := range evaRecords {
- evaRecordsWithRoles[i] = &EvaRecordWithRoleName{
- RunLogID: record.RunLogID,
- RunCourseID: record.RunCourseID,
- RunOperID: record.RunOperID,
- OperTime: record.OperTime,
- OperScore: record.OperScore,
- SeqNo: record.SeqNo,
- EvaTargetRole: record.EvaTargetRole,
- EvaName: record.EvaName,
- EvaMapID: record.EvaMapID,
- EvaType: record.EvaType,
- EvaSource: record.EvaSource,
- DisqualificationItem: record.DisqualificationItem,
- EvaScore: record.EvaScore,
- EvaDeduction: record.EvaDeduction,
- CourseName: courseRunLog.RunCourseName,
- IsOperated: record.IsOperated,
- TargetRoleName: roleMap[record.EvaTargetRole],
- }
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "count": count,
- "msg": "成功",
- "data": evaRecordsWithRoles,
- }
- c.ServeJSON()
- }
- // /eva/setoperated 设置节点是否操作,并计算得分
- func (c *EvaLogController) Setoperated() {
- type Request struct {
- RunLogID int `json:"runLogID"`
- SeqNo int `json:"seqNo"`
- IsOperated int `json:"isOperated"` // 0:未操作 1:已操作
- }
- var req Request
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if req.RunLogID == 0 || req.SeqNo == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少必要参数",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 查询对应的评价标准信息,直接从CourseEvaLog获取评分信息
- var evaLog models.CourseEvaLog
- err := o.Raw(`
- SELECT *
- FROM CourseEvaLog
- WHERE 运行记录编号 = ? AND 序号 = ?
- `, req.RunLogID, req.SeqNo).QueryRow(&evaLog)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询评价标准信息失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 查询课程运行记录信息
- var courseRunLog models.CourseRunLog
- err = o.Raw(`
- SELECT *
- FROM CourseRunLog
- WHERE 运行记录编号 = ?
- `, req.RunLogID).QueryRow(&courseRunLog)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询课程运行记录失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 计算操作时间
- operTime := 0
- // 只有在课程未结束时才计算操作时间(暂停标记为-1表示课程结束)
- if courseRunLog.PauseFlag != -1 {
- // 计算课程用时(从开始时间到当前时间的秒数)
- startTime, err := time.ParseInLocation("2006-01-02 15:04:05", courseRunLog.RunDateTime, time.Local)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "解析课程开始时间失败: " + err.Error(),
- "runStatus": 0,
- "data": nil,
- }
- c.ServeJSON()
- return
- }
- operTime = int(time.Since(startTime).Seconds()) - courseRunLog.PauseTotalTime
- // 确保操作时间不为负数
- if operTime < 0 {
- operTime = 0
- }
- }
- // 计算操作得分
- operScore := 0.0
- if req.IsOperated == 1 {
- // 如果已操作,得分为评价赋分-评价失分
- operScore = float64(evaLog.EvaScore - evaLog.EvaDeduction)
- }
- // 如果未操作,得分为0(默认值)
- // 更新CourseEvaLog表中的是否操作字段、操作得分和操作时间
- res, err := o.Raw(`
- UPDATE CourseEvaLog
- SET 是否操作 = ?, 操作得分 = ?, 操作时间 = ?
- WHERE 运行记录编号 = ? AND 序号 = ?
- `, req.IsOperated, operScore, operTime, req.RunLogID, req.SeqNo).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新节点操作状态失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- fmt.Println(res)
- // 重新计算整个课程的总得分
- // 查询该课程运行记录下的所有评价项
- var totalScore float64
- err = o.Raw(`
- SELECT COALESCE(SUM(操作得分), 0)
- FROM CourseEvaLog
- WHERE 运行记录编号 = ?
- `, req.RunLogID).QueryRow(&totalScore)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "计算总得分失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 更新课程运行日志中的总得分
- _, err = o.Raw(`
- UPDATE CourseRunLog
- SET 课程得分 = ?
- WHERE 运行记录编号 = ?
- `, totalScore, req.RunLogID).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新总得分失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "设置节点操作状态成功",
- "data": map[string]interface{}{
- "runLogID": req.RunLogID,
- "seqNo": req.SeqNo,
- "isOperated": req.IsOperated,
- "operScore": operScore,
- "operTime": operTime,
- "totalScore": totalScore,
- },
- }
- c.ServeJSON()
- }
- // /evaLog/add 添加评价日志记录
- func (c *EvaLogController) Add() {
- var eva models.CourseEvaLog
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &eva); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if eva.RunLogID == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少运行记录ID参数",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 如果SeqNo为0,则查询该运行记录下最大的SeqNo并加1
- if eva.SeqNo == 0 {
- var maxSeqNo int
- err := o.Raw("SELECT COALESCE(MAX(序号), 0) FROM CourseEvaLog WHERE 运行记录编号 = ?", eva.RunLogID).QueryRow(&maxSeqNo)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询序号失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- eva.SeqNo = maxSeqNo + 1
- } else {
- // 如果指定了SeqNo,检查该SeqNo是否已存在
- var count int64
- count, err := o.QueryTable(new(models.CourseEvaLog)).
- Filter("RunLogID", eva.RunLogID).
- Filter("SeqNo", eva.SeqNo).
- Count()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询序号失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 如果SeqNo已存在,则将该SeqNo及之后的所有序号都加1
- if count > 0 {
- // 使用倒序更新,避免主键冲突
- _, err := o.Raw("UPDATE CourseEvaLog SET 序号 = 序号 + 1 WHERE 运行记录编号 = ? AND 序号 >= ? ORDER BY 序号 DESC",
- eva.RunLogID, eva.SeqNo).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "调整序号失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- }
- }
- // 设置评价来源为教员自定义
- eva.EvaSource = 1
- // 默认设置为未操作
- eva.IsOperated = 0
- // 默认操作得分为0
- eva.OperScore = 0
- _, err := o.Insert(&eva)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "添加评价失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "添加评价成功",
- "seqNo": eva.SeqNo,
- }
- c.ServeJSON()
- }
- // /evaLog/delete 删除评价日志记录
- func (c *EvaLogController) Delete() {
- type Request struct {
- RunLogID int `json:"runLogID"`
- SeqNo int `json:"seqNo"`
- }
- var req Request
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if req.RunLogID == 0 || req.SeqNo == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少必要参数",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 先查询评价记录,判断是否来自流程图
- var eva models.CourseEvaLog
- err := o.Raw("SELECT * FROM CourseEvaLog WHERE 运行记录编号 = ? AND 序号 = ?", req.RunLogID, req.SeqNo).QueryRow(&eva)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询评价记录失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 如果评价来自流程图(EvaSource=0),则不允许删除
- if eva.EvaSource == 0 {
- c.Data["json"] = map[string]interface{}{
- "code": 403,
- "msg": "来自流程图的评价不能删除",
- }
- c.ServeJSON()
- return
- }
- // 执行删除操作(仅对教员自定义的评价)
- result, err := o.Raw("DELETE FROM CourseEvaLog WHERE 运行记录编号 = ? AND 序号 = ?", req.RunLogID, req.SeqNo).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "删除评价失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- n, _ := result.RowsAffected()
- if n == 0 {
- c.Data["json"] = map[string]interface{}{
- "code": 404,
- "msg": "未找到对应的评价记录",
- }
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "删除评价成功",
- }
- c.ServeJSON()
- }
- // /evaLog/update 更新评价日志记录
- func (c *EvaLogController) Update() {
- var eva models.CourseEvaLog
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &eva); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if eva.RunLogID == 0 || eva.SeqNo == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少必要参数",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 先尝试读取记录,判断是否存在
- existingEva := models.CourseEvaLog{RunLogID: eva.RunLogID, SeqNo: eva.SeqNo}
- err := o.Read(&existingEva, "RunLogID", "SeqNo")
- if err != nil {
- if err == orm.ErrNoRows {
- c.Data["json"] = map[string]interface{}{
- "code": 404,
- "msg": "未找到对应的评价记录",
- }
- c.ServeJSON()
- return
- } else {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询评价记录失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- }
- // 如果请求中没有提供相应字段,则使用现有记录中的值
- if eva.EvaTargetRole == 0 {
- eva.EvaTargetRole = existingEva.EvaTargetRole
- }
- if eva.EvaName == "" {
- eva.EvaName = existingEva.EvaName
- }
- if eva.EvaMapID == 0 {
- eva.EvaMapID = existingEva.EvaMapID
- }
- // 评价来源始终使用现有记录中的值(保留原始来源)
- eva.EvaSource = existingEva.EvaSource
- if eva.DisqualificationItem == 0 {
- eva.DisqualificationItem = existingEva.DisqualificationItem
- }
- // 使用原生SQL更新记录
- _, err = o.Raw("UPDATE CourseEvaLog SET 评价目标角色=?, 评价名称=?, 评价映射ID=?, 评价类型=?, 评价赋分=?, 评价失分=?, 评价来源=?, 失格项=? WHERE 运行记录编号=? AND 序号=?",
- eva.EvaTargetRole, eva.EvaName, eva.EvaMapID, eva.EvaType, eva.EvaScore, eva.EvaDeduction, eva.EvaSource, eva.DisqualificationItem,
- eva.RunLogID, eva.SeqNo).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新评价失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "更新评价成功",
- "data": eva,
- }
- c.ServeJSON()
- }
- // /evalog/orderupdate 评价记录排序更新
- func (c *EvaLogController) Orderupdate() {
- type Request struct {
- RunLogID int `json:"runLogID"`
- SeqNo int `json:"seqNo"`
- SetOrder int `json:"setorder"`
- }
- var req Request
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if req.RunLogID == 0 || req.SeqNo == 0 || req.SetOrder == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少必要参数",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 先尝试读取记录,判断是否存在
- existingEva := models.CourseEvaLog{RunLogID: req.RunLogID, SeqNo: req.SeqNo}
- err := o.Read(&existingEva, "RunLogID", "SeqNo")
- if err != nil {
- if err == orm.ErrNoRows {
- c.Data["json"] = map[string]interface{}{
- "code": 404,
- "msg": "未找到对应的评价记录",
- }
- } else {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询评价记录失败: " + err.Error(),
- }
- }
- c.ServeJSON()
- return
- }
- // 获取该运行记录的所有评价记录,按序号排序
- var evaList []models.CourseEvaLog
- _, err = o.QueryTable(new(models.CourseEvaLog)).
- Filter("RunLogID", req.RunLogID).
- OrderBy("SeqNo").
- All(&evaList)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询评价列表失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 检查边界条件
- if req.SetOrder == 1 && req.SeqNo <= 1 {
- // 上移但已经在最顶部
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "无法执行上移操作,已到达顶部",
- }
- c.ServeJSON()
- return
- }
- if req.SetOrder == 2 && req.SeqNo >= len(evaList) {
- // 下移但已经在最底部
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "无法执行下移操作,已到达底部",
- }
- c.ServeJSON()
- return
- }
- // 确定要交换的两个序号
- var targetSeqNo int
- if req.SetOrder == 1 {
- // 上移:与前一个序号交换
- targetSeqNo = req.SeqNo - 1
- } else {
- // 下移:与后一个序号交换
- targetSeqNo = req.SeqNo + 1
- }
- // 使用三步更新避免主键冲突
- // 第一步:将目标序号临时设为0
- _, err = o.Raw("UPDATE CourseEvaLog SET 序号 = 0 WHERE 运行记录编号 = ? AND 序号 = ?",
- req.RunLogID, targetSeqNo).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新序号失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 第二步:将当前序号更新为目标序号
- _, err = o.Raw("UPDATE CourseEvaLog SET 序号 = ? WHERE 运行记录编号 = ? AND 序号 = ?",
- targetSeqNo, req.RunLogID, req.SeqNo).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新序号失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 第三步:将临时序号0更新为当前序号
- _, err = o.Raw("UPDATE CourseEvaLog SET 序号 = ? WHERE 运行记录编号 = ? AND 序号 = 0",
- req.SeqNo, req.RunLogID).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新序号失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "顺序调整成功",
- }
- c.ServeJSON()
- }
- // 将秒数格式化为 HH:MM:SS 格式
- func formatSecondsToHMS(seconds int) string {
- hours := seconds / 3600
- minutes := (seconds % 3600) / 60
- secs := seconds % 60
- return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, secs)
- }
- // /evalog/export 导出课程评价记录
- func (c *EvaLogController) Export() {
- type Request struct {
- RunLogID int `json:"runLogID"`
- }
- var req Request
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if req.RunLogID == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少运行记录ID参数",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 查询课程运行记录信息
- var courseRunLog models.CourseRunLog
- err := o.Raw(`SELECT * FROM CourseRunLog WHERE 运行记录编号 = ?`, req.RunLogID).QueryRow(&courseRunLog)
- if err != nil {
- if err == orm.ErrNoRows {
- c.Data["json"] = map[string]interface{}{
- "code": 404,
- "msg": "未找到对应的课程运行记录",
- }
- } else {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询课程运行记录失败: " + err.Error(),
- }
- }
- c.ServeJSON()
- return
- }
- // 查询该课程运行记录下的所有评价项
- var evaLogs []models.CourseEvaLog
- _, err = o.Raw(`
- SELECT *
- FROM CourseEvaLog
- WHERE 运行记录编号 = ?
- ORDER BY 序号
- `, req.RunLogID).QueryRows(&evaLogs)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询评价记录列表失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 查询所有角色信息用于关联角色名称
- var roleInfos []*models.RoleInfo
- _, err = o.QueryTable(new(models.RoleInfo)).All(&roleInfos)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询角色信息失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if courseRunLog.CourseTime == 0 {
- // 课程正在进行中,计算运行时间并返回详细信息
- startTime, err := time.ParseInLocation("2006-01-02 15:04:05", courseRunLog.RunDateTime, time.Local)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "解析课程开始时间失败: " + err.Error(),
- "data": map[string]interface{}{
- "runStatus": 0,
- },
- }
- c.ServeJSON()
- return
- }
- // 获取当前时间
- now := time.Now()
- fmt.Printf("目标时间: %v\n", startTime)
- fmt.Printf("当前时间: %v\n", now)
- // 计算时间差(秒)
- diff := int(now.Sub(startTime).Seconds()) - courseRunLog.PauseTotalTime
- courseRunLog.CourseTime = diff
- }
- // 创建角色ID到角色名称的映射
- roleMap := make(map[int]string)
- for _, role := range roleInfos {
- roleMap[role.RoleID] = role.RoleName
- }
- // 生成HTML内容
- htmlContent := c.generateHTMLContent(courseRunLog, evaLogs, roleMap)
- // 创建临时HTML文件
- htmlFile, err := os.CreateTemp("", "course_evaluation_report_*.html")
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "创建HTML文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- defer os.Remove(htmlFile.Name())
- // 写入HTML内容
- _, err = htmlFile.WriteString(htmlContent)
- if err != nil {
- htmlFile.Close()
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "写入HTML文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- htmlFile.Close()
- // 创建临时PDF文件
- pdfFile, err := os.CreateTemp("", "course_evaluation_report_*.pdf")
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "创建PDF文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- defer os.Remove(pdfFile.Name())
- pdfFile.Close()
- // 使用wkhtmltopdf将HTML转换为PDF
- cmd := exec.Command("wkhtmltopdf", "--encoding", "utf-8", htmlFile.Name(), pdfFile.Name())
- var stderr bytes.Buffer
- cmd.Stderr = &stderr
- err = cmd.Run()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "HTML转PDF失败: " + err.Error() + "; " + stderr.String(),
- }
- c.ServeJSON()
- return
- }
- // 设置响应头,返回PDF文件
- c.Ctx.Output.Header("Content-Type", "application/pdf")
- c.Ctx.Output.Header("Content-Disposition", fmt.Sprintf("attachment; filename=course_evaluation_report_%d.pdf", req.RunLogID))
- // 读取PDF文件并写入响应
- pdfData, err := ioutil.ReadFile(pdfFile.Name())
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "读取PDF文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- c.Ctx.ResponseWriter.Write(pdfData)
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "导出成功",
- }
- c.ServeJSON()
- }
- // 生成HTML内容
- func (c *EvaLogController) generateHTMLContent(courseRunLog models.CourseRunLog, evaLogs []models.CourseEvaLog, roleMap map[int]string) string {
- var buf bytes.Buffer
- // 添加HTML头部
- buf.WriteString(`<!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>课程电子考评表</title>
- <style>
- body {
- font-family: "Microsoft YaHei", sans-serif;
- margin: 20px;
- }
- .header {
- text-align: center;
- font-size: 24px;
- font-weight: bold;
- margin-bottom: 20px;
- }
- .info-table {
- width: 100%;
- border-collapse: collapse;
- margin-bottom: 20px;
- }
- .info-table td {
- padding: 5px 10px;
- }
- .info-table .label {
- font-weight: bold;
- width: 20%;
- }
- .eva-table {
- width: 100%;
- border-collapse: collapse;
- margin-bottom: 20px;
- }
- .eva-table th, .eva-table td {
- border: 1px solid #000;
- padding: 8px;
- text-align: center;
- }
- .eva-table th {
- background-color: #f0f0f0;
- font-weight: bold;
- }
- .eva-table th:nth-child(4),
- .eva-table td:nth-child(4) {
- width: 200px; /* 或使用百分比 width: 25%; */
- max-width: 300px;
- text-align: left;
- }
- .footer {
- text-align: left;
- font-style: italic;
- margin-top: 20px;
- }
- </style>
- </head>
- <body>
- <div class="header">课程电子考评表</div>
-
- <table class="info-table">
- <tr>
- <td class="label">运行课程名称:</td>
- <td>` + courseRunLog.RunCourseName + `</td>
- <td class="label">开课时间:</td>
- <td>` + courseRunLog.RunDateTime + `</td>
- </tr>
- <tr>
- <td class="label">课程用时:</td>
- <td>` + formatSecondsToHMS(courseRunLog.CourseTime) + `</td>
- <td class="label">课程得分:</td>
- <td>` + fmt.Sprintf("%.2f", courseRunLog.CourseScore) + `</td>
- </tr>
- <tr>
- <td class="label">暂停累计时长(秒):</td>
- <td>` + fmt.Sprintf("%d", courseRunLog.PauseTotalTime) + `</td>
- <td class="label">课程状态:</td>
- <td>`)
- // 添加状态信息
- switch courseRunLog.PauseFlag {
- case -1:
- buf.WriteString("已结束")
- case 0:
- buf.WriteString("运行中")
- case 1:
- buf.WriteString("已暂停")
- }
- buf.WriteString(`</td>
- </tr>
- </table>
-
- <table class="eva-table">
- <thead>
- <tr>
- <th>序号</th>
- <th>操作时间</th>
- <th>角色名称</th>
- <th>评价名称</th>
- <th>评价类型</th>
- <th>赋分</th>
- <th>失分</th>
- <th>得分</th>
- <th>来源</th>
- <th>是否操作</th>
- </tr>
- </thead>
- <tbody>`)
- // 表格数据
- for _, evaLog := range evaLogs {
- // 处理评价目标角色名称
- targetRoleName := roleMap[evaLog.EvaTargetRole]
- if targetRoleName == "" {
- targetRoleName = fmt.Sprintf("角色%d", evaLog.EvaTargetRole)
- }
- // 处理操作时间 - 改为 hh:mm:ss 格式
- hours := evaLog.OperTime / 3600
- minutes := (evaLog.OperTime % 3600) / 60
- seconds := evaLog.OperTime % 60
- operTimeStr := fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
- // 处理评价类型
- evaType := "主观"
- if evaLog.EvaType == 1 {
- evaType = "客观"
- }
- // 处理评价来源
- source := "流程图"
- if evaLog.EvaSource == 1 {
- source = "教员自定义"
- }
- // 处理是否操作
- isOperated := "未操作"
- if evaLog.IsOperated == 1 {
- isOperated = "已操作"
- }
- buf.WriteString("<tr>")
- buf.WriteString("<td>" + fmt.Sprintf("%d", evaLog.SeqNo) + "</td>")
- buf.WriteString("<td>" + operTimeStr + "</td>")
- buf.WriteString("<td>" + targetRoleName + "</td>")
- buf.WriteString("<td style='text-align: left;'>" + evaLog.EvaName + "</td>")
- buf.WriteString("<td>" + evaType + "</td>")
- buf.WriteString("<td>" + fmt.Sprintf("%d", evaLog.EvaScore) + "</td>")
- buf.WriteString("<td>" + fmt.Sprintf("%d", evaLog.EvaDeduction) + "</td>")
- buf.WriteString("<td>" + fmt.Sprintf("%.1f", evaLog.OperScore) + "</td>")
- buf.WriteString("<td>" + source + "</td>")
- buf.WriteString("<td>" + isOperated + "</td>")
- buf.WriteString("</tr>")
- }
- buf.WriteString(`</tbody>
- </table>
-
- <div class="footer">导出时间: ` + time.Now().Format("2006-01-02 15:04:05") + `</div>
- </body>
- </html>`)
- return buf.String()
- }
- // /evalog/operRunRecordExport 导出操作记录
- func (c *EvaLogController) OperRunRecordExport() {
- type Request struct {
- RunLogID int `json:"runLogID"`
- }
- var req Request
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if req.RunLogID == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少运行记录ID参数",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 查询课程运行记录信息
- var courseRunLog models.CourseRunLog
- err := o.Raw(`SELECT * FROM CourseRunLog WHERE 运行记录编号 = ?`, req.RunLogID).QueryRow(&courseRunLog)
- if err != nil {
- if err == orm.ErrNoRows {
- c.Data["json"] = map[string]interface{}{
- "code": 404,
- "msg": "未找到对应的课程运行记录",
- }
- } else {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询课程运行记录失败: " + err.Error(),
- }
- }
- c.ServeJSON()
- return
- }
- // 查询该课程运行记录下的所有操作记录项
- var operLogs []models.CourseOperLog
- _, err = o.Raw(`
- SELECT *
- FROM CourseOperLog
- WHERE 运行记录编号 = ?
- ORDER BY 操作时间
- `, req.RunLogID).QueryRows(&operLogs)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询操作记录列表失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 生成HTML内容
- htmlContent := c.generateOperHTMLContent(courseRunLog, operLogs)
- // 创建临时HTML文件
- htmlFile, err := ioutil.TempFile("", "course_operation_report_*.html")
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "创建HTML文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- defer os.Remove(htmlFile.Name())
- // 写入HTML内容
- _, err = htmlFile.WriteString(htmlContent)
- if err != nil {
- htmlFile.Close()
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "写入HTML文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- htmlFile.Close()
- // 创建临时PDF文件
- pdfFile, err := ioutil.TempFile("", "course_operation_report_*.pdf")
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "创建PDF文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- defer os.Remove(pdfFile.Name())
- pdfFile.Close()
- // 使用wkhtmltopdf将HTML转换为PDF
- cmd := exec.Command("wkhtmltopdf", "--encoding", "utf-8", htmlFile.Name(), pdfFile.Name())
- var stderr bytes.Buffer
- cmd.Stderr = &stderr
- err = cmd.Run()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "HTML转PDF失败: " + err.Error() + "; " + stderr.String(),
- }
- c.ServeJSON()
- return
- }
- // 设置响应头,返回PDF文件
- c.Ctx.Output.Header("Content-Type", "application/pdf")
- c.Ctx.Output.Header("Content-Disposition", fmt.Sprintf("attachment; filename=course_operation_report_%d.pdf", req.RunLogID))
- // 读取PDF文件并写入响应
- pdfData, err := ioutil.ReadFile(pdfFile.Name())
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "读取PDF文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- c.Ctx.ResponseWriter.Write(pdfData)
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "导出成功",
- }
- c.ServeJSON()
- }
- // 生成操作记录HTML内容
- func (c *EvaLogController) generateOperHTMLContent(courseRunLog models.CourseRunLog, operLogs []models.CourseOperLog) string {
- var buf bytes.Buffer
- // 添加HTML头部
- buf.WriteString(`<!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>课程操作记录表</title>
- <style>
- body {
- font-family: "Microsoft YaHei", sans-serif;
- margin: 20px;
- }
- .header {
- text-align: center;
- font-size: 24px;
- font-weight: bold;
- margin-bottom: 20px;
- }
- .info-table {
- width: 100%;
- border-collapse: collapse;
- margin-bottom: 20px;
- }
- .info-table td {
- padding: 5px 10px;
- }
- .info-table .label {
- font-weight: bold;
- width: 20%;
- }
- .oper-table {
- width: 100%;
- border-collapse: collapse;
- margin-bottom: 20px;
- }
- .oper-table th, .oper-table td {
- border: 1px solid #000;
- padding: 8px;
- text-align: center;
- }
- .oper-table th {
- background-color: #f0f0f0;
- font-weight: bold;
- }
- .footer {
- text-align: left;
- font-style: italic;
- margin-top: 20px;
- }
- </style>
- </head>
- <body>
- <div class="header">课程操作记录表</div>
-
- <table class="info-table">
- <tr>
- <td class="label">运行课程名称:</td>
- <td>` + courseRunLog.RunCourseName + `</td>
- <td class="label">开课时间:</td>
- <td>` + courseRunLog.RunDateTime + `</td>
- </tr>
- <tr>
- <td class="label">课程用时:</td>
- <td>` + formatSecondsToHMS(courseRunLog.CourseTime) + `</td>
- <td class="label">课程得分:</td>
- <td>` + fmt.Sprintf("%.2f", courseRunLog.CourseScore) + `</td>
- </tr>
- <tr>
- <td class="label">暂停累计时长(秒):</td>
- <td>` + fmt.Sprintf("%d", courseRunLog.PauseTotalTime) + `</td>
- <td class="label">课程状态:</td>
- <td>`)
- // 添加状态信息
- switch courseRunLog.PauseFlag {
- case -1:
- buf.WriteString("已结束")
- case 0:
- buf.WriteString("运行中")
- case 1:
- buf.WriteString("已暂停")
- }
- buf.WriteString(`</td>
- </tr>
- </table>
-
- <table class="oper-table">
- <thead>
- <tr>
- <th>序号</th>
- <th>操作时间</th>
- <th>操作名称</th>
- <th>操作得分</th>
- <th>操作参数1</th>
- <th>操作参数2</th>
- <th>操作参数3</th>
- <th>操作参数4</th>
- <th>操作参数5</th>
- </tr>
- </thead>
- <tbody>`)
- // 表格数据
- for _, operLog := range operLogs {
- // 处理操作时间 - 改为 hh:mm:ss 格式
- hours := operLog.OperTime / 3600
- minutes := (operLog.OperTime % 3600) / 60
- seconds := operLog.OperTime % 60
- operTimeStr := fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds)
- buf.WriteString("<tr>")
- buf.WriteString("<td>" + fmt.Sprintf("%d", operLog.RunOperID) + "</td>")
- buf.WriteString("<td>" + operTimeStr + "</td>")
- buf.WriteString("<td style='text-align: left;'>" + operLog.OperName + "</td>")
- buf.WriteString("<td>" + fmt.Sprintf("%.1f", operLog.OperScore) + "</td>")
- buf.WriteString("<td>" + operLog.OperParam1 + "</td>")
- buf.WriteString("<td>" + operLog.OperParam2 + "</td>")
- buf.WriteString("<td>" + operLog.OperParam3 + "</td>")
- buf.WriteString("<td>" + operLog.OperParam4 + "</td>")
- buf.WriteString("<td>" + operLog.OperParam5 + "</td>")
- buf.WriteString("</tr>")
- }
- buf.WriteString(`</tbody>
- </table>
-
- <div class="footer">导出时间: ` + time.Now().Format("2006-01-02 15:04:05") + `</div>
- </body>
- </html>`)
- return buf.String()
- }
- // /执行上传对应电子考评表到信息还系统
- func DoUploadTMS(RunLogID int) error {
- o := orm.NewOrm()
- // 查询课程运行记录信息
- var courseRunLog models.CourseRunLog
- err := o.Raw(`SELECT * FROM CourseRunLog WHERE 运行记录编号 = ?`, RunLogID).QueryRow(&courseRunLog)
- if err != nil {
- return fmt.Errorf("查询课程运行记录失败: %v", err)
- }
- // 查询该课程运行记录下的所有评价项
- var evaLogs []models.CourseEvaLog
- _, err = o.Raw(`
- SELECT *
- FROM CourseEvaLog
- WHERE 运行记录编号 = ?
- ORDER BY 序号
- `, RunLogID).QueryRows(&evaLogs)
- if err != nil {
- return fmt.Errorf("查询评价记录列表失败: %v", err)
- }
- // 查询所有角色信息用于关联角色名称
- var roleInfos []*models.RoleInfo
- _, err = o.QueryTable(new(models.RoleInfo)).All(&roleInfos)
- if err != nil {
- return fmt.Errorf("查询角色信息失败: %v", err)
- }
- // 创建角色ID到角色名称的映射
- roleMap := make(map[int]string)
- for _, role := range roleInfos {
- roleMap[role.RoleID] = role.RoleName
- }
- // 生成HTML内容
- controller := &EvaLogController{}
- htmlContent := controller.generateHTMLContent(courseRunLog, evaLogs, roleMap)
- // 创建临时HTML文件
- htmlFile, err := os.CreateTemp("", "course_evaluation_report_*.html")
- if err != nil {
- return fmt.Errorf("创建HTML文件失败: %v", err)
- }
- defer os.Remove(htmlFile.Name())
- // 写入HTML内容
- _, err = htmlFile.WriteString(htmlContent)
- if err != nil {
- htmlFile.Close()
- return fmt.Errorf("写入HTML文件失败: %v", err)
- }
- htmlFile.Close()
- // 创建临时PDF文件
- pdfFile, err := os.CreateTemp("", "course_evaluation_report_*.pdf")
- if err != nil {
- return fmt.Errorf("创建PDF文件失败: %v", err)
- }
- defer os.Remove(pdfFile.Name())
- pdfFile.Close()
- // 使用wkhtmltopdf将HTML转换为PDF
- cmd := exec.Command("wkhtmltopdf", "--encoding", "utf-8", htmlFile.Name(), pdfFile.Name())
- var stderr bytes.Buffer
- cmd.Stderr = &stderr
- err = cmd.Run()
- if err != nil {
- return fmt.Errorf("HTML转PDF失败: %v; %s", err, stderr.String())
- }
- // 准备上传到信息化系统的数据
- // 学员GUID(可能有多个,用逗号分隔)
- traineeGUIDs := courseRunLog.TraineeGUID
- // 课程得分
- score := fmt.Sprintf("%.2f", courseRunLog.CourseScore)
- // 构造请求URL
- url := common.GetAPIEndpoint(common.SVR_API_UPLOAD_AbilityScore)
- // 创建multipart表单数据
- var buf bytes.Buffer
- writer := multipart.NewWriter(&buf)
- // 添加表单字段
- _ = writer.WriteField("PXJLGUID", courseRunLog.TrainingRecordGUID)
- _ = writer.WriteField("XYEMPID", traineeGUIDs)
- _ = writer.WriteField("DF", score)
- // 添加文件字段
- fileWriter, _ := writer.CreateFormFile("FILE", fmt.Sprintf("course_evaluation_report_%d.pdf", RunLogID))
- pdfData, _ := ioutil.ReadFile(pdfFile.Name())
- _, _ = fileWriter.Write(pdfData)
- // 添加Content-Type字段
- _ = writer.WriteField("Content-Type", "multipart/form-data")
- // 关闭multipart writer
- writer.Close()
- // 创建请求
- req, err := http.NewRequest("POST", url, &buf)
- if err != nil {
- return fmt.Errorf("创建请求失败: %v", err)
- }
- // 设置请求头
- req.Header.Set("Content-Type", writer.FormDataContentType())
- // 从配置中获取token
- token, err := beego.AppConfig.String("SYSTOKEN")
- if err == nil && token != "" {
- req.Header.Set("token", token)
- }
- // 发送请求
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return fmt.Errorf("发送请求失败: %v", err)
- }
- defer resp.Body.Close()
- // 读取响应
- body, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return fmt.Errorf("读取响应失败: %v", err)
- }
- // 检查状态码
- if resp.StatusCode != http.StatusOK {
- return fmt.Errorf("信息化系统返回错误状态码: %d, 响应内容: %s", resp.StatusCode, string(body))
- }
- // 解析响应
- var result struct {
- ResultCode string `json:"ResultCode"`
- ResultDesc string `json:"ResultDesc"`
- }
- err = json.Unmarshal(body, &result)
- if err != nil {
- return fmt.Errorf("解析响应失败: %v", err)
- }
- // 检查结果
- if result.ResultCode != "0" {
- return fmt.Errorf("信息化系统处理失败: %s", result.ResultDesc)
- }
- return nil
- }
- // /evalog/uploadtms 课程评价记录上传信息化系统
- func (c *EvaLogController) UploadTMS() {
- type Request struct {
- RunLogID int `json:"runLogID"`
- }
- var req Request
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if req.RunLogID == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少运行记录ID参数",
- }
- c.ServeJSON()
- return
- }
- err := DoUploadTMS(req.RunLogID)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "上传信息还系统失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "上传信息还系统成功",
- }
- c.ServeJSON()
- }
|