package data_manage

import (
	"eta/eta_mobile/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strconv"
	"strings"
	"time"
)

func AddEdbDataManualBySql(sqlStr string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sqlStr).Exec()
	return
}

func GetEdbDataManualMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_manual WHERE edb_code=? `
	err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
	return
}

type ManualEdbdata struct {
	TradeCode  string    `orm:"column(TRADE_CODE);pk" description:"指标编码"`
	Dt         string    `orm:"column(DT)" description:"日期"`
	Close      string    `orm:"column(CLOSE)" description:"值"`
	ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
}

func GetEdbdataManualByTradeCode(condition string, pars []interface{}) (item []*ManualEdbdata, err error) {
	sql := ` SELECT  * FROM edbdata WHERE 1=1 `
	o := orm.NewOrmUsingDB("edb")
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY DT DESC `
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

func GetEdbDataManualByCode(edbCode string, size int) (items []*EdbInfoSearchData, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM edb_data_manual WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
	_, err = o.Raw(sql, edbCode, size).QueryRows(&items)
	return
}

func GetEdbDataByManual(edbCode, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {
	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	var condition string
	var pars []interface{}

	if edbCode != "" {
		condition += " AND TRADE_CODE=? "
		pars = append(pars, edbCode)
	}

	manualDataList, err := GetEdbdataManualByTradeCode(condition, pars)
	if err != nil {
		return
	}
	searchItem = new(EdbInfoSearch)
	searchItem.EdbCode = edbCode
	dataLen := len(manualDataList)

	if dataLen > 0 {
		var isAdd bool
		addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
		nowStr := time.Now().Format(utils.FormatDateTime)
		dataList := make([]*EdbInfoSearchData, 0)
		for i := 0; i < dataLen; i++ {
			item := manualDataList[i]
			eDate := item.Dt
			sValue := item.Close
			dataTime, err := time.Parse(utils.FormatDate, eDate)
			if err != nil {
				return nil, err
			}
			timestamp := dataTime.UnixNano() / 1e6
			timeStr := fmt.Sprintf("%d", timestamp)
			addSql += "("
			addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
				"," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
			addSql += "),"
			isAdd = true
		}
		if isAdd {
			addSql = strings.TrimRight(addSql, ",")
			err = AddEdbDataManualBySql(addSql)
			if err != nil {
				utils.FileLogData.Info("AddEdbDataThsBySql Err:%s", err.Error())
				return
			}
		}

		size := utils.EDB_DATA_LIMIT
		dataList, err := GetEdbDataManualByCode(edbCode, size)
		if err != nil {
			utils.FileLogData.Info("GetEdbDataThsByCode Err:%s", err.Error())
			return searchItem, err
		}
		minDate, maxDate, err := GetEdbDataManualMaxOrMinDate(edbCode)
		if err != nil {
			return searchItem, err
		}
		searchItem.DataList = dataList
		searchItem.StartDate = minDate
		searchItem.EndDate = maxDate
	}
	if searchItem.DataList == nil {
		searchItem.DataList = make([]*EdbInfoSearchData, 0)
	}
	return
}

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

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

// 刷新手工指标数据
func RefreshEdbDataByManual(edbInfoId int, edbCode, startDate, endDate string) (err error) {
	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	if err != nil {
		return
	}
	edbInfoIdStr := strconv.Itoa(edbInfoId)
	//计算数据
	var condition string
	var pars []interface{}

	if edbCode != "" {
		condition += " AND TRADE_CODE=? "
		pars = append(pars, edbCode)
	}

	if startDate != "" {
		condition += " AND DT>=? "
		pars = append(pars, startDate)
	}

	if endDate != "" {
		condition += " AND DT<=? "
		pars = append(pars, endDate)
	}

	manualDataList, err := GetEdbdataManualByTradeCode(condition, pars)

	addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
	var isAdd bool
	for _, v := range manualDataList {
		item := v
		count, err := GetEdbDataManualByCodeAndDate(v.TradeCode, v.Dt)
		if err != nil && err.Error() != utils.ErrNoRow() {
			return err
		}
		if count <= 0 {
			nowStr := time.Now().Format(utils.FormatDateTime)
			eDate := item.Dt
			sValue := item.Close
			dataTime, err := time.Parse(utils.FormatDate, eDate)
			if err != nil {
				return err
			}
			timestamp := dataTime.UnixNano() / 1e6
			timeStr := fmt.Sprintf("%d", timestamp)
			addSql += "("
			addSql += edbInfoIdStr + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + sValue + "," + "'" + nowStr + "'" +
				"," + "'" + nowStr + "'" + "," + "1" + "," + "'" + timeStr + "'"
			addSql += "),"
			isAdd = true
		} else {
			err = ModifyEdbDataManual(int64(edbInfoId), v.Dt, v.Close)
			if err != nil {
				return err
			}
		}
	}
	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = to.Raw(addSql).Exec()
		if err != nil {
			return err
		}
	}
	return
}

