Skip to content

Commit

Permalink
add prize item cdk
Browse files Browse the repository at this point in the history
  • Loading branch information
easy-do committed Oct 20, 2024
1 parent 5b0e72f commit 452bf8c
Show file tree
Hide file tree
Showing 31 changed files with 484 additions and 171 deletions.
16 changes: 15 additions & 1 deletion rust-src/api/src/controller/live_prize_pool_item_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use common::error::MyError;
use common::r::JsonResult;
use common::state::AppState;
use entity::live_prize_pool_item;
use model::prize::LivePrizePoolItemPage;
use model::prize::{ImportPoolItemCdk, LivePrizePoolItemPage};
use service::live_prize_pool_item_service;

#[get("/api/live_prize_pool_item/list")]
Expand Down Expand Up @@ -49,3 +49,17 @@ async fn delete(app_state: web::Data<AppState>,
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}

#[post("/api/live_prize_pool_item/import_cdk")]
async fn import_cdk(app_state: web::Data<AppState>,
params: web::Json<ImportPoolItemCdk>, ) -> Result<HttpResponse, MyError> {
let res = live_prize_pool_item_service::import_cdk(&app_state.db, params.into_inner()).await?;
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}

#[get("/api/live_prize_pool_item/clean_cdk/{live_id}/{item_id}")]
async fn clean_cdk(app_state: web::Data<AppState>,
params: web::Path<(i64, i64)>, ) -> Result<HttpResponse, MyError> {
let param = params.into_inner();
let res = live_prize_pool_item_service::clean_cdk(&app_state.db, param.0,param.1).await?;
Ok(HttpResponse::Ok().json(JsonResult::ok(res)))
}
4 changes: 3 additions & 1 deletion rust-src/api/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ pub fn live_prize_pool_item_routes(cfg: &mut web::ServiceConfig) {
.service(live_prize_pool_item_controller::info)
.service(live_prize_pool_item_controller::page)
.service(live_prize_pool_item_controller::delete)
.service(live_prize_pool_item_controller::add);
.service(live_prize_pool_item_controller::add)
.service(live_prize_pool_item_controller::import_cdk)
.service(live_prize_pool_item_controller::clean_cdk);
}

