using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Novag.Traiding.Dal.Interfaces;
using AutoMapper;
namespace Traiding.Dal.SubSonicDao.Impl
{
public class JsonDbManager : IJsonDbManager
{
#region static
static JsonDbManager()
{
}
#endregion
#region IJsonDbManager Members
public IList<JsonCompanyModel> FindCompanies(int? pageId, int? countPerPage)
{
List<JsonCompanyModel> result = new List<JsonCompanyModel>();
#region список компаний
SubSonic.SqlQuery query = new SubSonic.Select().From(NtrJsonCompany.Schema.TableName);
if (pageId.HasValue && countPerPage.HasValue)
query.Paged(pageId.Value, countPerPage.Value);
NtrJsonCompanyCollection dbModelCollection = new NtrJsonCompanyCollection();
dbModelCollection.Load(query.ExecuteReader());
foreach (NtrJsonCompany someDbModel in dbModelCollection)
{
JsonCompanyModel newModel = new JsonCompanyModel();
newModel.Id = someDbModel.Id;
#region локализация
NtrJsonCompanyLocalizationCollection colLoc = new NtrJsonCompanyLocalizationCollection();
colLoc.Load(NtrJsonCompanyLocalization.FetchByParameter(NtrJsonCompanyLocalization.Columns.CompanyId, someDbModel.Id));
NtrJsonCompanyLocalization ru = colLoc.First(el => el.LocaleId == 1);
NtrJsonCompanyLocalization en = colLoc.First(el => el.LocaleId == 2);
newModel.Name = String.Format("{0} / {1}",
ru != null ? ru.CompanyName : String.Empty,
en != null ? en.CompanyName : String.Empty);
newModel.INN = ru.CompanyINN;
newModel.KPP = ru.CompanyKPP;
#endregion
result.Add(newModel);
}
#endregion
return result;
}
public IList<JsonCompanyModel> FindCompaniesPageByTradeGroupId(int pageIndex, int countPerPage,
bool needTotalRecordCount, out int totalRecordCount,
QueryPageSettings querySettings, int tradeGroupId)
{
List<JsonCompanyModel> result = new List<JsonCompanyModel>();
CompanyDbManager companyDbManager = new CompanyDbManager();
IList<CompanyModel> dbResult = companyDbManager.FindPageByTradeGroupId(
pageIndex, countPerPage,
needTotalRecordCount,
out totalRecordCount,
querySettings,
tradeGroupId);
foreach (CompanyModel someModel in dbResult)
{
JsonCompanyModel newJsonModel = new JsonCompanyModel();
newJsonModel.Id = someModel.Id;
CompanyLocalizationModel ru = someModel.Locales.First(el => el.LocaleId == 1);
CompanyLocalizationModel en = someModel.Locales.First(el => el.LocaleId == 2);
newJsonModel.Name = String.Format("{0} / {1}",
ru != null ? ru.CompanyName : String.Empty,
en != null ? en.CompanyName : String.Empty);
newJsonModel.INN = ru.CompanyINN;
newJsonModel.KPP = ru.CompanyKPP;
result.Add(newJsonModel);
}
return result;
}
#endregion
#region IJsonDbManager Members
public JsonCompanyModel FindCompanyById(int companyId, QueryPageSettings querySettings)
{
JsonCompanyModel result = null;
CompanyDbManager companyDbManager = new CompanyDbManager();
CompanyModel someModel = companyDbManager.Read(companyId, querySettings);
if (someModel != null)
{
result = new JsonCompanyModel();
result.Id = someModel.Id;
CompanyLocalizationModel ru = someModel.Locales.First(el => el.LocaleId == 1);
CompanyLocalizationModel en = someModel.Locales.First(el => el.LocaleId == 2);
result.Name = String.Format("{0} / {1}",
ru != null ? ru.CompanyName : String.Empty,
en != null ? en.CompanyName : String.Empty);
result.INN = ru.CompanyINN;
result.KPP = ru.CompanyKPP;
}
return result;
}
#endregion
#region IJsonDbManager Members
public IList<JsonGoodModel> FindGoodsByCompanyId(int pageIndex, int countPerPage,
bool needTotalRecordCount, out int totalRecordCount,
QueryPageSettings querySettings, int companyId)
{
List<JsonGoodModel> result = new List<JsonGoodModel>();
GoodDbManager goodManager = new GoodDbManager();
IList<GoodModel> dbResult = goodManager.FindByCompanyId(pageIndex, countPerPage,
needTotalRecordCount,
out totalRecordCount,
querySettings,
companyId);
foreach (GoodModel someModel in dbResult)
{
JsonGoodModel newJsonModel = new JsonGoodModel();
newJsonModel.Id = someModel.Id;
GoodLocalizationModel ru = someModel.Locales.First(el => el.LocaleId == 1);
GoodLocalizationModel en = someModel.Locales.First(el => el.LocaleId == 2);
newJsonModel.Name = String.Format("{0} / {1}",
ru != null ? ru.Marking : String.Empty,
en != null ? en.Marking : String.Empty);
newJsonModel.Measure = String.Format("{0} / {1}",
ru != null ? ru.Measure : String.Empty,
en != null ? en.Measure : String.Empty);
result.Add(newJsonModel);
}
return result;
}
#endregion
}
}