package Nmysql

import (
	"fmt"
	l4g "proxy/logger"
)

func QueryOfrIdByPrdInstId(prdInstId int) (int, error) {
	sqlStr := fmt.Sprintf("select OFR_ID from tb_prd_sub_prd_inst_551 where MAIN_PRD_INST_ID=%d limit 1;", prdInstId)
	rows, err := MySqlDb.Query(sqlStr)
	if err != nil {
		l4g.MysqlLog.Errorf("Query tb_prd_sub_prd_inst_551, prdInstId[%d] fail: %v", prdInstId, err)
		return 0, err
	}
	defer rows.Close()

	var ofrId int
	for rows.Next(){
		if err = rows.Scan(&ofrId); err != nil {
			l4g.MysqlLog.Errorf("query row of TB_RESOURCE_EXAM fail: %v", err)
			return 0, err
		}

		return ofrId, nil
	}

	return 0, nil
}

func QueryOfrLevelByOfrId(ofrId int) (int, error) {
	sqlStr := fmt.Sprintf("select OFR_LEVEL from tb_prd_ofr where OFR_ID=%d limit 1;", ofrId)
	rows, err := MySqlDb.Query(sqlStr)
	if err != nil {
		l4g.MysqlLog.Errorf("Query tb_prd_ofr, ofrId[%d] fail: %v", ofrId, err)
		return 0, err
	}
	defer rows.Close()

	var ofrLevel int
	for rows.Next(){
		if err = rows.Scan(&ofrLevel); err != nil {
			l4g.MysqlLog.Errorf("query row of tb_prd_ofr fail: %v", err)
			return 0, err
		}

		return ofrLevel, nil
	}

	return 0, nil
}

func QueryRentByOfrId(ofrId int) (int, error) {
	sqlStr := fmt.Sprintf("SELECT f.FEE_VALUE rentValue FROM tb_prd_prd_once_fee f WHERE f.PRD_ID = %d AND f.TYPE = 2 AND f.FEE_TYPE = 5;", ofrId)
	rows, err := MySqlDb.Query(sqlStr)
	if err != nil {
		l4g.MysqlLog.Errorf("Query tb_prd_prd_once_fee, ofrId[%d] fail: %v", ofrId, err)
		return 0, err
	}
	defer rows.Close()

	var onceFee int
	for rows.Next(){
		if err = rows.Scan(&onceFee); err != nil {
			l4g.MysqlLog.Errorf("query row of tb_prd_prd_once_fee fail: %v", err)
			return 0, err
		}

		return onceFee, nil
	}

	return 0, nil
}
