package cms

import (
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/smsmsg"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/business/partner"
	"git.rosy.net.cn/jx-callback/globals"
	"git.rosy.net.cn/jx-callback/globals/api"
)

type StoreAcctManager struct {
}

var (
	FixedStoreAcctManager *StoreAcctManager
)

func init() {
	FixedStoreAcctManager = &StoreAcctManager{}
	partner.InitStoreAcctManager(FixedStoreAcctManager)
}

func InsertStoreAcctIncome(ctx *jxcontext.Context, storeID, price, acctType int, vendorOrderID, vendorWaybillId string, expendID int) (err error) {
	var (
		userID, userName   string
		goodsVendorOrderID string
		db                 = dao.GetDB()
	)
	if ctx != nil {
		userID = ctx.GetUserID()
		userName = ctx.GetUserName()
		goodsVendorOrderID = vendorOrderID
	} else {
		storeOrder := &model.StoreAcctOrder{
			VendorOrderID: vendorOrderID,
		}
		if err = dao.GetEntity(db, storeOrder, "VendorOrderID"); err == nil && storeOrder.ID != 0 {
			userID = storeOrder.UserID
			userName = storeOrder.LastOperator
			goodsVendorOrderID = storeOrder.GoodsVendorOrderID
		}
	}
	storeAcctIncome := &model.StoreAcctIncome{
		StoreID:         storeID,
		IncomePrice:     price,
		Type:            acctType,
		UserID:          userID,
		VendorOrderID:   goodsVendorOrderID,
		VendorWaybillID: vendorWaybillId,
	}
	dao.WrapAddIDCULEntity(storeAcctIncome, userName)
	if expendID != 0 {
		storeAcctIncome.ExpID = expendID
	}
	err = dao.CreateEntity(db, storeAcctIncome)
	return err
}

func InsertStoreAcctExpend(ctx *jxcontext.Context, storeID, price, acctType int, vendorOrderID, vendorWaybillId string, expendID int) (err error) {
	var (
		userID, userName string
		db               = dao.GetDB()
	)
	if ctx != nil {
		userID = ctx.GetUserID()
		userName = ctx.GetUserName()
	} else {
		storeOrder := &model.StoreAcctOrder{
			VendorOrderID: vendorOrderID,
		}
		if err = dao.GetEntity(db, storeOrder, "VendorOrderID"); err == nil && storeOrder.ID != 0 {
			userID = storeOrder.UserID
			userName = storeOrder.LastOperator
		}
	}
	storeAcctExpend := &model.StoreAcctExpend{
		StoreID:         storeID,
		ExpendPrice:     price,
		Type:            acctType,
		UserID:          userID,
		VendorOrderID:   vendorOrderID,
		VendorWaybillID: vendorWaybillId,
	}
	dao.WrapAddIDCULEntity(storeAcctExpend, userName)
	if expendID != 0 {
		storeAcctExpend.ExpID = expendID
	}
	err = dao.CreateEntity(db, storeAcctExpend)
	return err
}

func (s *StoreAcctManager) UpdateStoreAcctBalance(ctx *jxcontext.Context, storeID, price int, isIncome bool) (err error) {
	var (
		db = dao.GetDB()
	)
	if ctx == nil {
		ctx = jxcontext.AdminCtx
	}
	storeAcct := &model.StoreAcct{
		StoreID: storeID,
	}
	txDB, _ := dao.Begin(db)
	defer func() {
		if r := recover(); r != nil || err != nil {
			dao.Rollback(db, txDB)
			if r != nil {
				panic(r)
			}
		}
	}()
	if err = dao.GetEntity(db, storeAcct, "StoreID"); err != nil && dao.IsNoRowsError(err) {
		//新增门店账单
		dao.WrapAddIDCULEntity(storeAcct, ctx.GetUserName())
		if err = dao.CreateEntityTx(txDB, storeAcct); err != nil {
			dao.Rollback(db, txDB)
			return err
		}
	} else {
		if isIncome {
			storeAcct.AccountBalance += price
		} else {
			storeAcct.AccountBalance -= price
		}
		if _, err = dao.UpdateEntityTx(txDB, storeAcct, "AccountBalance"); err != nil {
			dao.Rollback(db, txDB)
			return err
		}
	}
	dao.Commit(db, txDB)
	return err
}

