package data_manage

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

type lzSurveyData struct {
	DataTime   string `orm:"column(data_time)" description:"日期"`
	InputValue string `orm:"column(input_value)" description:"值"`
}

type LongzhongSurveyData struct {
	SurveyDataId         int `orm:"column(survey_data_id);pk"`
	SurveyProductId      int
	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 int64
	CustomId             string
	CustomType           int64
	Custom               string
	QuotaSampleId        int64
	TaskActualFinishTime int64
	AreaName             string
	ProvinceName         string
	ResearchStartData    int64
	ResearchStopData     int64
	DataTime             string
}

func GetLzSurveyDataByTradeCode(condition string, pars []interface{}) (item []*lzSurveyData, err error) {
	sql := ` SELECT  a.* FROM longzhong_survey_data AS a
				INNER JOIN longzhong_survey_product AS b ON a.survey_product_id=b.survey_product_id
				WHERE 1=1 `
	o := orm.NewOrm()
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.data_time DESC `
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

func GetLzSurveyDataExistByTradeCode(surveyProductId int) (items []*LongzhongSurveyData, err error) {
	sql := ` SELECT  * FROM longzhong_survey_data WHERE 1=1 AND survey_product_id=? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, surveyProductId).QueryRows(&items)
	return
}

func GetEdbDataLzByCodeAndDate(edbCode string, startDate string) (count int, err error) {
	o := orm.NewOrm()
	sql := ` SELECT COUNT(1) AS count FROM edb_data_lz WHERE edb_code=? AND data_time=? `
	err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
	return
}

func ModifyEdbDataLz(edbInfoId int64, dataTime, value string) (err error) {
	o := orm.NewOrm()
	sql := ` UPDATE edb_data_lz SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
	_, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
	return
}