package entity

import (
	"database/sql"
	"errors"
	"strings"

	. "app.yhyue.com/moapp/jybase/common"
	. "app.yhyue.com/moapp/jybase/date"
	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
)

var Base_resource_use = base_resource_use{}

//资源充值/消耗表
type base_resource_use struct {
	Id            int64
	Appid         string
	Account_id    int64  //账户id
	Function_code string //功能代码
	Add_count     int64  //新增数量
	Surplus_count int64  //剩余数量
	Deduct_count  int64  //扣除数量
	Create_time   string //创建时间
}

//查找最新一条的权益使用记录
func (b *base_resource_use) FindLastOne(appid, function_code string, account_id int64) (*base_resource_use, error) {
	list := Mysql_BaseService.SelectBySql(`select * from base_resource_use where appid=? and function_code=? and account_id=? order by create_time desc limit 1`, appid, function_code, account_id)
	if list == nil {
		return nil, errors.New("find error")
	}
	if len(*list) == 0 {
		return nil, nil
	}
	r, err := JsonUnmarshalByErr((*list)[0], &base_resource_use{})
	if err == nil {
		return r.(*base_resource_use), nil
	}
	return nil, err
}

//新增扣减记录
func (b *base_resource_use) Deduction(use_values []interface{}, detail_values [][]interface{}, update [][]interface{}) bool {
	return Mysql_BaseService.ExecTx("新增扣减记录", func(tx *sql.Tx) bool {
		v1, v2 := Mysql_BaseService.InsertBatchByTx(tx, "base_resource_use", []string{"appid", "account_id", "function_code", "add_count", "surplus_count", "deduct_count", "business_id", "create_time"}, use_values)
		array := []interface{}{}
		for _, v := range detail_values {
			array = append(array, v...)
			array = append(array, v2)
		}
		v3, v4 := Mysql_BaseService.InsertBatchByTx(tx, "base_resource_use_detail", []string{"appid", "power_id", "surplus_count", "deduct_count", "create_time", "use_id"}, array)
		ok := true
		if len(update) > 0 {
			ok = Mysql_BaseService.UpdateBathByTx(tx, "base_power", []string{"id", "surplus_count", "update_time"}, update) == int64(len(update))
		}
		return v1 > 0 && v2 > 0 && v3 > 0 && v4 > 0 && ok
	})
}

//新增充值记录
func (b *base_resource_use) Recharge(appid, function_code string, power_id, account_id int64, count int64, ids []string) int {
	status := 0
	Mysql_BaseService.ExecTx("新增充值记录", func(tx *sql.Tx) bool {
		power := Base_power.FindMyPowerForUpdate(power_id)
		if power == nil {
			return true
		}
		if power.Surplus_count+count > power.Use_count {
			status = -1
			return true
		}
		nowFormat := NowFormat(Date_Full_Layout)
		use_values := []interface{}{appid, account_id, function_code, count, power.Surplus_count + count, 0, strings.Join(ids, ","), nowFormat}
		v1, v2 := Mysql_BaseService.InsertBatchByTx(tx, "base_resource_use", []string{"appid", "account_id", "function_code", "add_count", "surplus_count", "deduct_count", "business_id", "create_time"}, use_values)
		use_detail_values := []interface{}{appid, v2, power_id, 0, 0, nowFormat}
		v4, v5 := Mysql_BaseService.InsertBatchByTx(tx, "base_resource_use_detail", []string{"appid", "use_id", "power_id", "surplus_count", "deduct_count", "create_time"}, use_detail_values)
		v3 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power set surplus_count=surplus_count+?,update_time=? where id=?`, count, nowFormat, power_id)
		if v1 > 0 && v2 > 0 && v3 > 0 && v4 > 0 && v5 > 0 {
			status = 1
			return true
		}
		return false
	})
	return status
}
