package model

import (
	"database/sql"
	"fmt"
	"strings"
	"time"

	"git.i2edu.net/i2/go-zero/core/stores/sqlc"
	"git.i2edu.net/i2/go-zero/core/stores/sqlx"
	"git.i2edu.net/i2/go-zero/core/stringx"
	"git.i2edu.net/i2/go-zero/tools/goctl/model/sql/builderx"
)

var (
	i2billSysAttachmentFieldNames          = builderx.RawFieldNames(&I2billSysAttachment{})
	i2billSysAttachmentRows                = strings.Join(i2billSysAttachmentFieldNames, ",")
	i2billSysAttachmentRowsExpectAutoSet   = strings.Join(stringx.Remove(i2billSysAttachmentFieldNames, "`create_time`", "`update_time`"), ",")
	i2billSysAttachmentRowsWithPlaceHolder = strings.Join(stringx.Remove(i2billSysAttachmentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
)

type (
	I2billSysAttachmentModel interface {
		Insert(data I2billSysAttachment) (sql.Result, error)
		FindOne(id string) (*I2billSysAttachment, error)
		Update(data I2billSysAttachment) error
		Delete(id string) error
	}

	defaultI2billSysAttachmentModel struct {
		conn  sqlx.SqlConn
		table string
	}

	I2billSysAttachment struct {
		CreateTime time.Time `db:"create_time" json:"create_time"`
		DelFlag    int64     `db:"del_flag" json:"del_flag"`
		Name       string    `db:"name" json:"name"`
		Size       int64     `db:"size" json:"size"`
		Ext        string    `db:"ext" json:"ext"`
		Hash       string    `db:"hash" json:"hash"`
		Url        string    `db:"url" json:"url"`
		Id         string    `db:"id" json:"id"`
		CreateBy   string    `db:"create_by" json:"create_by"`
	}
)

func NewI2billSysAttachmentModel(conn sqlx.SqlConn) I2billSysAttachmentModel {
	return &defaultI2billSysAttachmentModel{
		conn:  conn,
		table: "`i2bill_sys_attachment`",
	}
}

func (t *I2billSysAttachment) TableName() string {
	return "i2bill_sys_attachment"
}

func (m *defaultI2billSysAttachmentModel) Insert(data I2billSysAttachment) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, i2billSysAttachmentRowsExpectAutoSet)
	ret, err := m.conn.Exec(query, data.DelFlag, data.Name, data.Size, data.Ext, data.Hash, data.Url, data.Id, data.CreateBy)
	return ret, err
}

func (m *defaultI2billSysAttachmentModel) FindOne(id string) (*I2billSysAttachment, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", i2billSysAttachmentRows, m.table)
	var resp I2billSysAttachment
	err := m.conn.QueryRow(&resp, query, id)
	switch err {
	case nil:
		return &resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultI2billSysAttachmentModel) Update(data I2billSysAttachment) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, i2billSysAttachmentRowsWithPlaceHolder)
	_, err := m.conn.Exec(query, data.DelFlag, data.Name, data.Size, data.Ext, data.Hash, data.Url, data.CreateBy, data.Id)
	return err
}

func (m *defaultI2billSysAttachmentModel) Delete(id string) error {
	query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
	_, err := m.conn.Exec(query, id)
	return err
}
