checks.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package checks
  2. import (
  3. "errors"
  4. "zhiyuan/models"
  5. "zhiyuan/pkg/app"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/pkg/utils"
  8. "zhiyuan/services/form"
  9. "zhiyuan/services/train/check"
  10. )
  11. var Checks models.Checks
  12. func SelectChecks(form form.ChecksSelect, adminID int) error {
  13. if form.Answer == "" {
  14. return errors.New("选择非法")
  15. }
  16. checksInfo, _ := GetInfoByID(form.ChecksId, nil, nil)
  17. if checksInfo == nil {
  18. return errors.New("invalid id")
  19. }
  20. checkInfo, _ := check.GetInfoByID(checksInfo.CheckId, nil, nil)
  21. if checkInfo == nil {
  22. return errors.New("invalid id")
  23. }
  24. if checkInfo.AdminId != adminID {
  25. return errors.New("权限错误")
  26. }
  27. if checkInfo.State != 0 {
  28. return errors.New("考核状态错误")
  29. }
  30. if checksInfo.Answer == "" {
  31. _, err := db.Update(models.Check{}.TableName(), map[string]interface{}{"id": checkInfo.ID}, map[string]interface{}{
  32. "`progress`": checkInfo.Progress + 1,
  33. })
  34. if err != nil {
  35. return err
  36. }
  37. }
  38. _, err := db.Update(Checks.TableName(), map[string]interface{}{"id": checksInfo.ID}, map[string]interface{}{
  39. "`answer`": form.Answer,
  40. })
  41. return err
  42. }
  43. func Count(where map[string]interface{}) (int64, error) {
  44. return db.Count(Checks.TableName(), where)
  45. }
  46. func CountRaw(where string, param map[string]interface{}) (int64, error) {
  47. query := "`zy_checks` WHERE " + where
  48. return db.CountRaw(query, param)
  49. }
  50. func GetList(where map[string]interface{}, fields []string, page app.Page, retVal interface{}) ([]*models.Checks, error) {
  51. if page.PageNum > 0 && page.PageSize > 0 {
  52. where["_limit"] = db.GetOffset(uint(page.PageNum), uint(page.PageSize))
  53. }
  54. return Checks.GetMulti(where, fields, retVal)
  55. }
  56. func GetCheckssRaw(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  57. 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`"
  58. return db.GetMultiRaw(field, where, param, retVal)
  59. }
  60. func GetOne(where map[string]interface{}, fields []string, retVal interface{}) (*models.Checks, error) {
  61. return Checks.GetOne(where, fields, retVal)
  62. }
  63. func GetChecksOne(where map[string]string, param map[string]interface{}, retVal interface{}) error {
  64. 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`"
  65. where["_page_size"] = utils.ToStr(1)
  66. where["_page_num"] = utils.ToStr(1)
  67. return db.GetMultiRaw(field, where, param, retVal)
  68. }
  69. func GetChecksByID(id int, retVal interface{}) error {
  70. where := map[string]string{
  71. "where": "`zy_checks`.`id` = {{id}}",
  72. }
  73. param := map[string]interface{}{"id": id}
  74. return GetChecksOne(where, param, retVal)
  75. }
  76. func GetInfoByID(id int, fields []string, retVal interface{}) (*models.Checks, error) {
  77. return GetOne(map[string]interface{}{"id": id}, fields, retVal)
  78. }