package main

import (
	"PsycheEpic/src/config"
	"PsycheEpic/src/datasource"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

type Contact struct {
	UserId      int    `json:"user_id" xorm:"user_id"`
	Name        string `json:"name" jorm:"real_name" xorm:"real_name"`
	Age         int    `json:"age" xorm:"age"`
	PhoneNumber string `json:"phone_number" xorm:"phone_number"`
	HomeAddress string `json:"home_address" xorm:"home_address"`
	CreateTime  string `json:"create_time" xorm:"create_time"`
}

type ContactResult struct {
	UserID      int    `xorm:"user_id"`
	RealName    string `xorm:"real_name"`
	Age         int    `xorm:"age"`
	PhoneNumber string `xorm:"phone_number"`
	Address     string `xorm:"address"`
	CreateTime  string `xorm:"create_time"`
}

func QueryContact(name string) (*ContactResult, error) {
	var result ContactResult

	session := datasource.Engine.NewSession()

	// 由于 xorm 不支持 OUT 参数，我们直接获取返回的结果集
	_, err := session.SQL("CALL query_contact1(?)", name).Get(&result)
	if err != nil {
		return nil, err
	}

	return &result, nil
}

func main() {
	//读取参数配置
	config.InitConfig()
	//执行数据库初始化
	datasource.InitMysql()

	// 调用函数
	name := "Jerry"
	contact, err := QueryContact(name)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Result: %+v\n", contact)
}
