238 lines
8.9 KiB
C#
238 lines
8.9 KiB
C#
|
using Abp.Application.Services;
|
|||
|
using Abp.Domain.Repositories;
|
|||
|
using BCS.BMC.Authorization.Users;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using BCS.BMC.BMC.CompanyMasters;
|
|||
|
using Microsoft.AspNetCore.Identity;
|
|||
|
using Abp.Application.Services.Dto;
|
|||
|
using BCS.BMC.ClientMasters.Dto;
|
|||
|
using Abp.Extensions;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
using System.Security.Policy;
|
|||
|
using Abp.Collections.Extensions;
|
|||
|
using Abp.Linq.Extensions;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using BCS.BMC.Roles.Dto;
|
|||
|
using Abp.AutoMapper;
|
|||
|
using Abp.Domain.Entities;
|
|||
|
using Abp.Runtime.Session;
|
|||
|
using Abp.UI;
|
|||
|
using System.Runtime.Serialization;
|
|||
|
using BCS.BMC.CompanyMasters;
|
|||
|
using System.Net;
|
|||
|
//using System.Web.Mvc;
|
|||
|
using System.Text.Json;
|
|||
|
//using System.Web.Http;
|
|||
|
using Microsoft.AspNetCore.Mvc;
|
|||
|
using System.Web.Http;
|
|||
|
using System.Web.Mvc;
|
|||
|
using FromBodyAttribute = System.Web.Http.FromBodyAttribute;
|
|||
|
using BCS.BMC.CompanyMasters.Dto;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Net.Http.Headers;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using Abp.Json;
|
|||
|
|
|||
|
namespace BCS.BMC.CompanyMasters
|
|||
|
{
|
|||
|
public class CompanyMasterAppService : ICompanyMasterAppService
|
|||
|
{
|
|||
|
//private readonly IRepository<ClientMaster> _locationRepository;
|
|||
|
private readonly IRepository<CompanyMaster, int> _companyMaster;
|
|||
|
|
|||
|
public CompanyMasterAppService(IRepository<CompanyMaster, int> companyMaster)
|
|||
|
{
|
|||
|
_companyMaster = companyMaster;
|
|||
|
}
|
|||
|
public async Task<PagedResultDto<CompanyListDto>> GetCompanies(GetCompanyInput input)
|
|||
|
{
|
|||
|
string filter = "";
|
|||
|
if (!input.Filter.IsNullOrWhiteSpace())
|
|||
|
{
|
|||
|
filter = Regex.Replace(input.Filter.Trim(), @"[^0-9a-zA-Z\s]+", "");
|
|||
|
}
|
|||
|
try
|
|||
|
{
|
|||
|
var linqQuery = from company in _companyMaster.GetAll()
|
|||
|
select new
|
|||
|
{
|
|||
|
company,
|
|||
|
company.CompanyName,
|
|||
|
company.Url,
|
|||
|
company.DomainName,
|
|||
|
company.SubDomainName
|
|||
|
};
|
|||
|
//var query = linqQuery
|
|||
|
// .WhereIf(input.Id.HasValue && input.Id.Value > 0, x => x.zone.Location.ClientId == input.ClientId.Value);
|
|||
|
|
|||
|
var totalCount = await linqQuery.CountAsync();
|
|||
|
var items = await linqQuery.PageBy(input).ToListAsync();
|
|||
|
|
|||
|
var dtos = items.Select(c =>
|
|||
|
{
|
|||
|
var x = c.company;
|
|||
|
var dto = x.MapTo<CompanyListDto>();
|
|||
|
dto.Id = x.Id;
|
|||
|
dto.CompanyName = x.CompanyName != null ? x.CompanyName : string.Empty;
|
|||
|
dto.Url = x.Url != null ? x.Url : string.Empty;
|
|||
|
dto.DomainName = c.DomainName != null ? x.DomainName : String.Empty;
|
|||
|
dto.SubDomainName = c.SubDomainName != null ? x.SubDomainName : String.Empty;
|
|||
|
return dto;
|
|||
|
}).ToList();
|
|||
|
|
|||
|
|
|||
|
|
|||
|
return new PagedResultDto<CompanyListDto>(totalCount, dtos);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw ex;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public async Task<GetCompanyMasterForEditOutput> GetCompanyById(EntityDto input)
|
|||
|
{
|
|||
|
var entity = await _companyMaster.FirstOrDefaultAsync(x => x.Id == input.Id);
|
|||
|
|
|||
|
var dto = entity.MapTo<GetCompanyMasterForEditOutput>();
|
|||
|
//if (entity != null)
|
|||
|
dto.CompanyName = entity.CompanyName != null ? entity.CompanyName : string.Empty;
|
|||
|
dto.Url = entity.Url != null ? entity.Url : string.Empty;
|
|||
|
dto.DomainName = entity.DomainName != null ? entity.DomainName : string.Empty;
|
|||
|
dto.SubDomainName = entity.SubDomainName != null ? entity.SubDomainName : string.Empty;
|
|||
|
return dto;
|
|||
|
}
|
|||
|
public async Task<int> CreateOrUpdateCompanyMaster(CreateOrUpdateCompanyMasterInput input)
|
|||
|
{
|
|||
|
int getClientId = 0;
|
|||
|
if (input.Id > 0)
|
|||
|
{
|
|||
|
getClientId = (int)input.Id;
|
|||
|
await Update(input);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
getClientId = await Create(input);
|
|||
|
}
|
|||
|
return getClientId;
|
|||
|
}
|
|||
|
|
|||
|
protected async Task<int> Create(CreateOrUpdateCompanyMasterInput input)
|
|||
|
{
|
|||
|
int companyId = 0;
|
|||
|
|
|||
|
var company = from m in _companyMaster.GetAllList().Where(m => m.Id == input.Id && m.CompanyName.ToLower().Trim() == input.CompanyName.ToLower().Trim()
|
|||
|
&& m.Url == input.Url.Trim())
|
|||
|
select m;
|
|||
|
|
|||
|
var entity = input.MapTo<CompanyMaster>();
|
|||
|
// entity.TenantId = AbpSession.TenantId;
|
|||
|
|
|||
|
Uri uri = new Uri(input.Url, UriKind.Absolute);
|
|||
|
var host = uri.Host;
|
|||
|
|
|||
|
if (host.Split('.').Length > 2)
|
|||
|
{
|
|||
|
int lastIndex = host.LastIndexOf(".");
|
|||
|
int index = host.LastIndexOf(".", lastIndex - 1);
|
|||
|
var subdomain = host.Substring(0, index);
|
|||
|
if (subdomain != "www")
|
|||
|
{
|
|||
|
entity.SubDomainName = subdomain;
|
|||
|
}
|
|||
|
}
|
|||
|
entity.DomainName = host;
|
|||
|
if (company == null || company.Count() == 0)
|
|||
|
{
|
|||
|
companyId = await _companyMaster.InsertAndGetIdAsync(entity);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//throw new UserFriendlyException(string.Format(L("ClientDuplicateMessage"), L(input.ClientName)));
|
|||
|
throw new UserFriendlyException("Url Already Exist");
|
|||
|
}
|
|||
|
return companyId;
|
|||
|
}
|
|||
|
|
|||
|
protected async Task Update(CreateOrUpdateCompanyMasterInput input)
|
|||
|
{
|
|||
|
var entity = await _companyMaster.FirstOrDefaultAsync(x => x.Id == input.Id);
|
|||
|
if (entity == null)
|
|||
|
{
|
|||
|
//throw new UserFriendlyException(NotFoundRecord ("Name", input.ClientName));
|
|||
|
throw new UserFriendlyException("No Record Found");
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
var getclient = from m in _companyMaster.GetAllList()
|
|||
|
.Where(m => m.Id == input.Id && m.CompanyName.ToLower().Trim() == input.CompanyName.ToLower().Trim() && m.Id != input.Id)
|
|||
|
select m;
|
|||
|
input.MapTo(entity);
|
|||
|
if (getclient == null || getclient.Count() == 0)
|
|||
|
{
|
|||
|
await _companyMaster.UpdateAsync(entity);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new UserFriendlyException(string.Format(("ClientDuplicateMessage"), (input.CompanyName)));
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public async Task DeleteCompanyList(InputCompanyId Ids)
|
|||
|
{
|
|||
|
foreach (var item in Ids.Id)
|
|||
|
{
|
|||
|
await _companyMaster.DeleteAsync(item);
|
|||
|
}
|
|||
|
}
|
|||
|
public async Task DeleteCompany(EntityDto<int> input)
|
|||
|
{
|
|||
|
|
|||
|
await _companyMaster.DeleteAsync(x => x.Id == input.Id);
|
|||
|
}
|
|||
|
|
|||
|
public async Task<HttpStatusCodeResult> TokenByCompanyUrl(GetInputUrl input)
|
|||
|
{
|
|||
|
if (input.CompanyUrl == null)
|
|||
|
{
|
|||
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|||
|
}
|
|||
|
|
|||
|
var company = await _companyMaster.FirstOrDefaultAsync(x => x.Url == input.CompanyUrl.ToString());
|
|||
|
if(company == null)
|
|||
|
{
|
|||
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|||
|
}
|
|||
|
|
|||
|
using (HttpClient client = new HttpClient())
|
|||
|
{
|
|||
|
Uri uri = new Uri(input.CompanyUrl);
|
|||
|
var baseUrl = uri + "api/login";
|
|||
|
//client.BaseAddress = new Uri($"https://{uri.Host}/");
|
|||
|
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|||
|
var data = new
|
|||
|
{
|
|||
|
usernameOrEmailAddress = input.UsernameOrEmailAddress,
|
|||
|
password = input.Password
|
|||
|
};
|
|||
|
var requestJson = JsonConvert.SerializeObject(data);
|
|||
|
var requestContent = new StringContent(requestJson.ToString());
|
|||
|
requestContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json");
|
|||
|
HttpResponseMessage response = await client.PostAsync(baseUrl, requestContent);
|
|||
|
|
|||
|
response.EnsureSuccessStatusCode();
|
|||
|
if (response.IsSuccessStatusCode)
|
|||
|
{
|
|||
|
var responseStream = await response.Content.ReadAsStringAsync();
|
|||
|
return new HttpStatusCodeResult(HttpStatusCode.OK, responseStream);
|
|||
|
}
|
|||
|
}
|
|||
|
return new HttpStatusCodeResult(HttpStatusCode.OK);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|