//! Generated admin service skeleton.

use anyhow::Context;
use summer_common::error::{ApiErrors, ApiResult};
use summer_model::dto::{{ names.table_module }}::{ {{ names.create_dto }}, {{ names.query_dto }}, {{ names.update_dto }} };
use summer_model::entity::{{ names.table_module }};
use summer_model::vo::{{ names.table_module }}::{ {{ names.detail_vo }}, {{ names.vo }} };
use sea_orm::{ActiveModelTrait, EntityTrait, QueryFilter, QueryOrder};
use summer::plugin::Service;
use summer_sea_orm::DbConn;
use summer_sea_orm::pagination::{Page, Pagination, PaginationExt};
{% for import in imports.service %}
use {{ import }};
{% endfor %}

#[derive(Clone, Service)]
pub struct {{ names.service_struct }} {
    #[inject(component)]
    db: DbConn,
}

impl {{ names.service_struct }} {
    pub async fn list(&self, query: {{ names.query_dto }}, pagination: Pagination) -> ApiResult<Page<{{ names.vo }}>> {
        let page = {{ names.table_module }}::Entity::find()
            .filter(query)
{% for order_column in list_order_columns %}
            .order_by_desc({{ names.table_module }}::Column::{{ order_column }})
{% endfor %}
            .page(&self.db, &pagination)
            .await
            .context("查询{{ table.label }}列表失败")?;

        Ok(page.map({{ names.vo }}::from))
    }

    pub async fn get_by_id(&self, id: {{ primary_key.type_info.rust_input }}) -> ApiResult<{{ names.detail_vo }}> {
        let model = {{ names.table_module }}::Entity::find_by_id(id)
            .one(&self.db)
            .await
            .context("查询{{ table.label }}详情失败")?
            .ok_or_else(|| ApiErrors::NotFound("{{ table.label }}不存在".to_string()))?;

        Ok({{ names.detail_vo }}::from(model))
    }

    pub async fn create(&self, dto: {{ names.create_dto }}) -> ApiResult<()> {
        let active: {{ names.table_module }}::ActiveModel = dto.into();
        active.insert(&self.db).await.context("创建{{ table.label }}失败")?;
        Ok(())
    }

    pub async fn update(&self, id: {{ primary_key.type_info.rust_input }}, dto: {{ names.update_dto }}) -> ApiResult<()> {
        let model = {{ names.table_module }}::Entity::find_by_id(id)
            .one(&self.db)
            .await
            .context("查询{{ table.label }}详情失败")?
            .ok_or_else(|| ApiErrors::NotFound("{{ table.label }}不存在".to_string()))?;

        let mut active: {{ names.table_module }}::ActiveModel = model.into();
        dto.apply_to(&mut active);
        active.update(&self.db).await.context("更新{{ table.label }}失败")?;
        Ok(())
    }

    pub async fn delete(&self, id: {{ primary_key.type_info.rust_input }}) -> ApiResult<()> {
        let result = {{ names.table_module }}::Entity::delete_by_id(id)
            .exec(&self.db)
            .await
            .context("删除{{ table.label }}失败")?;

        if result.rows_affected == 0 {
            return Err(ApiErrors::NotFound("{{ table.label }}不存在".to_string()));
        }

        Ok(())
    }
}