pub fn live_prize_pool_routes(cfg: &mut web::ServiceConfig) {
Expand Down
2 changes: 1 addition & 1 deletion rust-src/entity/src/live_prize_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct TopDraw {
pub user_name: Option<String>,
}

#[derive(FromQueryResult,Serialize, Deserialize)]
#[derive(Clone, FromQueryResult,Serialize, Deserialize)]
pub struct DrawHistory {
pub action: Option<i64>,
pub user_id: Option<i64>,
Expand Down
15 changes: 15 additions & 0 deletions rust-src/entity/src/live_prize_pool_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Model {
pub level_name: Option<String>,
pub probability: Option<String>,
pub remaining_quantity: Option<i32>,
pub cdk_quantity: Option<i32>,
pub status: Option<bool>,
pub guarantees: Option<bool>,
pub create_time: Option<DateTime>,
Expand All @@ -40,4 +41,18 @@ pub struct PoolItemList {
pub level_name: Option<String>,
pub icon: Option<String>,
pub remaining_quantity: Option<i32>,
}

#[derive(Clone,FromQueryResult,Serialize, Deserialize)]
pub struct DrawPoolItemList {
pub id: i64,
pub live_id: Option<i64>,
pub prize_id: Option<i64>,
pub prize_name: Option<String>,
pub level: Option<i32>,
pub level_name: Option<String>,
pub icon: Option<String>,
pub remaining_quantity: Option<i32>,
pub cdk_id: Option<i64>,
pub cdk: Option<String>,
}
22 changes: 22 additions & 0 deletions rust-src/entity/src/live_prize_pool_item_cdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "live_prize_pool_item_cdk")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
pub live_id: Option<i64>,
pub prize_id: Option<i64>,
pub cdk: Option<String>,
pub status: Option<bool>,
pub create_time: Option<DateTime>,
pub update_time: Option<DateTime>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
3 changes: 2 additions & 1 deletion rust-src/entity/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pub mod live_prize_pool;
pub mod live_prize_pool_item;
pub mod live_prize_history;
pub mod cdk;
pub mod live_prize_pool_user;
pub mod live_prize_pool_user;
pub mod live_prize_pool_item_cdk;
2 changes: 1 addition & 1 deletion rust-src/entity/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ pub use super::role::Entity as Role;
pub use super::role_resource::Entity as RoleResource;
pub use super::user::Entity as User;
pub use super::user_role::Entity as UserRole;

pub use super::live_prize_pool_item_cdk::Entity as LivePrizePoolItemCdk;
3 changes: 2 additions & 1 deletion rust-src/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod m20240722_000001_create_table;
mod m20240805_000001_create_table;
mod m20241015_000001_create_table;
mod m20241015_000002_create_table;

mod m20241019_000001_create_table;

pub struct Migrator;

Expand All @@ -45,6 +45,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240805_000001_create_table::Migration),
Box::new(m20241015_000001_create_table::Migration),
Box::new(m20241015_000002_create_table::Migration),
Box::new(m20241019_000001_create_table::Migration),
]
}
}
61 changes: 61 additions & 0 deletions rust-src/migration/src/m20241019_000001_create_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(LivePrizePoolItemCdk::Table)
.if_not_exists()
.comment("活动奖池奖品CDK信息")
.col(
ColumnDef::new(LivePrizePoolItemCdk::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(LivePrizePoolItemCdk::LiveId).big_integer().comment("活动奖池ID"))
.col(ColumnDef::new(LivePrizePoolItemCdk::PrizeId).big_integer().comment("奖品ID"))
.col(ColumnDef::new(LivePrizePoolItemCdk::Cdk).string().char_len(64).comment("CDK"))
.col(ColumnDef::new(LivePrizePoolItemCdk::Status).boolean().default(1).comment("状态"))
.col(ColumnDef::new(LivePrizePoolItemCdk::CreateTime).date_time().comment("创建时间"))
.col(ColumnDef::new(LivePrizePoolItemCdk::UpdateTime).date_time().comment("更新时间"))
.to_owned(),
)
.await?;
manager.alter_table(
Table::alter()
.table(LivePrizePoolItem::Table)
.add_column(ColumnDef::new(LivePrizePoolItem::CdkQuantity).big_integer().default(0).comment("CDK数量"))
.to_owned()
).await?;
Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}

#[derive(DeriveIden)]
enum LivePrizePoolItemCdk {
Table,
Id,
LiveId,
PrizeId,
Cdk,
Status,
CreateTime,
UpdateTime,
}

