// ==========================================================================
// 生成日期：{{.CreatedAt}}
// 生成路径: apps/{{.PackageName}}/services/{{.TableName}}.go
// 生成人：{{.FunctionAuthor}}
// ==========================================================================

package services

import (
    "pandax/apps/{{.PackageName}}/entity"
    "pandax/pkg/global"
)

type (
	{{.ClassName}}Model interface {
		Insert(data entity.{{.ClassName}}) (*entity.{{.ClassName}}, error)
		FindOne({{.PkJsonField}} {{.PkGoType}}) (*entity.{{.ClassName}},error)
		FindListPage(page, pageSize int, data entity.{{.ClassName}}) (*[]entity.{{.ClassName}}, int64, error)
		FindList(data entity.{{ .ClassName }}) (*[]entity.{{.ClassName}},error)
		Update(data entity.{{.ClassName}}) error
		Delete({{.PkJsonField}}s []{{.PkGoType}}) error
	}

	{{.BusinessName}}ModelImpl struct {
		table string
	}
)
{{$model := .ClassName }}

var {{.ClassName}}ModelDao {{.ClassName}}Model = &{{.BusinessName}}ModelImpl{
	table: `{{.TableName}}`,
}

func (m *{{.BusinessName}}ModelImpl) Insert(data entity.{{$model}}) (*entity.{{$model}}, error) {
	err := global.Db.Table(m.table).Create(&data).Error
	return &data, err
}

func (m *{{.BusinessName}}ModelImpl) FindOne({{.PkJsonField}} {{.PkGoType}}) (*entity.{{$model}}, error) {
	resData := new(entity.{{$model}})
	db := global.Db.Table(m.table).Where("{{.PkColumn}} = ?", {{.PkJsonField}})
	{{- range $index, $column := .Columns -}}
	{{- if ne $column.LinkTableName "" }}
    db.Preload("{{$column.LinkTableClass}}")
    {{- end -}}
    {{- end}}
	err := db.First(resData).Error
	return resData, err
}

