question.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package learning
  2. import (
  3. "context"
  4. "encoding/json"
  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 LearningQuestionService struct {
  15. Dao *dao.LearningQuestionDao
  16. Tenant string
  17. userInfo request.UserInfo
  18. }
  19. func NewLearningQuestionService(ctx context.Context) (*LearningQuestionService, error) {
  20. tenant, err := micro_srv.GetTenant(ctx)
  21. if err != nil {
  22. return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
  23. }
  24. // 获取用户信息
  25. userInfo, err := micro_srv.GetUserInfo(ctx)
  26. if err != nil {
  27. return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
  28. }
  29. return &LearningQuestionService{
  30. Dao: dao.NewLearningQuestionDao(tenant),
  31. Tenant: tenant,
  32. userInfo: userInfo,
  33. }, nil
  34. }
  35. func (s LearningQuestionService) Get(ctx context.Context, req *learning.LearningQuestionGetReq) (ent *learning.LearningQuestionGetRsp, err error) {
  36. validErr := gvalid.CheckStruct(ctx, req, nil)
  37. if validErr != nil {
  38. return nil, validErr.Current()
  39. }
  40. q, err := s.Dao.Where("Id = ?", req.Id).One()
  41. if err != nil {
  42. return nil, err
  43. }
  44. if q == nil {
  45. return nil, myerrors.NewMsgError(nil, "题目不存在")
  46. }
  47. content := []learning.LearningQuestionOption{}
  48. err = json.Unmarshal([]byte(q.Content), &content)
  49. return &learning.LearningQuestionGetRsp{
  50. LearningQuestion: *q,
  51. Content: content,
  52. }, err
  53. }
  54. func (s LearningQuestionService) List(ctx context.Context, req *learning.LearningQuestionListReq) (int, []*learning.LearningQuestionGetRsp, error) {
  55. dao := &s.Dao.LearningQuestionDao
  56. if req.Name != "" {
  57. dao = dao.Where("Name LIKE ?", fmt.Sprintf("%%%s%%", req.Name))
  58. }
  59. if req.SkillId != 0 {
  60. dao = dao.Where("SkillId = ?", req.SkillId)
  61. }
  62. total, err := dao.Count()
  63. if err != nil {
  64. return 0, nil, err
  65. }
  66. if req.Page != nil {
  67. if req.Page.Current == 0 {
  68. req.Page.Current = 1
  69. }
  70. if req.Page.Size == 0 {
  71. req.Page.Size = 10
  72. }
  73. dao = dao.Page(req.Page.Current, req.Page.Size)
  74. }
  75. if req.OrderBy != nil && req.OrderBy.Value != "" {
  76. order := "asc"
  77. if req.OrderBy.Type == "desc" {
  78. order = "desc"
  79. }
  80. dao = dao.Order(req.OrderBy.Value, order)
  81. }
  82. ent, err := dao.All()
  83. if err != nil {
  84. return 0, nil, err
  85. }
  86. var questions []*learning.LearningQuestionGetRsp
  87. for _, q := range ent {
  88. content := []learning.LearningQuestionOption{}
  89. err = json.Unmarshal([]byte(q.Content), &content)
  90. if err != nil {
  91. return 0, nil, err
  92. }
  93. questions = append(questions, &learning.LearningQuestionGetRsp{
  94. LearningQuestion: *q,
  95. Content: content,
  96. })
  97. }
  98. return total, questions, err
  99. }
  100. func (s LearningQuestionService) Add(ctx context.Context, req *learning.LearningQuestionAddReq) (int, error) {
  101. validErr := gvalid.CheckStruct(ctx, req, nil)
  102. if validErr != nil {
  103. return 0, validErr.Current()
  104. }
  105. if len(req.Content) < 2 {
  106. return 0, myerrors.NewMsgError(nil, "至少需要两个选项")
  107. }
  108. correctCount := 0
  109. for _, o := range req.Content {
  110. if o.IsCorrect {
  111. correctCount += 1
  112. }
  113. }
  114. if correctCount < 1 {
  115. return 0, myerrors.NewMsgError(nil, "正确答案未设置")
  116. }
  117. if (req.Type == 1 || req.Type == 3) && correctCount != 1 {
  118. return 0, myerrors.NewMsgError(nil, "正确答案只能设置一个")
  119. }
  120. content, err := json.Marshal(req.Content)
  121. if err != nil {
  122. return 0, err
  123. }
  124. id, err := s.Dao.InsertAndGetId(learning.LearningQuestion{
  125. SkillId: req.SkillId,
  126. Name: req.Name,
  127. Type: req.Type,
  128. Enable: req.Enable,
  129. Content: string(content),
  130. Explanation: req.Explanation,
  131. OperateBy: s.userInfo.RealName,
  132. CreatedAt: gtime.Now(),
  133. UpdatedAt: gtime.Now(),
  134. })
  135. if err != nil {
  136. return 0, err
  137. }
  138. return int(id), err
  139. }
  140. func (s LearningQuestionService) Update(ctx context.Context, req *learning.LearningQuestionUpdateReq) error {
  141. validErr := gvalid.CheckStruct(ctx, req, nil)
  142. if validErr != nil {
  143. return validErr.Current()
  144. }
  145. if len(req.Content) != 0 {
  146. if len(req.Content) < 2 {
  147. return myerrors.NewMsgError(nil, "至少需要两个选项")
  148. }
  149. correctCount := 0
  150. for _, o := range req.Content {
  151. if o.IsCorrect {
  152. correctCount += 1
  153. }
  154. }
  155. if correctCount < 1 {
  156. return myerrors.NewMsgError(nil, "正确答案未设置")
  157. }
  158. if (req.Type == 1 || req.Type == 3) && correctCount != 1 {
  159. return myerrors.NewMsgError(nil, "正确答案只能设置一个")
  160. }
  161. }
  162. q, err := s.Dao.Where("Id = ?", req.Id).One()
  163. if err != nil {
  164. return err
  165. }
  166. if q == nil {
  167. return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在: %d", req.Id))
  168. }
  169. if req.SkillId != 0 {
  170. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  171. if err != nil {
  172. return err
  173. }
  174. if r.IsEmpty() {
  175. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  176. }
  177. }
  178. dao := &s.Dao.LearningQuestionDao
  179. toupdate := map[string]interface{}{}
  180. if req.SkillId != 0 {
  181. toupdate["SkillId"] = req.SkillId
  182. }
  183. if req.Name != "" {
  184. toupdate["Name"] = req.Name
  185. }
  186. if req.Type != 0 {
  187. toupdate["Type"] = req.Type
  188. }
  189. if req.Enable != nil {
  190. toupdate["Enable"] = req.Enable
  191. }
  192. if req.Content != nil {
  193. content, err := json.Marshal(req.Content)
  194. if err != nil {
  195. return err
  196. }
  197. toupdate["Content"] = content
  198. }
  199. if req.Explanation != "" {
  200. toupdate["Explanation"] = req.Explanation
  201. }
  202. if len(toupdate) == 0 {
  203. return nil
  204. }
  205. toupdate["OperateBy"] = s.userInfo.RealName
  206. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  207. return err
  208. }
  209. func (s LearningQuestionService) Delete(ctx context.Context, id int) error {
  210. _, err := s.Dao.Where("Id = ?", id).Delete()
  211. return err
  212. }