课程.go 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136
  1. package controllers
  2. import (
  3. "Tech/common"
  4. "Tech/models" // 根据实际项目结构调整导入路径
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "path"
  9. "strconv"
  10. "time"
  11. "github.com/beego/beego/v2/client/orm"
  12. beego "github.com/beego/beego/v2/server/web"
  13. "github.com/google/uuid" // 新增导入
  14. )
  15. type CourseController struct {
  16. beego.Controller
  17. }
  18. // GetCourseGUIDsByJXJHID 根据培训计划ID获取课程GUID列表
  19. func GetCourseGUIDsByJXJHID(jxjhID, userName string) ([]string, error) {
  20. // 定义请求结构体
  21. type Request struct {
  22. SBID string `json:"SBID"` // 设备编号
  23. JYEMPID string `json:"JYEMPID"` // 教员GUID
  24. XYEMPID string `json:"XYEMPID"` // 学员GUID
  25. }
  26. var req Request
  27. // 从app.conf中读取SYSDEVICEID作为SBID的值
  28. sbid, err := beego.AppConfig.String("SYSDEVICEID")
  29. if err != nil {
  30. return nil, fmt.Errorf("获取设备编号失败: %v", err)
  31. }
  32. req.SBID = sbid
  33. // 从缓存中获取用户信息
  34. userInfo, exists := GetUserInfoFromCache(userName)
  35. if !exists {
  36. return nil, fmt.Errorf("用户信息不存在: ")
  37. }
  38. switch userInfo.SF {
  39. case "1": // 如果是教员,则使用当前用户的RYEMPID
  40. req.XYEMPID = userInfo.RYEMPID
  41. case "2": // 如果是学员,则使用当前用户的RYEMPID
  42. req.JYEMPID = userInfo.RYEMPID
  43. }
  44. // 调用信息化接口获取培训计划
  45. response, err := common.GetTeachPlanFromInformationSystem(req.SBID, req.JYEMPID, req.XYEMPID)
  46. if err != nil {
  47. return nil, fmt.Errorf("获取培训计划失败: %v", err)
  48. }
  49. // 判断请求是否成功
  50. if response.ResultCode != "0" {
  51. return nil, fmt.Errorf("获取培训计划失败: %s", response.ResultDesc)
  52. }
  53. // 收集匹配的培训计划下的所有课程GUID
  54. var courseGUIDs []string
  55. courseGUIDSet := make(map[string]bool) // 用于去重
  56. // 遍历所有培训实施计划
  57. for _, pxss := range response.PXSS {
  58. // 遍历所有计划
  59. for _, jh := range pxss.JH {
  60. // 检查是否是目标培训计划
  61. if jh.ID == jxjhID {
  62. // 添加SDKC中的KCGUID
  63. for _, sdkc := range jh.SDKC {
  64. if sdkc.KCGUID != "" && !courseGUIDSet[sdkc.KCGUID] {
  65. courseGUIDs = append(courseGUIDs, sdkc.KCGUID)
  66. courseGUIDSet[sdkc.KCGUID] = true
  67. }
  68. }
  69. // 添加TESTST中的KCGUID
  70. for _, testst := range jh.TESTST {
  71. if testst.KCGUID != "" && !courseGUIDSet[testst.KCGUID] {
  72. courseGUIDs = append(courseGUIDs, testst.KCGUID)
  73. courseGUIDSet[testst.KCGUID] = true
  74. }
  75. }
  76. }
  77. }
  78. }
  79. return courseGUIDs, nil
  80. }
  81. // /course/list 获取所有课程信息列表
  82. func (c *CourseController) List() {
  83. type Query struct {
  84. CourseID int `form:"courseID"`
  85. CourseName string `form:"courseName"`
  86. TypeID int `form:"typeID"`
  87. Creator string `form:"creator"`
  88. LineNo int `form:"lineNo"`
  89. JXJHID string `form:"JXJHID"` // 培训计划ID
  90. }
  91. var query Query
  92. requestBody := c.Ctx.Input.RequestBody
  93. println("Request Body:", string(requestBody))
  94. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &query); err != nil {
  95. c.Ctx.Output.SetStatus(400)
  96. c.Data["json"] = map[string]interface{}{
  97. "code": 400,
  98. "msg": "请求参数格式错误: " + err.Error(),
  99. }
  100. c.ServeJSON()
  101. return
  102. }
  103. o := orm.NewOrm()
  104. qs := o.QueryTable(new(models.CourseInfo))
  105. // 编号查询
  106. if query.CourseID != 0 {
  107. qs = qs.Filter("CourseID", query.CourseID)
  108. }
  109. // 课程名称模糊查询
  110. if query.CourseName != "" {
  111. qs = qs.Filter("CourseName__icontains", query.CourseName)
  112. }
  113. // 培训类型查询(分类ID)
  114. if query.TypeID != 0 {
  115. qs = qs.Filter("TypeID", query.TypeID)
  116. }
  117. // 创建人查询
  118. if query.Creator != "" {
  119. qs = qs.Filter("Creator", query.Creator)
  120. }
  121. // 线路查询(线路号)
  122. if query.LineNo != 0 {
  123. qs = qs.Filter("LineNo", query.LineNo)
  124. }
  125. // 如果提供了培训计划ID(JXJHID),则从信息化系统获取对应的课程GUID列表
  126. var courseGUIDs []string
  127. if query.JXJHID != "" {
  128. // 从Header中获取userName
  129. authHeader := c.Ctx.Input.Header("userName")
  130. if authHeader == "" {
  131. c.Ctx.Output.SetStatus(401)
  132. c.Data["json"] = map[string]interface{}{
  133. "code": 401,
  134. "msg": "未提供认证信息",
  135. }
  136. c.ServeJSON()
  137. return
  138. }
  139. var err error
  140. courseGUIDs, err = GetCourseGUIDsByJXJHID(query.JXJHID, authHeader)
  141. if err != nil {
  142. c.Data["json"] = map[string]interface{}{
  143. "code": 500,
  144. "msg": "获取培训计划课程列表失败: " + err.Error(),
  145. }
  146. c.ServeJSON()
  147. return
  148. }
  149. // 如果找到了课程GUID列表,则添加过滤条件
  150. if len(courseGUIDs) > 0 {
  151. qs = qs.Filter("CourseGUID__in", courseGUIDs)
  152. } else {
  153. // 如果没有找到匹配的课程,则返回空结果
  154. var courses []models.CourseInfo
  155. c.Data["json"] = map[string]interface{}{"code": 200, "count": 0, "msg": "", "data": courses}
  156. c.ServeJSON()
  157. return
  158. }
  159. }
  160. var courses []models.CourseInfo
  161. count, err := qs.OrderBy("-CreateTime").All(&courses, "课程ID", "课程GUID", "课程名称", "分类ID", "外部分类ID", "虚拟起始时间",
  162. "课程总时长", "线路号", "运行图", "课程描述", "创建人", "创建时间", "修改时间", "指导描述", "指导文件地址", "虚拟角色", "课程分类ID", "课程分类名称", "创建人ID")
  163. if err != nil {
  164. c.Data["json"] = map[string]interface{}{"code": 500, "error": "查询失败: " + err.Error()}
  165. c.ServeJSON()
  166. return
  167. }
  168. c.Data["json"] = map[string]interface{}{"code": 200, "count": count, "msg": "", "data": courses}
  169. c.ServeJSON()
  170. }
  171. // /course/listpage 获取所有课程信息列表
  172. func (c *CourseController) ListPage() {
  173. type Request struct {
  174. PageNum int `json:"pageNum"`
  175. PageSize int `json:"pageSize"`
  176. CourseID int `json:"courseID"`
  177. CourseName string `json:"courseName"`
  178. TypeID int `json:"typeID"`
  179. Creator string `json:"creator"`
  180. LineNo int `json:"lineNo"`
  181. }
  182. var req Request
  183. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  184. c.Ctx.Output.SetStatus(400)
  185. c.Data["json"] = map[string]interface{}{
  186. "code": 400,
  187. "msg": "请求参数格式错误: " + err.Error(),
  188. }
  189. c.ServeJSON()
  190. return
  191. }
  192. // 设置默认值
  193. if req.PageNum <= 0 {
  194. req.PageNum = 1
  195. }
  196. if req.PageSize <= 0 {
  197. req.PageSize = 10
  198. }
  199. o := orm.NewOrm()
  200. qs := o.QueryTable(new(models.CourseInfo))
  201. // 编号查询
  202. if req.CourseID != 0 {
  203. qs = qs.Filter("CourseID", req.CourseID)
  204. }
  205. // 课程名称模糊查询
  206. if req.CourseName != "" {
  207. qs = qs.Filter("CourseName__icontains", req.CourseName)
  208. }
  209. // 培训类型查询(分类ID)
  210. if req.TypeID != 0 {
  211. qs = qs.Filter("TypeID", req.TypeID)
  212. }
  213. // 创建人查询
  214. if req.Creator != "" {
  215. qs = qs.Filter("Creator__icontains", req.Creator)
  216. }
  217. // 线路查询(线路号)
  218. if req.LineNo != 0 {
  219. qs = qs.Filter("LineNo", req.LineNo)
  220. }
  221. // 获取总数
  222. totalCount, err := qs.Count()
  223. if err != nil {
  224. c.Data["json"] = map[string]interface{}{
  225. "code": 500,
  226. "msg": "查询失败: " + err.Error(),
  227. }
  228. c.ServeJSON()
  229. return
  230. }
  231. // 计算总页数
  232. totalPage := int((totalCount + int64(req.PageSize) - 1) / int64(req.PageSize))
  233. // 分页查询,按创建时间倒序排列
  234. var courses []models.CourseInfo
  235. _, err = qs.OrderBy("-CreateTime").Limit(req.PageSize).Offset((req.PageNum-1)*req.PageSize).All(&courses,
  236. "课程ID", "课程GUID", "课程名称", "分类ID", "外部分类ID", "虚拟起始时间", "课程总时长", "线路号", "运行图",
  237. "课程描述", "创建人", "创建时间", "修改时间", "指导描述", "指导文件地址", "虚拟角色", "课程分类ID", "课程分类名称", "创建人ID")
  238. if err != nil {
  239. c.Data["json"] = map[string]interface{}{
  240. "code": 500,
  241. "msg": "查询失败: " + err.Error(),
  242. }
  243. c.ServeJSON()
  244. return
  245. }
  246. // 构造分页数据
  247. pageData := map[string]interface{}{
  248. "pageNum": req.PageNum,
  249. "pageSize": req.PageSize,
  250. "totalCount": totalCount,
  251. "totalPage": totalPage,
  252. "list": courses,
  253. }
  254. c.Data["json"] = map[string]interface{}{
  255. "code": 200,
  256. "msg": "success",
  257. "data": pageData,
  258. }
  259. c.ServeJSON()
  260. }
  261. // /course/add 新增课程信息
  262. func (c *CourseController) Add() {
  263. var course models.CourseInfo
  264. requestBody := c.Ctx.Input.RequestBody
  265. println("Request Body:", string(requestBody))
  266. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &course); err != nil {
  267. c.Ctx.Output.SetStatus(400)
  268. c.Data["json"] = map[string]interface{}{
  269. "code": 400,
  270. "msg": "请求参数格式错误: " + err.Error(),
  271. }
  272. c.ServeJSON()
  273. return
  274. }
  275. course.CourseID = 0
  276. course.CourseGUID = ""
  277. o := orm.NewOrm()
  278. // 自动生成 CourseID
  279. if course.CourseID == 0 {
  280. var maxID int
  281. err := o.Raw("SELECT MAX(课程ID) FROM CourseInfo").QueryRow(&maxID)
  282. if err != nil {
  283. c.Data["json"] = map[string]interface{}{"code": 500, "msg": "获取最大ID失败: " + err.Error()}
  284. c.ServeJSON()
  285. return
  286. }
  287. course.CourseID = maxID + 1
  288. }
  289. // 自动生成 CourseGUID
  290. if course.CourseGUID == "" {
  291. course.CourseGUID = uuid.New().String()
  292. }
  293. course.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  294. course.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
  295. _, err := o.Insert(&course)
  296. if err != nil {
  297. c.Data["json"] = map[string]interface{}{"code": 500, "msg": "新增失败: " + err.Error()}
  298. c.ServeJSON()
  299. return
  300. }
  301. c.Data["json"] = map[string]interface{}{
  302. "code": 200,
  303. "msg": "新增成功",
  304. "data": map[string]interface{}{
  305. "courseID": course.CourseID,
  306. "courseGUID": course.CourseGUID,
  307. },
  308. }
  309. c.ServeJSON()
  310. }
  311. // /course/update 更新课程信息
  312. func (c *CourseController) Update() {
  313. var course models.CourseInfo
  314. requestBody := c.Ctx.Input.RequestBody
  315. println("Request Body:", string(requestBody))
  316. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &course); err != nil {
  317. c.Ctx.Output.SetStatus(400)
  318. c.Data["json"] = map[string]interface{}{
  319. "code": 400,
  320. "msg": "请求参数格式错误: " + err.Error(),
  321. }
  322. c.ServeJSON()
  323. return
  324. }
  325. o := orm.NewOrm()
  326. course.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
  327. _, err := o.Update(&course, "课程名称", "分类ID", "外部分类ID", "虚拟起始时间", "课程总时长", "线路号", "运行图", "课程描述", "创建人",
  328. "修改时间", "指导描述", "指导文件地址", "课程分类ID", "课程分类名称", "创建人ID")
  329. if err != nil {
  330. c.Data["json"] = map[string]interface{}{"code": 500, "msg": "更新失败: " + err.Error()}
  331. c.ServeJSON()
  332. return
  333. }
  334. c.Data["json"] = map[string]interface{}{"code": 200, "msg": "更新成功"}
  335. c.ServeJSON()
  336. }
  337. // Upload 方法处理文件上传
  338. func (c *CourseController) Upload() {
  339. // 1. 获取上传的文件
  340. file, header, err := c.GetFile("file") // "file" 是表单中文件字段的 name:cite[3]:cite[6]
  341. if err != nil {
  342. c.Ctx.Output.SetStatus(500)
  343. c.Data["json"] = map[string]interface{}{
  344. "code": 500,
  345. "msg": "获取文件失败: " + err.Error(),
  346. }
  347. c.ServeJSON()
  348. return
  349. }
  350. defer file.Close()
  351. // // 2. 获取课程ID参数
  352. // courseId, err := c.GetInt("courseId")
  353. // if err != nil {
  354. // c.Ctx.Output.SetStatus(400)
  355. // c.Data["json"] = map[string]interface{}{
  356. // "code": 400,
  357. // "msg": "缺少课程ID参数或参数格式错误: " + err.Error(),
  358. // }
  359. // c.ServeJSON()
  360. // return
  361. // }
  362. // // 2. 节点ID为0
  363. // nodeId, err := c.GetInt("nodeId")
  364. // if err != nil {
  365. // println("节点ID为null")
  366. // }
  367. // strDir := fmt.Sprintf("static/upload/%d/", courseId)
  368. // if nodeId > 0 {
  369. // strDir = fmt.Sprintf("static/upload/%d/%d/", courseId, nodeId)
  370. // }
  371. strDir := "static/upload/"
  372. if _, err := os.Stat(strDir); os.IsNotExist(err) {
  373. os.MkdirAll(strDir, os.ModePerm)
  374. }
  375. uploadPath := path.Join(strDir, header.Filename) // 文件保存路径
  376. // 3. 保存文件:cite[3]:cite[6]
  377. err = c.SaveToFile("file", uploadPath)
  378. if err != nil {
  379. c.Ctx.Output.SetStatus(500)
  380. c.Data["json"] = map[string]interface{}{
  381. "code": 500,
  382. "msg": "文件保存失败: " + err.Error(),
  383. }
  384. c.ServeJSON()
  385. return
  386. }
  387. // 4. 构建文件的访问 URL:cite[2]
  388. fileUrl := uploadPath
  389. // //保存到课程信息中 指导文件地址 字段
  390. // o := orm.NewOrm()
  391. // //保存到课程或者节点里面
  392. // if nodeId == 0 {
  393. // course := models.CourseInfo{CourseID: courseId}
  394. // if o.Read(&course, "课程ID") == nil {
  395. // course.GuideFilePath = fileUrl
  396. // course.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
  397. // if _, err := o.Update(&course, "指导文件地址", "修改时间"); err != nil {
  398. // c.Ctx.Output.SetStatus(500)
  399. // c.Data["json"] = map[string]interface{}{
  400. // "code": 500,
  401. // "msg": "更新课程信息失败: " + err.Error(),
  402. // }
  403. // c.ServeJSON()
  404. // return
  405. // }
  406. // }
  407. // } else {
  408. // //保存到节点里面
  409. // node := models.CourseActionNode{NodeID: nodeId}
  410. // if o.Read(&node, "节点ID") == nil {
  411. // node.GuideFilePath = fileUrl
  412. // if _, err := o.Update(&node, "指导文件地址"); err != nil {
  413. // c.Ctx.Output.SetStatus(500)
  414. // c.Data["json"] = map[string]interface{}{
  415. // "code": 500,
  416. // "msg": "更新节点信息失败: " + err.Error(),
  417. // }
  418. // c.ServeJSON()
  419. // return
  420. // }
  421. // }
  422. // }
  423. // 5. 返回成功信息和文件 URL:cite[3]
  424. c.Data["json"] = map[string]interface{}{
  425. "code": 200,
  426. "message": "文件上传成功",
  427. "data": map[string]string{
  428. "url": fileUrl,
  429. },
  430. }
  431. c.ServeJSON()
  432. }
  433. // /course/delete 删除课程信息
  434. func (c *CourseController) Delete() {
  435. // 解析请求中的课程ID数组
  436. var req []struct {
  437. CourseID int `json:"courseId"`
  438. }
  439. requestBody := c.Ctx.Input.RequestBody
  440. println("Request Body:", string(requestBody))
  441. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  442. c.Ctx.Output.SetStatus(400)
  443. c.Data["json"] = map[string]interface{}{
  444. "code": 400,
  445. "msg": "请求参数格式错误: " + err.Error(),
  446. }
  447. c.ServeJSON()
  448. return
  449. }
  450. if len(req) == 0 {
  451. c.Data["json"] = map[string]interface{}{"code": 400, "msg": "请提供要删除的课程ID"}
  452. c.ServeJSON()
  453. return
  454. }
  455. o := orm.NewOrm()
  456. // 记录删除成功的课程数量
  457. successCount := 0
  458. failedCourses := make([]map[string]interface{}, 0)
  459. // 提取课程ID列表
  460. courseIds := make([]int, len(req))
  461. for i, item := range req {
  462. courseIds[i] = item.CourseID
  463. }
  464. for _, courseID := range courseIds {
  465. // 先删除相关的课程运行记录
  466. _, err := o.Raw("DELETE FROM CourseRunLog WHERE 运行课程ID = ?", courseID).Exec()
  467. if err != nil {
  468. failedCourses = append(failedCourses, map[string]interface{}{
  469. "courseId": courseID,
  470. "reason": "删除课程运行记录失败: " + err.Error(),
  471. })
  472. continue
  473. }
  474. // 删除课程运行操作日志记录
  475. _, err = o.Raw("DELETE FROM CourseOperLog WHERE 运行课程ID = ?", courseID).Exec()
  476. if err != nil {
  477. failedCourses = append(failedCourses, map[string]interface{}{
  478. "courseId": courseID,
  479. "reason": "删除课程操作日志失败: " + err.Error(),
  480. })
  481. continue
  482. }
  483. // 删除课程评价日志记录
  484. _, err = o.Raw("DELETE FROM CourseEvaLog WHERE 运行课程ID = ?", courseID).Exec()
  485. if err != nil {
  486. failedCourses = append(failedCourses, map[string]interface{}{
  487. "courseId": courseID,
  488. "reason": "删除课程评价日志失败: " + err.Error(),
  489. })
  490. continue
  491. }
  492. // 删除课程基本信息
  493. nCount, err := o.Delete(&models.CourseInfo{CourseID: courseID})
  494. if err != nil {
  495. failedCourses = append(failedCourses, map[string]interface{}{
  496. "courseId": courseID,
  497. "reason": "删除失败: " + err.Error(),
  498. })
  499. continue
  500. }
  501. if nCount == 0 {
  502. failedCourses = append(failedCourses, map[string]interface{}{
  503. "courseId": courseID,
  504. "reason": "课程不存在",
  505. })
  506. continue
  507. }
  508. // 成功删除课程基本信息后,清理相关节点数据
  509. ClearCourse(courseID)
  510. successCount++
  511. }
  512. // 构造返回结果
  513. result := map[string]interface{}{
  514. "code": 200,
  515. "msg": fmt.Sprintf("删除成功%d条记录,失败%d条记录", successCount, len(failedCourses)),
  516. }
  517. if len(failedCourses) > 0 {
  518. result["failed"] = failedCourses
  519. }
  520. c.Data["json"] = result
  521. c.ServeJSON()
  522. }
  523. // /course/detail/get 获取课程详细信息
  524. func (c *CourseController) GetDetail() {
  525. type Query struct {
  526. CourseId int `json:"courseId"`
  527. }
  528. var query Query
  529. requestBody := c.Ctx.Input.RequestBody
  530. println("Request Body:", string(requestBody))
  531. if err := json.Unmarshal(requestBody, &query); err != nil {
  532. c.Ctx.Output.SetStatus(400)
  533. c.Data["json"] = map[string]interface{}{
  534. "code": 400,
  535. "msg": "请求参数格式错误: " + err.Error(),
  536. }
  537. c.ServeJSON()
  538. return
  539. }
  540. if query.CourseId == 0 {
  541. c.Ctx.Output.SetStatus(400)
  542. c.Data["json"] = map[string]interface{}{
  543. "code": 400,
  544. "msg": "缺少课程ID",
  545. }
  546. c.ServeJSON()
  547. return
  548. }
  549. o := orm.NewOrm()
  550. var course models.CourseInfo
  551. err := o.QueryTable(new(models.CourseInfo)).Filter("课程ID", query.CourseId).One(&course)
  552. if err != nil {
  553. c.Ctx.Output.SetStatus(404)
  554. c.Data["json"] = map[string]interface{}{
  555. "code": 404,
  556. "msg": "课程不存在: " + err.Error(),
  557. }
  558. c.ServeJSON()
  559. return
  560. }
  561. c.Data["json"] = map[string]interface{}{
  562. "code": 200,
  563. "data": course,
  564. }
  565. c.ServeJSON()
  566. }
  567. // /course/detail/set 设置课程详细信息
  568. func (c *CourseController) SetDetail() {
  569. type Query struct {
  570. CourseId int `json:"courseId"`
  571. CourseDataStream string `json:"courseDataStream"`
  572. }
  573. var query Query
  574. requestBody := c.Ctx.Input.RequestBody
  575. // println("Request Body:", string(requestBody))
  576. if err := json.Unmarshal(requestBody, &query); err != nil {
  577. c.Ctx.Output.SetStatus(400)
  578. c.Data["json"] = map[string]interface{}{
  579. "code": 400,
  580. "msg": "传输的JSON格式不正确: " + err.Error(),
  581. }
  582. c.ServeJSON()
  583. return
  584. }
  585. if query.CourseId == 0 {
  586. c.Ctx.Output.SetStatus(400)
  587. c.Data["json"] = map[string]interface{}{
  588. "code": 400,
  589. "msg": "缺少课程ID",
  590. }
  591. c.ServeJSON()
  592. return
  593. }
  594. o := orm.NewOrm()
  595. var course models.CourseInfo
  596. err := o.QueryTable(new(models.CourseInfo)).Filter("课程ID", query.CourseId).One(&course)
  597. if err != nil {
  598. c.Ctx.Output.SetStatus(404)
  599. c.Data["json"] = map[string]interface{}{
  600. "code": 404,
  601. "msg": "课程不存在: " + err.Error(),
  602. }
  603. c.ServeJSON()
  604. return
  605. }
  606. // 更新 FlowData 字段
  607. course.CourseDataStream = query.CourseDataStream
  608. course.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
  609. _, err = o.Update(&course, "课程数据流", "修改时间")
  610. if err != nil {
  611. c.Ctx.Output.SetStatus(500)
  612. c.Data["json"] = map[string]interface{}{
  613. "code": 500,
  614. "msg": "更新失败: " + err.Error(),
  615. }
  616. c.ServeJSON()
  617. return
  618. }
  619. //保存数据
  620. isRet, errmsg := ParserAndSaveNode(query.CourseId, query.CourseDataStream)
  621. if !isRet {
  622. c.Ctx.Output.SetStatus(500)
  623. c.Data["json"] = map[string]interface{}{
  624. "code": 500,
  625. "msg": "更新节点失败: " + errmsg,
  626. }
  627. c.ServeJSON()
  628. return
  629. }
  630. c.Data["json"] = map[string]interface{}{
  631. "code": 200,
  632. "msg": "课程数据流更新成功",
  633. }
  634. c.ServeJSON()
  635. }
  636. // /coursetype/list 获取课程分类列表
  637. func (c *CourseController) GetCourseTypeList() {
  638. o := orm.NewOrm()
  639. // 查询所有课程分类
  640. var courseTypes []*models.CourseType
  641. count, err := o.QueryTable(new(models.CourseType)).All(&courseTypes)
  642. if err != nil {
  643. c.Data["json"] = map[string]interface{}{
  644. "code": 500,
  645. "msg": "查询课程分类失败: " + err.Error(),
  646. }
  647. c.ServeJSON()
  648. return
  649. }
  650. c.Data["json"] = map[string]interface{}{
  651. "code": 200,
  652. "count": count,
  653. "msg": "",
  654. "data": courseTypes,
  655. }
  656. c.ServeJSON()
  657. }
  658. // /role/list 获取所有通讯角色列表
  659. func (c *CourseController) GetRoleList() {
  660. o := orm.NewOrm()
  661. // 查询所有通讯角色
  662. var roleInfos []*models.RoleInfo
  663. count, err := o.QueryTable(new(models.RoleInfo)).All(&roleInfos)
  664. if err != nil {
  665. c.Data["json"] = map[string]interface{}{
  666. "code": 500,
  667. "msg": "查询通讯角色失败: " + err.Error(),
  668. }
  669. c.ServeJSON()
  670. return
  671. }
  672. c.Data["json"] = map[string]interface{}{
  673. "code": 200,
  674. "count": count,
  675. "msg": "",
  676. "data": roleInfos,
  677. }
  678. c.ServeJSON()
  679. }
  680. // /templateoperation/list 获取所有操作类型模板列表
  681. func (c *CourseController) GetTemplateOperationList() {
  682. o := orm.NewOrm()
  683. // 查询所有操作类型模板
  684. var templates []*models.TemplateOperation
  685. count, err := o.QueryTable(new(models.TemplateOperation)).All(&templates)
  686. if err != nil {
  687. c.Data["json"] = map[string]interface{}{
  688. "code": 500,
  689. "msg": "查询操作类型模板失败: " + err.Error(),
  690. }
  691. c.ServeJSON()
  692. return
  693. }
  694. c.Data["json"] = map[string]interface{}{
  695. "code": 200,
  696. "count": count,
  697. "msg": "",
  698. "data": templates,
  699. }
  700. c.ServeJSON()
  701. }
  702. // /templatevoice/list 获取所有语音类型模板列表
  703. func (c *CourseController) GetTemplateVoiceList() {
  704. o := orm.NewOrm()
  705. // 查询所有语音类型模板
  706. var templates []*models.TemplateVoice
  707. count, err := o.QueryTable(new(models.TemplateVoice)).All(&templates)
  708. if err != nil {
  709. c.Data["json"] = map[string]interface{}{
  710. "code": 500,
  711. "msg": "查询语音类型模板失败: " + err.Error(),
  712. }
  713. c.ServeJSON()
  714. return
  715. }
  716. c.Data["json"] = map[string]interface{}{
  717. "code": 200,
  718. "count": count,
  719. "msg": "",
  720. "data": templates,
  721. }
  722. c.ServeJSON()
  723. }
  724. // /templatefault/list 获取所有故障类型模板列表
  725. func (c *CourseController) GetTemplateFaultList() {
  726. o := orm.NewOrm()
  727. // 查询所有故障类型模板
  728. var templates []*models.TemplateFault
  729. count, err := o.QueryTable(new(models.TemplateFault)).All(&templates)
  730. if err != nil {
  731. c.Data["json"] = map[string]interface{}{
  732. "code": 500,
  733. "msg": "查询故障类型模板失败: " + err.Error(),
  734. }
  735. c.ServeJSON()
  736. return
  737. }
  738. c.Data["json"] = map[string]interface{}{
  739. "code": 200,
  740. "count": count,
  741. "msg": "",
  742. "data": templates,
  743. }
  744. c.ServeJSON()
  745. }
  746. // /dict/list 获取所有字典列表
  747. func (c *CourseController) GetDictList() {
  748. type Query struct {
  749. DictionaryType int `json:"dictionaryType"`
  750. DictionarySubType int `json:"dictionarySubType"`
  751. }
  752. var query Query
  753. requestBody := c.Ctx.Input.RequestBody
  754. println("Request Body:", string(requestBody))
  755. if err := json.Unmarshal(requestBody, &query); err != nil {
  756. c.Ctx.Output.SetStatus(400)
  757. c.Data["json"] = map[string]interface{}{
  758. "code": 400,
  759. "msg": "请求参数格式错误: " + err.Error(),
  760. }
  761. c.ServeJSON()
  762. return
  763. }
  764. o := orm.NewOrm()
  765. var dicts []*models.TypeMapList
  766. qs := o.QueryTable(new(models.TypeMapList))
  767. if query.DictionaryType != 0 {
  768. qs = qs.Filter("DictionaryType", query.DictionaryType)
  769. }
  770. if query.DictionarySubType != 0 {
  771. qs = qs.Filter("DictionarySubType", query.DictionarySubType)
  772. }
  773. count, err := qs.All(&dicts)
  774. if err != nil {
  775. c.Data["json"] = map[string]interface{}{
  776. "code": 500,
  777. "msg": "查询字典列表失败: " + err.Error(),
  778. }
  779. c.ServeJSON()
  780. return
  781. }
  782. c.Data["json"] = map[string]interface{}{
  783. "code": 200,
  784. "count": count,
  785. "msg": "",
  786. "data": dicts,
  787. }
  788. c.ServeJSON()
  789. }
  790. // /dict/listall 获取所有字典列表
  791. func (c *CourseController) GetDictListAll() {
  792. o := orm.NewOrm()
  793. var dicts []*models.TypeMapList
  794. qs := o.QueryTable(new(models.TypeMapList))
  795. count, err := qs.All(&dicts)
  796. if err != nil {
  797. c.Data["json"] = map[string]interface{}{
  798. "code": 500,
  799. "msg": "查询字典列表失败: " + err.Error(),
  800. }
  801. c.ServeJSON()
  802. return
  803. }
  804. // 按照指定格式组织数据
  805. type Item struct {
  806. KeyParam int `json:"keyParam"`
  807. ValueParam string `json:"valueParam"`
  808. }
  809. type Dict struct {
  810. DictionaryType int `json:"dictionaryType"`
  811. DictionaryTypeName string `json:"dictionaryTypeName"`
  812. DictionarySubType int `json:"dictionarySubType"`
  813. DictionarySubTypeName string `json:"dictionarySubTypeName"`
  814. KeyType string `json:"keyType"`
  815. Items []Item `json:"items"`
  816. }
  817. // 将数据转换为所需的格式
  818. dictMap := make(map[string]*Dict)
  819. for _, dict := range dicts {
  820. key := fmt.Sprintf("%d_%d", dict.DictionaryType, dict.DictionarySubType)
  821. if d, exists := dictMap[key]; exists {
  822. // 如果已存在该字典类型,则添加item
  823. d.Items = append(d.Items, Item{
  824. KeyParam: dict.KeyParam,
  825. ValueParam: dict.ValueParam,
  826. })
  827. } else {
  828. // 创建新的字典类型
  829. dictMap[key] = &Dict{
  830. DictionaryType: dict.DictionaryType,
  831. DictionaryTypeName: dict.DictionaryTypeName,
  832. DictionarySubType: dict.DictionarySubType,
  833. DictionarySubTypeName: dict.DictionarySubTypeName,
  834. KeyType: fmt.Sprintf("%d_%d", dict.DictionaryType, dict.DictionarySubType),
  835. Items: []Item{{
  836. KeyParam: dict.KeyParam,
  837. ValueParam: dict.ValueParam,
  838. }},
  839. }
  840. }
  841. }
  842. // 将map转换为slice
  843. result := make([]Dict, 0, len(dictMap))
  844. for _, dict := range dictMap {
  845. result = append(result, *dict)
  846. }
  847. c.Data["json"] = map[string]interface{}{
  848. "code": 200,
  849. "count": count,
  850. "msg": "",
  851. "data": map[string]interface{}{
  852. "dicts": result,
  853. },
  854. }
  855. c.ServeJSON()
  856. }
  857. // /course/copy 课程复制
  858. func (c *CourseController) CourseCopy() {
  859. // 解析请求中的课程ID数组
  860. var req []struct {
  861. CourseID int `json:"courseId"`
  862. }
  863. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  864. c.Ctx.Output.SetStatus(400)
  865. c.Data["json"] = map[string]interface{}{
  866. "code": 400,
  867. "msg": "请求参数格式错误: " + err.Error(),
  868. }
  869. c.ServeJSON()
  870. return
  871. }
  872. if len(req) == 0 {
  873. c.Ctx.Output.SetStatus(400)
  874. c.Data["json"] = map[string]interface{}{
  875. "code": 400,
  876. "msg": "请提供要复制的课程ID",
  877. }
  878. c.ServeJSON()
  879. return
  880. }
  881. o := orm.NewOrm()
  882. // 存储复制成功的课程信息
  883. successCourses := make([]map[string]interface{}, 0)
  884. // 存储复制失败的课程信息
  885. failedCourses := make([]map[string]interface{}, 0)
  886. // 提取课程ID列表
  887. courseIds := make([]int, len(req))
  888. for i, item := range req {
  889. courseIds[i] = item.CourseID
  890. }
  891. for _, courseID := range courseIds {
  892. // 获取原课程信息
  893. var originalCourse models.CourseInfo
  894. err := o.QueryTable(new(models.CourseInfo)).Filter("CourseID", courseID).One(&originalCourse)
  895. if err != nil {
  896. failedCourses = append(failedCourses, map[string]interface{}{
  897. "courseID": courseID,
  898. "reason": "未找到要复制的课程: " + err.Error(),
  899. })
  900. continue
  901. }
  902. // 创建新课程
  903. newCourse := originalCourse
  904. newCourse.CourseID = 0 // 让ORM自动生成新的ID
  905. newCourse.CourseGUID = uuid.New().String()
  906. newCourse.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  907. newCourse.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
  908. // 自动生成 CourseID
  909. if newCourse.CourseID == 0 {
  910. var maxID int
  911. err := o.Raw("SELECT MAX(课程ID) FROM CourseInfo").QueryRow(&maxID)
  912. if err != nil {
  913. failedCourses = append(failedCourses, map[string]interface{}{
  914. "courseID": courseID,
  915. "reason": "获取最大ID失败: " + err.Error(),
  916. })
  917. continue
  918. }
  919. newCourse.CourseID = maxID + 1
  920. }
  921. // 插入新课程
  922. _, err = o.Insert(&newCourse)
  923. if err != nil {
  924. failedCourses = append(failedCourses, map[string]interface{}{
  925. "courseID": courseID,
  926. "reason": "创建新课程失败: " + err.Error(),
  927. })
  928. continue
  929. }
  930. // 复制课程相关的节点数据
  931. // 复制 CourseActionNode
  932. var actionNodes []models.CourseActionNode
  933. _, err = o.QueryTable(new(models.CourseActionNode)).Filter("CourseID", courseID).All(&actionNodes)
  934. if err != nil && err != orm.ErrNoRows {
  935. failedCourses = append(failedCourses, map[string]interface{}{
  936. "courseID": courseID,
  937. "reason": "查询动作节点失败: " + err.Error(),
  938. })
  939. // 回滚已创建的新课程
  940. o.Delete(&newCourse)
  941. continue
  942. }
  943. for _, node := range actionNodes {
  944. node.CourseID = newCourse.CourseID
  945. _, err = o.Insert(&node)
  946. if err != nil {
  947. failedCourses = append(failedCourses, map[string]interface{}{
  948. "courseID": courseID,
  949. "reason": "复制动作节点失败: " + err.Error(),
  950. })
  951. // 回滚已创建的新课程和节点数据
  952. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  953. o.Delete(&newCourse)
  954. break
  955. }
  956. }
  957. // 如果复制动作节点失败,则跳过该课程
  958. if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
  959. continue
  960. }
  961. // 复制 CourseOperationNode
  962. var operationNodes []models.CourseOperationNode
  963. _, err = o.QueryTable(new(models.CourseOperationNode)).Filter("CourseID", courseID).All(&operationNodes)
  964. if err != nil && err != orm.ErrNoRows {
  965. failedCourses = append(failedCourses, map[string]interface{}{
  966. "courseID": courseID,
  967. "reason": "查询操作节点失败: " + err.Error(),
  968. })
  969. // 回滚已创建的数据
  970. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  971. o.Delete(&newCourse)
  972. continue
  973. }
  974. for _, node := range operationNodes {
  975. node.CourseID = newCourse.CourseID
  976. // 如果主观评价标记未设置,默认为0(客观评价)
  977. if node.SubjectiveEvaluation == 0 {
  978. node.SubjectiveEvaluation = 0 // 默认为客观评价
  979. }
  980. _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
  981. if err != nil {
  982. failedCourses = append(failedCourses, map[string]interface{}{
  983. "courseID": courseID,
  984. "reason": "复制操作节点失败: " + err.Error(),
  985. })
  986. // 回滚已创建的数据
  987. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  988. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  989. o.Delete(&newCourse)
  990. break
  991. }
  992. }
  993. // 如果复制操作节点失败,则跳过该课程
  994. if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
  995. continue
  996. }
  997. // 复制 CourseFaultNode
  998. var faultNodes []models.CourseFaultNode
  999. _, err = o.QueryTable(new(models.CourseFaultNode)).Filter("CourseID", courseID).All(&faultNodes)
  1000. if err != nil && err != orm.ErrNoRows {
  1001. failedCourses = append(failedCourses, map[string]interface{}{
  1002. "courseID": courseID,
  1003. "reason": "查询故障节点失败: " + err.Error(),
  1004. })
  1005. // 回滚已创建的数据
  1006. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1007. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1008. o.Delete(&newCourse)
  1009. continue
  1010. }
  1011. for _, node := range faultNodes {
  1012. node.CourseID = newCourse.CourseID
  1013. // 注意:CourseFaultNode的CourseID和NodeID是联合主键
  1014. _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
  1015. if err != nil {
  1016. failedCourses = append(failedCourses, map[string]interface{}{
  1017. "courseID": courseID,
  1018. "reason": "复制故障节点失败: " + err.Error(),
  1019. })
  1020. // 回滚已创建的数据
  1021. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1022. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1023. o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1024. o.Delete(&newCourse)
  1025. break
  1026. }
  1027. }
  1028. // 如果复制故障节点失败,则跳过该课程
  1029. if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
  1030. continue
  1031. }
  1032. // 复制 CourseVoiceNode
  1033. var voiceNodes []models.CourseVoiceNode
  1034. _, err = o.QueryTable(new(models.CourseVoiceNode)).Filter("CourseID", courseID).All(&voiceNodes)
  1035. if err != nil && err != orm.ErrNoRows {
  1036. failedCourses = append(failedCourses, map[string]interface{}{
  1037. "courseID": courseID,
  1038. "reason": "查询语音节点失败: " + err.Error(),
  1039. })
  1040. // 回滚已创建的数据
  1041. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1042. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1043. o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1044. o.Delete(&newCourse)
  1045. continue
  1046. }
  1047. for _, node := range voiceNodes {
  1048. node.CourseID = newCourse.CourseID
  1049. // 注意:CourseVoiceNode的CourseID和NodeID是联合主键
  1050. _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
  1051. if err != nil {
  1052. failedCourses = append(failedCourses, map[string]interface{}{
  1053. "courseID": courseID,
  1054. "reason": "复制语音节点失败: " + err.Error(),
  1055. })
  1056. // 回滚已创建的数据
  1057. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1058. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1059. o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1060. o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1061. o.Delete(&newCourse)
  1062. break
  1063. }
  1064. }
  1065. // 如果复制语音节点失败,则跳过该课程
  1066. if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
  1067. continue
  1068. }
  1069. // 复制 CourseNodeTrigger
  1070. var triggers []models.CourseNodeTrigger
  1071. _, err = o.QueryTable(new(models.CourseNodeTrigger)).Filter("CourseID", courseID).All(&triggers)
  1072. if err != nil && err != orm.ErrNoRows {
  1073. failedCourses = append(failedCourses, map[string]interface{}{
  1074. "courseID": courseID,
  1075. "reason": "查询节点触发器失败: " + err.Error(),
  1076. })
  1077. // 回滚已创建的数据
  1078. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1079. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1080. o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1081. o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1082. o.Delete(&newCourse)
  1083. continue
  1084. }
  1085. for _, trigger := range triggers {
  1086. trigger.CourseID = newCourse.CourseID
  1087. // 注意:CourseNodeTrigger的CourseID和NodeID是联合主键
  1088. _, _, err = o.ReadOrCreate(&trigger, "CourseID", "NodeID")
  1089. if err != nil {
  1090. failedCourses = append(failedCourses, map[string]interface{}{
  1091. "courseID": courseID,
  1092. "reason": "复制节点触发器失败: " + err.Error(),
  1093. })
  1094. // 回滚已创建的数据
  1095. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1096. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1097. o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1098. o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1099. o.Raw("DELETE FROM CourseNodeTrigger WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1100. o.Delete(&newCourse)
  1101. break
  1102. }
  1103. }
  1104. // 如果复制节点触发器失败,则跳过该课程
  1105. if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
  1106. continue
  1107. }
  1108. // 复制 CourseEvaList
  1109. var evals []models.CourseEvaList
  1110. _, err = o.QueryTable(new(models.CourseEvaList)).Filter("CourseID", courseID).All(&evals)
  1111. if err != nil && err != orm.ErrNoRows {
  1112. failedCourses = append(failedCourses, map[string]interface{}{
  1113. "courseID": courseID,
  1114. "reason": "查询评价列表失败: " + err.Error(),
  1115. })
  1116. // 回滚已创建的数据
  1117. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1118. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1119. o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1120. o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1121. o.Raw("DELETE FROM CourseNodeTrigger WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1122. o.Delete(&newCourse)
  1123. continue
  1124. }
  1125. for _, eval := range evals {
  1126. eval.CourseID = newCourse.CourseID
  1127. // 查询当前最大的序号
  1128. var maxSeqNo int
  1129. err = o.Raw("SELECT COALESCE(MAX(序号), 0) FROM CourseEvaList WHERE 课程ID = ?", newCourse.CourseID).QueryRow(&maxSeqNo)
  1130. if err != nil {
  1131. failedCourses = append(failedCourses, map[string]interface{}{
  1132. "courseID": courseID,
  1133. "reason": "查询评价序号失败: " + err.Error(),
  1134. })
  1135. // 回滚已创建的数据
  1136. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1137. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1138. o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1139. o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1140. o.Raw("DELETE FROM CourseNodeTrigger WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1141. o.Raw("DELETE FROM CourseEvaList WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1142. o.Delete(&newCourse)
  1143. break
  1144. }
  1145. eval.SeqNo = maxSeqNo + 1
  1146. _, err = o.Insert(&eval)
  1147. if err != nil {
  1148. failedCourses = append(failedCourses, map[string]interface{}{
  1149. "courseID": courseID,
  1150. "reason": "复制评价项失败: " + err.Error(),
  1151. })
  1152. // 回滚已创建的数据
  1153. o.Raw("DELETE FROM CourseActionNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1154. o.Raw("DELETE FROM CourseOperationNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1155. o.Raw("DELETE FROM CourseFaultNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1156. o.Raw("DELETE FROM CourseVoiceNode WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1157. o.Raw("DELETE FROM CourseNodeTrigger WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1158. o.Raw("DELETE FROM CourseEvaList WHERE 课程ID = ?", newCourse.CourseID).Exec()
  1159. o.Delete(&newCourse)
  1160. break
  1161. }
  1162. }
  1163. // 如果复制评价项失败,则跳过该课程
  1164. if len(failedCourses) > 0 && failedCourses[len(failedCourses)-1]["courseID"] == courseID {
  1165. continue
  1166. }
  1167. // 所有复制操作都成功完成
  1168. successCourses = append(successCourses, map[string]interface{}{
  1169. "originalCourseID": courseID,
  1170. "newCourseID": newCourse.CourseID,
  1171. "newCourseGUID": newCourse.CourseGUID,
  1172. "newCourseName": newCourse.CourseName,
  1173. })
  1174. }
  1175. // 构造返回结果
  1176. data := map[string]interface{}{}
  1177. if len(successCourses) > 0 {
  1178. data["success"] = successCourses
  1179. }
  1180. if len(failedCourses) > 0 {
  1181. data["failed"] = failedCourses
  1182. }
  1183. response := map[string]interface{}{
  1184. "code": 200,
  1185. "msg": fmt.Sprintf("批量复制完成,成功%d条,失败%d条", len(successCourses), len(failedCourses)),
  1186. "data": data,
  1187. }
  1188. c.Data["json"] = response
  1189. c.ServeJSON()
  1190. }
  1191. // /course/import 课程导入
  1192. func (c *CourseController) CourseImport() {
  1193. // 获取上传的文件
  1194. file, header, err := c.GetFile("file")
  1195. if err != nil {
  1196. c.Ctx.Output.SetStatus(500)
  1197. c.Data["json"] = map[string]interface{}{
  1198. "code": 500,
  1199. "msg": "获取文件失败: " + err.Error(),
  1200. }
  1201. c.ServeJSON()
  1202. return
  1203. }
  1204. defer file.Close()
  1205. // 检查文件扩展名
  1206. ext := path.Ext(header.Filename)
  1207. if ext != ".json" {
  1208. c.Data["json"] = map[string]interface{}{
  1209. "code": 400,
  1210. "msg": "只支持导入JSON格式文件",
  1211. }
  1212. c.ServeJSON()
  1213. return
  1214. }
  1215. // 读取文件内容
  1216. fileSize := header.Size
  1217. buffer := make([]byte, fileSize)
  1218. _, err = file.Read(buffer)
  1219. if err != nil {
  1220. c.Data["json"] = map[string]interface{}{
  1221. "code": 500,
  1222. "msg": "读取文件失败: " + err.Error(),
  1223. }
  1224. c.ServeJSON()
  1225. return
  1226. }
  1227. // 解析JSON内容 - 支持多个课程
  1228. var importData []struct {
  1229. CourseInfo models.CourseInfo `json:"courseInfo"`
  1230. ActionNodes []models.CourseActionNode `json:"actionNodes"`
  1231. OperationNodes []models.CourseOperationNode `json:"operationNodes"`
  1232. FaultNodes []models.CourseFaultNode `json:"faultNodes"`
  1233. VoiceNodes []models.CourseVoiceNode `json:"voiceNodes"`
  1234. NodeTriggers []models.CourseNodeTrigger `json:"nodeTriggers"`
  1235. EvalLists []models.CourseEvaList `json:"evalLists"`
  1236. }
  1237. err = json.Unmarshal(buffer, &importData)
  1238. if err != nil {
  1239. c.Data["json"] = map[string]interface{}{
  1240. "code": 500,
  1241. "msg": "解析JSON文件失败: " + err.Error(),
  1242. }
  1243. c.ServeJSON()
  1244. return
  1245. }
  1246. o := orm.NewOrm()
  1247. // 存储导入结果
  1248. importResults := make([]map[string]interface{}, 0)
  1249. // 遍历所有要导入的课程
  1250. for _, courseData := range importData {
  1251. // 创建新课程
  1252. newCourse := courseData.CourseInfo
  1253. newCourse.CourseID = 0 // 让ORM生成新的ID
  1254. newCourse.CourseGUID = uuid.New().String()
  1255. newCourse.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  1256. newCourse.ModifyTime = time.Now().Format("2006-01-02 15:04:05")
  1257. // 检查是否已存在同名课程,如果存在则添加序号进行区分
  1258. originalName := newCourse.CourseName
  1259. duplicateIndex := 1
  1260. for {
  1261. var existingCourse models.CourseInfo
  1262. err := o.QueryTable(new(models.CourseInfo)).Filter("CourseName", newCourse.CourseName).One(&existingCourse)
  1263. if err != nil && err != orm.ErrNoRows {
  1264. importResults = append(importResults, map[string]interface{}{
  1265. "courseName": originalName,
  1266. "status": "failed",
  1267. "msg": "检查课程名称唯一性失败: " + err.Error(),
  1268. })
  1269. break
  1270. }
  1271. // 如果没有找到同名课程,则可以使用该名称
  1272. if err == orm.ErrNoRows {
  1273. break
  1274. }
  1275. // 如果找到同名课程,则在名称后添加序号
  1276. newCourse.CourseName = fmt.Sprintf("%s(%d)", originalName, duplicateIndex)
  1277. duplicateIndex++
  1278. }
  1279. // 自动生成 CourseID
  1280. if newCourse.CourseID == 0 {
  1281. var maxID int
  1282. err := o.Raw("SELECT MAX(课程ID) FROM CourseInfo").QueryRow(&maxID)
  1283. if err != nil {
  1284. c.Data["json"] = map[string]interface{}{"code": 500, "msg": "获取最大ID失败: " + err.Error()}
  1285. c.ServeJSON()
  1286. return
  1287. }
  1288. newCourse.CourseID = maxID + 1
  1289. }
  1290. _, err = o.Insert(&newCourse)
  1291. if err != nil {
  1292. importResults = append(importResults, map[string]interface{}{
  1293. "courseName": courseData.CourseInfo.CourseName,
  1294. "status": "failed",
  1295. "msg": "创建课程失败: " + err.Error(),
  1296. })
  1297. continue
  1298. }
  1299. // 导入 CourseActionNode
  1300. for _, node := range courseData.ActionNodes {
  1301. node.CourseID = newCourse.CourseID
  1302. _, err = o.Insert(&node)
  1303. if err != nil {
  1304. importResults = append(importResults, map[string]interface{}{
  1305. "courseName": courseData.CourseInfo.CourseName,
  1306. "status": "failed",
  1307. "msg": "导入动作节点失败: " + err.Error(),
  1308. })
  1309. continue
  1310. }
  1311. }
  1312. // 导入 CourseOperationNode
  1313. for _, node := range courseData.OperationNodes {
  1314. node.CourseID = newCourse.CourseID
  1315. // 如果主观评价标记未设置,默认为0(客观评价)
  1316. if node.SubjectiveEvaluation == 0 {
  1317. node.SubjectiveEvaluation = 0 // 默认为客观评价
  1318. }
  1319. _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
  1320. if err != nil {
  1321. importResults = append(importResults, map[string]interface{}{
  1322. "courseName": courseData.CourseInfo.CourseName,
  1323. "status": "failed",
  1324. "msg": "导入操作节点失败: " + err.Error(),
  1325. })
  1326. continue
  1327. }
  1328. }
  1329. // 导入 CourseFaultNode
  1330. for _, node := range courseData.FaultNodes {
  1331. node.CourseID = newCourse.CourseID
  1332. _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
  1333. if err != nil {
  1334. importResults = append(importResults, map[string]interface{}{
  1335. "courseName": courseData.CourseInfo.CourseName,
  1336. "status": "failed",
  1337. "msg": "导入故障节点失败: " + err.Error(),
  1338. })
  1339. continue
  1340. }
  1341. }
  1342. // 导入 CourseVoiceNode
  1343. for _, node := range courseData.VoiceNodes {
  1344. node.CourseID = newCourse.CourseID
  1345. _, _, err = o.ReadOrCreate(&node, "CourseID", "NodeID")
  1346. if err != nil {
  1347. importResults = append(importResults, map[string]interface{}{
  1348. "courseName": courseData.CourseInfo.CourseName,
  1349. "status": "failed",
  1350. "msg": "导入语音节点失败: " + err.Error(),
  1351. })
  1352. continue
  1353. }
  1354. }
  1355. // 导入 CourseNodeTrigger
  1356. for _, trigger := range courseData.NodeTriggers {
  1357. trigger.CourseID = newCourse.CourseID
  1358. _, _, err = o.ReadOrCreate(&trigger, "CourseID", "NodeID")
  1359. if err != nil {
  1360. importResults = append(importResults, map[string]interface{}{
  1361. "courseName": courseData.CourseInfo.CourseName,
  1362. "status": "failed",
  1363. "msg": "导入节点触发器失败: " + err.Error(),
  1364. })
  1365. continue
  1366. }
  1367. }
  1368. // 导入 CourseEvaList
  1369. for i, eval := range courseData.EvalLists {
  1370. eval.CourseID = newCourse.CourseID
  1371. eval.SeqNo = i + 1 // 重新编号
  1372. _, err = o.Insert(&eval)
  1373. if err != nil {
  1374. importResults = append(importResults, map[string]interface{}{
  1375. "courseName": courseData.CourseInfo.CourseName,
  1376. "status": "failed",
  1377. "msg": "导入评价项失败: " + err.Error(),
  1378. })
  1379. continue
  1380. }
  1381. }
  1382. importResults = append(importResults, map[string]interface{}{
  1383. "courseName": newCourse.CourseName,
  1384. "status": "success",
  1385. "data": map[string]interface{}{
  1386. "courseID": newCourse.CourseID,
  1387. "courseGUID": newCourse.CourseGUID,
  1388. "courseName": newCourse.CourseName,
  1389. },
  1390. })
  1391. }
  1392. c.Data["json"] = map[string]interface{}{
  1393. "code": 200,
  1394. "msg": "课程导入完成",
  1395. "data": importResults,
  1396. "counts": map[string]int{
  1397. "total": len(importData),
  1398. "success": len(importResults) - failedCount(importResults),
  1399. "failed": failedCount(importResults),
  1400. },
  1401. }
  1402. c.ServeJSON()
  1403. }
  1404. // 辅助函数:计算导入失败的数量
  1405. func failedCount(results []map[string]interface{}) int {
  1406. count := 0
  1407. for _, result := range results {
  1408. if status, ok := result["status"].(string); ok && status == "failed" {
  1409. count++
  1410. }
  1411. }
  1412. return count
  1413. }
  1414. // // course/export 课程导出
  1415. func (c *CourseController) CourseExport() {
  1416. // 解析请求中的课程ID数组
  1417. var req []struct {
  1418. CourseID int `json:"courseId"`
  1419. }
  1420. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  1421. c.Ctx.Output.SetStatus(400)
  1422. c.Data["json"] = map[string]interface{}{
  1423. "code": 400,
  1424. "msg": "请求参数格式错误: " + err.Error(),
  1425. }
  1426. c.ServeJSON()
  1427. return
  1428. }
  1429. if len(req) == 0 {
  1430. c.Ctx.Output.SetStatus(400)
  1431. c.Data["json"] = map[string]interface{}{
  1432. "code": 400,
  1433. "msg": "参数不完整",
  1434. }
  1435. c.ServeJSON()
  1436. return
  1437. }
  1438. o := orm.NewOrm()
  1439. // 存储所有要导出的课程数据
  1440. exportData := make([]map[string]interface{}, 0)
  1441. // 遍历所有请求的课程ID
  1442. for _, item := range req {
  1443. if item.CourseID <= 0 {
  1444. continue
  1445. }
  1446. // 获取课程信息
  1447. var course models.CourseInfo
  1448. err := o.QueryTable(new(models.CourseInfo)).Filter("CourseID", item.CourseID).One(&course)
  1449. if err != nil {
  1450. // 如果找不到课程,继续处理下一个
  1451. continue
  1452. }
  1453. // 获取 CourseActionNode
  1454. var actionNodes []models.CourseActionNode
  1455. _, err = o.QueryTable(new(models.CourseActionNode)).Filter("CourseID", item.CourseID).All(&actionNodes)
  1456. if err != nil && err != orm.ErrNoRows {
  1457. // 错误处理,但继续处理下一个课程
  1458. continue
  1459. }
  1460. // 获取 CourseOperationNode
  1461. var operationNodes []models.CourseOperationNode
  1462. _, err = o.QueryTable(new(models.CourseOperationNode)).Filter("CourseID", item.CourseID).All(&operationNodes)
  1463. if err != nil && err != orm.ErrNoRows {
  1464. // 错误处理,但继续处理下一个课程
  1465. continue
  1466. }
  1467. // 获取 CourseFaultNode
  1468. var faultNodes []models.CourseFaultNode
  1469. _, err = o.QueryTable(new(models.CourseFaultNode)).Filter("CourseID", item.CourseID).All(&faultNodes)
  1470. if err != nil && err != orm.ErrNoRows {
  1471. // 错误处理,但继续处理下一个课程
  1472. continue
  1473. }
  1474. // 获取 CourseVoiceNode
  1475. var voiceNodes []models.CourseVoiceNode
  1476. _, err = o.QueryTable(new(models.CourseVoiceNode)).Filter("CourseID", item.CourseID).All(&voiceNodes)
  1477. if err != nil && err != orm.ErrNoRows {
  1478. // 错误处理,但继续处理下一个课程
  1479. continue
  1480. }
  1481. // 获取 CourseNodeTrigger
  1482. var triggers []models.CourseNodeTrigger
  1483. _, err = o.QueryTable(new(models.CourseNodeTrigger)).Filter("CourseID", item.CourseID).All(&triggers)
  1484. if err != nil && err != orm.ErrNoRows {
  1485. // 错误处理,但继续处理下一个课程
  1486. continue
  1487. }
  1488. // 获取 CourseEvaList
  1489. var evalLists []models.CourseEvaList
  1490. _, err = o.QueryTable(new(models.CourseEvaList)).Filter("CourseID", item.CourseID).All(&evalLists)
  1491. if err != nil && err != orm.ErrNoRows {
  1492. // 错误处理,但继续处理下一个课程
  1493. continue
  1494. }
  1495. // 构造单个课程的导出数据
  1496. singleExportData := map[string]interface{}{
  1497. "courseInfo": course,
  1498. "actionNodes": actionNodes,
  1499. "operationNodes": operationNodes,
  1500. "faultNodes": faultNodes,
  1501. "voiceNodes": voiceNodes,
  1502. "nodeTriggers": triggers,
  1503. "evalLists": evalLists,
  1504. }
  1505. exportData = append(exportData, singleExportData)
  1506. }
  1507. // 转换为JSON
  1508. jsonData, err := json.Marshal(exportData)
  1509. if err != nil {
  1510. c.Data["json"] = map[string]interface{}{
  1511. "code": 500,
  1512. "msg": "序列化数据失败: " + err.Error(),
  1513. }
  1514. c.ServeJSON()
  1515. return
  1516. }
  1517. // 设置响应头,触发浏览器下载
  1518. filename := "courses_export.json"
  1519. if len(req) == 1 {
  1520. filename = fmt.Sprintf("course_%d_export.json", req[0].CourseID)
  1521. }
  1522. c.Ctx.Output.Header("Content-Type", "application/octet-stream")
  1523. c.Ctx.Output.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
  1524. c.Ctx.Output.Header("Content-Length", fmt.Sprintf("%d", len(jsonData)))
  1525. c.Ctx.Output.Body(jsonData)
  1526. }
  1527. // /gettmscoursetype 获取课程分类列表
  1528. func (c *CourseController) GetTMSCourseType() {
  1529. // 调用信息化接口获取课程分类列表,传入FZXTLX参数为1
  1530. response, err := common.GetAllCourseTypeEx(1)
  1531. if err != nil {
  1532. c.Ctx.Output.SetStatus(500)
  1533. c.Data["json"] = map[string]interface{}{
  1534. "code": 500,
  1535. "msg": "获取课程分类列表失败: " + err.Error(),
  1536. }
  1537. c.ServeJSON()
  1538. return
  1539. }
  1540. // 判断请求是否成功
  1541. if response.ResultCode != "0" {
  1542. c.Ctx.Output.SetStatus(500)
  1543. c.Data["json"] = map[string]interface{}{
  1544. "code": 500,
  1545. "msg": "获取课程分类列表失败: " + response.ResultDesc,
  1546. }
  1547. c.ServeJSON()
  1548. return
  1549. }
  1550. // 返回成功数据
  1551. c.Data["json"] = map[string]interface{}{
  1552. "code": 200,
  1553. "msg": "获取课程分类列表成功",
  1554. "data": response.CourseTypes,
  1555. }
  1556. c.ServeJSON()
  1557. }
  1558. // /course/uploadtms 上传课程到信息化平台
  1559. func (c *CourseController) UploadTMS() {
  1560. // 解析请求中的课程ID数组
  1561. var req []struct {
  1562. CourseID int `json:"courseId"`
  1563. }
  1564. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  1565. c.Ctx.Output.SetStatus(400)
  1566. c.Data["json"] = map[string]interface{}{
  1567. "code": 400,
  1568. "msg": "请求参数格式错误: " + err.Error(),
  1569. }
  1570. c.ServeJSON()
  1571. return
  1572. }
  1573. if len(req) == 0 {
  1574. c.Ctx.Output.SetStatus(400)
  1575. c.Data["json"] = map[string]interface{}{
  1576. "code": 400,
  1577. "msg": "请提供要上传的课程ID",
  1578. }
  1579. c.ServeJSON()
  1580. return
  1581. }
  1582. o := orm.NewOrm()
  1583. // 准备要上传的课程信息
  1584. var coursesToUpload []common.UploadCourseInfo
  1585. // 提取课程ID列表
  1586. courseIds := make([]int, len(req))
  1587. for i, item := range req {
  1588. courseIds[i] = item.CourseID
  1589. }
  1590. // 查询课程信息
  1591. var courses []models.CourseInfo
  1592. _, err := o.QueryTable(new(models.CourseInfo)).Filter("课程ID__in", courseIds).All(&courses)
  1593. if err != nil {
  1594. c.Ctx.Output.SetStatus(500)
  1595. c.Data["json"] = map[string]interface{}{
  1596. "code": 500,
  1597. "msg": "查询课程信息失败: " + err.Error(),
  1598. }
  1599. c.ServeJSON()
  1600. return
  1601. }
  1602. // 如果没有找到任何课程
  1603. if len(courses) == 0 {
  1604. c.Data["json"] = map[string]interface{}{
  1605. "code": 200,
  1606. "msg": "没有找到指定的课程",
  1607. "data": []common.UploadCourseInfo{},
  1608. }
  1609. c.ServeJSON()
  1610. return
  1611. }
  1612. // 从配置文件中读取CLLX和SYCX
  1613. cllx, _ := beego.AppConfig.String("SYSCLLX")
  1614. if cllx == "" {
  1615. cllx = "牵引变电实训室" // 默认值
  1616. }
  1617. sycx, _ := beego.AppConfig.String("SYSSYCX")
  1618. if sycx == "" {
  1619. sycx = "SCADA系统" // 默认值
  1620. }
  1621. // 将课程信息转换为上传格式
  1622. for _, course := range courses {
  1623. // 从数据库中查询线路名称
  1624. var typeMapList []models.TypeMapList
  1625. lineName := "" // 默认线路名称
  1626. o := orm.NewOrm()
  1627. err := o.QueryTable(new(models.TypeMapList)).
  1628. Filter("DictionaryTypeName", "线路号").
  1629. Filter("KeyParam", course.LineNo).
  1630. One(&typeMapList)
  1631. if err == nil && len(typeMapList) > 0 {
  1632. lineName = typeMapList[0].ValueParam
  1633. }
  1634. uploadCourse := common.UploadCourseInfo{
  1635. KCGUID: course.CourseGUID,
  1636. KCMC: course.CourseName,
  1637. KCMS: course.CourseDesc,
  1638. KCNR: course.CourseDesc, // 课程内容暂时使用课程描述
  1639. KCFLID: course.CourseCategoryID, // 课程分类ID
  1640. CJRID: course.CreatorID, // 创建人作为创建人ID
  1641. CJSJ: course.CreateTime, // 创建时间
  1642. XGRID: course.CreatorID, // 创建人作为修改人ID
  1643. XGSJ: course.ModifyTime, // 修改时间
  1644. PXMS: "1", // 排序模式,默认为1
  1645. Alone: common.Alone{
  1646. FZXTLX: "1", // 独立系统类型,默认为1
  1647. MJ: common.MJ{
  1648. CLLX: cllx, // 车辆类型
  1649. SYCX: sycx, // 适用车型
  1650. XLBH: fmt.Sprintf("%d", course.LineNo), // 线路编号
  1651. XLMC: lineName, // 线路名称,这里使用固定值
  1652. },
  1653. },
  1654. }
  1655. coursesToUpload = append(coursesToUpload, uploadCourse)
  1656. }
  1657. // 调用信息化接口上传课程
  1658. response, err := common.UploadCourseToInformationSystem(coursesToUpload)
  1659. if err != nil {
  1660. c.Ctx.Output.SetStatus(500)
  1661. c.Data["json"] = map[string]interface{}{
  1662. "code": 500,
  1663. "msg": "上传课程到信息化平台失败: " + err.Error(),
  1664. }
  1665. c.ServeJSON()
  1666. return
  1667. }
  1668. // 判断请求是否成功
  1669. // 根据返回的SucceedCount判断是否全部成功
  1670. succeedCount := 0
  1671. if response.SucceedCount != "" {
  1672. count, err := strconv.Atoi(response.SucceedCount)
  1673. if err == nil {
  1674. succeedCount = count
  1675. }
  1676. }
  1677. totalCount := len(response.CourseResults)
  1678. // 如果有失败的课程,返回详细信息
  1679. if succeedCount < totalCount {
  1680. c.Ctx.Output.SetStatus(500)
  1681. c.Data["json"] = map[string]interface{}{
  1682. "code": 500,
  1683. "msg": fmt.Sprintf("上传课程部分失败: 成功%d个,失败%d个", succeedCount, totalCount-succeedCount),
  1684. "data": response.CourseResults,
  1685. }
  1686. c.ServeJSON()
  1687. return
  1688. }
  1689. // 返回成功数据
  1690. c.Data["json"] = map[string]interface{}{
  1691. "code": 200,
  1692. "msg": "上传课程到信息化平台成功",
  1693. "data": response.CourseResults,
  1694. }
  1695. c.ServeJSON()
  1696. }
  1697. // /course/tmsplan 获取信息化系统的课程计划
  1698. func (c *CourseController) GetTMSCoursePlan() {
  1699. // 定义请求结构体
  1700. type Request struct {
  1701. SBID string `json:"SBID"` // 设备编号
  1702. JYEMPID string `json:"JYEMPID"` // 教员GUID
  1703. XYEMPID string `json:"XYEMPID"` // 学员GUID
  1704. }
  1705. var req Request
  1706. // if CurrentLoginMode == 1 {
  1707. // c.Ctx.Output.SetStatus(500)
  1708. // c.Data["json"] = map[string]interface{}{
  1709. // "code": 500,
  1710. // "msg": "本地模式不支持该功能 ",
  1711. // }
  1712. // c.ServeJSON()
  1713. // return
  1714. // }
  1715. // 从app.conf中读取SYSDEVICEID作为SBID的值
  1716. sbid, err := beego.AppConfig.String("SYSDEVICEID")
  1717. if err != nil {
  1718. c.Ctx.Output.SetStatus(500)
  1719. c.Data["json"] = map[string]interface{}{
  1720. "code": 500,
  1721. "msg": "获取设备编号失败: " + err.Error(),
  1722. }
  1723. c.ServeJSON()
  1724. return
  1725. }
  1726. req.SBID = sbid
  1727. // 解析请求中的参数 Header 中的 userName 判断用户缓存中的是学员还是教员,将对应人员guid 传入req
  1728. // 从Header中获取userName
  1729. authHeader := c.Ctx.Input.Header("userName")
  1730. if authHeader == "" {
  1731. c.Ctx.Output.SetStatus(401)
  1732. c.Data["json"] = map[string]interface{}{
  1733. "code": 401,
  1734. "msg": "未提供认证信息",
  1735. }
  1736. c.ServeJSON()
  1737. return
  1738. }
  1739. // 从缓存中获取用户信息
  1740. userInfo, exists := GetUserInfoFromCache(authHeader)
  1741. if !exists {
  1742. c.Ctx.Output.SetStatus(401)
  1743. c.Data["json"] = map[string]interface{}{
  1744. "code": 401,
  1745. "msg": "用户信息不存在",
  1746. }
  1747. c.ServeJSON()
  1748. return
  1749. }
  1750. switch userInfo.SF {
  1751. case "1": // 如果是教员,则使用当前用户的RYEMPID
  1752. req.XYEMPID = userInfo.RYEMPID
  1753. case "2": // 如果是学员,则使用当前用户的RYEMPID
  1754. req.JYEMPID = userInfo.RYEMPID
  1755. }
  1756. // 调用信息化接口获取培训计划
  1757. response, err := common.GetTeachPlanFromInformationSystem(req.SBID, req.JYEMPID, req.XYEMPID)
  1758. if err != nil {
  1759. c.Ctx.Output.SetStatus(500)
  1760. c.Data["json"] = map[string]interface{}{
  1761. "code": 500,
  1762. "msg": "获取培训计划失败: " + err.Error(),
  1763. }
  1764. c.ServeJSON()
  1765. return
  1766. }
  1767. // 判断请求是否成功
  1768. if response.ResultCode != "0" {
  1769. c.Ctx.Output.SetStatus(500)
  1770. c.Data["json"] = map[string]interface{}{
  1771. "code": 500,
  1772. "msg": "获取培训计划失败: " + response.ResultDesc,
  1773. }
  1774. c.ServeJSON()
  1775. return
  1776. }
  1777. // 提取需要返回给前端的数据
  1778. type PlanInfo struct {
  1779. JXJHID string `json:"JXJHID"` // 培训计划ID
  1780. KMMC string `json:"KMMC"` // 科目名称
  1781. SDKCGUIDs []string `json:"SDKCGUIDs"` // 培训课程GUID列表
  1782. TESTSTGUIDs []string `json:"TESTKCGUIDs"` // 测试课程GUID列表
  1783. }
  1784. // 提取需要返回给前端的数据
  1785. type PlanDoInfo struct {
  1786. PXSSID string `json:"PXSSID"` // 培训实施ID
  1787. PXSSMC string `json:"PXSSMC"` // 培训实施名称
  1788. PXInfo []PlanInfo `json:"PlanInfo"` // 培训计划信息
  1789. }
  1790. var plans []PlanDoInfo
  1791. // 遍历所有培训实施计划
  1792. for _, pxss := range response.PXSS {
  1793. PlanDoInfo := PlanDoInfo{PXSSID: pxss.PXSSID, PXSSMC: pxss.PXSSMC}
  1794. // 遍历所有计划
  1795. for _, jh := range pxss.JH {
  1796. plan := PlanInfo{
  1797. JXJHID: jh.ID,
  1798. KMMC: jh.KMMC,
  1799. SDKCGUIDs: []string{},
  1800. TESTSTGUIDs: []string{},
  1801. }
  1802. // 添加SDKC中的KCGUID
  1803. for _, sdkc := range jh.SDKC {
  1804. if sdkc.KCGUID != "" {
  1805. plan.SDKCGUIDs = append(plan.SDKCGUIDs, sdkc.KCGUID)
  1806. }
  1807. }
  1808. // 添加TESTST中的KCGUID
  1809. for _, testst := range jh.TESTST {
  1810. if testst.KCGUID != "" {
  1811. plan.TESTSTGUIDs = append(plan.TESTSTGUIDs, testst.KCGUID)
  1812. }
  1813. }
  1814. PlanDoInfo.PXInfo = append(PlanDoInfo.PXInfo, plan)
  1815. }
  1816. plans = append(plans, PlanDoInfo)
  1817. }
  1818. // 返回成功数据
  1819. c.Data["json"] = map[string]interface{}{
  1820. "code": 200,
  1821. "msg": "获取培训计划成功",
  1822. "data": plans,
  1823. }
  1824. c.ServeJSON()
  1825. }
  1826. // /course/tmsplanstudent 获取TMS课程计划参与的学生
  1827. func (c *CourseController) GetTMSCoursePlanStudent() {
  1828. // 获取请求参数
  1829. var req struct {
  1830. PXSSID string `json:"PXSSID"`
  1831. }
  1832. // if CurrentLoginMode == 1 {
  1833. // c.Ctx.Output.SetStatus(500)
  1834. // c.Data["json"] = map[string]interface{}{
  1835. // "code": 500,
  1836. // "msg": "本地模式不支持该功能 ",
  1837. // }
  1838. // c.ServeJSON()
  1839. // return
  1840. // }
  1841. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  1842. c.Ctx.Output.SetStatus(400)
  1843. c.Data["json"] = map[string]interface{}{
  1844. "code": 400,
  1845. "msg": "请求参数格式错误: " + err.Error(),
  1846. }
  1847. c.ServeJSON()
  1848. return
  1849. }
  1850. if req.PXSSID == "" {
  1851. c.Ctx.Output.SetStatus(400)
  1852. c.Data["json"] = map[string]interface{}{
  1853. "code": 400,
  1854. "msg": "PXSSID不能为空",
  1855. }
  1856. c.ServeJSON()
  1857. return
  1858. }
  1859. // 调用信息化系统获取培训学生信息
  1860. trainingStudentResp, err := common.GetTrainingStudentFromInformationSystem(req.PXSSID)
  1861. if err != nil {
  1862. c.Ctx.Output.SetStatus(500)
  1863. c.Data["json"] = map[string]interface{}{
  1864. "code": 500,
  1865. "msg": "获取培训学生信息失败: " + err.Error(),
  1866. }
  1867. c.ServeJSON()
  1868. return
  1869. }
  1870. // 检查返回结果
  1871. if trainingStudentResp.ResultCode != "0" {
  1872. c.Ctx.Output.SetStatus(500)
  1873. c.Data["json"] = map[string]interface{}{
  1874. "code": 500,
  1875. "msg": "获取培训学生信息失败: " + trainingStudentResp.ResultDesc,
  1876. }
  1877. c.ServeJSON()
  1878. return
  1879. }
  1880. // 构造简化版的学生信息列表,只包含XYEMPID和XYXM
  1881. type SimpleStudentInfo struct {
  1882. XYEMPID string `json:"XYEMPID"`
  1883. XYXM string `json:"XYXM"`
  1884. }
  1885. simpleStudents := make([]SimpleStudentInfo, 0)
  1886. for _, student := range trainingStudentResp.Students {
  1887. simpleStudents = append(simpleStudents, SimpleStudentInfo{
  1888. XYEMPID: student.XYEMPID,
  1889. XYXM: student.XYXM,
  1890. })
  1891. }
  1892. // 返回成功数据
  1893. c.Data["json"] = map[string]interface{}{
  1894. "code": 200,
  1895. "msg": "获取培训学生信息成功",
  1896. "data": simpleStudents,
  1897. "count": trainingStudentResp.Count,
  1898. }
  1899. c.ServeJSON()
  1900. }