123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package check
- import (
- "errors"
- "time"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/train/course"
- "zhiyuan/services/train/question"
- )
- var Check models.UserCheck
- const courseId = 13
- func CheckCourse(userID int) (int64, error) {
- courseInfo, _ := course.GetInfoByID(courseId, nil, nil)
- if courseInfo == nil {
- return 0, errors.New("invalid id")
- }
- now := time.Now()
- today0 := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
- test, _ := GetOne(map[string]interface{}{"course_id": courseInfo.ID, "user_id": userID, "created_at >=": today0}, 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{}{
- "`user_id`": userID,
- "`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.UserChecks{}.TableName(), checksMap)
- if err != nil {
- db.DeleteSoft(models.UserChecks{}.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, userID int) error {
- checkInfo, _ := GetInfoByID(check_id, nil, nil)
- if checkInfo == nil {
- return errors.New("invalid id")
- }
- if checkInfo.UserId != userID {
- 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_user_checks`.`id`) as `count` from `zy_user_checks` left join `zy_question` on `zy_question`.`id` = `zy_user_checks`.`question_id` WHERE `zy_user_checks`.`check_id` = {{check_id}} AND `zy_user_checks`.`answer` = `zy_question`.`answer`", map[string]interface{}{"check_id": checkInfo.ID})
- if err != nil {
- return err
- }
- rightCount := utils.ToInt64(right["count"])
- integral := rightCount * 5 / int64(checkInfo.Total)
- db.InsertModel(db.Type(models.UserIntegral{}), map[string]interface{}{
- "user_id": userID,
- "num": integral,
- "explain": "每日答题",
- })
- 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 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_user_check` WHERE " + where
- return db.CountRaw(query, param)
- }
- func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.UserCheck, 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_user_check`.* FROM `zy_user_check` "
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.UserCheck, error) {
- return Check.GetOne(where, fields, retVal)
- }
- func GetCheckOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- field := "SELECT `zy_user_check`.* FROM `zy_user_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_user_check`.`id` = {{id}}",
- }
- param := map[string]interface{}{"id": id}
- return GetCheckOne(where, param, retVal)
- }
- func GetInfoByID(id int, fields []string, retVal interface{}) (*models.UserCheck, error) {
- return GetOne(map[string]interface{}{"id": id}, fields, retVal)
- }
|