import I<%= modelName %>Service from "@interfaces/service/I<%= modelName %>Service";
import I<%= modelName %>Dao from "@interfaces/dao/I<%= modelName %>Dao";
import <%= modelName %>DaoImpl from "@dao/<%= modelName %>DaoImpl";
import I<%= modelName %> from "@models/entities/<%= modelName %>";
import { ProvideSingleton, inject } from '@ioc/ioc';
import DaoError from "@common/exception/DaoError";
import ServiceError from "@common/exception/ServiceError";

/**
* Service <%= modelName %>ServiceImpl
*/
@ProvideSingleton(<%= modelName %>ServiceImpl)
export class <%= modelName %>ServiceImpl implements I<%= modelName %>Service {
  constructor(@inject(<%= modelName %>DaoImpl) private <%= modelVar %>Dao: I<%= modelName %>Dao) {}

  public async deleteById(id: string): Promise<I<%= modelName %>> {
    //Do some business logic...
    try {
      return await this.<%= modelVar %>Dao
        .deleteById(id);
    } catch (err) {
      if (err instanceof DaoError) {
        //Do the business logic error you want...
        const error = new ServiceError(err.message, err.stack);
        return Promise.reject(error);
      } else {
        return Promise.reject(new ServiceError("Unknown error"));
      }
    }
  }

  public async get(page: number, size: number): Promise<I<%= modelName %>[]>  {
    //Do some business logic...
    try {
      return await this.<%= modelVar %>Dao.get(page, size);
    } catch (err) {
      if (err instanceof DaoError) {
        //Do the business logic error you want...
        const error = new ServiceError(err.message, err.stack);
        return Promise.reject(error);
      } else {
        return Promise.reject(new ServiceError("Unknown error"));
      }
    }
  }


  public async getAll(): Promise<I<%= modelName %>[]> {
    //Do some business logic...
    try {
      return await this.<%= modelVar %>Dao
        .getAll();
    } catch (err) {
      if (err instanceof DaoError) {
        //Do the business logic error you want...
        const error = new ServiceError(err.message, err.stack);
        return Promise.reject(error);
      } else {
        return Promise.reject(new ServiceError("Unknown error"));
      }
    }
  }

  public async update(<%= modelVar %>: I<%= modelName %>): Promise<I<%= modelName %>> {
    //Do some business logic...
    try {
      return await this.<%= modelVar %>Dao
        .update(<%= modelVar %>);
    } catch (err) {
      if (err instanceof DaoError) {
        //Do the business logic error you want...
        const error = new ServiceError(err.message, err.stack);
        return Promise.reject(error);
      } else {
        return Promise.reject(new ServiceError("Unknown error"));
      }
    }
  }

  public async getById(id: string): Promise<I<%= modelName %>> {
    //Do some business logic...
    try {
      return await this.<%= modelVar %>Dao
        .getById(id);
    } catch (err) {
      if (err instanceof DaoError) {
        //Do the business logic error you want...
        const error = new ServiceError(err.message, err.stack);
        return Promise.reject(error);
      } else {
        return Promise.reject(new ServiceError("Unknown error"));
      }
    }
  }

  public async save(<%= modelVar %>: I<%= modelName %>): Promise<I<%= modelName %>> {
    //Do some business logic...
    try {
      return await this.<%= modelVar %>Dao
        .save(<%= modelVar %>);
    } catch (err) {
      if (err instanceof DaoError) {
        //Do the business logic error you want...
        const error = new ServiceError(err.message, err.stack);
        return Promise.reject(error);
      } else {
        return Promise.reject(new ServiceError("Unknown error"));
      }
    }
  }
}
