| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- // ==========================================================================
- // 生成日期:{{.table.CreateTime}}
- // 生成人:{{.table.FunctionAuthor}}
- // ==========================================================================
- package {{.table.BusinessName}}
- import (
- "github.com/gogf/gf/errors/gerror"
- "github.com/gogf/gf/frame/g"
- )
- {{$pk:=""}}
- {{$pkGoField:=""}}
- {{range $index, $column := .table.Columns}} {{if eq $column.IsPk "1"}}
- {{$pk = $column.ColumnName}}
- {{$pkGoField = $column.GoField}}
- {{end}}{{end}}
- // AddReq 用于存储新增请求的请求参数
- type AddReq struct {
- {{range $index, $column := .table.Columns}}
- {{if and (eq $column.IsInsert "1") (ne $column.IsPk "1")}} {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}` {{end}} {{end}}
- }
- // EditReq 用于存储修改请求参数
- type EditReq struct {
- {{.table.PkColumn.GoField}} {{.table.PkColumn.GoType}} `p:"{{.table.PkColumn.HtmlField}}" v:"required#主键ID不能为空"` {{range $index, $column := .table.Columns}} {{if eq $column.IsEdit "1"}}
- {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}` {{end}} {{end}}
- }
- type RemoveReq struct {
- Ids []int `p:"ids"` //删除id
- }
- // SelectPageReq 用于存储分页查询的请求参数
- type SelectPageReq struct { {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
- {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}"` //{{$column.ColumnComment}} {{end}} {{end}}
- BeginTime string `p:"beginTime"` //开始时间
- EndTime string `p:"endTime"` //结束时间
- PageNum int `p:"pageNum"` //当前页码
- PageSize int `p:"pageSize"` //每页数
- }
- // GetPlugAdByID 根据ID查询记录
- func GetByID(id int64) (*Entity, error) {
- entity, err := Model.FindOne(id)
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("根据ID查询记录出错")
- }
- if entity == nil {
- err = gerror.New("根据ID未能查询到记录")
- }
- return entity, nil
- }
- // AddSave 添加
- func AddSave(req *AddReq) error {
- entity:= new(Entity)
- {{range $index, $column := .table.Columns}} {{if eq $column.IsInsert "1"}}
- entity.{{$column.GoField}} = req.{{$column.GoField}}{{end}} {{end}}
- result, err := entity.Insert()
- if err != nil {
- return err
- }
- _, err = result.LastInsertId()
- if err != nil {
- return err
- }
- return nil
- }
- // 删除
- func DeleteByIds(Ids []int) error {
- _, err := Model.Delete("{{.table.PkColumn.ColumnName}} in(?)", Ids)
- if err != nil {
- g.Log().Error(err)
- return gerror.New("删除失败")
- }
- return nil
- }
- // 根据ID来修改信息
- func EditSave(req *EditReq) error {
- // 先根据ID来查询要修改的记录
- entity, err := GetByID(req.{{$pkGoField}})
- if err != nil {
- return err
- }
- // 修改实体
- {{range $index, $column := .table.Columns}} {{if eq $column.IsEdit "1"}}
- entity.{{$column.GoField}} = req.{{$column.GoField}}{{end}} {{end}}
- _, err = entity.Update()
- if err != nil {
- g.Log().Error(err)
- return gerror.New("修改失败")
- }
- return nil
- }
- // 分页查询,返回值total总记录数,page当前页
- func SelectListByPage(req *SelectPageReq) (total int, page int64, list []*Entity, err error) {
- model := Model
- if req != nil {
- {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
- {{if eq $column.QueryType "LIKE"}}
- if req.{{$column.GoField}} != "" {
- model.Where("{{$column.ColumnName}} like ?", "%"+req.{{$column.GoField}}+"%")
- } {{else if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}}
- if req.{{$column.GoField}} != "" {
- model.Where("{{$column.ColumnName}} = ?", req.{{$column.GoField}})
- } {{else if eq $column.GoType "int" "int64"}}
- if req.{{$column.GoField}} != 0 {
- model.Where("{{$column.ColumnName}} = ?", req.{{$column.GoField}})
- }
- {{end}} {{end}} {{end}} {{end}}
- }
- // 查询总记录数(总行数)
- total, err = model.Count()
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("获取总记录数失败")
- return
- }
- if req.PageNum == 0 {
- req.PageNum = 1
- }
- page = req.PageNum
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- // 分页排序查询
- list, err = model.Page(int(page), int(req.PageSize)).Order("{{$pk}} asc").All()
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("分页查询失败")
- return
- }
- return
- }
- // 获取所有数据
- func SelectListAll(req *SelectPageReq) (list []*Entity, err error) {
- model := Model
- if req != nil {
- {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
- {{if eq $column.QueryType "LIKE"}}
- if req.{{$column.GoField}} != "" {
- model.Where("{{$column.ColumnName}} like ?", "%"+req.{{$column.GoField}}+"%")
- } {{else if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}}
- if req.{{$column.GoField}} != "" {
- model.Where("{{$column.ColumnName}} = ?", req.{{$column.GoField}})
- } {{else if eq $column.GoType "int" "int64"}}
- if req.{{$column.GoField}} != 0 {
- model.Where("{{$column.ColumnName}} = ?", req.{{$column.GoField}})
- }
- {{end}} {{end}} {{end}} {{end}}
- }
- // 查询
- list, err = model.Order("{{$pk}} asc").All()
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("查询失败")
- return
- }
- return
- }
|