package mysql

import orm "duoduo/database"

type Pid struct {
	CreateTime string `gorm:"column:create_time" json:"create_time"`
	OpenID     string `gorm:"column:open_id" json:"open_id"`
	Pid        string `gorm:"column:pid" json:"pid"`
	PidStatus  int    `gorm:"column:pid_status" json:"pid_status"` //1-拼多多 2-淘宝天猫 3-京东
	UpdateTime string `gorm:"column:update_time" json:"update_time"`
}

// TableName sets the insert table name for this struct type
func (p *Pid) TableName() string {
	return "pid"
}

//     自己 上  上上
//级别 0.5 0.15 0.05  70% 交税+
func (p *Pid) Get() (Pid, error) {
	var doc Pid
	err := orm.Eloquent.Select("pid").Where("open_id = ?  and pid_status = ?", p.OpenID, p.PidStatus).Table(p.TableName()).First(&doc).Error
	if err != nil {
		return doc, err
	}
	return doc, nil

}

func (p *Pid) GetOpenId() (Pid, error) {
	var doc Pid
	err := orm.Eloquent.Select("open_id").Where("pid = ? ", p.Pid).Table(p.TableName()).First(&doc).Error
	if err != nil {
		return doc, err
	}
	return doc, nil

}

func (p *Pid) Create() (Pid, error) {
	var doc Pid
	result := orm.Eloquent.Table(p.TableName()).Create(&p)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *p
	return doc, nil
}