| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824 |
- 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()
- }
|