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 (
	i2billAcquirerStudentFieldNames          = builderx.RawFieldNames(&I2billAcquirerStudent{})
	i2billAcquirerStudentRows                = strings.Join(i2billAcquirerStudentFieldNames, ",")
	I2billAcquirerStudentRows                = strings.Join(i2billAcquirerStudentFieldNames, ",")
	i2billAcquirerStudentRowsExpectAutoSet   = strings.Join(stringx.Remove(i2billAcquirerStudentFieldNames, "`create_time`", "`update_time`"), ",")
	i2billAcquirerStudentRowsWithPlaceHolder = strings.Join(stringx.Remove(i2billAcquirerStudentFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
)

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

	defaultI2billAcquirerStudentModel struct {
		conn  sqlx.SqlConn
		table string
	}

	I2billAcquirerStudent struct {
		StuLinkPerson   sql.NullInt64  `db:"stu_link_person" json:"stu_link_person"` // 联系人关系
		StuPhone        sql.NullString `db:"stu_phone" json:"stu_phone"`             // 联系方式
		CreateTime      sql.NullTime   `db:"create_time" json:"create_time"`
		CheckTime       sql.NullTime   `db:"check_time" json:"check_time"`   // 审核日期
		CheckBy         sql.NullString `db:"check_by" json:"check_by"`       // 审核人
		MkId            sql.NullString `db:"mk_id" json:"mk_id"`             // 所属mk erp id
		UserId          sql.NullInt64  `db:"user_id" json:"user_id"`         // 收单宝用户id
		Address         sql.NullString `db:"address" json:"address"`         // 当前位置
		CheckState      sql.NullInt64  `db:"check_state" json:"check_state"` // 收单宝状态
		Id              int64          `db:"id" json:"id"`                   // 主键
		PartTimeUserId  sql.NullInt64  `db:"part_time_user_id" json:"part_time_user_id"`
		StuName         sql.NullString `db:"stu_name" json:"stu_name"`   // 学员名称
		AgeGroup        int64          `db:"age_group" json:"age_group"` // 学员年龄
		SchId           sql.NullInt64  `db:"sch_id" json:"sch_id"`       // 意向校区
		DelFlag         int64          `db:"del_flag" json:"del_flag"`
		QuaoYji         sql.NullInt64  `db:"quao_yji" json:"quao_yji"`                   // 渠道一级
		CallType        sql.NullInt64  `db:"call_type" json:"call_type"`                 // 渠道二级
		MaType          sql.NullInt64  `db:"ma_type"  json:"ma_type"`                    //渠道三级
		NetworkDetailId sql.NullInt64  `db:"network_detail_id" json:"network_detail_id"` //渠道四级
		LoadSchId       sql.NullInt64  `db:"load_sch_id" json:"load_sch_id"`             //资源收集校区
		ActiveId        sql.NullInt64  `db:"active_id" json:"active_id"`                 //活动
		Remark          sql.NullString `db:"remark" json:"remark"`                       //备注
	}

	I2billAcquirerStudentXorm struct {
		StuLinkPerson   int       `db:"stu_link_person" json:"stu_link_person"` // 联系人关系
		StuPhone        string    `db:"stu_phone" json:"stu_phone"`             // 联系方式
		CreateTime      time.Time `db:"create_time" json:"create_time"`
		CheckTime       time.Time `db:"check_time" json:"check_time"`   // 审核日期
		CheckBy         string    `db:"check_by" json:"check_by"`       // 审核人
		MkId            string    `db:"mk_id" json:"mk_id"`             // 所属mk erp id
		UserId          int64     `db:"user_id" json:"user_id"`         // 收单宝用户id
		Address         string    `db:"address" json:"address"`         // 当前位置
		CheckState      int64     `db:"check_state" json:"check_state"` // 收单宝状态
		Id              int64     `db:"id" json:"id"`                   // 主键
		PartTimeUserId  int64     `db:"part_time_user_id" json:"part_time_user_id"`
		StuName         string    `db:"stu_name" json:"stu_name"`   // 学员名称
		AgeGroup        int64     `db:"age_group" json:"age_group"` // 学员年龄
		SchId           int64     `db:"sch_id" json:"sch_id"`       // 意向校区
		DelFlag         int64     `db:"del_flag" json:"del_flag"`
		QuaoYji         int64     `db:"quao_yji" json:"quao_yji"`                   // 渠道一级
		CallType        int64     `db:"call_type" json:"call_type"`                 // 渠道二级
		MaType          int64     `db:"ma_type"  json:"ma_type"`                    //渠道三级
		NetworkDetailId int64     `db:"network_detail_id" json:"network_detail_id"` //渠道四级
		LoadSchId       int64     `db:"load_sch_id" json:"load_sch_id"`             //收集收集校区
		ActiveId        int64     `db:"active_id" json:"active_id"`                 //活动
		Remark          string    `db:"remark" json:"remark"`                       //备注
	}
)

func (t *I2billAcquirerStudentXorm) TableName() string {
	return "i2bill_acquirer_student"
}

func NewI2billAcquirerStudentModel(conn sqlx.SqlConn) I2billAcquirerStudentModel {
	return &defaultI2billAcquirerStudentModel{
		conn:  conn,
		table: "`i2bill_acquirer_student`",
	}
}

func (m *defaultI2billAcquirerStudentModel) Insert(data I2billAcquirerStudent) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?,?,?)", m.table, i2billAcquirerStudentRowsExpectAutoSet)
	ret, err := m.conn.Exec(query, data.StuLinkPerson, data.StuPhone, data.CheckTime, data.CheckBy, data.MkId, data.UserId, data.Address, data.CheckState, data.Id, data.PartTimeUserId, data.StuName, data.AgeGroup, data.SchId, data.DelFlag, data.QuaoYji, data.CallType, data.MaType, data.NetworkDetailId, data.LoadSchId, data.ActiveId)
	return ret, err
}

func (m *defaultI2billAcquirerStudentModel) FindOne(id int64) (*I2billAcquirerStudent, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", i2billAcquirerStudentRows, m.table)
	var resp I2billAcquirerStudent
	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 *defaultI2billAcquirerStudentModel) Update(data I2billAcquirerStudent) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, i2billAcquirerStudentRowsWithPlaceHolder)
	_, err := m.conn.Exec(query, data.StuLinkPerson, data.StuPhone, data.CheckTime, data.CheckBy, data.MkId, data.UserId, data.Address, data.CheckState, data.PartTimeUserId, data.StuName, data.AgeGroup, data.SchId, data.DelFlag, data.QuaoYji, data.CallType, data.MaType, data.NetworkDetailId, data.LoadSchId, data.ActiveId, data.Id)
	return err
}

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