package data_manage

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

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

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

func GetEdbDataGlMaxOrMinDate(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_gl WHERE edb_code=? `
	err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
	return
}

type GlData struct {
	InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
	DataTime   string `orm:"column(DATA_DATE)" description:"值"`
}

func GetGlDataByTradeCode(condition string, pars []interface{}) (item []*GlData, err error) {
	condition += " AND IS_DELETE=0 "
	sql := ` SELECT * FROM mb_index_main_data WHERE 1=1 `
	o := orm.NewOrmUsingDB("gl")
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY PUBLISH_TIME DESC `
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}

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

func GetEdbDataByGl(edbCode, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {

	var condition string
	var pars []interface{}

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

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

	existMap := make(map[string]string)
	if dataLen > 0 {
		var isAdd bool
		addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
		dataList := make([]*EdbInfoSearchData, 0)
		for i := 0; i < dataLen; i++ {
			item := glDataList[i]
			eDate := item.DataTime
			sValue := item.InputValue
			if sValue != "" {
				if _, ok := existMap[eDate]; !ok {
					dataTime, err := time.Parse(utils.FormatDate, eDate)
					if err != nil {
						return nil, err
					}
					timestamp := dataTime.UnixNano() / 1e6
					timeStr := fmt.Sprintf("%d", timestamp)
					addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
					isAdd = true
				}
			}
			existMap[eDate] = eDate
		}
		fmt.Println("isAdd:", isAdd)
		if isAdd {
			//addSql = strings.TrimRight(addSql, ",")
			//fmt.Println("addSql:exec start")
			//_, err = o.Raw(addSql).Exec()
			//if err != nil {
			//	utils.FileLogData.Info("GetEdbDataByGl Err:%s", err.Error())
			//	return
			//}
			//fmt.Println("addSql:exec end")
			addSql = strings.TrimRight(addSql, ",")
			utils.FileLog.Info(addSql)
			err = AddEdbDataGlBySql(addSql)
			if err != nil {
				utils.FileLogData.Info("AddEdbDataGlBySql Err:%s", err.Error())
				return
			}
		}
		dataList, err := GetEdbDataGlByCode(edbCode)
		if err != nil {
			utils.FileLogData.Info("GetEdbDataGlByCode Err:%s", err.Error())
			return searchItem, err
		}
		minDate, maxDate, err := GetEdbDataGlMaxOrMinDate(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 ModifyEdbDataGlStatus(edbInfoId int64, edbCode string) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` UPDATE edb_data_gl SET edb_info_id=?, status=1,modify_time=NOW() WHERE edb_code=? `
	_, err = o.Raw(sql, edbInfoId, edbCode).Exec()
	return
}

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

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

// 刷新钢联指标数据
func RefreshEdbDataByGl(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 INDEX_CODE=? "
		pars = append(pars, edbCode)
	}

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

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

	glDataList, err := GetGlDataByTradeCode(condition, pars)

	addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
	var isAdd bool
	existMap := make(map[string]string)
	for _, v := range glDataList {
		item := v
		if _, ok := existMap[v.DataTime]; !ok {
			count, err := GetEdbDataGlByCodeAndDate(edbCode, v.DataTime)
			if err != nil && err.Error() != utils.ErrNoRow() {
				return err
			}
			if count <= 0 {
				eDate := item.DataTime
				sValue := item.InputValue
				if sValue != "" {
					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 {
				err = ModifyEdbDataGl(int64(edbInfoId), v.DataTime, v.InputValue)
				if err != nil {
					return err
				}
			}
		}
		existMap[v.DataTime] = v.InputValue
	}
	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = to.Raw(addSql).Exec()
		if err != nil {
			return err
		}
	}
	return
}

// 全部刷新隆众数据
func RefreshAllEdbDataByGl(edbInfoId, source 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 INDEX_CODE=? "
		pars = append(pars, edbCode)
	}

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

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

	glDataList, err := GetGlDataByTradeCode(condition, pars)

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

	addSql := ` INSERT INTO edb_data_gl(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
	var isAdd bool
	existMap := make(map[string]string)
	for _, v := range glDataList {
		item := v
		if _, ok := existMap[v.DataTime]; !ok {
			eDate := item.DataTime
			sValue := item.InputValue
			if sValue != "" {
				dataTime, err := time.Parse(utils.FormatDate, eDate)
				if err != nil {
					return err
				}
				timestamp := dataTime.UnixNano() / 1e6
				timeStr := fmt.Sprintf("%d", timestamp)
				saveValue := sValue

				if existVal, ok := dataMap[eDate]; !ok {
					addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
					isAdd = true
				} else {
					if existVal != saveValue {
						sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
						sql = fmt.Sprintf(sql, dataTableName)
						_, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
						if err != nil {
							return err
						}
					}
				}
			}
		}
		existMap[v.DataTime] = v.DataTime
	}
	if isAdd {
		addSql = strings.TrimRight(addSql, ",")
		_, err = to.Raw(addSql).Exec()
		if err != nil {
			return err
		}
	}
	return
}
