testpaper.go 7.1 KB

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