package shanghu

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

type Merchant struct {
	ID                 int64     `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`            // 主键
	OpenId             string    `gorm:"column:open_id;type:varchar(255)" json:"open_id"`                           // open_id
	MerchantName       string    `gorm:"column:merchant_name;type:varchar(255)" json:"merchant_name"`               // 店铺名称
	Industry           string    `gorm:"column:industry;type:varchar(50)" json:"industry"`                          // 行业
	IndustryCategory   string    `gorm:"column:industry_category;type:varchar(50)" json:"industry_category"`        // 行业分类
	Contacts           string    `gorm:"column:contacts;type:varchar(50)" json:"contacts"`                          // 联系人
	BusinessHours      string    `gorm:"column:business_hours;type:varchar(100)" json:"business_hours"`             // 营业时间
	Consumption        string    `gorm:"column:consumption;type:varchar(50)" json:"consumption"`                    // 人均消费
	Iphone             string    `gorm:"column:iphone;type:varchar(20)" json:"iphone"`                              // 手机号
	Area               string    `gorm:"column:area;type:varchar(50)" json:"area"`                                  // 地区
	Address            string    `gorm:"column:address;type:varchar(100)" json:"address"`                           // 地址
	NumberPlate        string    `gorm:"column:number_plate;type:varchar(50)" json:"number_plate"`                  // 门牌号
	BusinessLicenseUrl string    `gorm:"column:business_license_url;type:varchar(255)" json:"business_license_url"` // 营业执照 url
	DoorHeaderUrl      string    `gorm:"column:door_header_url;type:varchar(255)" json:"door_header_url"`           // 门头照 url
	RechargeUrl        string    `gorm:"column:recharge_url;type:varchar(255)" json:"recharge_url"`                 // 充值 url
	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"`         // 删除时间
}

func (m *Merchant) TableName() string {
	return "merchant"
}

func (u *Merchant) Create() (Merchant, error) {
	var doc Merchant
	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 *Merchant) GetMerchant() (Merchant, error) {
	var doc Merchant

	table := orm.ShMysql.Table(m.TableName())
	table = table.Where("open_id = ?  ", m.OpenId)

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

	return doc, nil
}

// list 接口使用
func (m *Merchant) GetOpenIdList(pageSize int, pageIndex int) ([]Merchant, int, error) {
	var doc []Merchant

	table := orm.ShMysql.Table(m.TableName())

	table = table.Where("open_id = ?  ", m.OpenId)
	var count int
	if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Count(&count)
	return doc, count, nil
}

func (m *Merchant) UpdateMerchant() error {

	if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
		map[string]interface{}{
			"merchant_name":        m.MerchantName,
			"industry":             m.Industry,
			"industry_category":    m.IndustryCategory,
			"contacts":             m.Contacts,
			"business_hours":       m.BusinessHours,
			"consumption":          m.Consumption,
			"iphone":               m.Iphone,
			"area":                 m.Area,
			"address":              m.Address,
			"number_plate":         m.NumberPlate,
			"business_license_url": m.BusinessLicenseUrl,
			"door_header_url":      m.DoorHeaderUrl,
			"recharge_url":         m.RechargeUrl}).Error; err != nil {
		return err
	}

	return nil
}