using System.Collections.Generic;
using System.Linq;
using Edu.Common.Enum;
using Edu.Model.Entity.Finance;
using Edu.Model.ViewModel.Finance;

namespace Edu.Repository.Finance
{
    /// <summary>
    /// 公司银行账户表仓储类
    /// </summary>
    public class RB_BackAccountRepository : BaseRepository<RB_BackAccount>
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get { return nameof(RB_BackAccount); } }

        /// <summary>
        /// 获取公司银行账户列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<RB_BackAccount_Extend> GetList(RB_BackAccount_Extend model)
        {
            string where = " WHERE 1=1 ";
            where += string.Format(" AND A." + nameof(RB_BackAccount.Status) + "={0} ", (int)DateStateEnum.Normal);
            if (model.RB_Group_Id > 0)
            {
                where += string.Format(" AND A." + nameof(RB_BackAccount.RB_Group_Id) + "={0} ", model.RB_Group_Id);
            }
            if (model.BackId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_BackAccount.BackId) + "={0} ", model.BackId);
            }
            if (!string.IsNullOrWhiteSpace(model.AccountIdStr))
            {
                where += string.Format(" AND A." + nameof(RB_BackAccount.ID) + " in({0}) ", model.AccountIdStr);
            }
            if (model.TypeId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_BackAccount.TypeId) + "={0} ", model.TypeId);
            }
            if (model.CurrencyId > 0)
            {
                where += string.Format(" AND A." + nameof(RB_BackAccount.CurrencyId) + "={0} ", model.CurrencyId);
            }
            if (!string.IsNullOrWhiteSpace(model.Alias))
            {
                where += string.Format(" AND A." + nameof(RB_BackAccount.Alias) + " like '%{0}%' ", model.Alias);
            }
            return Get<RB_BackAccount_Extend>(string.Format("SELECT A.*,B.`Name` as BackName,B.Logo as BackLogo,C.`Name` as TypeName,D.`Name` as CurrencyName,D.Rate,D.CurrentRate as CurrencyRate,D.OutCashRate as PayRate,D.ExchangeRates,D.IsStandardCurrency as IsStandardCurrency FROM {0}  as A LEFT JOIN rb_financialinstitutions AS B ON A.BackId = B.ID left JOIN RB_AccountType as c ON A.TypeId = C.ID LEFT JOIN rb_currency as D ON D.ID = A.currencyID {1}", TableName, where)).ToList();
        }
    }
}