package entity import ( "database/sql" "errors" . "app.yhyue.com/moapp/jybase/common" . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db" ) var Base_resource_use = base_resource_use{} //资源充值/消耗表 type base_resource_use struct { Id int64 Appid string Ent_id int64 //企业id User_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, user_id, ent_id, power_type int64) (*base_resource_use, error) { query := `select * from base_resource_use where appid=? and function_code=?` args := []interface{}{appid, function_code} if power_type == 1 { query += ` and user_id=?` args = append(args, user_id) } else if power_type == 2 { query += ` and ent_id=?` args = append(args, ent_id) } else { return nil, errors.New("power_type error") } query += ` order by create_time desc limit 1` list := Mysql_BaseService.SelectBySql(query, args...) 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", "ent_id", "user_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 }) }