6
0

testpaper.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package learning
  2. import (
  3. "context"
  4. "fmt"
  5. "lims_adapter/dao/learning"
  6. "lims_adapter/model/learning"
  7. "dashoo.cn/micro_libary/micro_srv"
  8. "dashoo.cn/micro_libary/myerrors"
  9. "dashoo.cn/micro_libary/request"
  10. "github.com/gogf/gf/os/gtime"
  11. "github.com/gogf/gf/util/gvalid"
  12. )
  13. type LearningTestpaperService struct {
  14. Dao *dao.LearningTestpaperDao
  15. QuestionDao *dao.LearningQuestionDao
  16. QuestionTestpaperDao *dao.LearningQuestionTestpaperDao
  17. ExamRecordService *LearningExamRecordService
  18. Tenant string
  19. userInfo request.UserInfo
  20. }
  21. func NewLearningTestpaperService(ctx context.Context) (*LearningTestpaperService, error) {
  22. tenant, err := micro_srv.GetTenant(ctx)
  23. if err != nil {
  24. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  25. }
  26. // 获取用户信息
  27. userInfo, err := micro_srv.GetUserInfo(ctx)
  28. if err != nil {
  29. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  30. }
  31. examRecordService, err := NewLearningExamRecordService(ctx)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &LearningTestpaperService{
  36. Dao: dao.NewLearningTestpaperDao(tenant),
  37. QuestionDao: dao.NewLearningQuestionDao(tenant),
  38. QuestionTestpaperDao: dao.NewLearningQuestionTestpaperDao(tenant),
  39. ExamRecordService: examRecordService,
  40. Tenant: tenant,
  41. userInfo: userInfo,
  42. }, nil
  43. }
  44. func (s LearningTestpaperService) Get(ctx context.Context, req *learning.LearningTestpaperGetReq) (*learning.LearningTestpaperGetRsp, error) {
  45. validErr := gvalid.CheckStruct(ctx, req, nil)
  46. if validErr != nil {
  47. return nil, myerrors.NewMsgError(nil, validErr.Current().Error())
  48. }
  49. tp, err := s.Dao.Where("Id = ?", req.Id).One()
  50. if err != nil {
  51. return nil, err
  52. }
  53. if tp == nil {
  54. return nil, myerrors.NewMsgError(nil, "试卷不存在")
  55. }
  56. _, record, err := s.ExamRecordService.List(ctx,
  57. &learning.LearningExamRecordListReq{
  58. TestpaperId: tp.Id,
  59. })
  60. if err != nil {
  61. return nil, err
  62. }
  63. bind, err := s.QuestionTestpaperDao.Where("TestpaperId", req.Id).All()
  64. if err != nil {
  65. return nil, err
  66. }
  67. question := []int{}
  68. for _, b := range bind {
  69. question = append(question, b.QuestionId)
  70. }
  71. return &learning.LearningTestpaperGetRsp{
  72. LearningTestpaper: *tp,
  73. ExamRecord: record,
  74. Question: question,
  75. }, nil
  76. }
  77. func (s LearningTestpaperService) List(ctx context.Context, req *learning.LearningTestpaperListReq) (int, []*learning.LearningTestpaper, error) {
  78. dao := &s.Dao.LearningTestpaperDao
  79. if req.SkillId != 0 {
  80. dao = dao.Where("SkillId = ?", req.SkillId)
  81. }
  82. if req.Enable != nil {
  83. dao = dao.Where("Enable = ?", req.Enable)
  84. }
  85. total, err := dao.Count()
  86. if err != nil {
  87. return 0, nil, err
  88. }
  89. if req.Page != nil {
  90. if req.Page.Current == 0 {
  91. req.Page.Current = 1
  92. }
  93. if req.Page.Size == 0 {
  94. req.Page.Size = 10
  95. }
  96. dao = dao.Page(req.Page.Current, req.Page.Size)
  97. }
  98. if req.OrderBy != nil && req.OrderBy.Value != "" {
  99. order := "asc"
  100. if req.OrderBy.Type == "desc" {
  101. order = "desc"
  102. }
  103. dao = dao.Order(req.OrderBy.Value, order)
  104. }
  105. ent, err := dao.All()
  106. return total, ent, err
  107. }
  108. func (s LearningTestpaperService) Add(ctx context.Context, req *learning.LearningTestpaperAddReq) (int, error) {
  109. validErr := gvalid.CheckStruct(ctx, req, nil)
  110. if validErr != nil {
  111. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  112. }
  113. if req.PassLimit > len(req.Question) {
  114. return 0, myerrors.NewMsgError(nil, "合格标准大于题目总数量")
  115. }
  116. r, err := s.Dao.DB.Table("learning_skill").Where("Id = ?", req.SkillId).One()
  117. if err != nil {
  118. return 0, err
  119. }
  120. if r.IsEmpty() {
  121. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  122. }
  123. for _, qid := range req.Question {
  124. q, err := s.QuestionDao.Where("Id = ?", qid).One()
  125. if err != nil {
  126. return 0, err
  127. }
  128. if q == nil {
  129. return 0, myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在 %d", qid))
  130. }
  131. }
  132. id, err := s.Dao.InsertAndGetId(learning.LearningTestpaper{
  133. SkillId: req.SkillId,
  134. Name: req.Name,
  135. Enable: 0,
  136. TimeLimit: req.TimeLimit,
  137. PassLimit: req.PassLimit,
  138. CreatedAt: gtime.Now(),
  139. UpdatedAt: gtime.Now(),
  140. })
  141. if err != nil {
  142. return 0, err
  143. }
  144. if len(req.Question) == 0 {
  145. return int(id), err
  146. }
  147. bind := []learning.LearningQuestionTestpaper{}
  148. for _, qid := range req.Question {
  149. bind = append(bind, learning.LearningQuestionTestpaper{
  150. QuestionId: qid,
  151. TestpaperId: int(id),
  152. })
  153. }
  154. _, err = s.QuestionTestpaperDao.Insert(bind)
  155. return int(id), err
  156. }
  157. func (s LearningTestpaperService) Update(ctx context.Context, req *learning.LearningTestpaperUpdateReq) error {
  158. validErr := gvalid.CheckStruct(ctx, req, nil)
  159. if validErr != nil {
  160. return myerrors.NewMsgError(nil, validErr.Current().Error())
  161. }
  162. if req.PassLimit > len(req.Question) {
  163. return myerrors.NewMsgError(nil, "合格标准大于题目总数量")
  164. }
  165. tp, err := s.Dao.Where("Id = ?", req.Id).One()
  166. if err != nil {
  167. return err
  168. }
  169. if tp == nil {
  170. return myerrors.NewMsgError(nil, fmt.Sprintf("资料不存在: %d", req.Id))
  171. }
  172. if req.SkillId != 0 {
  173. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  174. if err != nil {
  175. return err
  176. }
  177. if r.IsEmpty() {
  178. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  179. }
  180. }
  181. dao := &s.Dao.LearningTestpaperDao
  182. toupdate := map[string]interface{}{}
  183. if req.SkillId != 0 {
  184. toupdate["SkillId"] = req.SkillId
  185. }
  186. if req.Name != "" {
  187. toupdate["Name"] = req.Name
  188. }
  189. if req.Enable != nil {
  190. toupdate["Enable"] = req.Enable
  191. // 禁用已启用的试卷
  192. if *req.Enable == 1 {
  193. _, err = dao.Where("SkillId", tp.SkillId).Data("Enable", 0).Update()
  194. if err != nil {
  195. return err
  196. }
  197. }
  198. }
  199. if req.TimeLimit != 0 {
  200. toupdate["TimeLimit"] = req.TimeLimit
  201. }
  202. if req.PassLimit != 0 {
  203. toupdate["PassLimit"] = req.PassLimit
  204. }
  205. if len(toupdate) != 0 {
  206. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  207. if err != nil {
  208. return err
  209. }
  210. }
  211. if req.Question == nil {
  212. return nil
  213. }
  214. _, err = s.QuestionTestpaperDao.Where("TestpaperId = ?", req.Id).Delete()
  215. if err != nil {
  216. return err
  217. }
  218. if len(req.Question) == 0 {
  219. return nil
  220. }
  221. bind := []learning.LearningQuestionTestpaper{}
  222. for _, qid := range req.Question {
  223. q, err := s.QuestionDao.Where("Id = ?", qid).One()
  224. if err != nil {
  225. return err
  226. }
  227. if q == nil {
  228. return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在 %d", qid))
  229. }
  230. bind = append(bind, learning.LearningQuestionTestpaper{
  231. QuestionId: qid,
  232. TestpaperId: tp.Id,
  233. })
  234. }
  235. _, err = s.QuestionTestpaperDao.Insert(bind)
  236. return err
  237. }
  238. func (s LearningTestpaperService) Delete(ctx context.Context, id []int) error {
  239. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  240. if err != nil {
  241. return err
  242. }
  243. _, err = s.QuestionTestpaperDao.Where("TestpaperId IN (?)", id).Delete()
  244. return err
  245. }