package shanghu

import (
	orm "duoduo/database"
	"time"
)

type MerchantPayInfo struct {
	ID              int       `gorm:"column:id;type:int(11);primary_key;AUTO_INCREMENT" json:"id"`       // 主键
	MerchantOpenID  string    `gorm:"column:merchant_open_id;type:varchar(255)" json:"merchant_open_id"` // 商户openid
	BusinessLicense string    `gorm:"column:business_license;type:varchar(255)" json:"business_license"` // 营业执照照片
	IDCardFront     string    `gorm:"column:id_card_front;type:varchar(255)" json:"id_card_front"`       // 身份证正面
	IDCardBack      string    `gorm:"column:id_card_back;type:varchar(255)" json:"id_card_back"`         // 身份证反面
	IDCardHand      string    `gorm:"column:id_card_hand;type:varchar(255)" json:"id_card_hand"`         // 手持身份证
	CreateBy        int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                 // 创建者
	UpdateBy        int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                 // 更新者
	CreatedAt       time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"`              // 创建时间
	UpdatedAt       time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`              // 最后更新时间
	DeletedAt       time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
	BankAccount     string    `gorm:"column:bank_account;type:varchar(50)" json:"bank_account"`          // 开户行
	BankCode        string    `gorm:"column:bank_code;type:varchar(50)" json:"bank_code"`                // 银行卡号
	BankAddress     string    `gorm:"column:bank_address;type:varchar(50)" json:"bank_address"`          // 网点
	AccountName     string    `gorm:"column:account_name;type:varchar(50)" json:"account_name"`          // 姓名
	MerchantType    int       `gorm:"column:merchant_type;type:int(11)" json:"merchant_type"`
	EntryInfo       string    `gorm:"column:entry_info;type:varchar(255)" json:"entry_info"` // 入账信息

}

func (m *MerchantPayInfo) TableName() string {
	return "merchant_pay_info"
}

func (u *MerchantPayInfo) Create() (MerchantPayInfo, error) {
	var doc MerchantPayInfo
	var err error

	doc = *u
	err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
	if err != nil {
		return doc, err
	}

	return doc, nil
}

func (m *MerchantPayInfo) GetMerchantPayInfo() (MerchantPayInfo, error) {
	var doc MerchantPayInfo

	table := orm.ShMysql.Table(m.TableName())
	table = table.Where("merchant_open_id = ?  ", m.MerchantOpenID)

	if err := table.Select("*").First(&doc).Error; err != nil {
		return doc, err
	}

	return doc, nil
}

func (m *MerchantPayInfo) UpdateMerchantPayInfo() error {

	if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
		map[string]interface{}{
			"business_license": m.BusinessLicense,
			"id_card_front":    m.IDCardFront,
			"id_card_back":     m.IDCardBack,
			"id_card_hand":     m.IDCardHand,
			"bank_account":     m.BankAccount,
			"bank_code":        m.BankCode,
			"bank_address":     m.BankAddress,
			"account_name":     m.AccountName,
			"merchant_type":    m.MerchantType,
			"entry_info":       m.EntryInfo,
			"updated_at":       time.Now()}).Error; err != nil {
		return err
	}

	return nil
}