package model

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

	"github.com/xormplus/xorm"

	"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 (
	i2billMktPartTimeUserFieldNames          = builderx.RawFieldNames(&I2billMktPartTimeUser{})
	i2billMktPartTimeUserRows                = strings.Join(i2billMktPartTimeUserFieldNames, ",")
	i2billMktPartTimeUserRowsExpectAutoSet   = strings.Join(stringx.Remove(i2billMktPartTimeUserFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
	i2billMktPartTimeUserRowsWithPlaceHolder = strings.Join(stringx.Remove(i2billMktPartTimeUserFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
)

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

	defaultI2billMktPartTimeUserModel struct {
		conn  sqlx.SqlConn
		table string
	}

	I2billMktPartTimeUser struct {
		CheckDesc      string    `db:"check_desc" json:"check_desc"` // 审核失败描述
		UserId         int64     `db:"user_id" json:"user_id"`       // user_id
		DelFlag        int64     `db:"del_flag" json:"del_flag"`
		Name           string    `db:"name" json:"name"`
		Sex            int64     `db:"sex" json:"sex"`
		CheckState     int64     `db:"check_state" json:"check_state"` // 审核状态
		CreateTime     time.Time `db:"create_time" json:"create_time"`
		LastUpdateTime time.Time `db:"last_update_time" json:"last_update_time"`
		LastUpdateBy   string    `db:"last_update_by" json:"last_update_by"`
		Id             int64     `db:"id" json:"id"`
		IponeNumber    string    `db:"ipone_number" json:"ipone_number"`
		Target         int64     `db:"target" json:"target"`   // 目标人数
		MkId           string    `db:"mk_id" json:"mk_id"`     // 所属erp mk id
		CityId         int64     `db:"city_id" json:"city_id"` // 城市
		CreateBy       string    `db:"create_by" json:"create_by"`
	}
	I2billMktPartTimeXorm struct {
		CheckDesc      string    `db:"check_desc" json:"check_desc"` // 审核失败描述
		UserId         int64     `db:"user_id" json:"user_id"`       // user_id
		DelFlag        int64     `db:"del_flag" json:"del_flag"`
		Name           string    `db:"name" json:"name"`
		Sex            int64     `db:"sex" json:"sex"`
		CheckState     int64     `db:"check_state" json:"check_state"` // 审核状态
		CreateTime     time.Time `db:"create_time" json:"create_time"`
		LastUpdateTime time.Time `db:"last_update_time" json:"last_update_time"`
		LastUpdateBy   string    `db:"last_update_by" json:"last_update_by"`
		Id             int64     `db:"id" json:"id"`
		IponeNumber    string    `db:"ipone_number" json:"ipone_number"`
		Target         int64     `db:"target" json:"target"`   // 目标人数
		MkId           string    `db:"mk_id" json:"mk_id"`     // 所属erp mk id
		CityId         int64     `db:"city_id" json:"city_id"` // 城市
		CreateBy       string    `db:"create_by" json:"create_by"`
	}
)

func NewI2billMktPartTimeUserModel(conn sqlx.SqlConn) I2billMktPartTimeUserModel {
	return &defaultI2billMktPartTimeUserModel{
		conn:  conn,
		table: "`i2bill_mkt_part_time_user`",
	}
}

func (t *I2billMktPartTimeXorm) TableName() string {
	return "i2bill_mkt_part_time_user"
}

func (m *defaultI2billMktPartTimeUserModel) Insert(data I2billMktPartTimeUser) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, i2billMktPartTimeUserRowsExpectAutoSet)
	ret, err := m.conn.Exec(query, data.CheckDesc, data.UserId, data.DelFlag, data.Name, data.Sex, data.CheckState, data.LastUpdateTime, data.LastUpdateBy, data.IponeNumber, data.Target, data.MkId, data.CityId, data.CreateBy)
	return ret, err
}

func (m *defaultI2billMktPartTimeUserModel) FindOne(id int64) (*I2billMktPartTimeUser, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", i2billMktPartTimeUserRows, m.table)
	var resp I2billMktPartTimeUser
	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 *defaultI2billMktPartTimeUserModel) Update(data I2billMktPartTimeUser) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, i2billMktPartTimeUserRowsWithPlaceHolder)
	_, err := m.conn.Exec(query, data.CheckDesc, data.UserId, data.DelFlag, data.Name, data.Sex, data.CheckState, data.LastUpdateTime, data.LastUpdateBy, data.IponeNumber, data.Target, data.MkId, data.CityId, data.CreateBy, data.Id)
	return err
}

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

func GetPartTimeXormByUserId(userId int64, engine *xorm.Engine) (*I2billMktPartTimeXorm, error) {
	partUser := new(I2billMktPartTimeXorm)
	_, err := engine.SQL("select * from i2bill_mkt_part_time_user where del_flag = 0 and check_state = 57 and user_id = ?", userId).Get(partUser)
	if err != nil {
		return nil, err
	}
	return partUser, nil
}
