testpaper.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. OperateBy: s.userInfo.RealName,
  139. CreatedAt: gtime.Now(),
  140. UpdatedAt: gtime.Now(),
  141. })
  142. if err != nil {
  143. return 0, err
  144. }
  145. if len(req.Question) == 0 {
  146. return int(id), err
  147. }
  148. bind := []learning.LearningQuestionTestpaper{}
  149. for _, qid := range req.Question {
  150. bind = append(bind, learning.LearningQuestionTestpaper{
  151. QuestionId: qid,
  152. TestpaperId: int(id),
  153. })
  154. }
  155. _, err = s.QuestionTestpaperDao.Insert(bind)
  156. return int(id), err
  157. }
  158. func (s LearningTestpaperService) Update(ctx context.Context, req *learning.LearningTestpaperUpdateReq) error {
  159. validErr := gvalid.CheckStruct(ctx, req, nil)
  160. if validErr != nil {
  161. return myerrors.NewMsgError(nil, validErr.Current().Error())
  162. }
  163. if req.PassLimit > len(req.Question) {
  164. return myerrors.NewMsgError(nil, "合格标准大于题目总数量")
  165. }
  166. tp, err := s.Dao.Where("Id = ?", req.Id).One()
  167. if err != nil {
  168. return err
  169. }
  170. if tp == nil {
  171. return myerrors.NewMsgError(nil, fmt.Sprintf("资料不存在: %d", req.Id))
  172. }
  173. if req.SkillId != 0 {
  174. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  175. if err != nil {
  176. return err
  177. }
  178. if r.IsEmpty() {
  179. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  180. }
  181. }
  182. dao := &s.Dao.LearningTestpaperDao
  183. toupdate := map[string]interface{}{}
  184. if req.SkillId != 0 {
  185. toupdate["SkillId"] = req.SkillId
  186. }
  187. if req.Name != "" {
  188. toupdate["Name"] = req.Name
  189. }
  190. if req.Enable != nil {
  191. toupdate["Enable"] = req.Enable
  192. // 禁用已启用的试卷
  193. if *req.Enable == 1 {
  194. _, err = dao.Where("SkillId", tp.SkillId).Data("Enable", 0).Update()
  195. if err != nil {
  196. return err
  197. }
  198. }
  199. }
  200. if req.TimeLimit != 0 {
  201. toupdate["TimeLimit"] = req.TimeLimit
  202. }
  203. if req.PassLimit != 0 {
  204. toupdate["PassLimit"] = req.PassLimit
  205. }
  206. if len(toupdate) != 0 {
  207. toupdate["OperateBy"] = s.userInfo.RealName
  208. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  209. if err != nil {
  210. return err
  211. }
  212. }
  213. if req.Question == nil {
  214. return nil
  215. }
  216. _, err = s.QuestionTestpaperDao.Where("TestpaperId = ?", req.Id).Delete()
  217. if err != nil {
  218. return err
  219. }
  220. if len(req.Question) == 0 {
  221. return nil
  222. }
  223. bind := []learning.LearningQuestionTestpaper{}
  224. for _, qid := range req.Question {
  225. q, err := s.QuestionDao.Where("Id = ?", qid).One()
  226. if err != nil {
  227. return err
  228. }
  229. if q == nil {
  230. return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在 %d", qid))
  231. }
  232. bind = append(bind, learning.LearningQuestionTestpaper{
  233. QuestionId: qid,
  234. TestpaperId: tp.Id,
  235. })
  236. }
  237. _, err = s.QuestionTestpaperDao.Insert(bind)
  238. return err
  239. }
  240. func (s LearningTestpaperService) Delete(ctx context.Context, id []int) error {
  241. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  242. if err != nil {
  243. return err
  244. }
  245. _, err = s.QuestionTestpaperDao.Where("TestpaperId IN (?)", id).Delete()
  246. return err
  247. }