package customer

import (
	"errors"
	"spaceofcheng/services/models"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/utils"
	"strconv"
	"time"

	"github.com/yl10/kit/guid"
)

// SaveAccount 保存账户信息
func (m *CustomerDAO) SaveAccount(account *model.TaCustomerAccount) error {
	if account.CustomerId == "" {
		return errors.New("账户未关联人员")
	}

	account.Status = models.STATUS_NORMAL
	account.CreateDate = time.Now().Local()
	account.AccountId = guid.NewGUIDString()

	_, err := m.db.Insert(account)
	return err
}

// InsertAccountRecords 新增账户消费记录
func (m *CustomerDAO) InsertAccountRecords(rec *model.TaAccountChange) error {
	if rec.AccountId == "" {
		return errors.New("无法确定消费账户")
	}

	if rec.CaseId == "" {
		return errors.New("无法确定消费案场")
	}

	acc, err := m.GetAccountByID(rec.AccountId)
	if err != nil {
		utils.LogError("获取人员账户失败: " + err.Error())
		return errors.New("找不到账户消费记录")
	}

	if acc.Status != models.STATUS_NORMAL {
		return errors.New("人员账户状态不正常")
	}

	// 补充部分字段
	rec.DetailId = guid.NewGUIDString()
	rec.CreateDate = time.Now().Local()
	rec.Status = models.STATUS_NORMAL
	rec.OrgId = acc.OrgId

	// 消费内容
	points := float64(0.0)
	moenyCheng := float64(0.0)
	amount, _ := strconv.ParseFloat(rec.Amount, 64)
	if rec.ChangeType == models.CONSUME_POINTS {
		points = amount
	} else if rec.ChangeType == models.CONSUME_COINCHG {
		moenyCheng = amount
	}

	// 反向更新账户表
	accAmount, _ := strconv.ParseFloat(acc.Amount, 64)
	accPoints, _ := strconv.ParseFloat(acc.Points, 64)
	accPayedAmount, _ := strconv.ParseFloat(acc.PayedAmount, 64)
	accPayedPoints, _ := strconv.ParseFloat(acc.PayedPoints, 64)

	if rec.FloatType == models.ACCOUNT_SPENT {
		accPayedAmount += moenyCheng
		accPayedPoints += points

		accAmount -= moenyCheng
		accPoints -= points
	}

	// 账户不能为负
	if accAmount < 0 || accPoints < 0 {
		return errors.New("账户不足, 不能消费")
	}

	acc.Amount = strconv.FormatFloat(accAmount, 'f', -1, 64)
	acc.Points = strconv.FormatFloat(accPoints, 'f', -1, 64)
	acc.PayedAmount = strconv.FormatFloat(accPayedAmount, 'f', -1, 64)
	acc.PayedPoints = strconv.FormatFloat(accPayedPoints, 'f', -1, 64)

	if _, err := m.db.Insert(rec); err != nil {
		utils.LogError("插入消费记录失败: " + err.Error())
		return errors.New("记录消费记录失败")
	}

	if _, err := m.db.Where("account_id=?", acc.AccountId).
		Cols([]string{"amount", "points", "payed_amount", "payed_points"}...).
		Update(acc); err != nil {
		utils.LogError("更新账户信息失败: " + err.Error())
		return errors.New("更新消费信息失败")
	}

	return nil
}

// GetAccountByID 获取账户信息
func (m *CustomerDAO) GetAccountByID(id string) (*model.TaCustomerAccount, error) {
	acc := new(model.TaCustomerAccount)

	if _, err := m.db.Where("account_id=?", id).Get(acc); err != nil {
		return nil, err
	}

	return acc, nil
}

// GetAccountByCust 获取账户信息
func (m *CustomerDAO) GetAccountByCust(id string) (*model.TaCustomerAccount, error) {
	acc := new(model.TaCustomerAccount)

	if _, err := m.db.Where("customer_id=?", id).Get(acc); err != nil {
		return nil, err
	}

	return acc, nil
}
