| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- package learning
- import (
- "context"
- "encoding/json"
- "fmt"
- "lims_adapter/dao/learning"
- "lims_adapter/model/learning"
- "strconv"
- "dashoo.cn/micro_libary/micro_srv"
- "dashoo.cn/micro_libary/myerrors"
- "dashoo.cn/micro_libary/request"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- "github.com/xuri/excelize/v2"
- )
- type LearningQuestionService struct {
- Dao *dao.LearningQuestionDao
- Tenant string
- userInfo request.UserInfo
- }
- func NewLearningQuestionService(ctx context.Context) (*LearningQuestionService, error) {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取组合码异常:%s", err.Error())
- }
- // 获取用户信息
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- return &LearningQuestionService{
- Dao: dao.NewLearningQuestionDao(tenant),
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s LearningQuestionService) Get(ctx context.Context, req *learning.LearningQuestionGetReq) (ent *learning.LearningQuestionGetRsp, err error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return nil, myerrors.NewMsgError(nil, validErr.Current().Error())
- }
- q, err := s.Dao.Where("Id = ?", req.Id).One()
- if err != nil {
- return nil, err
- }
- if q == nil {
- return nil, myerrors.NewMsgError(nil, "题目不存在")
- }
- content := []learning.LearningQuestionOption{}
- err = json.Unmarshal([]byte(q.Content), &content)
- return &learning.LearningQuestionGetRsp{
- LearningQuestion: *q,
- Content: content,
- }, err
- }
- func (s LearningQuestionService) List(ctx context.Context, req *learning.LearningQuestionListReq) (int, []*learning.LearningQuestionGetRsp, error) {
- dao := &s.Dao.LearningQuestionDao
- if req.Name != "" {
- dao = dao.Where("Name LIKE ?", fmt.Sprintf("%%%s%%", req.Name))
- }
- if req.SkillId != 0 {
- dao = dao.Where("SkillId = ?", req.SkillId)
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.Page != nil {
- if req.Page.Current == 0 {
- req.Page.Current = 1
- }
- if req.Page.Size == 0 {
- req.Page.Size = 10
- }
- dao = dao.Page(req.Page.Current, req.Page.Size)
- }
- if req.OrderBy != nil && req.OrderBy.Value != "" {
- order := "asc"
- if req.OrderBy.Type == "desc" {
- order = "desc"
- }
- dao = dao.Order(req.OrderBy.Value, order)
- }
- ent, err := dao.All()
- if err != nil {
- return 0, nil, err
- }
- var questions []*learning.LearningQuestionGetRsp
- for _, q := range ent {
- content := []learning.LearningQuestionOption{}
- err = json.Unmarshal([]byte(q.Content), &content)
- if err != nil {
- return 0, nil, err
- }
- questions = append(questions, &learning.LearningQuestionGetRsp{
- LearningQuestion: *q,
- Content: content,
- })
- }
- return total, questions, err
- }
- func (s LearningQuestionService) Add(ctx context.Context, req *learning.LearningQuestionAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.NewMsgError(nil, validErr.Current().Error())
- }
- if req.Name == "" && req.NameImage == "" {
- return 0, myerrors.NewMsgError(nil, "请输入题目或题目图片")
- }
- if len(req.Content) < 2 {
- return 0, myerrors.NewMsgError(nil, "至少需要两个选项")
- }
- correctCount := 0
- for _, o := range req.Content {
- if o.IsCorrect {
- correctCount += 1
- }
- }
- if correctCount < 1 {
- return 0, myerrors.NewMsgError(nil, "正确答案未设置")
- }
- if (req.Type == 1 || req.Type == 3) && correctCount != 1 {
- return 0, myerrors.NewMsgError(nil, "正确答案只能设置一个")
- }
- content, err := json.Marshal(req.Content)
- if err != nil {
- return 0, err
- }
- id, err := s.Dao.InsertAndGetId(learning.LearningQuestion{
- SkillId: req.SkillId,
- Name: req.Name,
- NameImage: req.NameImage,
- Type: req.Type,
- Enable: req.Enable,
- Content: string(content),
- Explanation: req.Explanation,
- ExplanationImage: req.ExplanationImage,
- OperateBy: s.userInfo.RealName,
- CreatedAt: gtime.Now(),
- UpdatedAt: gtime.Now(),
- })
- if err != nil {
- return 0, err
- }
- return int(id), err
- }
- func (s LearningQuestionService) Update(ctx context.Context, req *learning.LearningQuestionUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.NewMsgError(nil, validErr.Current().Error())
- }
- if len(req.Content) != 0 {
- if req.Type == nil {
- return myerrors.NewMsgError(nil, "请输入题型")
- }
- questionType := *req.Type
- if len(req.Content) < 2 {
- return myerrors.NewMsgError(nil, "至少需要两个选项")
- }
- correctCount := 0
- for _, o := range req.Content {
- if o.IsCorrect {
- correctCount += 1
- }
- }
- if correctCount < 1 {
- return myerrors.NewMsgError(nil, "正确答案未设置")
- }
- if (questionType == 1 || questionType == 3) && correctCount != 1 {
- return myerrors.NewMsgError(nil, "正确答案只能设置一个")
- }
- }
- q, err := s.Dao.Where("Id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if q == nil {
- return myerrors.NewMsgError(nil, fmt.Sprintf("题目不存在: %d", req.Id))
- }
- if req.SkillId != 0 {
- r, err := s.Dao.DB.Table("learning_skill").Where("Id", req.SkillId).One()
- if err != nil {
- return err
- }
- if r.IsEmpty() {
- return myerrors.NewMsgError(nil, fmt.Sprintf("技能不存在: %d", req.SkillId))
- }
- }
- dao := &s.Dao.LearningQuestionDao
- toupdate := map[string]interface{}{}
- if req.SkillId != 0 {
- toupdate["SkillId"] = req.SkillId
- }
- if req.Name != nil {
- toupdate["Name"] = req.Name
- }
- if req.NameImage != nil {
- toupdate["NameImage"] = req.NameImage
- }
- if req.Type != nil {
- toupdate["Type"] = req.Type
- }
- if req.Enable != nil {
- toupdate["Enable"] = req.Enable
- }
- if req.Content != nil {
- content, err := json.Marshal(req.Content)
- if err != nil {
- return err
- }
- toupdate["Content"] = content
- }
- if req.Explanation != nil {
- toupdate["Explanation"] = req.Explanation
- }
- if req.ExplanationImage != nil {
- toupdate["ExplanationImage"] = req.ExplanationImage
- }
- if len(toupdate) == 0 {
- return nil
- }
- toupdate["OperateBy"] = s.userInfo.RealName
- _, err = dao.Where("Id", req.Id).Data(toupdate).Update()
- return err
- }
- func (s LearningQuestionService) Delete(ctx context.Context, id []int) error {
- _, err := s.Dao.Where("Id IN (?)", id).Delete()
- return err
- }
- func QuestionTemplate() (*excelize.File, error) {
- f := excelize.NewFile()
- sheet := "Sheet1"
- header := []string{
- "序号", "题型", "题目", "选项A", "选项B", "选项C", "选项D", "正确答案", "解析",
- }
- colWidth := []float64{
- 12, 12, 40, 20, 20, 20, 20, 12, 20,
- }
- tempData := [][]string{
- {"1", "单选题", "凝胶成像系统不能对以下哪些进行图像采集分析?", "蛋白凝胶", "培养皿", "DNA凝胶", "96孔细胞板", "B", "无"},
- {"2", "多选题", "凝胶成像系统有哪几种紫外光源可以选择?", "254mm为中心的宽波长紫外光源", "302mm为中心的宽波长紫外光源", "365mm为中心的宽波长紫外光源", "380mm为中心的宽波长紫外光源", "A B C", "无"},
- {"3", "判断题", "凝胶成像系统对紫外线光源有保护功能,如暗室门未关紧,系统将自动切断紫外,是否正确?", "正确", "错误", "", "", "A", "无"},
- }
- colStyle, err := f.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- },
- Font: &excelize.Font{
- Size: 11,
- Family: "宋体",
- },
- })
- if err != nil {
- return nil, err
- }
- headerStyle, err := f.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{
- Horizontal: "center",
- },
- Fill: excelize.Fill{
- Type: "pattern",
- Color: []string{"#a6a6a6"},
- Pattern: 1,
- },
- Border: []excelize.Border{
- {Type: "left", Color: "#000000", Style: 1},
- {Type: "top", Color: "#000000", Style: 1},
- {Type: "bottom", Color: "#000000", Style: 1},
- {Type: "right", Color: "#000000", Style: 1},
- },
- })
- if err != nil {
- return nil, err
- }
- err = f.SetColStyle(sheet, "A:I", colStyle)
- if err != nil {
- return nil, err
- }
- err = f.SetCellStyle(sheet, "A1", "I1", headerStyle)
- if err != nil {
- return nil, err
- }
- for i := range header {
- n, err := excelize.ColumnNumberToName(i + 1)
- if err != nil {
- return nil, err
- }
- f.SetCellValue(sheet, n+"1", header[i])
- }
- for i, w := range colWidth {
- n, err := excelize.ColumnNumberToName(i + 1)
- if err != nil {
- return nil, err
- }
- err = f.SetColWidth(sheet, n, n, w)
- if err != nil {
- return nil, err
- }
- }
- for row, item := range tempData {
- for col, v := range item {
- colName, err := excelize.ColumnNumberToName(col + 1)
- if err != nil {
- return nil, err
- }
- rowName := strconv.Itoa(row + 2)
- f.SetCellValue(sheet, colName+rowName, v)
- }
- }
- index := f.NewSheet(sheet)
- f.SetActiveSheet(index)
- return f, nil
- }
|