6
0

testpaper.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package learning
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "lims_adapter/dao/learning"
  7. "lims_adapter/model/learning"
  8. "dashoo.cn/micro_libary/micro_srv"
  9. "dashoo.cn/micro_libary/myerrors"
  10. "dashoo.cn/micro_libary/request"
  11. "github.com/gogf/gf/os/gtime"
  12. "github.com/gogf/gf/util/gvalid"
  13. )
  14. type LearningTestpaperService struct {
  15. Dao *dao.LearningTestpaperDao
  16. QuestionDao *dao.LearningQuestionDao
  17. QuestionTestpaperDao *dao.LearningQuestionTestpaperDao
  18. SkillDao *dao.LearningSkillDao
  19. LearningMaterialSrv *LearningMaterialService
  20. LearningRecordSrv *LearningLearningRecordService
  21. ExamRecordService *LearningExamRecordService
  22. Tenant string
  23. userInfo request.UserInfo
  24. }
  25. func NewLearningTestpaperService(ctx context.Context) (*LearningTestpaperService, error) {
  26. tenant, err := micro_srv.GetTenant(ctx)
  27. if err != nil {
  28. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  29. }
  30. // 获取用户信息
  31. userInfo, err := micro_srv.GetUserInfo(ctx)
  32. if err != nil {
  33. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  34. }
  35. examRecordService, err := NewLearningExamRecordService(ctx)
  36. if err != nil {
  37. return nil, err
  38. }
  39. lrSrv, err := NewLearningLearningRecordService(ctx)
  40. if err != nil {
  41. return nil, err
  42. }
  43. lmSrv, err := NewLearningMaterialService(ctx)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &LearningTestpaperService{
  48. Dao: dao.NewLearningTestpaperDao(tenant),
  49. QuestionDao: dao.NewLearningQuestionDao(tenant),
  50. QuestionTestpaperDao: dao.NewLearningQuestionTestpaperDao(tenant),
  51. SkillDao: dao.NewLearningSkillDao(tenant),
  52. LearningMaterialSrv: lmSrv,
  53. LearningRecordSrv: lrSrv,
  54. ExamRecordService: examRecordService,
  55. Tenant: tenant,
  56. userInfo: userInfo,
  57. }, nil
  58. }
  59. func (s LearningTestpaperService) Get(ctx context.Context, req *learning.LearningTestpaperGetReq) (*learning.LearningTestpaperGetRsp, error) {
  60. validErr := gvalid.CheckStruct(ctx, req, nil)
  61. if validErr != nil {
  62. return nil, myerrors.NewMsgError(nil, validErr.Current().Error())
  63. }
  64. tp, err := s.Dao.Where("Id = ?", req.Id).One()
  65. if err != nil {
  66. return nil, err
  67. }
  68. if tp == nil {
  69. return nil, myerrors.NewMsgError(nil, "试卷不存在")
  70. }
  71. _, record, err := s.ExamRecordService.List(ctx,
  72. &learning.LearningExamRecordListReq{
  73. TestpaperId: tp.Id,
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. questionEnt := []*learning.LearningQuestion{}
  79. err = s.QuestionTestpaperDao.
  80. LeftJoin("learning_question",
  81. "learning_question_testpaper.QuestionId=learning_question.Id").
  82. Where("learning_question_testpaper.TestpaperId", req.Id).
  83. Fields("learning_question.*").Structs(&questionEnt)
  84. if err != nil && err != sql.ErrNoRows {
  85. return nil, err
  86. }
  87. quesitons, err := ConvLearningQuestionGetRsp(questionEnt)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &learning.LearningTestpaperGetRsp{
  92. LearningTestpaper: *tp,
  93. ExamRecord: record,
  94. Question: quesitons,
  95. }, nil
  96. }
  97. func (s LearningTestpaperService) List(ctx context.Context, req *learning.LearningTestpaperListReq) (int, []*learning.LearningTestpaper, error) {
  98. dao := &s.Dao.LearningTestpaperDao
  99. if req.SkillId != 0 {
  100. dao = dao.Where("SkillId = ?", req.SkillId)
  101. }
  102. if req.Enable != nil {
  103. dao = dao.Where("Enable = ?", req.Enable)
  104. }
  105. total, err := dao.Count()
  106. if err != nil {
  107. return 0, nil, err
  108. }
  109. if req.Page != nil {
  110. if req.Page.Current == 0 {
  111. req.Page.Current = 1
  112. }
  113. if req.Page.Size == 0 {
  114. req.Page.Size = 10
  115. }
  116. dao = dao.Page(req.Page.Current, req.Page.Size)
  117. }
  118. if req.OrderBy != nil && req.OrderBy.Value != "" {
  119. order := "asc"
  120. if req.OrderBy.Type == "desc" {
  121. order = "desc"
  122. }
  123. dao = dao.Order(req.OrderBy.Value, order)
  124. }
  125. ent, err := dao.All()
  126. return total, ent, err
  127. }
  128. func (s LearningTestpaperService) ListMy(ctx context.Context) ([]*learning.LearningTestpaperMy, error) {
  129. enable := 1
  130. _, testpaper, err := s.List(ctx, &learning.LearningTestpaperListReq{Enable: &enable})
  131. if err != nil {
  132. return nil, err
  133. }
  134. list := []*learning.LearningTestpaperMy{}
  135. for _, tp := range testpaper {
  136. skill, err := s.SkillDao.Where("Id = ?", tp.SkillId).One()
  137. if err != nil {
  138. return nil, err
  139. }
  140. if skill == nil {
  141. continue
  142. }
  143. materialIds, err := s.LearningMaterialSrv.MaterialIds(ctx, skill.Id)
  144. if err != nil {
  145. return nil, err
  146. }
  147. recordIds, err := s.LearningRecordSrv.LearntMaterialIds(ctx, int(s.userInfo.Id))
  148. if err != nil {
  149. return nil, err
  150. }
  151. // 如果没有关联的资料,直接算学完了
  152. learntAll := true
  153. for _, mid := range materialIds {
  154. found := false
  155. for _, rid := range recordIds {
  156. if mid == rid {
  157. found = true
  158. }
  159. }
  160. if !found {
  161. learntAll = false
  162. break
  163. }
  164. }
  165. list = append(list, &learning.LearningTestpaperMy{
  166. LearningTestpaper: *tp,
  167. SkillName: skill.Name,
  168. LearntAll: learntAll,
  169. })
  170. }
  171. return list, nil
  172. }
  173. func (s LearningTestpaperService) Add(ctx context.Context, req *learning.LearningTestpaperAddReq) (int, error) {
  174. validErr := gvalid.CheckStruct(ctx, req, nil)
  175. if validErr != nil {
  176. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  177. }
  178. if req.PassLimit > len(req.Question) {
  179. return 0, myerrors.NewMsgError(nil, "合格标准大于题目总数量")
  180. }
  181. r, err := s.Dao.DB.Table("learning_skill").Where("Id = ?", req.SkillId).One()
  182. if err != nil {
  183. return 0, err
  184. }
  185. if r.IsEmpty() {
  186. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  187. }
  188. for _, qid := range req.Question {
  189. q, err := s.QuestionDao.Where("Id = ?", qid).One()
  190. if err != nil {
  191. return 0, err
  192. }
  193. if q == nil {
  194. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在 %d", qid))
  195. }
  196. }
  197. id, err := s.Dao.InsertAndGetId(learning.LearningTestpaper{
  198. SkillId: req.SkillId,
  199. Name: req.Name,
  200. Enable: 0,
  201. TimeLimit: req.TimeLimit,
  202. PassLimit: req.PassLimit,
  203. OperateBy: s.userInfo.RealName,
  204. CreatedAt: gtime.Now(),
  205. UpdatedAt: gtime.Now(),
  206. })
  207. if err != nil {
  208. return 0, err
  209. }
  210. if len(req.Question) == 0 {
  211. return int(id), err
  212. }
  213. bind := []learning.LearningQuestionTestpaper{}
  214. for _, qid := range req.Question {
  215. bind = append(bind, learning.LearningQuestionTestpaper{
  216. QuestionId: qid,
  217. TestpaperId: int(id),
  218. })
  219. }
  220. _, err = s.QuestionTestpaperDao.Insert(bind)
  221. return int(id), err
  222. }
  223. func (s LearningTestpaperService) Update(ctx context.Context, req *learning.LearningTestpaperUpdateReq) error {
  224. validErr := gvalid.CheckStruct(ctx, req, nil)
  225. if validErr != nil {
  226. return myerrors.NewMsgError(nil, validErr.Current().Error())
  227. }
  228. if req.PassLimit > len(req.Question) {
  229. return myerrors.NewMsgError(nil, "合格标准大于题目总数量")
  230. }
  231. tp, err := s.Dao.Where("Id = ?", req.Id).One()
  232. if err != nil {
  233. return err
  234. }
  235. if tp == nil {
  236. return myerrors.NewMsgError(nil, fmt.Sprintf("资料不存在: %d", req.Id))
  237. }
  238. if req.SkillId != 0 {
  239. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  240. if err != nil {
  241. return err
  242. }
  243. if r.IsEmpty() {
  244. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  245. }
  246. }
  247. dao := &s.Dao.LearningTestpaperDao
  248. toupdate := map[string]interface{}{}
  249. if req.SkillId != 0 {
  250. toupdate["SkillId"] = req.SkillId
  251. }
  252. if req.Name != "" {
  253. toupdate["Name"] = req.Name
  254. }
  255. if req.Enable != nil {
  256. toupdate["Enable"] = req.Enable
  257. // 禁用已启用的试卷
  258. if *req.Enable == 1 {
  259. _, err = dao.Where("SkillId", tp.SkillId).Data("Enable", 0).Update()
  260. if err != nil {
  261. return err
  262. }
  263. }
  264. }
  265. if req.TimeLimit != 0 {
  266. toupdate["TimeLimit"] = req.TimeLimit
  267. }
  268. if req.PassLimit != 0 {
  269. toupdate["PassLimit"] = req.PassLimit
  270. }
  271. if len(toupdate) != 0 {
  272. toupdate["OperateBy"] = s.userInfo.RealName
  273. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  274. if err != nil {
  275. return err
  276. }
  277. }
  278. if req.Question == nil {
  279. return nil
  280. }
  281. _, err = s.QuestionTestpaperDao.Where("TestpaperId = ?", req.Id).Delete()
  282. if err != nil {
  283. return err
  284. }
  285. if len(req.Question) == 0 {
  286. return nil
  287. }
  288. bind := []learning.LearningQuestionTestpaper{}
  289. for _, qid := range req.Question {
  290. q, err := s.QuestionDao.Where("Id = ?", qid).One()
  291. if err != nil {
  292. return err
  293. }
  294. if q == nil {
  295. return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在 %d", qid))
  296. }
  297. bind = append(bind, learning.LearningQuestionTestpaper{
  298. QuestionId: qid,
  299. TestpaperId: tp.Id,
  300. })
  301. }
  302. _, err = s.QuestionTestpaperDao.Insert(bind)
  303. return err
  304. }
  305. func (s LearningTestpaperService) Delete(ctx context.Context, id []int) error {
  306. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  307. if err != nil {
  308. return err
  309. }
  310. _, err = s.QuestionTestpaperDao.Where("TestpaperId IN (?)", id).Delete()
  311. return err
  312. }