package service

import (
	"errors"
	"fmt"
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	uuid "github.com/satori/go.uuid"
	"gorm.io/gorm"
	"strconv"
	"time"
)

func UserRegister(u model.SysUser) (err error, userInter model.SysUser) {
	var user model.SysUser
	if !errors.Is(global.GVA_DB.Where("mobile = ?", u.Mobile).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
		return errors.New("手机已注册"), userInter
	}
	// 否则 附加uuid 密码md5简单加密 注册
	u.UUID = uuid.NewV4()
	err = global.GVA_DB.Create(&u).Error
	return err, u
}

func IsRegister(u model.SysUser) int64 {
	var user model.SysUser
	if !errors.Is(global.GVA_DB.Where("mobile = ?", u.Mobile).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
		return 1
	}
	return 2
}

func IsBindCode(u model.SysUser) int64 {
	var user model.SysUser
	if !errors.Is(global.GVA_DB.Where("mobile = ? and hospital_code = ?", u.Mobile, u.HospitalCode).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
		return 1
	}
	return 2
}

func FindUserByMobile(mobile string) model.SysUser {
	var user model.SysUser
	global.GVA_DB.Where("mobile = ?", mobile).First(&user) // 判断用户名是否注册
	return user
}

func IsAuthe(userId int) int64 {
	var totalItem int64 = 0

	sqlStr := "SELECT count(id) as totalItem FROM sys_user_authe where user_id='" + strconv.Itoa(userId) + "'"
	global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数

	if totalItem > 0 { // 判断用户名是否注册
		return 1
	}
	return 2
}

func UpdateUserHospitalCode(mobile, hospitalCode string) error {
	sqlStr := "UPDATE sys_users SET hospital_code=? WHERE mobile=?"

	global.GVA_DB.Exec(sqlStr, hospitalCode, mobile)
	return global.GVA_DB.Error
}

func UpdateUserOpenid(user model.SysUser, userId int) error {
	sqlStr := "UPDATE sys_users SET openid=? WHERE id=?"

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

func UpdateUser(user model.SysUser, userId int) error {
	sqlStr := "UPDATE sys_users SET header_img=?,nick_name=? WHERE id=?"

	global.GVA_DB.Exec(sqlStr, user.HeaderImg, user.NickName, userId)
	return global.GVA_DB.Error
}

func GetUserInfo(userId uint) (error, model.SysUser) {
	data := new(model.SysUser)
	table := " sys_users "
	field := " id, mobile, openid, points_num, points_num_freeze, user_type "

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE id =?"

	global.GVA_DB.Raw(sqlStr, userId).Find(&data)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, model.SysUser{}
	}
	return nil, *data
}

func UpdateUserPoints(userId int, setType int, pointsNum int) error {
	sqlStr := "UPDATE sys_users "
	if setType == 1 {
		sqlStr += " SET points_num = points_num+?"
	} else {
		sqlStr += " SET points_num = points_num-?"
	}
	sqlStr += " WHERE id = ?"

	global.GVA_DB.Exec(sqlStr, pointsNum, userId)

	return global.GVA_DB.Error
}

func UpdateUserPointsFreeze(userId int, setType int, pointsNum int) error {
	sqlStr := "UPDATE sys_users "
	if setType == 1 {
		sqlStr += " SET points_num_freeze = points_num_freeze+?"
	} else {
		sqlStr += " SET points_num_freeze = points_num_freeze-?"
	}
	sqlStr += " WHERE id = ?"

	global.GVA_DB.Exec(sqlStr, pointsNum, userId)

	return global.GVA_DB.Error
}

func GetUserName(mobile string) (error, request.GetUserNameReply) {
	data := new(request.GetUserNameReply)
	table := " sys_users as su left join sys_user_authe as sua on su.id = sua.user_id "
	field := " su.id, su.mobile, sua.name, su.nick_name "

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE su.mobile = ?"

	global.GVA_DB.Raw(sqlStr, mobile).Find(&data)
	if global.GVA_DB.Error != nil {
		return global.GVA_DB.Error, request.GetUserNameReply{}
	}
	return nil, *data
}

func SetMobileCode(req model.MobileCode) error {
	err := global.GVA_DB.Table("mobile_code").Create(&req).Error
	return err
}

func GetMobileCode(mobile string) model.MobileCode {
	nowtime := time.Now()
	d, _ := time.ParseDuration("-180s")
	beforetime := nowtime.Add(d)

	data := new(model.MobileCode)
	table := " mobile_code "
	field := " id, mobile, code, status "

	sqlStr := "SELECT " + field +
		" FROM " + table +
		" WHERE mobile = ? and status = 1 and create_time > ?" +
		" ORDER BY create_time desc "

	err := global.GVA_DB.Raw(sqlStr, mobile, beforetime).Find(&data)

	if err != nil {
		fmt.Println(err)
	}
	return *data
}
