package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

func CreateUserAuthe(userAuthe model.SysUserAuthe) error {
	sqlStr := "INSERT INTO sys_user_authe(user_id, identity, name, phone, working_years," +
		" working_place, position, hospital_province, hospital_city, hospital_county," +
		" hospital_address, hospital_face_img, qualification_img,delflag" +
		" ) VALUE(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"

	global.GVA_DB.Exec(sqlStr, userAuthe.UserId, userAuthe.Identity, userAuthe.Name, userAuthe.Phone, userAuthe.WorkingYears,
		userAuthe.WorkingPlace, userAuthe.Position, userAuthe.HospitalProvince, userAuthe.HospitalCity, userAuthe.HospitalCounty,
		userAuthe.HospitalAddress, userAuthe.HospitalFaceImg, userAuthe.QualificationImg, 0)
	return global.GVA_DB.Error
}

func IsUserAuthe(u model.SysUserAuthe) int64 {
	var line int64

	sqlStr := "SELECT count(id) as line " +
		" FROM sys_user_authe" +
		" where user_id=? AND delflag=0"

	global.GVA_DB.Raw(sqlStr, u.UserId).Find(&line)
	if global.GVA_DB.Error != nil || line > 0 {
		return 1
	}
	return 2
}

func DeleteUserAuthe(userId int) (err error) {
	sqlStr := "UPDATE sys_user_authe SET delflag=? WHERE user_id=?"

	global.GVA_DB.Exec(sqlStr, 1, userId)
	return global.GVA_DB.Error
}

func UserAutheInfo(userId uint) (error, request.GetUserInfoReply) {
	userInfo := new(request.GetUserInfoReply)

	sqlStr := "SELECT id, user_id, identity, name, phone, working_years, working_place, " +
		" position, hospital_province, hospital_city, hospital_county, hospital_address," +
		" hospital_face_img, qualification_img, create_time, update_time " +
		" FROM sys_user_authe" +
		" where user_id=? AND delflag=0"

	global.GVA_DB.Raw(sqlStr, userId).Find(&userInfo)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, request.GetUserInfoReply{}
	}
	return nil, *userInfo
}