// 刷新手工指标数据
func RefreshAllEdbDataByManual(edbInfoId, source, subSource int, edbCode string) (err error) {
	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	if err != nil {
		return
	}
	edbInfoIdStr := strconv.Itoa(edbInfoId)
	//计算数据
	var condition string
	var pars []interface{}

	if edbCode != "" {
		condition += " AND TRADE_CODE=? "
		pars = append(pars, edbCode)
	}

	manualDataList, err := GetEdbdataManualByTradeCode(condition, pars)
	//获取指标所有数据
	existDataList := make([]*EdbDataBase, 0)
	dataTableName := GetEdbDataTableName(source, subSource)
	sql := `SELECT * FROM %s WHERE edb_info_id=? `
	sql = fmt.Sprintf(sql, dataTableName)
	_, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
	if err != nil {
		return err
	}
	existDataMap := make(map[string]*EdbDataBase)
	for _, v := range existDataList {
		existDataMap[v.DataTime] = v
	}

	addSql := ` INSERT INTO edb_data_manual(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
	var isAdd bool
	manualMap := make(map[string]*ManualEdbdata)
	fmt.Println("manualDataList:", len(manualDataList))
	for _, v := range manualDataList {
		item := v
		fmt.Println("Item:", item.Dt, item.Close, item.TradeCode, item.ModifyTime)
		if findItem, ok := existDataMap[v.Dt]; !ok {
			eDate := item.Dt
			sValue := item.Close

			dataTime, err := time.Parse(utils.FormatDate, eDate)
			if err != nil {
				return err
			}
			timestamp := dataTime.UnixNano() / 1e6
			timeStr := fmt.Sprintf("%d", timestamp)

			addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
			isAdd = true
		} else {
			if findItem.Value != item.Close {
				sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
				sql = fmt.Sprintf(sql, dataTableName)
				_, err = to.Raw(sql, item.Close, edbInfoId, item.Dt).Exec()
				if err != nil {
					return err
				}
			}
		}
		manualMap[v.Dt] = v
	}
	for _, v := range existDataList {
		if _, ok := manualMap[v.DataTime]; !ok {
			go DeleteEdbDataByIdAndSource(v.EdbDataId, utils.DATA_SOURCE_MANUAL, utils.DATA_SUB_SOURCE_EDB)
		}
	}
	fmt.Println("isAdd:", isAdd)
	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = to.Raw(addSql).Exec()
		if err != nil {
			fmt.Println("RefreshAllEdbDataByManual add Err", err.Error())
			return
		}
	}
	return
}

func DeleteEdbDataByIdAndSource(edbDataId, source, subSource int) (err error) {
	sql := ` DELETE FROM %s WHERE edb_data_id=? `
	tableName := GetEdbDataTableName(source, subSource)
	sql = fmt.Sprintf(sql, tableName)

	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, edbDataId).Exec()
	return
}
