package data_manage

import (
	"eta_gn/eta_api/global"
	"eta_gn/eta_api/utils"
	"fmt"
)

func EdbInfoUpdateStatusByEdbInfoId(edbInfoIds []int, isStop int, calculateEdbInfoIds []int) (err error) {
	o := global.DmSQL["data"].Begin()
	defer func() {
		if err != nil {
			_ = o.Rollback()
			return
		}
		_ = o.Commit()
	}()

	// 更改指标的更新状态
	if len(edbInfoIds) == 1 {
		sql := ` UPDATE edb_info SET no_update = ? WHERE edb_info_id=? `
		err = o.Exec(sql, isStop, edbInfoIds[0]).Error
		if err != nil {
			return
		}
	} else {
		sql := ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) `
		err = o.Exec(sql, isStop, edbInfoIds).Error
		if err != nil {
			return
		}
	}

	if len(calculateEdbInfoIds) > 0 {
		// 批量更新相关联的指标ID
		sql := ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) `
		err = o.Exec(sql, isStop, calculateEdbInfoIds).Error
		if err != nil {
			return
		}
	}

	return
}

func ModifyEdbInfoWindWsdDataStatus(source, subSource int, edbInfoId int64, edbCode string) (err error) {
	tableName := GetEdbDataTableNameAndSubSource(source, subSource)
	sql := ` UPDATE %s SET edb_info_id=?,modify_time=NOW() WHERE edb_code=? `
	sql = fmt.Sprintf(sql, tableName)
	err = global.DmSQL["data"].Exec(sql, edbInfoId, edbCode).Error

	return
}