package mapper

import (
	webServerEntity "cicv-data-closedloop/amd64/score_server/entity"
	"cicv-data-closedloop/common/config/c_db"
	"cicv-data-closedloop/common/config/c_log"
	"cicv-data-closedloop/common/util"
)

func SelectPage(teamName string, topic string, offset int, size int) ([]webServerEntity.ExamPo, []int) {
	var resultPos []webServerEntity.ExamPo
	var resultPosTotal []int
	var pageSql string
	var totalSql string
	if teamName == "" && topic == "" {
		pageSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-page.sql"])
		totalSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-total.sql"])
		err := c_db.MysqlDb.Select(&resultPos, pageSql, offset, size)
		if err != nil {
			c_log.GlobalLogger.Error(err)
		}
		err = c_db.MysqlDb.Select(&resultPosTotal, totalSql)
		if err != nil {
			c_log.GlobalLogger.Error(err)
		}
	}
	if teamName != "" && topic == "" {
		pageSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-page-by-team_name.sql"])
		totalSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-total-by-team_name.sql"])
		_ = c_db.MysqlDb.Select(&resultPos, pageSql, "%"+teamName+"%", offset, size)
		_ = c_db.MysqlDb.Select(&resultPosTotal, totalSql, "%"+teamName+"%")
	}
	if teamName == "" && topic != "" {
		pageSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-page-by-topic.sql"])
		totalSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-total-by-topic.sql"])
		_ = c_db.MysqlDb.Select(&resultPos, pageSql, "%"+topic+"%", offset, size)
		_ = c_db.MysqlDb.Select(&resultPosTotal, totalSql, "%"+topic+"%")
	}
	if teamName != "" && topic != "" {
		pageSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-page-by-team_name-and-topic.sql"])
		totalSql, _ = util.ReadFile(c_db.SqlFilesMap["exam-select-total-by-team_name-and-topic.sql"])
		_ = c_db.MysqlDb.Select(&resultPos, pageSql, "%"+teamName+"%", "%"+topic+"%", offset, size)
		_ = c_db.MysqlDb.Select(&resultPosTotal, totalSql, "%"+teamName+"%", "%"+topic+"%")
	}
	return resultPos, resultPosTotal
}

func SelectAllFromExam() []webServerEntity.ExamPo {
	var result []webServerEntity.ExamPo
	selectSql, err := util.ReadFile(c_db.SqlFilesMap["exam-select-all.sql"])
	if err != nil {
		c_log.GlobalLogger.Error("读取sql文件报错：", err)
		return nil
	}
	// 可以传参数
	if err = c_db.MysqlDb.Select(&result, selectSql); err != nil {
		c_log.GlobalLogger.Error("数据库查询报错：", err)
		return nil
	}
	return result
}

func UpdateDeductScoreOffline(e *webServerEntity.ExamOfflinePao) {
	// 更新记录结束时间为默认时间
	sqlTemplate, _ := util.ReadFile(c_db.SqlFilesMap["exam-update-deduct_score_offline-by-id.sql"])
	if err := c_db.DoTx(sqlTemplate, []any{
		e.DeductScoreOffline_1_1,
		e.DeductScoreOffline_1_2,
		e.DeductScoreOffline_1_3,
		e.DeductScoreOffline_1_4,
		e.DeductScoreOffline_2_1,
		e.DeductScoreOffline_2_2,
		e.DeductScoreOffline_3_1,
		e.DeductScoreOffline_3_2,
		e.DeductScoreOffline_3_3,
		e.DeductScoreOffline_3_4,
		e.DeductScoreOffline_4_1,
		e.DeductScoreOffline_4_2,
		e.DeductScoreOffline_4_3,
		e.DeductScoreOffline_5_1,
		e.DeductScoreOffline_5_2,
		e.DeductScoreOffline_5_3,
		e.DeductScoreOffline_6_1,
		e.DeductScoreOffline_6_2,
		e.DeductScoreOffline_7_1,
		e.DeductScoreOffline_7_2,
		e.DeductScoreOffline_7_3,
		e.DeductScoreOffline_8_1,
		e.DeductScoreOffline_8_2,
		e.DeductScoreOffline_8_3,
		e.DeductScoreOffline_9_1,
		e.DeductScoreOffline_9_2,
		e.DeductScoreOffline_9_3,
		e.DeductScoreOffline_10_1,
		e.DeductScoreOffline_10_2,
		e.DeductScoreOffline_11_1,
		e.DeductScoreOffline_11_2,
		e.DeductScoreOffline_12_1,
		e.DeductScoreOffline_12_2,
		e.DeductScoreOffline_12_3,
		e.DeductScoreOffline_13_1,
		e.DeductScoreOffline_13_2,
		e.Topic,
		e.TeamName,
	}); err != nil {
		c_log.GlobalLogger.Error("插入数据报错：", err)
		return
	}
}

func SelectLatestByTeamName(teamName string) []webServerEntity.ExamPo {
	// 车辆不是在起点启动的自动驾驶模式
	var result []webServerEntity.ExamPo
	selectSql, err := util.ReadFile(c_db.SqlFilesMap["exam-select-latest-by-team_name.sql"])
	if err != nil {
		c_log.GlobalLogger.Error("读取sql文件报错：", err)
		return nil
	}
	if err = c_db.MysqlDb.Select(&result, selectSql, teamName); err != nil {
		c_log.GlobalLogger.Error("数据库查询报错：", err)
		return nil
	}
	return result
}
