question.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. package learning
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "lims_adapter/dao/learning"
  7. "lims_adapter/model/learning"
  8. "strconv"
  9. "dashoo.cn/micro_libary/micro_srv"
  10. "dashoo.cn/micro_libary/myerrors"
  11. "dashoo.cn/micro_libary/request"
  12. "github.com/gogf/gf/os/gtime"
  13. "github.com/gogf/gf/util/gvalid"
  14. "github.com/xuri/excelize/v2"
  15. )
  16. type LearningQuestionService struct {
  17. Dao *dao.LearningQuestionDao
  18. Tenant string
  19. userInfo request.UserInfo
  20. }
  21. func NewLearningQuestionService(ctx context.Context) (*LearningQuestionService, 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. return &LearningQuestionService{
  32. Dao: dao.NewLearningQuestionDao(tenant),
  33. Tenant: tenant,
  34. userInfo: userInfo,
  35. }, nil
  36. }
  37. func (s LearningQuestionService) Get(ctx context.Context, req *learning.LearningQuestionGetReq) (ent *learning.LearningQuestionGetRsp, err error) {
  38. validErr := gvalid.CheckStruct(ctx, req, nil)
  39. if validErr != nil {
  40. return nil, myerrors.NewMsgError(nil, validErr.Current().Error())
  41. }
  42. q, err := s.Dao.Where("Id = ?", req.Id).One()
  43. if err != nil {
  44. return nil, err
  45. }
  46. if q == nil {
  47. return nil, myerrors.NewMsgError(nil, "题目不存在")
  48. }
  49. content := []learning.LearningQuestionOption{}
  50. err = json.Unmarshal([]byte(q.Content), &content)
  51. return &learning.LearningQuestionGetRsp{
  52. LearningQuestion: *q,
  53. Content: content,
  54. }, err
  55. }
  56. func (s LearningQuestionService) List(ctx context.Context, req *learning.LearningQuestionListReq) (int, []*learning.LearningQuestionGetRsp, error) {
  57. dao := &s.Dao.LearningQuestionDao
  58. if req.Name != "" {
  59. dao = dao.Where("Name LIKE ?", fmt.Sprintf("%%%s%%", req.Name))
  60. }
  61. if req.SkillId != 0 {
  62. dao = dao.Where("SkillId = ?", req.SkillId)
  63. }
  64. total, err := dao.Count()
  65. if err != nil {
  66. return 0, nil, err
  67. }
  68. if req.Page != nil {
  69. if req.Page.Current == 0 {
  70. req.Page.Current = 1
  71. }
  72. if req.Page.Size == 0 {
  73. req.Page.Size = 10
  74. }
  75. dao = dao.Page(req.Page.Current, req.Page.Size)
  76. }
  77. if req.OrderBy != nil && req.OrderBy.Value != "" {
  78. order := "asc"
  79. if req.OrderBy.Type == "desc" {
  80. order = "desc"
  81. }
  82. dao = dao.Order(req.OrderBy.Value, order)
  83. }
  84. ent, err := dao.All()
  85. if err != nil {
  86. return 0, nil, err
  87. }
  88. var questions []*learning.LearningQuestionGetRsp
  89. for _, q := range ent {
  90. content := []learning.LearningQuestionOption{}
  91. err = json.Unmarshal([]byte(q.Content), &content)
  92. if err != nil {
  93. return 0, nil, err
  94. }
  95. questions = append(questions, &learning.LearningQuestionGetRsp{
  96. LearningQuestion: *q,
  97. Content: content,
  98. })
  99. }
  100. return total, questions, err
  101. }
  102. func (s LearningQuestionService) Add(ctx context.Context, req *learning.LearningQuestionAddReq) (int, error) {
  103. validErr := gvalid.CheckStruct(ctx, req, nil)
  104. if validErr != nil {
  105. return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
  106. }
  107. if req.Name == "" && req.NameImage == "" {
  108. return 0, myerrors.NewMsgError(nil, "请输入题目或题目图片")
  109. }
  110. if len(req.Content) < 2 {
  111. return 0, myerrors.NewMsgError(nil, "至少需要两个选项")
  112. }
  113. correctCount := 0
  114. for _, o := range req.Content {
  115. if o.IsCorrect {
  116. correctCount += 1
  117. }
  118. }
  119. if correctCount < 1 {
  120. return 0, myerrors.NewMsgError(nil, "正确答案未设置")
  121. }
  122. if (req.Type == 1 || req.Type == 3) && correctCount != 1 {
  123. return 0, myerrors.NewMsgError(nil, "正确答案只能设置一个")
  124. }
  125. content, err := json.Marshal(req.Content)
  126. if err != nil {
  127. return 0, err
  128. }
  129. id, err := s.Dao.InsertAndGetId(learning.LearningQuestion{
  130. SkillId: req.SkillId,
  131. Name: req.Name,
  132. NameImage: req.NameImage,
  133. Type: req.Type,
  134. Enable: req.Enable,
  135. Content: string(content),
  136. Explanation: req.Explanation,
  137. ExplanationImage: req.ExplanationImage,
  138. OperateBy: s.userInfo.RealName,
  139. CreatedAt: gtime.Now(),
  140. UpdatedAt: gtime.Now(),
  141. })
  142. if err != nil {
  143. return 0, err
  144. }
  145. return int(id), err
  146. }
  147. func (s LearningQuestionService) Update(ctx context.Context, req *learning.LearningQuestionUpdateReq) error {
  148. validErr := gvalid.CheckStruct(ctx, req, nil)
  149. if validErr != nil {
  150. return myerrors.NewMsgError(nil, validErr.Current().Error())
  151. }
  152. if len(req.Content) != 0 {
  153. if req.Type == nil {
  154. return myerrors.NewMsgError(nil, "请输入题型")
  155. }
  156. questionType := *req.Type
  157. if len(req.Content) < 2 {
  158. return myerrors.NewMsgError(nil, "至少需要两个选项")
  159. }
  160. correctCount := 0
  161. for _, o := range req.Content {
  162. if o.IsCorrect {
  163. correctCount += 1
  164. }
  165. }
  166. if correctCount < 1 {
  167. return myerrors.NewMsgError(nil, "正确答案未设置")
  168. }
  169. if (questionType == 1 || questionType == 3) && correctCount != 1 {
  170. return myerrors.NewMsgError(nil, "正确答案只能设置一个")
  171. }
  172. }
  173. q, err := s.Dao.Where("Id = ?", req.Id).One()
  174. if err != nil {
  175. return err
  176. }
  177. if q == nil {
  178. return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在: %d", req.Id))
  179. }
  180. if req.SkillId != 0 {
  181. r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
  182. if err != nil {
  183. return err
  184. }
  185. if r.IsEmpty() {
  186. return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
  187. }
  188. }
  189. dao := &s.Dao.LearningQuestionDao
  190. toupdate := map[string]interface{}{}
  191. if req.SkillId != 0 {
  192. toupdate["SkillId"] = req.SkillId
  193. }
  194. if req.Name != nil {
  195. toupdate["Name"] = req.Name
  196. }
  197. if req.NameImage != nil {
  198. toupdate["NameImage"] = req.NameImage
  199. }
  200. if req.Type != nil {
  201. toupdate["Type"] = req.Type
  202. }
  203. if req.Enable != nil {
  204. toupdate["Enable"] = req.Enable
  205. }
  206. if req.Content != nil {
  207. content, err := json.Marshal(req.Content)
  208. if err != nil {
  209. return err
  210. }
  211. toupdate["Content"] = content
  212. }
  213. if req.Explanation != nil {
  214. toupdate["Explanation"] = req.Explanation
  215. }
  216. if req.ExplanationImage != nil {
  217. toupdate["ExplanationImage"] = req.ExplanationImage
  218. }
  219. if len(toupdate) == 0 {
  220. return nil
  221. }
  222. toupdate["OperateBy"] = s.userInfo.RealName
  223. _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
  224. return err
  225. }
  226. func (s LearningQuestionService) Delete(ctx context.Context, id []int) error {
  227. _, err := s.Dao.Where("Id IN (?)", id).Delete()
  228. return err
  229. }
  230. func QuestionTemplate() (*excelize.File, error) {
  231. f := excelize.NewFile()
  232. sheet := "Sheet1"
  233. header := []string{
  234. "序号", "题型", "题目", "选项A", "选项B", "选项C", "选项D", "正确答案", "解析",
  235. }
  236. colWidth := []float64{
  237. 12, 12, 40, 20, 20, 20, 20, 12, 20,
  238. }
  239. tempData := [][]string{
  240. {"1", "单选题", "凝胶成像系统不能对以下哪些进行图像采集分析?", "蛋白凝胶", "培养皿", "DNA凝胶", "96孔细胞板", "B", "无"},
  241. {"2", "多选题", "凝胶成像系统有哪几种紫外光源可以选择?", "254mm为中心的宽波长紫外光源", "302mm为中心的宽波长紫外光源", "365mm为中心的宽波长紫外光源", "380mm为中心的宽波长紫外光源", "A B C", "无"},
  242. {"3", "判断题", "凝胶成像系统对紫外线光源有保护功能,如暗室门未关紧,系统将自动切断紫外,是否正确?", "正确", "错误", "", "", "A", "无"},
  243. }
  244. colStyle, err := f.NewStyle(&excelize.Style{
  245. Alignment: &excelize.Alignment{
  246. Horizontal: "center",
  247. Vertical: "center",
  248. WrapText: true,
  249. },
  250. Font: &excelize.Font{
  251. Size: 11,
  252. Family: "宋体",
  253. },
  254. })
  255. if err != nil {
  256. return nil, err
  257. }
  258. headerStyle, err := f.NewStyle(&excelize.Style{
  259. Alignment: &excelize.Alignment{
  260. Horizontal: "center",
  261. },
  262. Fill: excelize.Fill{
  263. Type: "pattern",
  264. Color: []string{"#a6a6a6"},
  265. Pattern: 1,
  266. },
  267. Border: []excelize.Border{
  268. {Type: "left", Color: "#000000", Style: 1},
  269. {Type: "top", Color: "#000000", Style: 1},
  270. {Type: "bottom", Color: "#000000", Style: 1},
  271. {Type: "right", Color: "#000000", Style: 1},
  272. },
  273. })
  274. if err != nil {
  275. return nil, err
  276. }
  277. err = f.SetColStyle(sheet, "A:I", colStyle)
  278. if err != nil {
  279. return nil, err
  280. }
  281. err = f.SetCellStyle(sheet, "A1", "I1", headerStyle)
  282. if err != nil {
  283. return nil, err
  284. }
  285. for i := range header {
  286. n, err := excelize.ColumnNumberToName(i + 1)
  287. if err != nil {
  288. return nil, err
  289. }
  290. f.SetCellValue(sheet, n+"1", header[i])
  291. }
  292. for i, w := range colWidth {
  293. n, err := excelize.ColumnNumberToName(i + 1)
  294. if err != nil {
  295. return nil, err
  296. }
  297. err = f.SetColWidth(sheet, n, n, w)
  298. if err != nil {
  299. return nil, err
  300. }
  301. }
  302. for row, item := range tempData {
  303. for col, v := range item {
  304. colName, err := excelize.ColumnNumberToName(col + 1)
  305. if err != nil {
  306. return nil, err
  307. }
  308. rowName := strconv.Itoa(row + 2)
  309. f.SetCellValue(sheet, colName+rowName, v)
  310. }
  311. }
  312. index := f.NewSheet(sheet)
  313. f.SetActiveSheet(index)
  314. return f, nil
  315. }