123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package check
- import (
- "errors"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/train/course"
- "zhiyuan/services/train/question"
- )
- var Check models.Check
- func CheckCourse(course_id int, adminID int) (int64, error) {
- courseInfo, _ := course.GetInfoByID(course_id, nil, nil)
- if courseInfo == nil {
- return 0, errors.New("invalid id")
- }
- test, _ := GetOne(map[string]interface{}{"course_id": courseInfo.ID, "admin_id": adminID, "state": 0}, nil, nil)
- if test != nil {
- return 0, errors.New("存在未完成的考核")
- }
- qWhere := map[string]string{
- "where": "`zy_question`.`id`>0 AND `zy_question`.`deleted_at`=0 AND `zy_question`.`show`=1 AND `zy_question`.`course_id` = {{course_id}}",
- "_order_by": "rand()",
- "_page_size": utils.ToStr(courseInfo.Checks),
- "_page_num": utils.ToStr(1),
- }
- qParam := map[string]interface{}{
- "course_id": courseInfo.ID,
- }
- questionList := make([]models.Question, 0)
- if err := question.GetQuestionsRaw(qWhere, qParam, &questionList); err != nil {
- return 0, nil
- }
- if len(questionList) == 0 {
- return 0, errors.New("无考核试题")
- }
- checkMap := map[string]interface{}{
- "`course_id`": courseInfo.ID,
- "`admin_id`": adminID,
- "`total`": len(questionList),
- }
- checkID, err := db.InsertOne(Check.TableName(), checkMap)
- if err != nil {
- return 0, nil
- }
- for _, v := range questionList {
- checksMap := map[string]interface{}{
- "`check_id`": checkID,
- "`question_id`": v.ID,
- }
- _, err := db.InsertOne(models.Checks{}.TableName(), checksMap)
- if err != nil {
- db.DeleteSoft(models.Checks{}.TableName(), map[string]interface{}{"check_id": checkID})
- db.DeleteSoft(Check.TableName(), map[string]interface{}{"id": checkID})
- return 0, nil
- }
- }
- return checkID, nil
- }
- func SubmitCheck(check_id int, adminID int) error {
- checkInfo, _ := GetInfoByID(check_id, nil, nil)
- if checkInfo == nil {
- return errors.New("invalid id")
- }
- if checkInfo.AdminId != adminID {
- return errors.New("权限错误")
- }
- if checkInfo.State != 0 {
- return errors.New("考核状态错误")
- }
- if checkInfo.Total != checkInfo.Progress {
- return errors.New("存在未作答的试题")
- }
- right, err := db.GetOneMapRaw("select count(distinct `zy_checks`.`id`) as `count` from `zy_checks` left join `zy_question` on `zy_question`.`id` = `zy_checks`.`question_id` WHERE `zy_checks`.`check_id` = {{check_id}} AND `zy_checks`.`answer` = `zy_question`.`answer`", map[string]interface{}{"check_id": checkInfo.ID})
- if err != nil {
- return err
- }
- checkMap := map[string]interface{}{
- "`right`": utils.ToInt64(right["count"]),
- "`state`": 1,
- }
- _, err = db.Update(Check.TableName(), map[string]interface{}{"id": checkInfo.ID}, checkMap)
- return err
- }
- func CencelCheck(course_id int, adminID int) error {
- _, err := db.Update(Check.TableName(), map[string]interface{}{"course_id": course_id, "admin_id": adminID, "state": 0}, map[string]interface{}{"state": 2})
- return err
- }
- func Count(where map[string]interface{}) (int64, error) {
- return db.Count(Check.TableName(), where)
- }
- func CountRaw(where string, param map[string]interface{}) (int64, error) {
- query := "`zy_check` WHERE " + where
- return db.CountRaw(query, param)
- }
- func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Check, error) {
- if page.PageNum > 0 && page.PageSize > 0 {
- where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
- }
- return Check.GetMulti(where, fields, retVal)
- }
- func GetChecksRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- field := "SELECT `zy_check`.* FROM `zy_check` "
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Check, error) {
- return Check.GetOne(where, fields, retVal)
- }
- func GetCheckOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- field := "SELECT `zy_check`.* FROM `zy_check` "
- where["_page_size"] = utils.ToStr(1)
- where["_page_num"] = utils.ToStr(1)
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetCheckByID(id int, retVal interface{}) error {
- where := map[string]string{
- "where": "`zy_check`.`id` = {{id}}",
- }
- param := map[string]interface{}{"id": id}
- return GetCheckOne(where, param, retVal)
- }
- func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Check, error) {
- return GetOne(map[string]interface{}{"id": id}, fields, retVal)
- }
|