123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- package train
- import (
- "encoding/json"
- "fmt"
- "strconv"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- "zhiyuan/services/train/course"
- "zhiyuan/services/train/question"
- "github.com/gin-gonic/gin"
- "github.com/tealeg/xlsx/v3"
- )
- func QuestionList(c *gin.Context) {
- course_id := utils.ToInt(c.Query("course_id"))
- if course_id <= 0 {
- app.Error(c, "课程id有误")
- return
- }
- courseInfo, err := course.GetOne(map[string]interface{}{"id": course_id}, []string{"`id`", "`name`", "`intro`", "`role_ids`", "`show`"}, nil)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- page := app.HandlePageNums(c)
- where := map[string]string{
- "where": "`zy_question`.`id`>0 AND `zy_question`.`deleted_at`=0",
- "_group_by": "`zy_question`.`id`",
- "_order_by": "`zy_question`.`id` desc",
- }
- if page.PageSize != 0 {
- where["_page_size"] = utils.ToStr(page.PageSize)
- where["_page_num"] = utils.ToStr(page.PageNum)
- }
- param := make(map[string]interface{})
- where["where"] = where["where"] + " AND `zy_question`.`course_id` = {{course_id}}"
- param["course_id"] = course_id
- content := c.Query("content")
- if content != "" {
- where["where"] = where["where"] + " AND `zy_question`.`content` LIKE {{content}}"
- param["content"] = "%" + content + "%"
- }
- total, err := question.CountRaw(where["where"], param)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- type QuestionList struct {
- ID int `json:"id"`
- CourseId int `json:"course_id"`
- Type int `json:"type"`
- Content string `json:"content"`
- Options string `json:"options"`
- Answer string `json:"answer"`
- Show int `json:"show"`
- Checks int `json:"checks"`
- Rights int `json:"rights"`
- CreatedAt string `json:"created_at"`
- UpdatedAt int `json:"updated_at"`
- }
- questionList := make([]QuestionList, 0)
- if err = db.GetMultiRaw("SELECT `zy_question`.*, COUNT(`zy_checks`.`id`) as `checks`, COUNT(`zy_checks`.`answer` = `zy_question`.`answer` or NULL) as `rights` FROM `zy_question` left join `zy_checks` on `zy_checks`.`question_id` = `zy_question`.`id`", where, param, &questionList); err != nil {
- app.Error(c, err.Error())
- return
- }
- for k, v := range questionList {
- v.CreatedAt = utils.DateS(v.CreatedAt, "YYYY-MM-DD HH:mm")
- questionList[k] = v
- }
- data := gin.H{
- "courseInfo": courseInfo,
- "list": questionList,
- "total": total,
- "limit": page.PageSize,
- }
- app.Success(c, data)
- }
- func QuestionAdd(c *gin.Context) {
- var addForm form.QuestionAdd
- if app.Bind(c, &addForm) != nil {
- return
- }
- id, err := question.Add(addForm)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, gin.H{"id": id})
- }
- func QuestionEdit(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "question id must be a number", nil)
- return
- }
- var addForm form.QuestionAdd
- if app.Bind(c, &addForm) != nil {
- return
- }
- err := question.EditByID(addForm, id)
- if err != nil {
- app.ErrorMsg(c, err.Error(), nil)
- return
- }
- app.Success(c, nil)
- }
- func QuestionDel(c *gin.Context) {
- id := utils.ToInt(c.Param("id"))
- if id <= 0 {
- app.ErrorMsg(c, "question id must be a number", nil)
- return
- }
- err := question.DeleteByID(id)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- app.Success(c, nil)
- }
- func QuestionImport(c *gin.Context) {
- course_id := utils.ToInt(c.Param("id"))
- if course_id <= 0 {
- app.ErrorMsg(c, "question id must be a number", nil)
- return
- }
- file, err := c.FormFile("file")
- if err != nil {
- app.Error(c, fmt.Sprintf("get form err: %s", err.Error()))
- return
- }
- fileExt := utils.FileExt(file.Filename)
- attachKey := utils.RandomStr() + fileExt
- file.Filename = "data/" + attachKey
- if err := c.SaveUploadedFile(file, file.Filename); err != nil {
- app.Error(c, fmt.Sprintf("上传文件失败%s", err.Error()))
- return
- }
- wb, err := xlsx.OpenFile(file.Filename)
- if err != nil {
- app.Error(c, err.Error())
- return
- }
- sh, ok := wb.Sheet["Sheet1"]
- if !ok {
- fmt.Println("Sheet does not exist")
- return
- }
- courseInfo, _ := course.GetInfoByID(course_id, nil, nil)
- if courseInfo == nil {
- app.ErrorMsg(c, "invalid id", nil)
- return
- }
- sh.ForEachRow(func(r *xlsx.Row) error {
- if r.GetCoordinate() <= 0 {
- return nil
- }
- types := r.GetCell(0).String()
- content := r.GetCell(1).String()
- if content == "" {
- return nil
- }
- if types == "选择" {
- answer := 0
- answerText := r.GetCell(2).String()
- options := make([]string, 0)
- for n := 3; r.GetCell(n).String() != ""; n++ {
- if answerText == r.GetCell(n).String() {
- answer = n - 2
- }
- options = append(options, r.GetCell(n).String())
- }
- _, err := question.Add(form.QuestionAdd{
- CourseId: course_id,
- Type: 0,
- Content: content,
- Options: options,
- Answer: strconv.Itoa(answer),
- Show: 1,
- })
- if err != nil {
- app.Error(c, err.Error())
- return err
- }
- } else {
- answers := make([]string, 0)
- for n := 2; r.GetCell(n).String() != ""; n++ {
- answerText := r.GetCell(n).String()
- answers = append(answers, answerText)
- }
- answer, err := json.Marshal(answers)
- if err != nil {
- app.Error(c, err.Error())
- return err
- }
- _, err = question.Add(form.QuestionAdd{
- CourseId: course_id,
- Type: 1,
- Content: content,
- Answer: string(answer),
- Show: 1,
- })
- if err != nil {
- app.Error(c, err.Error())
- return err
- }
- }
- return nil
- })
- app.Success(c, nil)
- }
|