question.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 req.Name == "" && req.NameImage == "" {
  106. return 0, myerrors.NewMsgError(nil, "请输入题目或题目图片")
  107. }
  108. if len(req.Content) < 2 {
  109. return 0, myerrors.NewMsgError(nil, "至少需要两个选项")
  110. }
  111. correctCount := 0
  112. for _, o := range req.Content {
  113. if o.IsCorrect {
  114. correctCount += 1
  115. }
  116. }
  117. if correctCount < 1 {
  118. return 0, myerrors.NewMsgError(nil, "正确答案未设置")
  119. }
  120. if (req.Type == 1 || req.Type == 3) && correctCount != 1 {
  121. return 0, myerrors.NewMsgError(nil, "正确答案只能设置一个")
  122. }
  123. content, err := json.Marshal(req.Content)
  124. if err != nil {
  125. return 0, err
  126. }
  127. id, err := s.Dao.InsertAndGetId(learning.LearningQuestion{
  128. SkillId: req.SkillId,
  129. Name: req.Name,
  130. NameImage: req.NameImage,
  131. Type: req.Type,
  132. Enable: req.Enable,
  133. Content: string(content),
  134. Explanation: req.Explanation,
  135. ExplanationImage: req.ExplanationImage,
  136. OperateBy: s.userInfo.RealName,
  137. CreatedAt: gtime.Now(),
  138. UpdatedAt: gtime.Now(),
  139. })
  140. if err != nil {
  141. return 0, err
  142. }
  143. return int(id), err
  144. }
  145. func (s LearningQuestionService) Update(ctx context.Context, req *learning.LearningQuestionUpdateReq) error {
  146. validErr := gvalid.CheckStruct(ctx, req, nil)
  147. if validErr != nil {
  148. return validErr.Current()
  149. }
  150. if len(req.Content) != 0 {
  151. if req.Type == nil {
  152. return myerrors.NewMsgError(nil, "请输入题型")
  153. }
  154. questionType := *req.Type
  155. if len(req.Content) < 2 {
  156. return myerrors.NewMsgError(nil, "至少需要两个选项")
  157. }
  158. correctCount := 0
  159. for _, o := range req.Content {
  160. if o.IsCorrect {
  161. correctCount += 1
  162. }
  163. }
  164. if correctCount < 1 {
  165. return myerrors.NewMsgError(nil, "正确答案未设置")
  166. }
  167. if (questionType == 1 || questionType == 3) && correctCount != 1 {
  168. return myerrors.NewMsgError(nil, "正确答案只能设置一个")
  169. }
  170. }
  171. q, err := s.Dao.Where("Id = ?", req.Id).One()
  172. if err != nil {
  173. return err
  174. }
  175. if q == nil {
  176. return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在: %d", req.Id))
  177. }
  178. if req.SkillId != 0 {
  179. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  180. if err != nil {
  181. return err
  182. }
  183. if r.IsEmpty() {
  184. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  185. }
  186. }
  187. dao := &s.Dao.LearningQuestionDao
  188. toupdate := map[string]interface{}{}
  189. if req.SkillId != 0 {
  190. toupdate["SkillId"] = req.SkillId
  191. }
  192. if req.Name != nil {
  193. toupdate["Name"] = req.Name
  194. }
  195. if req.NameImage != nil {
  196. toupdate["NameImage"] = req.NameImage
  197. }
  198. if req.Type != nil {
  199. toupdate["Type"] = req.Type
  200. }
  201. if req.Enable != nil {
  202. toupdate["Enable"] = req.Enable
  203. }
  204. if req.Content != nil {
  205. content, err := json.Marshal(req.Content)
  206. if err != nil {
  207. return err
  208. }
  209. toupdate["Content"] = content
  210. }
  211. if req.Explanation != nil {
  212. toupdate["Explanation"] = req.Explanation
  213. }
  214. if req.ExplanationImage != nil {
  215. toupdate["ExplanationImage"] = req.ExplanationImage
  216. }
  217. if len(toupdate) == 0 {
  218. return nil
  219. }
  220. toupdate["OperateBy"] = s.userInfo.RealName
  221. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  222. return err
  223. }
  224. func (s LearningQuestionService) Delete(ctx context.Context, id int) error {
  225. _, err := s.Dao.Where("Id = ?", id).Delete()
  226. return err
  227. }