package eta_business

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

// 操作类型
const (
	EtaBusinessOperationTypeAdd = iota + 1
	EtaBusinessOperationTypeEdit
	EtaBusinessOperationTypeDisable
	EtaBusinessOperationTypeEnable
	EtaBusinessOperationTypeMoveSeller
	EtaBusinessOperationTypeRenewalContract
	EtaBusinessOperationTypeEditContract
	EtaBusinessOperationTypeRemoveContract
)

// EtaBusinessOperationRecord ETA商家操作日志表
type EtaBusinessOperationRecord struct {
	Id              int       `orm:"column(id);pk"`
	EtaBusinessId   int       `description:"ETA商家ID"`
	SellerId        int       `description:"操作时所对应的销售ID"`
	SysUserId       int       `description:"操作人ID"`
	SysRealName     string    `description:"操作人姓名"`
	OperationType   int       `description:"操作类型：1-新增；2-编辑；3-禁用；4-启用；5-移动销售；6-添加续约；7-编辑签约；8-删除签约"`
	OperationRemark string    `description:"操作内容"`
	CreateTime      time.Time `description:"创建时间"`
}

func (m *EtaBusinessOperationRecord) TableName() string {
	return "eta_business_operation_record"
}

func (m *EtaBusinessOperationRecord) PrimaryId() string {
	return EtaBusinessOperationRecordColumns.Id
}

var EtaBusinessOperationRecordColumns = struct {
	Id              string
	EtaBusinessId   string
	SellerId        string
	SysUserId       string
	SysRealName     string
	OperationType   string
	OperationRemark string
	CreateTime      string
}{
	Id:              "id",
	EtaBusinessId:   "eta_business_id",
	SellerId:        "seller_id",
	SysUserId:       "sys_user_id",
	SysRealName:     "sys_real_name",
	OperationType:   "operation_type",
	OperationRemark: "operation_remark",
	CreateTime:      "create_time",
}

func (m *EtaBusinessOperationRecord) Create() (err error) {
	o := orm.NewOrm()
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.Id = int(id)
	return
}

func (m *EtaBusinessOperationRecord) CreateMulti(items []*EtaBusinessOperationRecord) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(items), items)
	return
}

func (m *EtaBusinessOperationRecord) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

func (m *EtaBusinessOperationRecord) Del() (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.Id).Exec()
	return
}

func (m *EtaBusinessOperationRecord) GetItemById(id int) (item *EtaBusinessOperationRecord, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

func (m *EtaBusinessOperationRecord) GetItemByCondition(condition string, pars []interface{}) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&m)
	return
}

func (m *EtaBusinessOperationRecord) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *EtaBusinessOperationRecord) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaBusinessOperationRecord, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// EtaBusinessOperationRecordItem ETA商家操作日志信息
type EtaBusinessOperationRecordItem struct {
	EtaBusinessId   int    `description:"ETA商家ID"`
	SysUserId       int    `description:"操作人ID"`
	SysRealName     string `description:"操作人姓名"`
	OperationType   int    `description:"操作类型：1-新增；2-编辑；3-禁用；4-启用；5-移动销售；6-添加续约"`
	OperationRemark string `description:"操作内容"`
	CreateTime      string `description:"创建时间"`
}
