package model

import (
	"database/sql"
	"fmt"
	"github.com/xormplus/xorm"
	"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 (
	i2billAcquirerMktQrFieldNames          = builderx.RawFieldNames(&I2billAcquirerMktQr{})
	i2billAcquirerMktQrRows                = strings.Join(i2billAcquirerMktQrFieldNames, ",")
	i2billAcquirerMktQrRowsExpectAutoSet   = strings.Join(stringx.Remove(i2billAcquirerMktQrFieldNames, "`id`", "`create_time`", "`update_time`"), ",")
	i2billAcquirerMktQrRowsWithPlaceHolder = strings.Join(stringx.Remove(i2billAcquirerMktQrFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?"
)

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

	defaultI2billAcquirerMktQrModel struct {
		conn  sqlx.SqlConn
		table string
	}

	I2billAcquirerMktQr struct {
		SchoolId        sql.NullInt64  `db:"school_id" json:"school_id"` // 校区id
		Qr              sql.NullString `db:"qr" json:"qr"`               // 二维码url
		LastUpdateBy    sql.NullInt64  `db:"last_update_by" json:"last_update_by"`
		Id              int64          `db:"id" json:"id"`                   // 主键
		UserId          sql.NullInt64  `db:"user_id" json:"user_id"`         // 用户id
		ActivityId      sql.NullInt64  `db:"activity_id" json:"activity_id"` // 活动id
		CreateBy        sql.NullInt64  `db:"create_by" json:"create_by"`
		LastUpdateTime  sql.NullTime   `db:"last_update_time" json:"last_update_time"`
		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"` //渠道四级
		DelFlag         sql.NullInt64  `db:"del_flag" json:"del_flag"`
		CreateTime      sql.NullTime   `db:"create_time" json:"create_time"`
		Role            sql.NullInt64  `db:"role" json:"role"` //1-兼职2-mk
	}
	I2billAcquirerMktQrXorm struct {
		SchoolId        int64     `json:"school_id"`
		Qr              string    `json:"qr"`
		LastUpdateBy    int64     `json:"last_update_by"`
		Id              int64     `json:"id"`
		UserId          int64     `json:"user_id"`
		ActivityId      int64     `json:"activity_id"`
		CreateBy        int64     `json:"create_by"`
		LastUpdateTime  time.Time `json:"last_update_time"`
		QuaoYji         int64     `json:"quao_yji"`          // 渠道一级
		CallType        int64     `json:"call_type"`         // 渠道二级
		MaType          int64     `json:"ma_type"`           //渠道三级
		NetworkDetailId int64     `json:"network_detail_id"` //渠道四级
		DelFlag         int64     `json:"del_flag"`
		CreateTime      time.Time `json:"create_time"`
		Role            int64     `json:"role"` //1-兼职2-mk
	}
)

func NewI2billAcquirerMktQrModel(conn sqlx.SqlConn) I2billAcquirerMktQrModel {
	return &defaultI2billAcquirerMktQrModel{
		conn:  conn,
		table: "`i2bill_acquirer_mkt_qr`",
	}
}

func (m *defaultI2billAcquirerMktQrModel) Insert(data I2billAcquirerMktQr) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, i2billAcquirerMktQrRowsExpectAutoSet)
	ret, err := m.conn.Exec(query, data.SchoolId, data.Qr, data.LastUpdateBy, data.UserId, data.ActivityId, data.CreateBy, data.LastUpdateTime, data.QuaoYji, data.CallType, data.MaType, data.NetworkDetailId, data.DelFlag)
	return ret, err
}

func (m *defaultI2billAcquirerMktQrModel) FindOne(id int64) (*I2billAcquirerMktQr, error) {
	query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", i2billAcquirerMktQrRows, m.table)
	var resp I2billAcquirerMktQr
	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 *defaultI2billAcquirerMktQrModel) Update(data I2billAcquirerMktQr) error {
	query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, i2billAcquirerMktQrRowsWithPlaceHolder)
	_, err := m.conn.Exec(query, data.SchoolId, data.Qr, data.LastUpdateBy, data.UserId, data.ActivityId, data.CreateBy, data.LastUpdateTime, data.QuaoYji, data.CallType, data.MaType, data.NetworkDetailId, data.DelFlag, data.Id)
	return err
}

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

func (t *I2billAcquirerMktQrXorm) TableName() string {
	return "i2bill_acquirer_mkt_qr"
}

func GetAcquirerMktQr(userId int64, engine *xorm.Engine) (*I2billAcquirerMktQrXorm, error) {
	bean := new(I2billAcquirerMktQrXorm)
	_, err := engine.Where("user_id = ? and del_flag = 0", userId).Get(bean)
	return bean, err
}