// 门店到账(老版本,修改门店的交易记录)
func (s *StoreAcctManager) InsertStoreAcctExpendAndUpdateStoreAcctBalance(ctx *jxcontext.Context, storeID, price, acctType int, vendorOrderID, vendorWaybillID string, expendID int) (err error) {
	utils.CallFuncAsync(func() {
		if err = InsertStoreAcctExpend(ctx, storeID, price, acctType, vendorOrderID, vendorWaybillID, expendID); err == nil {
			s.UpdateStoreAcctBalance(ctx, storeID, price, false)
		}
	})
	return err
}

func (s *StoreAcctManager) InsertStoreAcctIncomeAndUpdateStoreAcctBalance(ctx *jxcontext.Context, storeID, price, acctType int, vendorOrderID, vendorWaybillID string, expendID int) (err error) {
	utils.CallFuncAsync(func() {
		if err = InsertStoreAcctIncome(ctx, storeID, price, acctType, vendorOrderID, vendorWaybillID, expendID); err == nil {
			s.UpdateStoreAcctBalance(ctx, storeID, price, true)
		}
	})
	return err
}

// 检查当前订单的状态以及支出收入比
func (s *StoreAcctManager) CheckStoreAcctExpendExist(vendorOrderID string) (isEqual, isZero bool, err error) {
	var (
		expends, incomes int
		db               = dao.GetDB()
	)
	// 当前订单的支出金额(临时运费+真实运费)[小费] partner.StoreAcctTypeExpendCreateWaybillDeductFee 违约金不参与计算,本来就要扣除
	expends, err = dao.GetStoreAcctExpendTotal(db, 0, []int{partner.StoreAcctTypeExpendCreateWaybillEx, partner.StoreAcctTypeExpendCreateWaybillTip, partner.StoreAcctTypeExpendCreateWaybill2ndMore, partner.StoreAcctTypeRealFeeExpend}, vendorOrderID, utils.ZeroTimeValue, utils.ZeroTimeValue)
	// 当前订单的退费金额()
	incomes, err = dao.GetStoreAcctIncomeTotal(db, 0, []int{partner.StoreAcctTypeRealFeeIncome, partner.StoreAcctTypeIncomeCancelTemp, partner.StoreAcctTypeIncomeCancelReal}, vendorOrderID, utils.ZeroTimeValue, utils.ZeroTimeValue)
	if expends != incomes {
		if expends > incomes {
			return false, false, err
		} else {
			globals.SugarLogger.Debugf("CheckStoreAcctExpendExist 收入大于支出！ orderID:[%v]", vendorOrderID)
		}
	} else {
		if expends == 0 && incomes == 0 {
			return true, true, err
		} else {
			return true, false, err
		}
	}
	return false, false, err
}

func (s *StoreAcctManager) GetStoreAcctExpendLastCreateWayBillFee(vendorOrderID string) (expend *dao.GetStoreAcctExpendLastCreateWayBillFeeResult, lastFee int, err error) {
	return dao.GetStoreAcctExpendLastCreateWayBillFee(dao.GetDB(), vendorOrderID)
}

func (s *StoreAcctManager) GetBrandBalance(brandID int) (balance int, err error) {
	return dao.GetBrandBalance(dao.GetDB(), brandID)
}

func (s *StoreAcctManager) InsertBrandBill(ctx *jxcontext.Context, brandID, price, billType, feeType int, vendorOrderID string, wayVendorOrderId string) (err error) {
	utils.CallFuncAsync(func() {
		var (
			db = dao.GetDB()
		)
		if price == 0 {
			return
		}
		brandBill := &model.BrandBill{
			BrandID:       brandID,
			Price:         price,
			BillType:      billType,
			FeeType:       feeType,
			VendorOrderID: vendorOrderID,
			OrderID:       wayVendorOrderId,
		}
		dao.WrapAddIDCULEntity(brandBill, ctx.GetUserName())
		//扣除后，如果余额小于10元要发消息通知 ,每天通知一次
		if err = dao.CreateEntity(db, brandBill); err == nil {
			if mark := api.Cacher.Get("brandID" + utils.Int2Str(brandID)); mark == nil {
				if balance, _ := s.GetBrandBalance(brandID); balance < model.BrandBalanceLimit {
					smsmsg.NotifyBrandBalance(brandID)
				}
			}
		}
	})
	return err
}
