package data_manage

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type LzClassify struct {
	BreedId   int    `description:"分类id"`
	BreedName string `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 := orm.NewOrmUsingDB("edb")
	o.Raw(sql).QueryRows(&items)
	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 := orm.NewOrmUsingDB("edb")
	_, err = o.Raw(sql, breedId).QueryRows(&items)
	return
}

type LongzhongSurveyProduct struct {
	SurveyProductId      int `orm:"column(survey_product_id);pk"`
	ProjectQuotaId       int64
	BreedId              string
	BreedName            string
	QuotaId              string
	QuotaName            string
	UnitId               string
	UnitName             string
	SampleType           int64
	SampleId             string
	SampleName           string
	DeviceId             string
	Device               string
	ProductCraftId       string
	ProductCraft         string
	ProductLine          string
	InputMode            int64
	Frequency            int64
	InputValue           string
	TaskShouldFinishTime int
	CustomId             string
	CustomType           int64
	Custom               string
	QuotaSampleId        int64
	StartDate            string
	EndDate              string
	ModifyTime           time.Time
	LzCode               string
}

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 := orm.NewOrmUsingDB("edb")
	_, err = o.Raw(sql, breedId, frequency).QueryRows(&items)
	return
}

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

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

func GetLongzhongSurveyProductData(surveyProductId int) (items []*LzProductData, err error) {
	sql := ` SELECT *  FROM longzhong_survey_data WHERE survey_product_id=? ORDER BY data_time DESC `
	o := orm.NewOrmUsingDB("edb")
	_, err = o.Raw(sql, surveyProductId).QueryRows(&items)
	return
}

func GetLongzhongSurveyProductByCode(lzCode string) (items *LongzhongSurveyProduct, err error) {
	sql := ` SELECT *  FROM longzhong_survey_product WHERE lz_code=? `
	o := orm.NewOrmUsingDB("edb")
	err = o.Raw(sql, lzCode).QueryRow(&items)
	return
}
