package api

import (
	"errors"
	"github.com/kataras/iris/v12"
	"github.com/sirupsen/logrus"
	"main/database"
	"main/model"
	"main/utils"
	"time"
)

func getYeb(ctx iris.Context) {
	username := utils.GetLoginUser(ctx).Username
	if username == "" {
		return
	}
	var balances []model.Balances
	db := database.GetInstance().GetMysqlDb()
	if err := db.Where("username = ?", username).Order("card").Find(&balances).Error; err != nil {
		logrus.Errorln("sql执行失败：", err)
	}
	err := ctx.JSON(utils.FormatRes(iris.StatusOK, "", balances))
	if utils.ErrHandle(ctx, err) {
		return
	}
}

func addYeb(ctx iris.Context) {
	username := utils.GetLoginUser(ctx).Username
	if username == "" {
		return
	}
	var balance paramsBalance
	var tmpBalance []model.Balances
	err := ctx.ReadJSON(&balance)
	if utils.ErrHandle(ctx, err) {
		return
	}
	db := database.GetInstance().GetMysqlDb()
	if err1 := db.Where("username = ? and card = ?", username, balance.Card).Find(&tmpBalance).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	if len(tmpBalance) > 0 {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.SetErr(errors.New("card已存在"))
		return
	}
	if err1 := db.Create(&model.Balances{
		Username: username,
		Card:     balance.Card,
		Type:     balance.Type,
		Balance:  balance.Balance,
	}).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	if balance.Type {
		balance.Balance = -balance.Balance
	}
	if err1 := db.Create(&model.BalanceLogs{
		Username: username,
		Card:     balance.Card,
		Date:     time.Now().Format("2006-01-02"),
		Balance:  balance.Balance,
	}).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", "success"))
	if utils.ErrHandle(ctx, err) {
		return
	}
}

func updateYeb(ctx iris.Context) {
	username := utils.GetLoginUser(ctx).Username
	if username == "" {
		return
	}
	var params paramsBalance
	var balances []model.Balances
	err := ctx.ReadJSON(&params)
	if utils.ErrHandle(ctx, err) {
		return
	}
	db := database.GetInstance().GetMysqlDb()
	if err1 := db.Where("username = ? and card = ?", username, params.Card).Find(&balances).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	if len(balances) == 0 {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.SetErr(errors.New("card 不存在"))
		return
	}
	balances[0].Balance = params.Balance
	balances[0].Type = params.Type
	if err1 := db.Save(&balances[0]).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	//if err1 := db.Model(&balances).Update("balance", params.Balance).Error; err1 != nil {
	//	logrus.Errorln("sql执行失败：", err1)
	//}
	if params.Type {
		params.Balance = -params.Balance
	}
	if err1 := db.Create(&model.BalanceLogs{
		Username: username,
		Card:     params.Card,
		Date:     time.Now().Format("2006-01-02"),
		Balance:  params.Balance,
	}).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", "success"))
	if utils.ErrHandle(ctx, err) {
		return
	}
}

func deleteYeb(ctx iris.Context) {
	username := utils.GetLoginUser(ctx).Username
	if username == "" {
		return
	}
	var params paramsBalance
	var balances []model.Balances
	err := ctx.ReadJSON(&params)
	if utils.ErrHandle(ctx, err) {
		return
	}
	db := database.GetInstance().GetMysqlDb()
	if err1 := db.Where("username = ? and card = ?", username, params.Card).Find(&balances).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	if len(balances) == 0 {
		ctx.StatusCode(iris.StatusInternalServerError)
		ctx.SetErr(errors.New("card 不存在"))
		return
	}
	if err1 := db.Delete(&balances).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	if err1 := db.Create(&model.BalanceLogs{
		Username: username,
		Card:     params.Card,
		Date:     time.Now().Format("2006-01-02"),
		Balance:  0,
	}).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", "success"))
	if utils.ErrHandle(ctx, err) {
		return
	}
}

func szYeb(ctx iris.Context) {
	username := utils.GetLoginUser(ctx).Username
	if username == "" {
		return
	}
	var params paramSZBalance
	var balance model.Balances
	err := ctx.ReadJSON(&params)
	if utils.ErrHandle(ctx, err) {
		return
	}
	db := database.GetInstance().GetMysqlDb()
	if err1 := db.Where("username = ? and card = ?", username, params.Card).Find(&balance).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	if utils.DataIsNil(balance) {
		utils.ErrHandle(ctx, errors.New("card 不存在"))
		return
	}
	if balance.Type == params.Type {
		balance.Balance = utils.Round(balance.Balance+params.Amount, 2)
	} else {
		balance.Balance = utils.Round(balance.Balance-params.Amount, 2)
	}
	if err1 := db.Save(&balance).Error; err1 != nil {
		logrus.Errorln("sql执行失败：", err1)
	}
	if balance.Type {
		if err1 := db.Create(&model.BalanceLogs{
			Username: username,
			Card:     params.Card,
			Date:     time.Now().Format("2006-01-02"),
			Balance:  -balance.Balance,
		}).Error; err1 != nil {
			logrus.Errorln("sql执行失败：", err1)
		}
	} else {
		if err1 := db.Create(&model.BalanceLogs{
			Username: username,
			Card:     params.Card,
			Date:     time.Now().Format("2006-01-02"),
			Balance:  balance.Balance,
		}).Error; err1 != nil {
			logrus.Errorln("sql执行失败：", err1)
		}
	}
	err = ctx.JSON(utils.FormatRes(iris.StatusOK, "", "success"))
	if utils.ErrHandle(ctx, err) {
		return
	}
}