#[derive(DeriveIden)]
enum LivePrizePoolItem {
Table,
CdkQuantity,
}
7 changes: 7 additions & 0 deletions rust-src/model/src/prize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,10 @@ pub struct UserDrawHistoryPage {
pub live_id: Option<i64>,
pub create_time: Option<Vec<String>>,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ImportPoolItemCdk {
pub live_id: i64,
pub prize_id: i64,
pub cdk: Vec<String>,
}
2 changes: 1 addition & 1 deletion rust-src/security/src/wtt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn has_permission(req: &ServiceRequest) -> Result<bool, MyError> {
} else {
let empty_method_conf = &auth_conf.empty_method;
//如果没有根据请求方法鉴权的规则就遍历所有规则
for api_auth_path_conf_model in empty_method_conf {
while let Some(api_auth_path_conf_model) = empty_method_conf {
//路径匹配
let api_path_check_res = api_path_check(request_path, Some(api_auth_path_conf_model), token_auth_cache)?;
//表达式匹配
Expand Down
16 changes: 14 additions & 2 deletions rust-src/service/src/live_prize_pool_item_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use sea_orm::DbConn;
use common::error::MyError;
use common::page::PageResult;
use entity::live_prize_pool_item;
use model::prize::LivePrizePoolItemPage;
use model::prize::{ImportPoolItemCdk, LivePrizePoolItemPage};

use crate::manager::live_prize_pool_item_manager;
use crate::manager::{live_prize_pool_item_cdk_manager, live_prize_pool_item_manager};

pub async fn list(db: &DbConn) -> Result<Vec<live_prize_pool_item::Model>, MyError> {
Ok(live_prize_pool_item_manager::get_prize_pool_item_list(db).await?)
Expand All @@ -32,3 +32,15 @@ pub async fn add(db: &DbConn, live_id: i64, item_id: i64) -> Result<i64, MyError
pub async fn delete(db: &DbConn, id: i64) -> Result<bool, MyError> {
Ok(live_prize_pool_item_manager::delete(db, id).await?)
}

pub async fn clean_cdk(db: &DbConn, live_id: i64, prize_id: i64) -> Result<bool, MyError> {
live_prize_pool_item_cdk_manager::clean_cdk(db, live_id,prize_id).await?;
live_prize_pool_item_manager::update_cdk_quantity(db, live_id, prize_id, 0).await?;
Ok(true)
}

pub async fn import_cdk(db: &DbConn, form: ImportPoolItemCdk) -> Result<i32, MyError> {
let res= live_prize_pool_item_cdk_manager::import_cdk(db, form.clone()).await?;
live_prize_pool_item_manager::update_cdk_quantity(db, form.live_id, form.prize_id, res).await?;
Ok(res)
}
26 changes: 23 additions & 3 deletions rust-src/service/src/live_prize_pool_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use entity::{live_prize_history, live_prize_pool, live_prize_pool_item, live_pri
use model::prize::{LivePrizePoolPage, PoolDrawCount};
use security::state::AuthState;

use crate::manager::{live_prize_history_manager, live_prize_pool_item_manager, live_prize_pool_manager, live_prize_pool_user_manager};
use crate::manager::{live_prize_history_manager, live_prize_pool_item_cdk_manager, live_prize_pool_item_manager, live_prize_pool_manager, live_prize_pool_user_manager};

pub async fn list(db: &DbConn) -> Result<Vec<live_prize_pool::Model>, MyError> {
Ok(live_prize_pool_manager::get_live_prize_pool_list(db).await?)
Expand All @@ -36,7 +36,7 @@ pub async fn page(db: &DbConn, page: LivePrizePoolPage) -> Result<PageResult<liv
live_prize_pool_manager::page(db, page).await
}

pub async fn draw(db: &DbConn, live_id: i64, draw_num: i32, token: &str, req: HttpRequest) -> Result<Vec<live_prize_pool_item::PoolItemList>, MyError> {
pub async fn draw(db: &DbConn, live_id: i64, draw_num: i32, token: &str, req: HttpRequest) -> Result<Vec<live_prize_pool_item::DrawPoolItemList>, MyError> {
let auth_state = req.app_data::<Arc<Mutex<AuthState>>>().ok_or(MyError::ServerError("get auth_state fail".to_string()))?;
let auth_state = auth_state.lock().unwrap();
let uid = auth_state.token_auth_cache.get(token).ok_or(MyError::UnauthorizedError("no auth cache".to_string()))?.uid;
Expand Down Expand Up @@ -145,10 +145,14 @@ pub async fn draw(db: &DbConn, live_id: i64, draw_num: i32, token: &str, req: Ht
}
live_prize_pool_item_manager::update_remaining_quantity(db, item_map).await?;



//抽奖结果
let mut result = Vec::new();
for item in &draw_item {
result.push(live_prize_pool_item::PoolItemList {
//消费cdk
let cdk = live_prize_pool_item_cdk_manager::consumer_cdk(db,live_id,item.prize_id).await?;
result.push(live_prize_pool_item::DrawPoolItemList {
id: item.id.clone(),
live_id: None,
prize_id: item.prize_id,
Expand All @@ -157,6 +161,22 @@ pub async fn draw(db: &DbConn, live_id: i64, draw_num: i32, token: &str, req: Ht
level_name: item.level_name.clone(),
icon: item.icon.clone(),
remaining_quantity: None,
cdk_id: match cdk.clone() {
None => {
None
}
Some(c) => {
Option::from(c.id)
}
},
cdk: match cdk.clone() {
None => {
None
}
Some(c) => {
Option::from(c.cdk)
}
},
});
}

Expand Down
Loading

0 comments on commit 452bf8c

Please sign in to comment.