课程.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116
  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. // 从app.conf中读取SYSDEVICEID作为SBID的值
  1707. sbid, err := beego.AppConfig.String("SYSDEVICEID")
  1708. if err != nil {
  1709. c.Ctx.Output.SetStatus(500)
  1710. c.Data["json"] = map[string]interface{}{
  1711. "code": 500,
  1712. "msg": "获取设备编号失败: " + err.Error(),
  1713. }
  1714. c.ServeJSON()
  1715. return
  1716. }
  1717. req.SBID = sbid
  1718. // 解析请求中的参数 Header 中的 userName 判断用户缓存中的是学员还是教员,将对应人员guid 传入req
  1719. // 从Header中获取userName
  1720. authHeader := c.Ctx.Input.Header("userName")
  1721. if authHeader == "" {
  1722. c.Ctx.Output.SetStatus(401)
  1723. c.Data["json"] = map[string]interface{}{
  1724. "code": 401,
  1725. "msg": "未提供认证信息",
  1726. }
  1727. c.ServeJSON()
  1728. return
  1729. }
  1730. // 从缓存中获取用户信息
  1731. userInfo, exists := GetUserInfoFromCache(authHeader)
  1732. if !exists {
  1733. c.Ctx.Output.SetStatus(401)
  1734. c.Data["json"] = map[string]interface{}{
  1735. "code": 401,
  1736. "msg": "用户信息不存在",
  1737. }
  1738. c.ServeJSON()
  1739. return
  1740. }
  1741. switch userInfo.SF {
  1742. case "1": // 如果是教员,则使用当前用户的RYEMPID
  1743. req.XYEMPID = userInfo.RYEMPID
  1744. case "2": // 如果是学员,则使用当前用户的RYEMPID
  1745. req.JYEMPID = userInfo.RYEMPID
  1746. }
  1747. // 调用信息化接口获取培训计划
  1748. response, err := common.GetTeachPlanFromInformationSystem(req.SBID, req.JYEMPID, req.XYEMPID)
  1749. if err != nil {
  1750. c.Ctx.Output.SetStatus(500)
  1751. c.Data["json"] = map[string]interface{}{
  1752. "code": 500,
  1753. "msg": "获取培训计划失败: " + err.Error(),
  1754. }
  1755. c.ServeJSON()
  1756. return
  1757. }
  1758. // 判断请求是否成功
  1759. if response.ResultCode != "0" {
  1760. c.Ctx.Output.SetStatus(500)
  1761. c.Data["json"] = map[string]interface{}{
  1762. "code": 500,
  1763. "msg": "获取培训计划失败: " + response.ResultDesc,
  1764. }
  1765. c.ServeJSON()
  1766. return
  1767. }
  1768. // 提取需要返回给前端的数据
  1769. type PlanInfo struct {
  1770. JXJHID string `json:"JXJHID"` // 培训计划ID
  1771. KMMC string `json:"KMMC"` // 科目名称
  1772. SDKCGUIDs []string `json:"SDKCGUIDs"` // 培训课程GUID列表
  1773. TESTSTGUIDs []string `json:"TESTKCGUIDs"` // 测试课程GUID列表
  1774. }
  1775. // 提取需要返回给前端的数据
  1776. type PlanDoInfo struct {
  1777. PXSSID string `json:"PXSSID"` // 培训实施ID
  1778. PXSSMC string `json:"PXSSMC"` // 培训实施名称
  1779. PXInfo []PlanInfo `json:"PlanInfo"` // 培训计划信息
  1780. }
  1781. var plans []PlanDoInfo
  1782. // 遍历所有培训实施计划
  1783. for _, pxss := range response.PXSS {
  1784. PlanDoInfo := PlanDoInfo{PXSSID: pxss.PXSSID, PXSSMC: pxss.PXSSMC}
  1785. // 遍历所有计划
  1786. for _, jh := range pxss.JH {
  1787. plan := PlanInfo{
  1788. JXJHID: jh.ID,
  1789. KMMC: jh.KMMC,
  1790. SDKCGUIDs: []string{},
  1791. TESTSTGUIDs: []string{},
  1792. }
  1793. // 添加SDKC中的KCGUID
  1794. for _, sdkc := range jh.SDKC {
  1795. if sdkc.KCGUID != "" {
  1796. plan.SDKCGUIDs = append(plan.SDKCGUIDs, sdkc.KCGUID)
  1797. }
  1798. }
  1799. // 添加TESTST中的KCGUID
  1800. for _, testst := range jh.TESTST {
  1801. if testst.KCGUID != "" {
  1802. plan.TESTSTGUIDs = append(plan.TESTSTGUIDs, testst.KCGUID)
  1803. }
  1804. }
  1805. PlanDoInfo.PXInfo = append(PlanDoInfo.PXInfo, plan)
  1806. }
  1807. plans = append(plans, PlanDoInfo)
  1808. }
  1809. // 返回成功数据
  1810. c.Data["json"] = map[string]interface{}{
  1811. "code": 200,
  1812. "msg": "获取培训计划成功",
  1813. "data": plans,
  1814. }
  1815. c.ServeJSON()
  1816. }
  1817. // /course/tmsplanstudent 获取TMS课程计划参与的学生
  1818. func (c *CourseController) GetTMSCoursePlanStudent() {
  1819. // 获取请求参数
  1820. var req struct {
  1821. PXSSID string `json:"PXSSID"`
  1822. }
  1823. if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
  1824. c.Ctx.Output.SetStatus(400)
  1825. c.Data["json"] = map[string]interface{}{
  1826. "code": 400,
  1827. "msg": "请求参数格式错误: " + err.Error(),
  1828. }
  1829. c.ServeJSON()
  1830. return
  1831. }
  1832. if req.PXSSID == "" {
  1833. c.Ctx.Output.SetStatus(400)
  1834. c.Data["json"] = map[string]interface{}{
  1835. "code": 400,
  1836. "msg": "PXSSID不能为空",
  1837. }
  1838. c.ServeJSON()
  1839. return
  1840. }
  1841. // 调用信息化系统获取培训学生信息
  1842. trainingStudentResp, err := common.GetTrainingStudentFromInformationSystem(req.PXSSID)
  1843. if err != nil {
  1844. c.Ctx.Output.SetStatus(500)
  1845. c.Data["json"] = map[string]interface{}{
  1846. "code": 500,
  1847. "msg": "获取培训学生信息失败: " + err.Error(),
  1848. }
  1849. c.ServeJSON()
  1850. return
  1851. }
  1852. // 检查返回结果
  1853. if trainingStudentResp.ResultCode != "0" {
  1854. c.Ctx.Output.SetStatus(500)
  1855. c.Data["json"] = map[string]interface{}{
  1856. "code": 500,
  1857. "msg": "获取培训学生信息失败: " + trainingStudentResp.ResultDesc,
  1858. }
  1859. c.ServeJSON()
  1860. return
  1861. }
  1862. // 构造简化版的学生信息列表,只包含XYEMPID和XYXM
  1863. type SimpleStudentInfo struct {
  1864. XYEMPID string `json:"XYEMPID"`
  1865. XYXM string `json:"XYXM"`
  1866. }
  1867. simpleStudents := make([]SimpleStudentInfo, 0)
  1868. for _, student := range trainingStudentResp.Students {
  1869. simpleStudents = append(simpleStudents, SimpleStudentInfo{
  1870. XYEMPID: student.XYEMPID,
  1871. XYXM: student.XYXM,
  1872. })
  1873. }
  1874. // 返回成功数据
  1875. c.Data["json"] = map[string]interface{}{
  1876. "code": 200,
  1877. "msg": "获取培训学生信息成功",
  1878. "data": simpleStudents,
  1879. "count": trainingStudentResp.Count,
  1880. }
  1881. c.ServeJSON()
  1882. }