apisql_service.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package apisql_service
  2. import (
  3. "errors"
  4. "github.com/kptyun/KPTCOMM/models"
  5. )
  6. type Apisql struct {
  7. ID int
  8. Sqlstr string
  9. Sqlname string
  10. Returetype string
  11. Method string
  12. CreatedBy string
  13. ModifiedBy string
  14. PageNum int
  15. PageSize int
  16. }
  17. func (a *Apisql) Add() (id int, err error) {
  18. data := map[string]interface{}{
  19. "Sqlstr": a.Sqlstr,
  20. "Sqlname": a.Sqlname,
  21. "Returetype": a.Returetype,
  22. "Method": a.Method,
  23. }
  24. Sqlname, _ := models.CheckSqlname(a.Sqlname)
  25. if Sqlname {
  26. return 0, errors.New("名字重复,请更改!")
  27. }
  28. if id, err := models.AddApisql(data); err == nil {
  29. return id, err
  30. } else {
  31. return 0, err
  32. }
  33. }
  34. func (a *Apisql) Edit() error {
  35. data := map[string]interface{}{
  36. "Sqlstr": a.Sqlstr,
  37. "Sqlname": a.Sqlname,
  38. "Returetype": a.Returetype,
  39. "Method": a.Method,
  40. }
  41. Sqlname, _ := models.CheckSqlname(a.Sqlname)
  42. if Sqlname {
  43. return errors.New("名字重复,请更改!")
  44. }
  45. err := models.EditApisql(a.ID, data)
  46. if err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func (a *Apisql) GetByName() (*models.Apisql, error) {
  52. apisql, err := models.GetApisqlByName(a.Sqlname)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return apisql, nil
  57. }
  58. func (a *Apisql) GetAll() ([]*models.Apisql, error) {
  59. if a.Sqlname != "" {
  60. maps := make(map[string]interface{})
  61. maps["deleted_on"] = 0
  62. maps["sqlname"] = a.Sqlname
  63. apisql, err := models.GetApisqls(a.PageNum, a.PageSize, maps)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return apisql, nil
  68. } else {
  69. apisql, err := models.GetApisqls(a.PageNum, a.PageSize, a.getMaps())
  70. if err != nil {
  71. return nil, err
  72. }
  73. return apisql, nil
  74. }
  75. }
  76. func (a *Apisql) Delete() error {
  77. err := models.DeleteApisqlByName(a.Sqlname)
  78. if err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. func (a *Apisql) ExistByID() (bool, error) {
  84. return models.ExistApisqlByID(a.ID)
  85. }
  86. func (a *Apisql) Count() (int, error) {
  87. return models.GetApisqlTotal(a.getMaps())
  88. }
  89. func (a *Apisql) getMaps() map[string]interface{} {
  90. maps := make(map[string]interface{})
  91. maps["deleted_on"] = 0
  92. return maps
  93. }