package mysqlLy

import (
	orm "duoduo/database"
	"github.com/shopspring/decimal"
	"time"
)

type Pets struct {
	ID                  int64           `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
	UserID              int64           `gorm:"column:user_id;type:bigint(20)" json:"user_id"`
	Name                string          `gorm:"column:name;type:varchar(64)" json:"name"`                                  // 昵称
	Pet                 string          `gorm:"column:pet;type:varchar(64)" json:"pet"`                                    // 品种
	Age                 string          `gorm:"column:age;type:varchar(16)" json:"age"`                                    // 年龄  '0-3个月','3-6个月','6-9个月','小于1岁','小于2岁','小于3岁','大于3岁'
	Sex                 string          `gorm:"column:sex;type:varchar(4)" json:"sex"`                                     // 性别 MM GG
	PetStatus           int             `gorm:"column:pet_status;type:tinyint(4)" json:"pet_status"`                       // 1-流浪 2-家养 3-收养所
	PetDes              string          `gorm:"column:pet_des;type:varchar(16)" json:"pet_des"`                            // 宠物详细种类
	IsSterilization     int             `gorm:"column:is_sterilization;type:tinyint(4)" json:"is_sterilization"`           // 绝育状态:1-是 2-否 3-不确认
	IsExpellingParasite int             `gorm:"column:is_expelling_parasite;type:tinyint(4)" json:"is_expelling_parasite"` // 驱虫状态:1-是 2-否 3-不确认
	IsImmune            int             `gorm:"column:is_immune;type:tinyint(4)" json:"is_immune"`                         // 免疫状态:1-是 2-否 3-不确认
	IsFee               int             `gorm:"column:is_fee;type:tinyint(4)" json:"is_fee"`                               // 1-免费 2-押金 3-有偿
	Deposit             decimal.Decimal `gorm:"column:deposit;type:decimal(10,2)" json:"deposit"`                          // 押金
	DepositDate         time.Time       `gorm:"column:deposit_date;type:date;default:null" json:"deposit_date"`            // 押金到期时间
	Amount              decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"`                            // 金额
	Province            string          `gorm:"column:province;type:varchar(16)" json:"province"`                          // 省
	City                string          `gorm:"column:city;type:varchar(16)" json:"city"`                                  // 市
	Area                string          `gorm:"column:area;type:varchar(16)" json:"area"`                                  // 区
	PetInfo             string          `gorm:"column:pet_info;type:text" json:"pet_info"`                                 // 宠物信息
	AdoptInfo           string          `gorm:"column:adopt_info;type:text" json:"adopt_info"`                             // 领养要求
	Pictures            string          `gorm:"column:pictures;type:json" json:"pictures"`                                 // 图片url
	Status              int             `gorm:"column:status;type:tinyint(4)" json:"status"`                               // 状态 1-待领养 2-已领养
	CreateTime          time.Time       `gorm:"column:create_time;type:datetime" json:"create_time"`                       // 创建时间
	UpdateTime          time.Time       `gorm:"column:update_time;type:datetime" json:"update_time"`                       // 更新时间
	OpenId              string          `gorm:"column:open_id;type:varchar(255)" json:"open_id"`                           //openid
}

func (m *Pets) TableName() string {
	return "pets"
}

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

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

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

	if m.OpenId != "" {
		table = table.Where("open_id = ?  ", m.OpenId)
	}
	if m.Pet != "" { //品种
		table = table.Where("pet = ?  ", m.Pet)
	}
	if m.City != "" { //城市
		table = table.Where("city = ?", m.City)
	}
	if m.Status != 0 { //状态
		table = table.Where("status = ?", m.Status)
	}
	if m.IsFee == 1 || m.IsFee == 2 { //
		table = table.Where("is_fee in (1,2)")
	} else if m.IsFee == 3 {
		table = table.Where("is_fee = 3")
	}

	var count int
	if err := table.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 *Pets) Get() (Pets, error) {
	var doc Pets
	err := orm.LyMysql.Where("id = ? ", m.ID).Table(m.TableName()).First(&doc).Error
	if err != nil {
		return doc, err
	}
	return doc, nil

}