package models

import (
	"rdluck_tools/orm"
	"time"
)

type Longzhongdata struct {
	LongzhongdataId int `orm:"column(longzhongdata_id);pk"`
	LongzhonginfoId int
	TradeCode       string
	Dt              string
	Close           float64
	CreateTime      time.Time
	ModifyTime      time.Time
	UnitDesc        string
	UpdTime         string
	AddTime         string
	DisplayTime     string
}

type Longzhonginfo struct {
	LongzhonginfoId int `orm:"column(longzhonginfo_id);pk"`
	TradeCode       string
	SecName         string
	Frequency       string
	ClassifyId      int
	ClassifyName    string
	Unit            string
	CreateTime      time.Time
	Remark          string
	IsNormal        string
	LastModifyDate  string
	Unitid          int64
	TempId          int64
	TempName        string
	ModifyTime      time.Time
}

func GetLongzhonginfoBySecName(secName string) (item *Longzhongdata, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT * FROM longzhonginfo WHERE sec_name=? `
	err = o.Raw(sql, secName).QueryRow(&item)
	return
}

//判断指标数据是否已经录入
func GetLongzhongdataCount(longzhonginfoId int, dt string) (count int, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT COUNT(1) AS count FROM longzhongdata WHERE longzhonginfo_id=? AND dt=? `
	err = o.Raw(sql, longzhonginfoId, dt).QueryRow(&count)
	return
}

func AddLongzhongdata(item *Longzhongdata) (err error) {
	o := orm.NewOrm()
	o.Using("edb")
	_, err = o.Insert(item)
	return
}

func ModifyLongzhongdata(item *Longzhongdata) (err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := ` UPDATE  longzhongdata
			SET
			  close = ?,
			  modify_time= NOW(),
			  unit_desc= ?,
			  upd_time = ?,
			  add_time=?,
			  display_time=?
			WHERE longzhonginfo_id = ? AND dt=?
			 `
	_, err = o.Raw(sql, item.Close, item.UnitDesc, item.UpdTime, item.AddTime, item.DisplayTime, item.LongzhonginfoId, item.Dt).Exec()
	return
}

func AddLongzhonginfo(item *Longzhonginfo) (newId int64, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	newId, err = o.Insert(item)
	return
}

func ModifyLongzhonginfo(item *Longzhonginfo) (err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `UPDATE  longzhonginfo
		SET
		  sec_name = ?,
		  unit = ?,
		  frequency = ?,
		  classify_id = ?,
		  classify_name = ?,
		  remark = ?,
		  last_modify_date = ?,
		  temp_name = ?,
		  is_normal = ?,
		  modify_time=NOW()
		WHERE unitid = ? AND temp_id = ? `
	_, err = o.Raw(sql, item.SecName, item.Unit, item.Frequency, item.ClassifyId, item.ClassifyName, item.Remark, item.LastModifyDate, item.TempName, item.IsNormal, item.Unitid, item.TempId).Exec()
	return
}

//判断指标数据是否已经录入
func GetLongzhonginfoCount(classifyId int, unitid int64) (count int, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT COUNT(1) AS count FROM longzhonginfo WHERE classify_id=? AND unitid=? `
	err = o.Raw(sql, classifyId, unitid).QueryRow(&count)
	return
}

//判断指标数据是否已经录入
func GetLongzhonginfoBySecNameCount(secName string) (count int, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT COUNT(1) AS count FROM longzhonginfo WHERE sec_name=? `
	err = o.Raw(sql, secName).QueryRow(&count)
	return
}

type LzProductInfoResp struct {
	Msg  string           `json:"msg"`
	Code string           `json:"code"`
	Data []*LzProductInfo `json:"data"`
}

type LzProductInfo struct {
	Unitid      int64  `json:"unitid"`
	ProUnitName string `json:"proUnitName"`
	Tempid      int64  `json:"tempid"`
	TempName    string `json:"tempName"`
	Proid       int    `json:"proid"`
	ProName     string `json:"proName"`
	Unit        string `json:"unit"`
	LzType      string `json:"type"`
	Maxdate     string `json:"maxdate"`
	IsNormal    string `json:"isNormal"`
}

