1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package checks
- import (
- "errors"
- "zhiyuan/models"
- "zhiyuan/pkg/app"
- "zhiyuan/pkg/db"
- "zhiyuan/pkg/utils"
- "zhiyuan/services/form"
- "zhiyuan/services/train/check"
- )
- var Checks models.Checks
- func SelectChecks(form form.ChecksSelect, adminID int) error {
- if form.Answer == "" {
- return errors.New("选择非法")
- }
- checksInfo, _ := GetInfoByID(form.ChecksId, nil, nil)
- if checksInfo == nil {
- return errors.New("invalid id")
- }
- checkInfo, _ := check.GetInfoByID(checksInfo.CheckId, 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 checksInfo.Answer == "" {
- _, err := db.Update(models.Check{}.TableName(), map[string]interface{}{"id": checkInfo.ID}, map[string]interface{}{
- "`progress`": checkInfo.Progress + 1,
- })
- if err != nil {
- return err
- }
- }
- _, err := db.Update(Checks.TableName(), map[string]interface{}{"id": checksInfo.ID}, map[string]interface{}{
- "`answer`": form.Answer,
- })
- return err
- }
- func Count(where map[string]interface{}) (int64, error) {
- return db.Count(Checks.TableName(), where)
- }
- func CountRaw(where string, param map[string]interface{}) (int64, error) {
- query := "`zy_checks` WHERE " + where
- return db.CountRaw(query, param)
- }
- func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Checks, error) {
- if page.PageNum > 0 && page.PageSize > 0 {
- where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
- }
- return Checks.GetMulti(where, fields, retVal)
- }
- func GetCheckssRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- field := "SELECT `zy_checks`.*, `zy_question`.`content`, `zy_question`.`type`, `zy_question`.`options`, IF(`zy_check`.`state` = 1, `zy_question`.`answer`, '') as `right` FROM `zy_checks` left join `zy_question` on `zy_question`.`id` = `zy_checks`.`question_id` left join `zy_check` on `zy_check`.`id` = `zy_checks`.`check_id`"
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Checks, error) {
- return Checks.GetOne(where, fields, retVal)
- }
- func GetChecksOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
- field := "SELECT `zy_checks`.*, `zy_question`.`content`, `zy_question`.`type`, `zy_question`.`options`, IF(`zy_check`.`state` = 1, `zy_question`.`answer`, 0) as `right` FROM `zy_checks` left join `zy_question` on `zy_question`.`id` = `zy_checks`.`question_id` left join `zy_check` on `zy_check`.`id` = `zy_checks`.`check_id`"
- where["_page_size"] = utils.ToStr(1)
- where["_page_num"] = utils.ToStr(1)
- return db.GetMultiRaw(field, where, param, retVal)
- }
- func GetChecksByID(id int, retVal interface{}) error {
- where := map[string]string{
- "where": "`zy_checks`.`id` = {{id}}",
- }
- param := map[string]interface{}{"id": id}
- return GetChecksOne(where, param, retVal)
- }
- func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Checks, error) {
- return GetOne(map[string]interface{}{"id": id}, fields, retVal)
- }
|