package model

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

	"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 (
	i2billAcquirerAttendanceRecordFieldNames          = builderx.RawFieldNames(&I2billAcquirerAttendanceRecord{})
	i2billAcquirerAttendanceRecordRows                = strings.Join(i2billAcquirerAttendanceRecordFieldNames, ",")
	i2billAcquirerAttendanceRecordRowsExpectAutoSet   = strings.Join(stringx.Remove(i2billAcquirerAttendanceRecordFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
	i2billAcquirerAttendanceRecordRowsWithPlaceHolder = strings.Join(stringx.Remove(i2billAcquirerAttendanceRecordFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
)

type (
	I2billAcquirerAttendanceRecordModel interface {
		Insert(data I2billAcquirerAttendanceRecord) (sql.Result, error)
		FindOne(id int64) (*I2billAcquirerAttendanceRecord, error)
		Update(data I2billAcquirerAttendanceRecord) error
		Delete(id int64) error
	}

	defaultI2billAcquirerAttendanceRecordModel struct {
		conn  sqlx.SqlConn
		table string
	}

	I2billAcquirerAttendanceRecord struct {
		Date         sql.NullTime   `db:"date" json:"date"`
		UserId       sql.NullInt64  `db:"user_id" json:"user_id"` // 用户id
		StartLat     sql.NullString `db:"start_lat" json:"start_lat"`
		StartAddress sql.NullString `db:"start_address" json:"start_address"` // 上班地址
		EndAddress   sql.NullString `db:"end_address" json:"end_address"`     // 下班地址
		StartTime    sql.NullTime   `db:"start_time" json:"start_time"`       // 上班时间
		EndTime      sql.NullTime   `db:"end_time" json:"end_time"`           // 下班时间
		DelFlag      sql.NullInt64  `db:"del_flag" json:"del_flag"`
		Id           int64          `db:"id" json:"id"`
		EndLat       sql.NullString `db:"end_lat" json:"end_lat"`
		CreateTime   sql.NullTime   `db:"create_time" json:"create_time"`
	}
)

func (t *I2billAcquirerAttendanceRecord) TableName() string {
	return "i2bill_acquirer_attendance_record"
}

func NewI2billAcquirerAttendanceRecordModel(conn sqlx.SqlConn) I2billAcquirerAttendanceRecordModel {
	return &defaultI2billAcquirerAttendanceRecordModel{
		conn:  conn,
		table: "`i2bill_acquirer_attendance_record`",
	}
}

func (m *defaultI2billAcquirerAttendanceRecordModel) Insert(data I2billAcquirerAttendanceRecord) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, i2billAcquirerAttendanceRecordRowsExpectAutoSet)
	ret, err := m.conn.Exec(query, data.Date, data.UserId, data.StartLat, data.StartAddress, data.EndAddress, data.StartTime, data.EndTime, data.DelFlag, data.EndLat)
	return ret, err
}

func (m *defaultI2billAcquirerAttendanceRecordModel) FindOne(id int64) (*I2billAcquirerAttendanceRecord, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", i2billAcquirerAttendanceRecordRows, m.table)
	var resp I2billAcquirerAttendanceRecord
	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 *defaultI2billAcquirerAttendanceRecordModel) Update(data I2billAcquirerAttendanceRecord) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, i2billAcquirerAttendanceRecordRowsWithPlaceHolder)
	_, err := m.conn.Exec(query, data.Date, data.UserId, data.StartLat, data.StartAddress, data.EndAddress, data.StartTime, data.EndTime, data.DelFlag, data.EndLat, data.Id)
	return err
}

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