package apisql_service import ( "errors" "../../models" ) type Apisql struct { ID int Sqlstr string Sqlname string Returetype string Method string CreatedBy string ModifiedBy string PageNum int PageSize int } func (a *Apisql) Add() (id int, err error) { data := map[string]interface{}{ "Sqlstr": a.Sqlstr, "Sqlname": a.Sqlname, "Returetype": a.Returetype, "Method": a.Method, } Sqlname, _ := models.CheckSqlname(a.Sqlname) if Sqlname { return 0, errors.New("名字重复,请更改!") } if id, err := models.AddApisql(data); err == nil { return id, err } else { return 0, err } } func (a *Apisql) Edit() error { data := map[string]interface{}{ "Sqlstr": a.Sqlstr, "Sqlname": a.Sqlname, "Returetype": a.Returetype, "Method": a.Method, } Sqlname, _ := models.CheckSqlname(a.Sqlname) if Sqlname { return errors.New("名字重复,请更改!") } err := models.EditApisql(a.ID, data) if err != nil { return err } return nil } func (a *Apisql) GetByName() (*models.Apisql, error) { apisql, err := models.GetApisqlByName(a.Sqlname) if err != nil { return nil, err } return apisql, nil } func (a *Apisql) GetAll() ([]*models.Apisql, error) { if a.Sqlname != "" { maps := make(map[string]interface{}) maps["deleted_on"] = 0 maps["sqlname"] = a.Sqlname apisql, err := models.GetApisqls(a.PageNum, a.PageSize, maps) if err != nil { return nil, err } return apisql, nil } else { apisql, err := models.GetApisqls(a.PageNum, a.PageSize, a.getMaps()) if err != nil { return nil, err } return apisql, nil } } func (a *Apisql) Delete() error { err := models.DeleteApisqlByName(a.Sqlname) if err != nil { return err } return nil } func (a *Apisql) ExistByID() (bool, error) { return models.ExistApisqlByID(a.ID) } func (a *Apisql) Count() (int, error) { return models.GetApisqlTotal(a.getMaps()) } func (a *Apisql) getMaps() map[string]interface{} { maps := make(map[string]interface{}) maps["deleted_on"] = 0 return maps }