func GetLongzhonginfoMaxId() (count int, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT MAX(longzhonginfo_id) AS count FROM longzhonginfo`
	err = o.Raw(sql).QueryRow(&count)
	return
}

type LzProductInfoDetail struct {
	UnitId      int64   `json:"unit_id"`
	UnitName    string  `json:"unit_name"`
	TempId      int     `json:"temp_id"`
	TempName    string  `json:"temp_name"`
	TempUnit    string  `json:"temp_unit"`
	ProId       int     `json:"pro_id"`
	ProCnName   string  `json:"pro_cn_name"`
	UnitValue   float64 `json:"unit_value"`
	UnitDesc    string  `json:"unit_desc"`
	DataTime    string  `json:"data_time"`
	UpdTime     string  `json:"upd_time"`
	AddTime     string  `json:"add_time"`
	DisplayTime string  `json:"display_time"`
}

type LzProductInfoDetailResp struct {
	Msg      string                 `json:"msg"`
	Code     string                 `json:"code"`
	Data     []*LzProductInfoDetail `json:"data"`
	Pagesize int                    `json:"pagesize"`
	Page     int                    `json:"page"`
}

func GetLongzhonginfoByUnitId(classifyName string, unitId int64) (item *Longzhongdata, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT * FROM longzhonginfo WHERE unitid=? AND classify_name=?`
	err = o.Raw(sql, unitId, classifyName).QueryRow(&item)
	return
}

func GetLongzhongDataById(lzInfoId int) (items []*Longzhongdata, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT * FROM longzhongdata WHERE longzhonginfo_id=? ORDER BY dt DESC `
	_, err = o.Raw(sql, lzInfoId).QueryRows(&items)
	return
}

func GetLongzhongDataMaxCount(classifyId int) (count int, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT MAX(t.num) AS count FROM (
				SELECT COUNT(1) AS num  FROM longzhonginfo AS a
				INNER JOIN longzhongdata AS b ON a.longzhonginfo_id=b.longzhonginfo_id
				WHERE a.classify_id=?
				GROUP BY a.longzhonginfo_id
			)AS t `
	err = o.Raw(sql, classifyId).QueryRow(&count)
	return
}

type LongzhonginfoList struct {
	LongzhonginfoId int `orm:"column(longzhonginfo_id);pk"`
	TradeCode       string
	SecName         string
	Frequency       string
	ClassifyId      int
	ClassifyName    string
	Unit            string
	IsNormal        string
	LastModifyDate  string
	Unitid          int
}

