123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package question
- import (
- "encoding/json"
- "errors"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- )
- var Question models.Question
- func Add(form form.QuestionAdd) (int64, error) {
- options, err := json.Marshal(form.Options)
- if err != nil {
- return 0, errors.New("invalid json")
- }
- questionMap := map[string]interface{}{
- "`course_id`": form.CourseId,
- "`type`": form.Type,
- "`content`": form.Content,
- "`options`": options,
- "`answer`": form.Answer,
- "`show`": form.Show,
- }
- questionID, err := db.InsertOne(Question.TableName(), questionMap)
- if err != nil {
- return 0, nil
- }
- return questionID, nil
- }
- func EditByID(form form.QuestionAdd, id int) error {
- info, err := GetInfoByID(id, nil, nil)
- if info == nil {
- return errors.New("invalid id")
- }
- options, err := json.Marshal(form.Options)
- if err != nil {
- return errors.New("invalid json")
- }
- questionMap := map[string]interface{}{
- "`course_id`": form.CourseId,
- "`type`": form.Type,
- "`content`": form.Content,
- "`options`": options,
- "`answer`": form.Answer,
- "`show`": form.Show,
- }
- _, err = db.Update(Question.TableName(), map[string]interface{}{"id": id}, questionMap)
- return err
- }
- func DeleteByID(id int) error {
- info, _ := GetInfoByID(id, nil, nil)
- if info == nil {
- return errors.New("invalid id")
- }
- _, err := db.DeleteSoft(Question.TableName(), map[string]interface{}{"id": id})
- return err
- }
- func Count(where map[string]interface{}) (int64, error) {
- return db.Count(Question.TableName(), where)
- }
- func CountRaw(where string, param map[string]interface{}) (int64, error) {
- query := "`zy_question` WHERE " + where
- return db.CountRaw(query, param)
- }
- func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Question, error) {
- if page.PageNum > 0 && page.PageSize > 0 {
- where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
- }
- return Question.GetMulti(where, fields, retVal)
- }
- func GetQuestionsRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- field := "SELECT `zy_question`.* FROM `zy_question` "
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Question, error) {
- return Question.GetOne(where, fields, retVal)
- }
- func GetQuestionOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- field := "SELECT `zy_question`.* FROM `zy_question` "
- where["_page_size"] = utils.ToStr(1)
- where["_page_num"] = utils.ToStr(1)
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetQuestionByID(id int, retVal interface{}) error {
- where := map[string]string{
- "where": "`zy_question`.`id` = {{id}}",
- }
- param := map[string]interface{}{"id": id}
- return GetQuestionOne(where, param, retVal)
- }
- func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Question, error) {
- return GetOne(map[string]interface{}{"id": id}, fields, retVal)
- }
|