| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136 |
- package controllers
- import (
- "Tech/common"
- "Tech/models" // 根据实际项目结构调整导入路径
- "encoding/json"
- "fmt"
- "os"
- "path"
- "strconv"
- "time"
- "github.com/beego/beego/v2/client/orm"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/google/uuid" // 新增导入
- )
- type CourseController struct {
- beego.Controller
- }
- // GetCourseGUIDsByJXJHID 根据培训计划ID获取课程GUID列表
- func GetCourseGUIDsByJXJHID(jxjhID, userName string) ([]string, error) {
- // 定义请求结构体
- type Request struct {
- SBID string `json:"SBID"` // 设备编号
- JYEMPID string `json:"JYEMPID"` // 教员GUID
- XYEMPID string `json:"XYEMPID"` // 学员GUID
- }
- var req Request
- // 从app.conf中读取SYSDEVICEID作为SBID的值
- sbid, err := beego.AppConfig.String("SYSDEVICEID")
- if err != nil {
- return nil, fmt.Errorf("获取设备编号失败: %v", err)
- }
- req.SBID = sbid
- // 从缓存中获取用户信息
- userInfo, exists := GetUserInfoFromCache(userName)
- if !exists {
- return nil, fmt.Errorf("用户信息不存在: ")
- }
- switch userInfo.SF {
- case "1": // 如果是教员,则使用当前用户的RYEMPID
- req.XYEMPID = userInfo.RYEMPID
- case "2": // 如果是学员,则使用当前用户的RYEMPID
- req.JYEMPID = userInfo.RYEMPID
- }
- // 调用信息化接口获取培训计划
- response, err := common.GetTeachPlanFromInformationSystem(req.SBID, req.JYEMPID, req.XYEMPID)
- if err != nil {
- return nil, fmt.Errorf("获取培训计划失败: %v", err)
- }
- // 判断请求是否成功
- if response.ResultCode != "0" {
- return nil, fmt.Errorf("获取培训计划失败: %s", response.ResultDesc)
- }
- // 收集匹配的培训计划下的所有课程GUID
- var courseGUIDs []string
- courseGUIDSet := make(map[string]bool) // 用于去重
- // 遍历所有培训实施计划
- for _, pxss := range response.PXSS {
- // 遍历所有计划
- for _, jh := range pxss.JH {
- // 检查是否是目标培训计划
- if jh.ID == jxjhID {
- // 添加SDKC中的KCGUID
- for _, sdkc := range jh.SDKC {
- if sdkc.KCGUID != "" && !courseGUIDSet[sdkc.KCGUID] {
- courseGUIDs = append(courseGUIDs, sdkc.KCGUID)
- courseGUIDSet[sdkc.KCGUID] = true
- }
- }
- // 添加TESTST中的KCGUID
- for _, testst := range jh.TESTST {
- if testst.KCGUID != "" && !courseGUIDSet[testst.KCGUID] {
- courseGUIDs = append(courseGUIDs, testst.KCGUID)
- courseGUIDSet[testst.KCGUID] = true
- }
- }
- }
- }
- }
- return courseGUIDs, nil
- }
- // /course/list 获取所有课程信息列表
- func (c *CourseController) List() {
- type Query struct {
- CourseID int `form:"courseID"`
- CourseName string `form:"courseName"`
- TypeID int `form:"typeID"`
- Creator string `form:"creator"`
- LineNo int `form:"lineNo"`
- JXJHID string `form:"JXJHID"` // 培训计划ID
- }
- var query Query
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &query); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(models.CourseInfo))
- // 编号查询
- if query.CourseID != 0 {
- qs = qs.Filter("CourseID", query.CourseID)
- }
- // 课程名称模糊查询
- if query.CourseName != "" {
- qs = qs.Filter("CourseName__icontains", query.CourseName)
- }
- // 培训类型查询(分类ID)
- if query.TypeID != 0 {
- qs = qs.Filter("TypeID", query.TypeID)
- }
- // 创建人查询
- if query.Creator != "" {
- qs = qs.Filter("Creator", query.Creator)
- }
- // 线路查询(线路号)
- if query.LineNo != 0 {
- qs = qs.Filter("LineNo", query.LineNo)
- }
- // 如果提供了培训计划ID(JXJHID),则从信息化系统获取对应的课程GUID列表
- var courseGUIDs []string
- if query.JXJHID != "" {
- // 从Header中获取userName
- authHeader := c.Ctx.Input.Header("userName")
- if authHeader == "" {
- c.Ctx.Output.SetStatus(401)
- c.Data["json"] = map[string]interface{}{
- "code": 401,
- "msg": "未提供认证信息",
- }
- c.ServeJSON()
- return
- }
- var err error
- courseGUIDs, err = GetCourseGUIDsByJXJHID(query.JXJHID, authHeader)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取培训计划课程列表失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 如果找到了课程GUID列表,则添加过滤条件
- if len(courseGUIDs) > 0 {
- qs = qs.Filter("CourseGUID__in", courseGUIDs)
- } else {
- // 如果没有找到匹配的课程,则返回空结果
- var courses []models.CourseInfo
- c.Data["json"] = map[string]interface{}{"code": 200, "count": 0, "msg": "", "data": courses}
- c.ServeJSON()
- return
- }
- }
- var courses []models.CourseInfo
- count, err := qs.OrderBy("-CreateTime").All(&courses, "课程ID", "课程GUID", "课程名称", "分类ID", "外部分类ID", "虚拟起始时间",
- "课程总时长", "线路号", "运行图", "课程描述", "创建人", "创建时间", "修改时间", "指导描述", "指导文件地址", "虚拟角色", "课程分类ID", "课程分类名称", "创建人ID")
- if err != nil {
- c.Data["json"] = map[string]interface{}{"code": 500, "error": "查询失败: " + err.Error()}
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{"code": 200, "count": count, "msg": "", "data": courses}
- c.ServeJSON()
- }
- // /course/listpage 获取所有课程信息列表
- func (c *CourseController) ListPage() {
- type Request struct {
- PageNum int `json:"pageNum"`
- PageSize int `json:"pageSize"`
- CourseID int `json:"courseID"`
- CourseName string `json:"courseName"`
- TypeID int `json:"typeID"`
- Creator string `json:"creator"`
- LineNo int `json:"lineNo"`
- }
- var req Request
- if err := json.Unmarshal(c.Ctx.Input.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.PageNum <= 0 {
- req.PageNum = 1
- }
- if req.PageSize <= 0 {
- req.PageSize = 10
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(models.CourseInfo))
- // 编号查询
- if req.CourseID != 0 {
- qs = qs.Filter("CourseID", req.CourseID)
- }
- // 课程名称模糊查询
- if req.CourseName != "" {
- qs = qs.Filter("CourseName__icontains", req.CourseName)
- }
- // 培训类型查询(分类ID)
- if req.TypeID != 0 {
- qs = qs.Filter("TypeID", req.TypeID)
- }
- // 创建人查询
- if req.Creator != "" {
- qs = qs.Filter("Creator__icontains", req.Creator)
- }
- // 线路查询(线路号)
- if req.LineNo != 0 {
- qs = qs.Filter("LineNo", req.LineNo)
- }
- // 获取总数
- totalCount, err := qs.Count()
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 计算总页数
- totalPage := int((totalCount + int64(req.PageSize) - 1) / int64(req.PageSize))
- // 分页查询,按创建时间倒序排列
- var courses []models.CourseInfo
- _, err = qs.OrderBy("-CreateTime").Limit(req.PageSize).Offset((req.PageNum-1)*req.PageSize).All(&courses,
- "课程ID", "课程GUID", "课程名称", "分类ID", "外部分类ID", "虚拟起始时间", "课程总时长", "线路号", "运行图",
- "课程描述", "创建人", "创建时间", "修改时间", "指导描述", "指导文件地址", "虚拟角色", "课程分类ID", "课程分类名称", "创建人ID")
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 构造分页数据
- pageData := map[string]interface{}{
- "pageNum": req.PageNum,
- "pageSize": req.PageSize,
- "totalCount": totalCount,
- "totalPage": totalPage,
- "list": courses,
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "success",
- "data": pageData,
- }
- c.ServeJSON()
- }
- // /course/add 新增课程信息
- func (c *CourseController) Add() {
- var course models.CourseInfo
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &course); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- course.CourseID = 0
- course.CourseGUID = ""
- o := orm.NewOrm()
- // 自动生成 CourseID
- if course.CourseID == 0 {
- var maxID int
- err := o.Raw("SELECT MAX(课程ID) FROM CourseInfo").QueryRow(&maxID)
- if err != nil {
- c.Data["json"] = map[string]interface{}{"code": 500, "msg": "获取最大ID失败: " + err.Error()}
- c.ServeJSON()
- return
- }
- course.CourseID = maxID + 1
- }
- // 自动生成 CourseGUID
- if course.CourseGUID == "" {
- course.CourseGUID = uuid.New().String()
- }
- course.CreateTime = time.Now().Format("2006-01-02 15:04:05")
- course.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
- _, err := o.Insert(&course)
- 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{}{
- "courseID": course.CourseID,
- "courseGUID": course.CourseGUID,
- },
- }
- c.ServeJSON()
- }
- // /course/update 更新课程信息
- func (c *CourseController) Update() {
- var course models.CourseInfo
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &course); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- course.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
- _, err := o.Update(&course, "课程名称", "分类ID", "外部分类ID", "虚拟起始时间", "课程总时长", "线路号", "运行图", "课程描述", "创建人",
- "修改时间", "指导描述", "指导文件地址", "课程分类ID", "课程分类名称", "创建人ID")
- 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()
- }
- // Upload 方法处理文件上传
- func (c *CourseController) Upload() {
- // 1. 获取上传的文件
- file, header, err := c.GetFile("file") // "file" 是表单中文件字段的 name:cite[3]:cite[6]
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- defer file.Close()
- // // 2. 获取课程ID参数
- // courseId, err := c.GetInt("courseId")
- // if err != nil {
- // c.Ctx.Output.SetStatus(400)
- // c.Data["json"] = map[string]interface{}{
- // "code": 400,
- // "msg": "缺少课程ID参数或参数格式错误: " + err.Error(),
- // }
- // c.ServeJSON()
- // return
- // }
- // // 2. 节点ID为0
- // nodeId, err := c.GetInt("nodeId")
- // if err != nil {
- // println("节点ID为null")
- // }
- // strDir := fmt.Sprintf("static/upload/%d/", courseId)
- // if nodeId > 0 {
- // strDir = fmt.Sprintf("static/upload/%d/%d/", courseId, nodeId)
- // }
- strDir := "static/upload/"
- if _, err := os.Stat(strDir); os.IsNotExist(err) {
- os.MkdirAll(strDir, os.ModePerm)
- }
- uploadPath := path.Join(strDir, header.Filename) // 文件保存路径
- // 3. 保存文件:cite[3]:cite[6]
- err = c.SaveToFile("file", uploadPath)
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "文件保存失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 4. 构建文件的访问 URL:cite[2]
- fileUrl := uploadPath
- // //保存到课程信息中 指导文件地址 字段
- // o := orm.NewOrm()
- // //保存到课程或者节点里面
- // if nodeId == 0 {
- // course := models.CourseInfo{CourseID: courseId}
- // if o.Read(&course, "课程ID") == nil {
- // course.GuideFilePath = fileUrl
- // course.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
- // if _, err := o.Update(&course, "指导文件地址", "修改时间"); err != nil {
- // c.Ctx.Output.SetStatus(500)
- // c.Data["json"] = map[string]interface{}{
- // "code": 500,
- // "msg": "更新课程信息失败: " + err.Error(),
- // }
- // c.ServeJSON()
- // return
- // }
- // }
- // } else {
- // //保存到节点里面
- // node := models.CourseActionNode{NodeID: nodeId}
- // if o.Read(&node, "节点ID") == nil {
- // node.GuideFilePath = fileUrl
- // if _, err := o.Update(&node, "指导文件地址"); err != nil {
- // c.Ctx.Output.SetStatus(500)
- // c.Data["json"] = map[string]interface{}{
- // "code": 500,
- // "msg": "更新节点信息失败: " + err.Error(),
- // }
- // c.ServeJSON()
- // return
- // }
- // }
- // }
- // 5. 返回成功信息和文件 URL:cite[3]
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "message": "文件上传成功",
- "data": map[string]string{
- "url": fileUrl,
- },
- }
- c.ServeJSON()
- }
- // /course/delete 删除课程信息
- func (c *CourseController) Delete() {
- // 解析请求中的课程ID数组
- var req []struct {
- CourseID int `json:"courseId"`
- }
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if len(req) == 0 {
- c.Data["json"] = map[string]interface{}{"code": 400, "msg": "请提供要删除的课程ID"}
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 记录删除成功的课程数量
- successCount := 0
- failedCourses := make([]map[string]interface{}, 0)
- // 提取课程ID列表
- courseIds := make([]int, len(req))
- for i, item := range req {
- courseIds[i] = item.CourseID
- }
- for _, courseID := range courseIds {
- // 先删除相关的课程运行记录
- _, err := o.Raw("DELETE FROM CourseRunLog WHERE 运行课程ID = ?", courseID).Exec()
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseId": courseID,
- "reason": "删除课程运行记录失败: " + err.Error(),
- })
- continue
- }
- // 删除课程运行操作日志记录
- _, err = o.Raw("DELETE FROM CourseOperLog WHERE 运行课程ID = ?", courseID).Exec()
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseId": courseID,
- "reason": "删除课程操作日志失败: " + err.Error(),
- })
- continue
- }
- // 删除课程评价日志记录
- _, err = o.Raw("DELETE FROM CourseEvaLog WHERE 运行课程ID = ?", courseID).Exec()
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseId": courseID,
- "reason": "删除课程评价日志失败: " + err.Error(),
- })
- continue
- }
- // 删除课程基本信息
- nCount, err := o.Delete(&models.CourseInfo{CourseID: courseID})
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseId": courseID,
- "reason": "删除失败: " + err.Error(),
- })
- continue
- }
- if nCount == 0 {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseId": courseID,
- "reason": "课程不存在",
- })
- continue
- }
- // 成功删除课程基本信息后,清理相关节点数据
- ClearCourse(courseID)
- successCount++
- }
- // 构造返回结果
- result := map[string]interface{}{
- "code": 200,
- "msg": fmt.Sprintf("删除成功%d条记录,失败%d条记录", successCount, len(failedCourses)),
- }
- if len(failedCourses) > 0 {
- result["failed"] = failedCourses
- }
- c.Data["json"] = result
- c.ServeJSON()
- }
- // /course/detail/get 获取课程详细信息
- func (c *CourseController) GetDetail() {
- type Query struct {
- CourseId int `json:"courseId"`
- }
- var query Query
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &query); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if query.CourseId == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少课程ID",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- var course models.CourseInfo
- err := o.QueryTable(new(models.CourseInfo)).Filter("课程ID", query.CourseId).One(&course)
- if err != nil {
- c.Ctx.Output.SetStatus(404)
- c.Data["json"] = map[string]interface{}{
- "code": 404,
- "msg": "课程不存在: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "data": course,
- }
- c.ServeJSON()
- }
- // /course/detail/set 设置课程详细信息
- func (c *CourseController) SetDetail() {
- type Query struct {
- CourseId int `json:"courseId"`
- CourseDataStream string `json:"courseDataStream"`
- }
- var query Query
- requestBody := c.Ctx.Input.RequestBody
- // println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &query); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "传输的JSON格式不正确: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if query.CourseId == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "缺少课程ID",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- var course models.CourseInfo
- err := o.QueryTable(new(models.CourseInfo)).Filter("课程ID", query.CourseId).One(&course)
- if err != nil {
- c.Ctx.Output.SetStatus(404)
- c.Data["json"] = map[string]interface{}{
- "code": 404,
- "msg": "课程不存在: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 更新 FlowData 字段
- course.CourseDataStream = query.CourseDataStream
- course.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
- _, err = o.Update(&course, "课程数据流", "修改时间")
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- //保存数据
- isRet, errmsg := ParserAndSaveNode(query.CourseId, query.CourseDataStream)
- if !isRet {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "更新节点失败: " + errmsg,
- }
- c.ServeJSON()
- return
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "课程数据流更新成功",
- }
- c.ServeJSON()
- }
- // /coursetype/list 获取课程分类列表
- func (c *CourseController) GetCourseTypeList() {
- o := orm.NewOrm()
- // 查询所有课程分类
- var courseTypes []*models.CourseType
- count, err := o.QueryTable(new(models.CourseType)).All(&courseTypes)
- 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,
- "count": count,
- "msg": "",
- "data": courseTypes,
- }
- c.ServeJSON()
- }
- // /role/list 获取所有通讯角色列表
- func (c *CourseController) GetRoleList() {
- o := orm.NewOrm()
- // 查询所有通讯角色
- var roleInfos []*models.RoleInfo
- count, 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
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "count": count,
- "msg": "",
- "data": roleInfos,
- }
- c.ServeJSON()
- }
- // /templateoperation/list 获取所有操作类型模板列表
- func (c *CourseController) GetTemplateOperationList() {
- o := orm.NewOrm()
- // 查询所有操作类型模板
- var templates []*models.TemplateOperation
- count, err := o.QueryTable(new(models.TemplateOperation)).All(&templates)
- 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,
- "count": count,
- "msg": "",
- "data": templates,
- }
- c.ServeJSON()
- }
- // /templatevoice/list 获取所有语音类型模板列表
- func (c *CourseController) GetTemplateVoiceList() {
- o := orm.NewOrm()
- // 查询所有语音类型模板
- var templates []*models.TemplateVoice
- count, err := o.QueryTable(new(models.TemplateVoice)).All(&templates)
- 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,
- "count": count,
- "msg": "",
- "data": templates,
- }
- c.ServeJSON()
- }
- // /templatefault/list 获取所有故障类型模板列表
- func (c *CourseController) GetTemplateFaultList() {
- o := orm.NewOrm()
- // 查询所有故障类型模板
- var templates []*models.TemplateFault
- count, err := o.QueryTable(new(models.TemplateFault)).All(&templates)
- 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,
- "count": count,
- "msg": "",
- "data": templates,
- }
- c.ServeJSON()
- }
- // /dict/list 获取所有字典列表
- func (c *CourseController) GetDictList() {
- type Query struct {
- DictionaryType int `json:"dictionaryType"`
- DictionarySubType int `json:"dictionarySubType"`
- }
- var query Query
- requestBody := c.Ctx.Input.RequestBody
- println("Request Body:", string(requestBody))
- if err := json.Unmarshal(requestBody, &query); 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 dicts []*models.TypeMapList
- qs := o.QueryTable(new(models.TypeMapList))
- if query.DictionaryType != 0 {
- qs = qs.Filter("DictionaryType", query.DictionaryType)
- }
- if query.DictionarySubType != 0 {
- qs = qs.Filter("DictionarySubType", query.DictionarySubType)
- }
- count, err := qs.All(&dicts)
- 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,
- "count": count,
- "msg": "",
- "data": dicts,
- }
- c.ServeJSON()
- }
- // /dict/listall 获取所有字典列表
- func (c *CourseController) GetDictListAll() {
- o := orm.NewOrm()
- var dicts []*models.TypeMapList
- qs := o.QueryTable(new(models.TypeMapList))
- count, err := qs.All(&dicts)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询字典列表失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 按照指定格式组织数据
- type Item struct {
- KeyParam int `json:"keyParam"`
- ValueParam string `json:"valueParam"`
- }
- type Dict struct {
- DictionaryType int `json:"dictionaryType"`
- DictionaryTypeName string `json:"dictionaryTypeName"`
- DictionarySubType int `json:"dictionarySubType"`
- DictionarySubTypeName string `json:"dictionarySubTypeName"`
- KeyType string `json:"keyType"`
- Items []Item `json:"items"`
- }
- // 将数据转换为所需的格式
- dictMap := make(map[string]*Dict)
- for _, dict := range dicts {
- key := fmt.Sprintf("%d_%d", dict.DictionaryType, dict.DictionarySubType)
- if d, exists := dictMap[key]; exists {
- // 如果已存在该字典类型,则添加item
- d.Items = append(d.Items, Item{
- KeyParam: dict.KeyParam,
- ValueParam: dict.ValueParam,
- })
- } else {
- // 创建新的字典类型
- dictMap[key] = &Dict{
- DictionaryType: dict.DictionaryType,
- DictionaryTypeName: dict.DictionaryTypeName,
- DictionarySubType: dict.DictionarySubType,
- DictionarySubTypeName: dict.DictionarySubTypeName,
- KeyType: fmt.Sprintf("%d_%d", dict.DictionaryType, dict.DictionarySubType),
- Items: []Item{{
- KeyParam: dict.KeyParam,
- ValueParam: dict.ValueParam,
- }},
- }
- }
- }
- // 将map转换为slice
- result := make([]Dict, 0, len(dictMap))
- for _, dict := range dictMap {
- result = append(result, *dict)
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "count": count,
- "msg": "",
- "data": map[string]interface{}{
- "dicts": result,
- },
- }
- c.ServeJSON()
- }
- // /course/copy 课程复制
- func (c *CourseController) CourseCopy() {
- // 解析请求中的课程ID数组
- var req []struct {
- CourseID int `json:"courseId"`
- }
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if len(req) == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请提供要复制的课程ID",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 存储复制成功的课程信息
- successCourses := make([]map[string]interface{}, 0)
- // 存储复制失败的课程信息
- failedCourses := make([]map[string]interface{}, 0)
- // 提取课程ID列表
- courseIds := make([]int, len(req))
- for i, item := range req {
- courseIds[i] = item.CourseID
- }
- for _, courseID := range courseIds {
- // 获取原课程信息
- var originalCourse models.CourseInfo
- err := o.QueryTable(new(models.CourseInfo)).Filter("CourseID", courseID).One(&originalCourse)
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "未找到要复制的课程: " + err.Error(),
- })
- continue
- }
- // 创建新课程
- newCourse := originalCourse
- newCourse.CourseID = 0 // 让ORM自动生成新的ID
- newCourse.CourseGUID = uuid.New().String()
- newCourse.CreateTime = time.Now().Format("2006-01-02 15:04:05")
- newCourse.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
- // 自动生成 CourseID
- if newCourse.CourseID == 0 {
- var maxID int
- err := o.Raw("SELECT MAX(课程ID) FROM CourseInfo").QueryRow(&maxID)
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "获取最大ID失败: " + err.Error(),
- })
- continue
- }
- newCourse.CourseID = maxID + 1
- }
- // 插入新课程
- _, err = o.Insert(&newCourse)
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "创建新课程失败: " + err.Error(),
- })
- continue
- }
- // 复制课程相关的节点数据
- // 复制 CourseActionNode
- var actionNodes []models.CourseActionNode
- _, err = o.QueryTable(new(models.CourseActionNode)).Filter("CourseID", courseID).All(&actionNodes)
- if err != nil && err != orm.ErrNoRows {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "查询动作节点失败: " + err.Error(),
- })
- // 回滚已创建的新课程
- o.Delete(&newCourse)
- continue
- }
- for _, node := range actionNodes {
- node.CourseID = newCourse.CourseID
- _, err = o.Insert(&node)
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "复制动作节点失败: " + err.Error(),
- })
- // 回滚已创建的新课程和节点数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- break
- }
- }
- // 如果复制动作节点失败,则跳过该课程
- if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
- continue
- }
- // 复制 CourseOperationNode
- var operationNodes []models.CourseOperationNode
- _, err = o.QueryTable(new(models.CourseOperationNode)).Filter("CourseID", courseID).All(&operationNodes)
- if err != nil && err != orm.ErrNoRows {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "查询操作节点失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- continue
- }
- for _, node := range operationNodes {
- node.CourseID = newCourse.CourseID
- // 如果主观评价标记未设置,默认为0(客观评价)
- if node.SubjectiveEvaluation == 0 {
- node.SubjectiveEvaluation = 0 // 默认为客观评价
- }
- _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "复制操作节点失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- break
- }
- }
- // 如果复制操作节点失败,则跳过该课程
- if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
- continue
- }
- // 复制 CourseFaultNode
- var faultNodes []models.CourseFaultNode
- _, err = o.QueryTable(new(models.CourseFaultNode)).Filter("CourseID", courseID).All(&faultNodes)
- if err != nil && err != orm.ErrNoRows {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "查询故障节点失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- continue
- }
- for _, node := range faultNodes {
- node.CourseID = newCourse.CourseID
- // 注意:CourseFaultNode的CourseID和NodeID是联合主键
- _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "复制故障节点失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- break
- }
- }
- // 如果复制故障节点失败,则跳过该课程
- if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
- continue
- }
- // 复制 CourseVoiceNode
- var voiceNodes []models.CourseVoiceNode
- _, err = o.QueryTable(new(models.CourseVoiceNode)).Filter("CourseID", courseID).All(&voiceNodes)
- if err != nil && err != orm.ErrNoRows {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "查询语音节点失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- continue
- }
- for _, node := range voiceNodes {
- node.CourseID = newCourse.CourseID
- // 注意:CourseVoiceNode的CourseID和NodeID是联合主键
- _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "复制语音节点失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- break
- }
- }
- // 如果复制语音节点失败,则跳过该课程
- if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
- continue
- }
- // 复制 CourseNodeTrigger
- var triggers []models.CourseNodeTrigger
- _, err = o.QueryTable(new(models.CourseNodeTrigger)).Filter("CourseID", courseID).All(&triggers)
- if err != nil && err != orm.ErrNoRows {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "查询节点触发器失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- continue
- }
- for _, trigger := range triggers {
- trigger.CourseID = newCourse.CourseID
- // 注意:CourseNodeTrigger的CourseID和NodeID是联合主键
- _, _, err = o.ReadOrCreate(&trigger, "CourseID", "NodeID")
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "复制节点触发器失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseNodeTrigger WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- break
- }
- }
- // 如果复制节点触发器失败,则跳过该课程
- if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
- continue
- }
- // 复制 CourseEvaList
- var evals []models.CourseEvaList
- _, err = o.QueryTable(new(models.CourseEvaList)).Filter("CourseID", courseID).All(&evals)
- if err != nil && err != orm.ErrNoRows {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "查询评价列表失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseNodeTrigger WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- continue
- }
- for _, eval := range evals {
- eval.CourseID = newCourse.CourseID
- // 查询当前最大的序号
- var maxSeqNo int
- err = o.Raw("SELECT COALESCE(MAX(序号), 0) FROM CourseEvaList WHERE 课程ID = ?", newCourse.CourseID).QueryRow(&maxSeqNo)
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "查询评价序号失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseNodeTrigger WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseEvaList WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- break
- }
- eval.SeqNo = maxSeqNo + 1
- _, err = o.Insert(&eval)
- if err != nil {
- failedCourses = append(failedCourses, map[string]interface{}{
- "courseID": courseID,
- "reason": "复制评价项失败: " + err.Error(),
- })
- // 回滚已创建的数据
- o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseNodeTrigger WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Raw("DELETE FROM CourseEvaList WHERE 课程ID = ?", newCourse.CourseID).Exec()
- o.Delete(&newCourse)
- break
- }
- }
- // 如果复制评价项失败,则跳过该课程
- if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
- continue
- }
- // 所有复制操作都成功完成
- successCourses = append(successCourses, map[string]interface{}{
- "originalCourseID": courseID,
- "newCourseID": newCourse.CourseID,
- "newCourseGUID": newCourse.CourseGUID,
- "newCourseName": newCourse.CourseName,
- })
- }
- // 构造返回结果
- data := map[string]interface{}{}
- if len(successCourses) > 0 {
- data["success"] = successCourses
- }
- if len(failedCourses) > 0 {
- data["failed"] = failedCourses
- }
- response := map[string]interface{}{
- "code": 200,
- "msg": fmt.Sprintf("批量复制完成,成功%d条,失败%d条", len(successCourses), len(failedCourses)),
- "data": data,
- }
- c.Data["json"] = response
- c.ServeJSON()
- }
- // /course/import 课程导入
- func (c *CourseController) CourseImport() {
- // 获取上传的文件
- file, header, err := c.GetFile("file")
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- defer file.Close()
- // 检查文件扩展名
- ext := path.Ext(header.Filename)
- if ext != ".json" {
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "只支持导入JSON格式文件",
- }
- c.ServeJSON()
- return
- }
- // 读取文件内容
- fileSize := header.Size
- buffer := make([]byte, fileSize)
- _, err = file.Read(buffer)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "读取文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 解析JSON内容 - 支持多个课程
- var importData []struct {
- CourseInfo models.CourseInfo `json:"courseInfo"`
- ActionNodes []models.CourseActionNode `json:"actionNodes"`
- OperationNodes []models.CourseOperationNode `json:"operationNodes"`
- FaultNodes []models.CourseFaultNode `json:"faultNodes"`
- VoiceNodes []models.CourseVoiceNode `json:"voiceNodes"`
- NodeTriggers []models.CourseNodeTrigger `json:"nodeTriggers"`
- EvalLists []models.CourseEvaList `json:"evalLists"`
- }
- err = json.Unmarshal(buffer, &importData)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "解析JSON文件失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 存储导入结果
- importResults := make([]map[string]interface{}, 0)
- // 遍历所有要导入的课程
- for _, courseData := range importData {
- // 创建新课程
- newCourse := courseData.CourseInfo
- newCourse.CourseID = 0 // 让ORM生成新的ID
- newCourse.CourseGUID = uuid.New().String()
- newCourse.CreateTime = time.Now().Format("2006-01-02 15:04:05")
- newCourse.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
- // 检查是否已存在同名课程,如果存在则添加序号进行区分
- originalName := newCourse.CourseName
- duplicateIndex := 1
- for {
- var existingCourse models.CourseInfo
- err := o.QueryTable(new(models.CourseInfo)).Filter("CourseName", newCourse.CourseName).One(&existingCourse)
- if err != nil && err != orm.ErrNoRows {
- importResults = append(importResults, map[string]interface{}{
- "courseName": originalName,
- "status": "failed",
- "msg": "检查课程名称唯一性失败: " + err.Error(),
- })
- break
- }
- // 如果没有找到同名课程,则可以使用该名称
- if err == orm.ErrNoRows {
- break
- }
- // 如果找到同名课程,则在名称后添加序号
- newCourse.CourseName = fmt.Sprintf("%s(%d)", originalName, duplicateIndex)
- duplicateIndex++
- }
- // 自动生成 CourseID
- if newCourse.CourseID == 0 {
- var maxID int
- err := o.Raw("SELECT MAX(课程ID) FROM CourseInfo").QueryRow(&maxID)
- if err != nil {
- c.Data["json"] = map[string]interface{}{"code": 500, "msg": "获取最大ID失败: " + err.Error()}
- c.ServeJSON()
- return
- }
- newCourse.CourseID = maxID + 1
- }
- _, err = o.Insert(&newCourse)
- if err != nil {
- importResults = append(importResults, map[string]interface{}{
- "courseName": courseData.CourseInfo.CourseName,
- "status": "failed",
- "msg": "创建课程失败: " + err.Error(),
- })
- continue
- }
- // 导入 CourseActionNode
- for _, node := range courseData.ActionNodes {
- node.CourseID = newCourse.CourseID
- _, err = o.Insert(&node)
- if err != nil {
- importResults = append(importResults, map[string]interface{}{
- "courseName": courseData.CourseInfo.CourseName,
- "status": "failed",
- "msg": "导入动作节点失败: " + err.Error(),
- })
- continue
- }
- }
- // 导入 CourseOperationNode
- for _, node := range courseData.OperationNodes {
- node.CourseID = newCourse.CourseID
- // 如果主观评价标记未设置,默认为0(客观评价)
- if node.SubjectiveEvaluation == 0 {
- node.SubjectiveEvaluation = 0 // 默认为客观评价
- }
- _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
- if err != nil {
- importResults = append(importResults, map[string]interface{}{
- "courseName": courseData.CourseInfo.CourseName,
- "status": "failed",
- "msg": "导入操作节点失败: " + err.Error(),
- })
- continue
- }
- }
- // 导入 CourseFaultNode
- for _, node := range courseData.FaultNodes {
- node.CourseID = newCourse.CourseID
- _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
- if err != nil {
- importResults = append(importResults, map[string]interface{}{
- "courseName": courseData.CourseInfo.CourseName,
- "status": "failed",
- "msg": "导入故障节点失败: " + err.Error(),
- })
- continue
- }
- }
- // 导入 CourseVoiceNode
- for _, node := range courseData.VoiceNodes {
- node.CourseID = newCourse.CourseID
- _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
- if err != nil {
- importResults = append(importResults, map[string]interface{}{
- "courseName": courseData.CourseInfo.CourseName,
- "status": "failed",
- "msg": "导入语音节点失败: " + err.Error(),
- })
- continue
- }
- }
- // 导入 CourseNodeTrigger
- for _, trigger := range courseData.NodeTriggers {
- trigger.CourseID = newCourse.CourseID
- _, _, err = o.ReadOrCreate(&trigger, "CourseID", "NodeID")
- if err != nil {
- importResults = append(importResults, map[string]interface{}{
- "courseName": courseData.CourseInfo.CourseName,
- "status": "failed",
- "msg": "导入节点触发器失败: " + err.Error(),
- })
- continue
- }
- }
- // 导入 CourseEvaList
- for i, eval := range courseData.EvalLists {
- eval.CourseID = newCourse.CourseID
- eval.SeqNo = i + 1 // 重新编号
- _, err = o.Insert(&eval)
- if err != nil {
- importResults = append(importResults, map[string]interface{}{
- "courseName": courseData.CourseInfo.CourseName,
- "status": "failed",
- "msg": "导入评价项失败: " + err.Error(),
- })
- continue
- }
- }
- importResults = append(importResults, map[string]interface{}{
- "courseName": newCourse.CourseName,
- "status": "success",
- "data": map[string]interface{}{
- "courseID": newCourse.CourseID,
- "courseGUID": newCourse.CourseGUID,
- "courseName": newCourse.CourseName,
- },
- })
- }
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "课程导入完成",
- "data": importResults,
- "counts": map[string]int{
- "total": len(importData),
- "success": len(importResults) - failedCount(importResults),
- "failed": failedCount(importResults),
- },
- }
- c.ServeJSON()
- }
- // 辅助函数:计算导入失败的数量
- func failedCount(results []map[string]interface{}) int {
- count := 0
- for _, result := range results {
- if status, ok := result["status"].(string); ok && status == "failed" {
- count++
- }
- }
- return count
- }
- // // course/export 课程导出
- func (c *CourseController) CourseExport() {
- // 解析请求中的课程ID数组
- var req []struct {
- CourseID int `json:"courseId"`
- }
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if len(req) == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "参数不完整",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 存储所有要导出的课程数据
- exportData := make([]map[string]interface{}, 0)
- // 遍历所有请求的课程ID
- for _, item := range req {
- if item.CourseID <= 0 {
- continue
- }
- // 获取课程信息
- var course models.CourseInfo
- err := o.QueryTable(new(models.CourseInfo)).Filter("CourseID", item.CourseID).One(&course)
- if err != nil {
- // 如果找不到课程,继续处理下一个
- continue
- }
- // 获取 CourseActionNode
- var actionNodes []models.CourseActionNode
- _, err = o.QueryTable(new(models.CourseActionNode)).Filter("CourseID", item.CourseID).All(&actionNodes)
- if err != nil && err != orm.ErrNoRows {
- // 错误处理,但继续处理下一个课程
- continue
- }
- // 获取 CourseOperationNode
- var operationNodes []models.CourseOperationNode
- _, err = o.QueryTable(new(models.CourseOperationNode)).Filter("CourseID", item.CourseID).All(&operationNodes)
- if err != nil && err != orm.ErrNoRows {
- // 错误处理,但继续处理下一个课程
- continue
- }
- // 获取 CourseFaultNode
- var faultNodes []models.CourseFaultNode
- _, err = o.QueryTable(new(models.CourseFaultNode)).Filter("CourseID", item.CourseID).All(&faultNodes)
- if err != nil && err != orm.ErrNoRows {
- // 错误处理,但继续处理下一个课程
- continue
- }
- // 获取 CourseVoiceNode
- var voiceNodes []models.CourseVoiceNode
- _, err = o.QueryTable(new(models.CourseVoiceNode)).Filter("CourseID", item.CourseID).All(&voiceNodes)
- if err != nil && err != orm.ErrNoRows {
- // 错误处理,但继续处理下一个课程
- continue
- }
- // 获取 CourseNodeTrigger
- var triggers []models.CourseNodeTrigger
- _, err = o.QueryTable(new(models.CourseNodeTrigger)).Filter("CourseID", item.CourseID).All(&triggers)
- if err != nil && err != orm.ErrNoRows {
- // 错误处理,但继续处理下一个课程
- continue
- }
- // 获取 CourseEvaList
- var evalLists []models.CourseEvaList
- _, err = o.QueryTable(new(models.CourseEvaList)).Filter("CourseID", item.CourseID).All(&evalLists)
- if err != nil && err != orm.ErrNoRows {
- // 错误处理,但继续处理下一个课程
- continue
- }
- // 构造单个课程的导出数据
- singleExportData := map[string]interface{}{
- "courseInfo": course,
- "actionNodes": actionNodes,
- "operationNodes": operationNodes,
- "faultNodes": faultNodes,
- "voiceNodes": voiceNodes,
- "nodeTriggers": triggers,
- "evalLists": evalLists,
- }
- exportData = append(exportData, singleExportData)
- }
- // 转换为JSON
- jsonData, err := json.Marshal(exportData)
- if err != nil {
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "序列化数据失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 设置响应头,触发浏览器下载
- filename := "courses_export.json"
- if len(req) == 1 {
- filename = fmt.Sprintf("course_%d_export.json", req[0].CourseID)
- }
- c.Ctx.Output.Header("Content-Type", "application/octet-stream")
- c.Ctx.Output.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
- c.Ctx.Output.Header("Content-Length", fmt.Sprintf("%d", len(jsonData)))
- c.Ctx.Output.Body(jsonData)
- }
- // /gettmscoursetype 获取课程分类列表
- func (c *CourseController) GetTMSCourseType() {
- // 调用信息化接口获取课程分类列表,传入FZXTLX参数为1
- response, err := common.GetAllCourseTypeEx(1)
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取课程分类列表失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 判断请求是否成功
- if response.ResultCode != "0" {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取课程分类列表失败: " + response.ResultDesc,
- }
- c.ServeJSON()
- return
- }
- // 返回成功数据
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "获取课程分类列表成功",
- "data": response.CourseTypes,
- }
- c.ServeJSON()
- }
- // /course/uploadtms 上传课程到信息化平台
- func (c *CourseController) UploadTMS() {
- // 解析请求中的课程ID数组
- var req []struct {
- CourseID int `json:"courseId"`
- }
- if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请求参数格式错误: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- if len(req) == 0 {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "请提供要上传的课程ID",
- }
- c.ServeJSON()
- return
- }
- o := orm.NewOrm()
- // 准备要上传的课程信息
- var coursesToUpload []common.UploadCourseInfo
- // 提取课程ID列表
- courseIds := make([]int, len(req))
- for i, item := range req {
- courseIds[i] = item.CourseID
- }
- // 查询课程信息
- var courses []models.CourseInfo
- _, err := o.QueryTable(new(models.CourseInfo)).Filter("课程ID__in", courseIds).All(&courses)
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "查询课程信息失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 如果没有找到任何课程
- if len(courses) == 0 {
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "没有找到指定的课程",
- "data": []common.UploadCourseInfo{},
- }
- c.ServeJSON()
- return
- }
- // 从配置文件中读取CLLX和SYCX
- cllx, _ := beego.AppConfig.String("SYSCLLX")
- if cllx == "" {
- cllx = "牵引变电实训室" // 默认值
- }
- sycx, _ := beego.AppConfig.String("SYSSYCX")
- if sycx == "" {
- sycx = "SCADA系统" // 默认值
- }
- // 将课程信息转换为上传格式
- for _, course := range courses {
- // 从数据库中查询线路名称
- var typeMapList []models.TypeMapList
- lineName := "" // 默认线路名称
- o := orm.NewOrm()
- err := o.QueryTable(new(models.TypeMapList)).
- Filter("DictionaryTypeName", "线路号").
- Filter("KeyParam", course.LineNo).
- One(&typeMapList)
- if err == nil && len(typeMapList) > 0 {
- lineName = typeMapList[0].ValueParam
- }
- uploadCourse := common.UploadCourseInfo{
- KCGUID: course.CourseGUID,
- KCMC: course.CourseName,
- KCMS: course.CourseDesc,
- KCNR: course.CourseDesc, // 课程内容暂时使用课程描述
- KCFLID: course.CourseCategoryID, // 课程分类ID
- CJRID: course.CreatorID, // 创建人作为创建人ID
- CJSJ: course.CreateTime, // 创建时间
- XGRID: course.CreatorID, // 创建人作为修改人ID
- XGSJ: course.ModifyTime, // 修改时间
- PXMS: "1", // 排序模式,默认为1
- Alone: common.Alone{
- FZXTLX: "1", // 独立系统类型,默认为1
- MJ: common.MJ{
- CLLX: cllx, // 车辆类型
- SYCX: sycx, // 适用车型
- XLBH: fmt.Sprintf("%d", course.LineNo), // 线路编号
- XLMC: lineName, // 线路名称,这里使用固定值
- },
- },
- }
- coursesToUpload = append(coursesToUpload, uploadCourse)
- }
- // 调用信息化接口上传课程
- response, err := common.UploadCourseToInformationSystem(coursesToUpload)
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "上传课程到信息化平台失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 判断请求是否成功
- // 根据返回的SucceedCount判断是否全部成功
- succeedCount := 0
- if response.SucceedCount != "" {
- count, err := strconv.Atoi(response.SucceedCount)
- if err == nil {
- succeedCount = count
- }
- }
- totalCount := len(response.CourseResults)
- // 如果有失败的课程,返回详细信息
- if succeedCount < totalCount {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": fmt.Sprintf("上传课程部分失败: 成功%d个,失败%d个", succeedCount, totalCount-succeedCount),
- "data": response.CourseResults,
- }
- c.ServeJSON()
- return
- }
- // 返回成功数据
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "上传课程到信息化平台成功",
- "data": response.CourseResults,
- }
- c.ServeJSON()
- }
- // /course/tmsplan 获取信息化系统的课程计划
- func (c *CourseController) GetTMSCoursePlan() {
- // 定义请求结构体
- type Request struct {
- SBID string `json:"SBID"` // 设备编号
- JYEMPID string `json:"JYEMPID"` // 教员GUID
- XYEMPID string `json:"XYEMPID"` // 学员GUID
- }
- var req Request
- // if CurrentLoginMode == 1 {
- // c.Ctx.Output.SetStatus(500)
- // c.Data["json"] = map[string]interface{}{
- // "code": 500,
- // "msg": "本地模式不支持该功能 ",
- // }
- // c.ServeJSON()
- // return
- // }
- // 从app.conf中读取SYSDEVICEID作为SBID的值
- sbid, err := beego.AppConfig.String("SYSDEVICEID")
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取设备编号失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- req.SBID = sbid
- // 解析请求中的参数 Header 中的 userName 判断用户缓存中的是学员还是教员,将对应人员guid 传入req
- // 从Header中获取userName
- authHeader := c.Ctx.Input.Header("userName")
- if authHeader == "" {
- c.Ctx.Output.SetStatus(401)
- c.Data["json"] = map[string]interface{}{
- "code": 401,
- "msg": "未提供认证信息",
- }
- c.ServeJSON()
- return
- }
- // 从缓存中获取用户信息
- userInfo, exists := GetUserInfoFromCache(authHeader)
- if !exists {
- c.Ctx.Output.SetStatus(401)
- c.Data["json"] = map[string]interface{}{
- "code": 401,
- "msg": "用户信息不存在",
- }
- c.ServeJSON()
- return
- }
- switch userInfo.SF {
- case "1": // 如果是教员,则使用当前用户的RYEMPID
- req.XYEMPID = userInfo.RYEMPID
- case "2": // 如果是学员,则使用当前用户的RYEMPID
- req.JYEMPID = userInfo.RYEMPID
- }
- // 调用信息化接口获取培训计划
- response, err := common.GetTeachPlanFromInformationSystem(req.SBID, req.JYEMPID, req.XYEMPID)
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取培训计划失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 判断请求是否成功
- if response.ResultCode != "0" {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取培训计划失败: " + response.ResultDesc,
- }
- c.ServeJSON()
- return
- }
- // 提取需要返回给前端的数据
- type PlanInfo struct {
- JXJHID string `json:"JXJHID"` // 培训计划ID
- KMMC string `json:"KMMC"` // 科目名称
- SDKCGUIDs []string `json:"SDKCGUIDs"` // 培训课程GUID列表
- TESTSTGUIDs []string `json:"TESTKCGUIDs"` // 测试课程GUID列表
- }
- // 提取需要返回给前端的数据
- type PlanDoInfo struct {
- PXSSID string `json:"PXSSID"` // 培训实施ID
- PXSSMC string `json:"PXSSMC"` // 培训实施名称
- PXInfo []PlanInfo `json:"PlanInfo"` // 培训计划信息
- }
- var plans []PlanDoInfo
- // 遍历所有培训实施计划
- for _, pxss := range response.PXSS {
- PlanDoInfo := PlanDoInfo{PXSSID: pxss.PXSSID, PXSSMC: pxss.PXSSMC}
- // 遍历所有计划
- for _, jh := range pxss.JH {
- plan := PlanInfo{
- JXJHID: jh.ID,
- KMMC: jh.KMMC,
- SDKCGUIDs: []string{},
- TESTSTGUIDs: []string{},
- }
- // 添加SDKC中的KCGUID
- for _, sdkc := range jh.SDKC {
- if sdkc.KCGUID != "" {
- plan.SDKCGUIDs = append(plan.SDKCGUIDs, sdkc.KCGUID)
- }
- }
- // 添加TESTST中的KCGUID
- for _, testst := range jh.TESTST {
- if testst.KCGUID != "" {
- plan.TESTSTGUIDs = append(plan.TESTSTGUIDs, testst.KCGUID)
- }
- }
- PlanDoInfo.PXInfo = append(PlanDoInfo.PXInfo, plan)
- }
- plans = append(plans, PlanDoInfo)
- }
- // 返回成功数据
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "获取培训计划成功",
- "data": plans,
- }
- c.ServeJSON()
- }
- // /course/tmsplanstudent 获取TMS课程计划参与的学生
- func (c *CourseController) GetTMSCoursePlanStudent() {
- // 获取请求参数
- var req struct {
- PXSSID string `json:"PXSSID"`
- }
- // if CurrentLoginMode == 1 {
- // c.Ctx.Output.SetStatus(500)
- // c.Data["json"] = map[string]interface{}{
- // "code": 500,
- // "msg": "本地模式不支持该功能 ",
- // }
- // c.ServeJSON()
- // return
- // }
- if err := json.Unmarshal(c.Ctx.Input.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.PXSSID == "" {
- c.Ctx.Output.SetStatus(400)
- c.Data["json"] = map[string]interface{}{
- "code": 400,
- "msg": "PXSSID不能为空",
- }
- c.ServeJSON()
- return
- }
- // 调用信息化系统获取培训学生信息
- trainingStudentResp, err := common.GetTrainingStudentFromInformationSystem(req.PXSSID)
- if err != nil {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取培训学生信息失败: " + err.Error(),
- }
- c.ServeJSON()
- return
- }
- // 检查返回结果
- if trainingStudentResp.ResultCode != "0" {
- c.Ctx.Output.SetStatus(500)
- c.Data["json"] = map[string]interface{}{
- "code": 500,
- "msg": "获取培训学生信息失败: " + trainingStudentResp.ResultDesc,
- }
- c.ServeJSON()
- return
- }
- // 构造简化版的学生信息列表,只包含XYEMPID和XYXM
- type SimpleStudentInfo struct {
- XYEMPID string `json:"XYEMPID"`
- XYXM string `json:"XYXM"`
- }
- simpleStudents := make([]SimpleStudentInfo, 0)
- for _, student := range trainingStudentResp.Students {
- simpleStudents = append(simpleStudents, SimpleStudentInfo{
- XYEMPID: student.XYEMPID,
- XYXM: student.XYXM,
- })
- }
- // 返回成功数据
- c.Data["json"] = map[string]interface{}{
- "code": 200,
- "msg": "获取培训学生信息成功",
- "data": simpleStudents,
- "count": trainingStudentResp.Count,
- }
- c.ServeJSON()
- }
|