func GetLongzhonginfoList() (items []*LongzhonginfoList, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := ` SELECT * FROM longzhonginfo `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

func ModifyLongzhonginfoIsNormal(longzhonginfoId int) (err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := ` UPDATE longzhonginfo SET is_normal=0 WHERE longzhonginfo_id=? `
	_, err = o.Raw(sql, longzhonginfoId).Exec()
	return
}

type LzPriceInfo struct {
	Standard        string `json:"standard"`
	ModelName       string `json:"modelName"`
	Unit            string `json:"unit"`
	AreaName        string `json:"areaName"`
	PriceType       string `json:"priceType"`
	Memo            string `json:"memo"`
	Id              string `json:"id"`
	ProductName     string `json:"productName"`
	MarketName      string `json:"marketName"`
	ManufactureName string `json:"manufactureName"`
}

type LzPriceInfoResp struct {
	Msg  string         `json:"msg"`
	Code int            `json:"code"`
	Data []*LzPriceInfo `json:"data"`
}

type Longzhongpriceinfo struct {
	LongzhongpriceinfoId int `orm:"column(longzhongpriceinfo_id);pk"`
	Standard             string
	ModelName            string
	Unit                 string
	AreaName             string
	PriceType            string
	Memo                 string
	PriceId              string
	ProductName          string
	InfoType             string
	InfoTypeRemark       string
	MarketName           string
	ManufactureName      string
}

func AddLongzhongpriceinfo(item *Longzhongpriceinfo) (newId int64, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	newId, err = o.Insert(item)
	return
}

//判断指标数据是否已经录入
func GetLongzhongpriceinfoCount(unitid string) (count int, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT COUNT(1) AS count FROM longzhongpriceinfo WHERE price_id=? `
	err = o.Raw(sql, unitid).QueryRow(&count)
	return
}

//判断指标数据是否已经录入
func GetLongzhongpriceinfo() (items []*Longzhongpriceinfo, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT * FROM longzhongpriceinfo `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type Longzhongpricedata struct {
	LongzhongpricedataId int `orm:"column(longzhongpricedata_id);pk"`
	LongzhongpriceinfoId int
	PriceDate            string
	Memo                 string
	Price                string
	CnyPrice             string
	ZsyPrice             string
	ZshPrice             string
	LowPrice             string
	HighPrice            string
	RisePrice            string
	TonPrice             string
	PriceType            string
	UpdateDate           string
}

func AddLongzhongpricedata(item *Longzhongpricedata) (newId int64, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	newId, err = o.Insert(item)
	return
}

//判断指标数据是否已经录入
func GetLongzhongpricedataCount(longzhongpriceinfoId int, priceDate string) (count int, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT COUNT(1) AS count FROM longzhongpricedata WHERE longzhongpriceinfo_id=? AND price_date=? `
	err = o.Raw(sql, longzhongpriceinfoId, priceDate).QueryRow(&count)
	return
}

type LzPriceData struct {
	Id         string `json:"id"`
	PriceDate  string `json:"priceDate"`
	Memo       string `json:"memo"`
	Price      string `json:"price"`
	CnyPrice   string `json:"cnyPrice"`
	ZsyPrice   string `json:"zsyPrice"`
	ZshPrice   string `json:"zshPrice"`
	LowPrice   string `json:"lowPrice"`
	HighPrice  string `json:"highPrice"`
	RisePrice  string `json:"risePrice"`
	TonPrice   string `json:"tonPrice"`
	PriceType  string `json:"priceType"`
	UpdateDate string `json:"updateDate"`
}

type LzPriceDataResp struct {
	Msg  string         `json:"msg"`
	Code int            `json:"code"`
	Data []*LzPriceData `json:"data"`
}

func GetLongzhongPriceDataMaxCount(productName string) (count int, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT MAX(t.num) AS count FROM (
				SELECT COUNT(1) AS num  FROM longzhongpriceinfo AS a
				INNER JOIN longzhongpricedata AS b ON a.longzhongpriceinfo_id=b.longzhongpriceinfo_id
				WHERE a.product_name=?
				GROUP BY a.product_name
			)AS t `
	err = o.Raw(sql, productName).QueryRow(&count)
	return
}

type LongzhongpricedataItems struct {
	LongzhongpricedataId int `orm:"column(longzhongpricedata_id);pk"`
	LongzhongpriceinfoId int
	PriceDate            string
	Memo                 string
	Price                float64
	CnyPrice             float64
	ZsyPrice             float64
	ZshPrice             float64
	LowPrice             float64
	HighPrice            float64
	RisePrice            float64
	TonPrice             float64
	PriceType            string
	UpdateDate           string
}

func GetLongzhongPriceDataById(lzPriceInfoId int) (items []*LongzhongpricedataItems, err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `SELECT * FROM longzhongpricedata WHERE longzhongpriceinfo_id=? ORDER BY price_date DESC `
	_, err = o.Raw(sql, lzPriceInfoId).QueryRows(&items)
	return
}

func ModifyLongzhongpriceinfo(item *Longzhongpriceinfo) (err error) {
	o := orm.NewOrm()
	o.Using("edb")
	sql := `UPDATE  longzhongpriceinfo
SET
  standard = ?,
  model_name = ?,
  unit= ?,
  area_name=?,
  price_type = ?,
  memo = ?,
  market_name = ?,
  manufacture_name = ?
WHERE price_id = ? `
	_, err = o.Raw(sql, item.Standard, item.ModelName, item.Unit, item.AreaName, item.PriceType, item.Memo, item.MarketName, item.ManufactureName, item.PriceId).Exec()
	return
}
