| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823 |
- 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()
- }
- // /evalog/setscore 设置课程得分
- // /evalog/setscore 设置课程得分
- func (c *EvaLogController) SetScore() {
- type Request struct {
- RunLogID int `json:"runLogID"`
- SeqNo int `json:"seqNo"`
- EvaScore int `json:"evaScore"`
- }
- 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 evaLog models.CourseEvaLog
- err := o.Raw(`
- SELECT *
- FROM CourseEvaLog
- WHERE 运行记录编号 = ? AND 序号 = ?
- `, req.RunLogID, req.SeqNo).QueryRow(&evaLog)
- 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
- }
- // 验证分数范围应在0到评价赋分之间
- if req.EvaScore < 0 || req.EvaScore > evaLog.EvaScore {
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": fmt.Sprintf("分数应在0到%d之间", evaLog.EvaScore),
- }
- c.ServeJSON()
- return
- }
- operScore := req.EvaScore
- // 更新CourseEvaLog表中的操作得分
- _, err = o.Raw(`
- UPDATE CourseEvaLog
- SET 操作得分 = ?
- WHERE 运行记录编号 = ? AND 序号 = ?
- `, operScore, req.RunLogID, req.SeqNo).Exec()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新评价分数失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 重新计算整个课程的总得分
- 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,
- "evaScore": req.EvaScore,
- "operScore": operScore,
- "totalScore": totalScore,
- },
- }
- c.ServeJSON()
- }
|