func (m *{{.BusinessName}}ModelImpl) FindListPage(page, pageSize int, data entity.{{$model}}) (*[]entity.{{$model}}, int64, error) {
	list := make([]entity.{{$model}}, 0)
	var total int64 = 0
	offset := pageSize * (page - 1)
	db := global.Db.Table(m.table)
	// 此处填写 where参数判断
	{{- range $index, $column := .Columns -}}
	{{- if eq $column.IsQuery "1" -}}
    {{- if eq $column.QueryType "LIKE" }}
    if data.{{$column.GoField}} != "" {
        db = db.Where("{{$column.ColumnName}} like ?", "%"+data.{{$column.GoField}}+"%")
    }
    {{- end -}}
    {{- if or (eq $column.QueryType "EQ") (eq $column.QueryType "NE") -}}
    {{- if eq $column.GoType "string" }}
    if data.{{$column.GoField}} != "" {
        {{- if eq $column.QueryType "EQ" }}
        db = db.Where("{{$column.ColumnName}} = ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "NE"}}
        db = db.Where("{{$column.ColumnName}} != ?", data.{{$column.GoField}})
        {{- end }}
    }
    {{- else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }}
    if data.{{$column.GoField}} != 0 {
        {{- if eq $column.QueryType "EQ" }}
        db = db.Where("{{$column.ColumnName}} = ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "NE"}}
        db = db.Where("{{$column.ColumnName}} != ?", data.{{$column.GoField}})
        {{- end }}
    }
    {{- end -}}
    {{- end -}}
    {{- if or (eq $column.QueryType "GT") (eq $column.QueryType "GTE") (eq $column.QueryType "LT") (eq $column.QueryType "LTE")}}
    {{- if eq $column.GoType "Time" }}
    if data.{{$column.GoField}}.Unix() > 0 {
        {{- if eq $column.QueryType "GT" }}
        db = db.Where("{{$column.ColumnName}} > ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "GTE"}}
        db = db.Where("{{$column.ColumnName}} >= ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "LT"}}
        db = db.Where("{{$column.ColumnName}} < ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "LTE"}}
        db = db.Where("{{$column.ColumnName}} <= ?", data.{{$column.GoField}})
        {{- end }}
    }
    {{- else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }}
    if data.{{$column.GoField}} != 0 {
        {{- if eq $column.QueryType "GT" }}
        db = db.Where("{{$column.ColumnName}} > ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "GTE"}}
        db = db.Where("{{$column.ColumnName}} >= ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "LT"}}
        db = db.Where("{{$column.ColumnName}} < ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "LTE"}}
        db = db.Where("{{$column.ColumnName}} <= ?", data.{{$column.GoField}})
        {{- end }}
    }
    {{- end -}}
    {{- end -}}
    {{- end -}}
    {{- if eq $column.ColumnName "delete_time" }}
    db.Where("delete_time IS NULL")
    {{- end -}}
    {{- if ne $column.LinkTableName "" }}
    db.Preload("{{$column.LinkTableClass}}")
    {{- end -}}
    {{- end}}
	err := db.Count(&total).Error
	if err != nil {
        return &list, total, err
    }
	err = db.Limit(pageSize).Offset(offset).Find(&list).Error
	return &list, total, err
}

func (m *{{.BusinessName}}ModelImpl) FindList(data entity.{{$model}}) (*[]entity.{{$model}}, error) {
	list := make([]entity.{{$model}}, 0)
	db := global.Db.Table(m.table)
	// 此处填写 where参数判断
    {{- range $index, $column := .Columns -}}
	{{- if eq $column.IsQuery "1" -}}
    {{- if eq $column.QueryType "LIKE" }}
    if data.{{$column.GoField}} != "" {
        db = db.Where("{{$column.ColumnName}} like ?", "%"+data.{{$column.GoField}}+"%")
    }
    {{- end -}}
    {{- if or (eq $column.QueryType "EQ") (eq $column.QueryType "NE") -}}
    {{- if eq $column.GoType "string" }}
    if data.{{$column.GoField}} != "" {
        {{- if eq $column.QueryType "EQ" }}
        db = db.Where("{{$column.ColumnName}} = ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "NE"}}
        db = db.Where("{{$column.ColumnName}} != ?", data.{{$column.GoField}})
        {{- end }}
    }
    {{- else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }}
    if data.{{$column.GoField}} != 0 {
        {{- if eq $column.QueryType "EQ" }}
        db = db.Where("{{$column.ColumnName}} = ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "NE"}}
        db = db.Where("{{$column.ColumnName}} != ?", data.{{$column.GoField}})
        {{- end }}
    }
    {{- end -}}
    {{- end -}}
    {{- if or (eq $column.QueryType "GT") (eq $column.QueryType "GTE") (eq $column.QueryType "LT") (eq $column.QueryType "LTE")}}
    {{- if eq $column.GoType "Time" }}
    if data.{{$column.GoField}}.Unix() > 0 {
        {{- if eq $column.QueryType "GT" }}
        db = db.Where("{{$column.ColumnName}} > ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "GTE"}}
        db = db.Where("{{$column.ColumnName}} >= ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "LT"}}
        db = db.Where("{{$column.ColumnName}} < ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "LTE"}}
        db = db.Where("{{$column.ColumnName}} <= ?", data.{{$column.GoField}})
        {{- end }}
    }
    {{- else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }}
    if data.{{$column.GoField}} != 0 {
        {{- if eq $column.QueryType "GT" }}
        db = db.Where("{{$column.ColumnName}} > ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "GTE"}}
        db = db.Where("{{$column.ColumnName}} >= ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "LT"}}
        db = db.Where("{{$column.ColumnName}} < ?", data.{{$column.GoField}})
        {{- else if $column.QueryType "LTE"}}
        db = db.Where("{{$column.ColumnName}} <= ?", data.{{$column.GoField}})
        {{- end }}
    }
    {{- end -}}
    {{- end -}}
    {{- end -}}
    {{- if eq $column.ColumnName "delete_time" }}
    db.Where("delete_time IS NULL")
    {{- end -}}
    {{- if ne $column.LinkTableName "" }}
    db.Preload("{{$column.LinkTableClass}}")
    {{- end -}}
    {{- end}}
	err := db.Find(&list).Error
	return &list, err
}

func (m *{{.BusinessName}}ModelImpl) Update(data entity.{{$model}}) error {
	return global.Db.Table(m.table).Updates(&data).Error
}

func (m *{{.BusinessName}}ModelImpl) Delete({{.PkJsonField}}s []{{.PkGoType}}) error {
	return global.Db.Table(m.table).Delete(&entity.{{$model}}{}, "{{.PkColumn}} in (?)", {{.PkJsonField}}s).Error
}