package data_manage

import (
	"gorm.io/gorm"
	"time"

	"eta/eta_api/global"
	"eta/eta_api/utils"
)

type LzClassify struct {
	BreedId   int    `gorm:"column:breed_id" description:"分类id"`
	BreedName string `gorm:"column:breed_name" description:"分类名称"`
}

func GetLzSurveyClassify() (items []*LzClassify, err error) {
	sql := `SELECT breed_id,breed_name FROM longzhong_survey_product GROUP BY breed_name ORDER BY breed_name DESC`
	o := global.DbMap[utils.DbNameManualIndex]
	err = o.Raw(sql).Find(&items).Error
	return
}

type LzFrequency struct {
	Frequency int `description:"频度:1-日度 2-周度 3-月度 4-季度 5-年度 99-无固定频率"`
}

func GetLzFrequencyByClassifyId(breedId int) (items []*LzFrequency, err error) {
	sql := `SELECT frequency FROM longzhong_survey_product WHERE breed_id=? GROUP BY frequency ORDER BY frequency ASC`
	o := global.DbMap[utils.DbNameManualIndex]
	err = o.Raw(sql, breedId).Find(&items).Error
	return
}

type LongzhongSurveyProduct struct {
	SurveyProductId      int       `gorm:"column:survey_product_id;primaryKey"`
	ProjectQuotaId       int64     `gorm:"column:project_quota_id"`
	BreedId              string    `gorm:"column:breed_id"`
	BreedName            string    `gorm:"column:breed_name"`
	QuotaId              string    `gorm:"column:quota_id"`
	QuotaName            string    `gorm:"column:quota_name"`
	UnitId               string    `gorm:"column:unit_id"`
	UnitName             string    `gorm:"column:unit_name"`
	SampleType           int64     `gorm:"column:sample_type"`
	SampleId             string    `gorm:"column:sample_id"`
	SampleName           string    `gorm:"column:sample_name"`
	DeviceId             string    `gorm:"column:device_id"`
	Device               string    `gorm:"column:device"`
	ProductCraftId       string    `gorm:"column:product_craft_id"`
	ProductCraft         string    `gorm:"column:product_craft"`
	ProductLine          string    `gorm:"column:product_line"`
	InputMode            int64     `gorm:"column:input_mode"`
	Frequency            int64     `gorm:"column:frequency"`
	InputValue           string    `gorm:"column:input_value"`
	TaskShouldFinishTime int       `gorm:"column:task_should_finish_time"`
	CustomId             string    `gorm:"column:custom_id"`
	CustomType           int64     `gorm:"column:custom_type"`
	Custom               string    `gorm:"column:custom"`
	QuotaSampleId        int64     `gorm:"column:quota_sample_id"`
	StartDate            string    `gorm:"column:start_date"`
	EndDate              string    `gorm:"column:end_date"`
	ModifyTime           time.Time `gorm:"column:modify_time"`
	LzCode               string    `gorm:"column:lz_code"`
}

func (m *LongzhongSurveyProduct) AfterFind(db *gorm.DB) (err error) {
	m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
	m.EndDate = utils.GormDateStrToDateStr(m.StartDate)

	return
}

func GetLongzhongSurveyProduct(breedId, frequency int) (items []*LongzhongSurveyProduct, err error) {
	sql := `SELECT * FROM longzhong_survey_product WHERE breed_id=? AND frequency=? ORDER BY survey_product_id ASC`
	o := global.DbMap[utils.DbNameManualIndex]
	err = o.Raw(sql, breedId, frequency).Find(&items).Error
	return
}

type LzProductList struct {
	SurveyProductId int              `gorm:"column:survey_product_id;primaryKey"`
	BreedName       string           `gorm:"column:breed_name" description:"品种名称"`
	QuotaName       string           `gorm:"column:quota_name" description:"指标名称"`
	UnitName        string           `gorm:"column:unit_name" description:"单位"`
	SampleType      int64            `gorm:"column:sample_type" description:"样本类型 0-空 1-企业 2-港口 3-运距 4-区域/国家 99-不确定"`
	SampleName      string           `gorm:"column:sample_name" description:"样本名称"`
	Device          string           `gorm:"column:device" description:"设备"`
	Frequency       int64            `gorm:"column:frequency" description:"频度"`
	Custom          string           `gorm:"column:custom" description:"扩展字段"`
	StartDate       string           `gorm:"column:start_date" description:"开始日期"`
	EndDate         string           `gorm:"column:end_date" description:"结束日期"`
	ModifyTime      string           `gorm:"column:modify_time" description:"修改时间"`
	LzCode          string           `gorm:"column:lz_code" description:"指标编码"`
	DataList        []*LzProductData `gorm:"-"`
}

type LzProductData struct {
	InputValue float64 `description:"日期"`
	DataTime   string  `description:"值"`
}

func (m *LzProductData) AfterFind(db *gorm.DB) (err error) {
			m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
	return
}

func GetLongzhongSurveyProductData(surveyProductId int) (items []*LzProductData, err error) {
	sql := `SELECT * FROM longzhong_survey_data WHERE survey_product_id=? ORDER BY data_time DESC`
	o := global.DbMap[utils.DbNameManualIndex]
	err = o.Raw(sql, surveyProductId).Find(&items).Error
	return
}

func GetLongzhongSurveyProductByCode(lzCode string) (items *LongzhongSurveyProduct, err error) {
	sql := `SELECT * FROM longzhong_survey_product WHERE lz_code=?`
	o := global.DbMap[utils.DbNameManualIndex]
	err = o.Raw(sql, lzCode).First(&items).Error
	return
}