using DocumentFormat.OpenXml.Office2010.Excel;
using Hotline.Caching.Interfaces;
using Hotline.File;
using Hotline.Repository.SqlSugar.Extensions;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Share.Tools;
using Hotline.Snapshot;
using Hotline.Snapshot.Interfaces;
using Hotline.Tools;
using Mapster;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using XF.Domain.Dependency;
using XF.Domain.Exceptions;
namespace Hotline.Application.Snapshot;
public class IndustryApplication : IIndustryApplication, IScopeDependency
{
private readonly IIndustryRepository _industryRepository;
private readonly IFileRepository _fileRepository;
private readonly ISystemSettingCacheManager _sysSetting;
private readonly IIndustryCaseRepository _industryCaseRepository;
private readonly ISnapshotSMSTemplateRepository _snapshotSMSTemplateRepository;
private readonly IPractitionerRepository _practitionerRepository;
public IndustryApplication(IIndustryRepository industryRepository, IFileRepository fileRepository, ISystemSettingCacheManager sysSetting, IIndustryCaseRepository industryCaseRepository, ISnapshotSMSTemplateRepository snapshotSMSTemplateRepository, IPractitionerRepository practitionerRepository)
{
_industryRepository = industryRepository;
_fileRepository = fileRepository;
_sysSetting = sysSetting;
_industryCaseRepository = industryCaseRepository;
_snapshotSMSTemplateRepository = snapshotSMSTemplateRepository;
_practitionerRepository = practitionerRepository;
}
///
/// 新增行业
///
///
///
///
public async Task AddIndustryAsync(AddIndustryDto dto, CancellationToken cancellationToken)
{
var entity = dto.Adapt();
var id = await _industryRepository.AddAsync(entity, cancellationToken);
if (dto.Files.NotNullOrEmpty())
{
var fileEntities = dto.Files.Adapt>();
fileEntities.ForEach(m => m.Key = id);
await _fileRepository.AddRangeAsync(fileEntities, cancellationToken);
}
return id;
}
public ISugarQueryable GetIndustres(IndustryListInDto dto)
{
var query = _industryRepository.Queryable()
.WhereIF(dto.Name.NotNullOrEmpty(), m => m.Name.Contains(dto.Name))
.WhereIF(dto.ApproveOrgName.NotNullOrEmpty(), m => m.ApproveOrgName.Contains(dto.ApproveOrgName))
.Select();
return query;
}
public async Task GetIndustryDetailAsync(string id)
{
var fileServiceUrl = _sysSetting.FileServerUrl;
var fileDownloadApi = fileServiceUrl + _sysSetting.FileDownloadApi;
var industry = await _industryRepository.GetAsync(id);
var files = await _fileRepository.GetByKeyAsync(id, CancellationToken.None);
var outDto = industry.Adapt();
outDto.Files = files.Adapt>();
if (outDto.BackgroundImgUrl.NotNullOrEmpty())
outDto.BackgroundImgUrl = fileDownloadApi + outDto.BackgroundImgUrl;
if (outDto.BannerImgUrl.NotNullOrEmpty())
outDto.BannerImgUrl = fileDownloadApi + outDto.BannerImgUrl;
if (outDto.CareCellImgUrl.NotNullOrEmpty())
outDto.CareCellImgUrl = fileDownloadApi + outDto.CareCellImgUrl;
if (outDto.CellImgUrl.NotNullOrEmpty())
outDto.CellImgUrl = fileDownloadApi + outDto.CellImgUrl;
return outDto;
}
///
/// 修改行业
///
///
///
///
///
public async Task UpdateIndustryAsync(UpdateIndustryInDto dto, CancellationToken requestAborted)
{
dto.ValidateObject();
var entity = await _industryRepository.GetAsync(dto.Id) ?? throw UserFriendlyException.SameMessage($"行业不存在 {dto.Id}");
if (dto.Files.NotNullOrEmpty())
{
var fileEntities = dto.Files.Adapt>();
fileEntities.ForEach(m => m.Key = entity.Id);
await _fileRepository.Removeable().Where(m => m.Key == entity.Id).ExecuteCommandAsync(requestAborted);
await _fileRepository.AddRangeAsync(fileEntities, requestAborted);
}
dto.Adapt(entity);
await _industryRepository.UpdateAsync(entity, requestAborted);
}
#region 行业线索
public ISugarQueryable GetIndustryCaseItems(IndustryCaseItemInDto dto)
{
var query = _industryCaseRepository.Queryable()
.LeftJoin((c, i) => c.IndustryId == i.Id)
.WhereIF(dto.IndustryName.NotNullOrEmpty(), (c, i) => i.Name.Contains(dto.IndustryName))
.WhereIF(dto.CaseName.NotNullOrEmpty(), (c, i) => c.Name.Contains(dto.CaseName))
.Select((c, i) =>
new IndustryCaseItemOutDto {
Id = c.Id,
Name = c.Name,
IndustryId = i.Id,
IndustryName = i.Name}, true);
return query;
}
///
/// 添加行业线索
///
///
///
public async Task AddIndustryCaseAsync(AddIndustryCaseDto dto)
{
dto.ValidateObject();
var entity = dto.Adapt();
entity.CitizenReadPackAmount = entity.CitizenReadPackAmount * 100;
entity.GuiderReadPackAmount = entity.GuiderReadPackAmount * 100;
return await _industryCaseRepository.AddAsync(entity);
}
///
/// 修改行业线索
///
///
///
public async Task UpdateIndustryCaseAsync(UpdateIndustryCaseDto dto)
{
dto.ValidateObject();
var entity = await _industryCaseRepository.GetAsync(dto.Id)
?? throw UserFriendlyException.SameMessage($"行业线索不存在 {dto.Id}");
dto.Adapt(entity);
await _industryCaseRepository.UpdateAsync(entity);
}
public async Task GetIndustryCaseAsync(string caseId)
{
return await _industryCaseRepository.GetAsync(caseId);
}
#endregion
#region 行业短信模板
///
/// 行业模板集合
///
///
///
public ISugarQueryable GetSMSTemplates(SnapshotSMSTemplateItemsInDto dto)
{
var query = _snapshotSMSTemplateRepository.Queryable()
.LeftJoin((s, i) => s.IndustryId == i.Id)
.WhereIF(dto.IndustryName.NotNullOrEmpty(), (s, i) => i.Name.Contains(dto.IndustryName))
.Select();
return query;
}
///
/// 添加行业模板
///
///
///
public async Task AddSMSTemplateAsync(AddSnapshotSMSTemplateInDto dto)
{
dto.ValidateObject();
var entity = dto.Adapt();
return await _snapshotSMSTemplateRepository.AddAsync(entity);
}
///
/// 修改行业模板
///
///
///
public async Task UpdateSMSTemplateAsync(UpdateSnapshotSMSTemplateInDto dto)
{
dto.ValidateObject();
var entity = await _snapshotSMSTemplateRepository.GetAsync(dto.Id)
?? throw UserFriendlyException.SameMessage($"行业短信模板不存在 {dto.Id}");
dto.Adapt(entity);
await _snapshotSMSTemplateRepository.UpdateAsync(entity);
}
///
/// 短信详情
///
///
///
public async Task GetSMSTemplateDetailAsync(string id)
{
return await _snapshotSMSTemplateRepository.Queryable()
.LeftJoin((s, i) => s.IndustryId == i.Id)
.Select()
.FirstAsync();
}
#endregion
#region 区域从业人员
///
/// 区域从业人员集合
///
///
///
///
public ISugarQueryable GetPractitionerItemsAsync(PractitionerItemsInDto dto)
{
var query = _practitionerRepository.Queryable()
.WhereIF(dto.Name.NotNullOrEmpty(), m => m.Name.Contains(dto.Name!))
.WhereIF(dto.SystemAreaName.NotNullOrEmpty(), m => m.SystemAreaName.Contains(dto.SystemAreaName!))
.WhereIF(dto.PhoneNumber.NotNullOrEmpty(), m => m.PhoneNumber.Contains(dto.PhoneNumber!))
.Select();
return query;
}
///
/// 添加区域从业人员
///
///
///
public async Task AddPractitionerAsync(AddBatchPractitionerInDto dto)
{
dto.ValidateObject();
var entity = dto.Adapt();
return await _practitionerRepository.AddAsync(entity);
}
///
/// 区域从业人员详情
///
///
///
public async Task GetPractitionerAsync(string id)
{
return (await _practitionerRepository.GetAsync(id)).Adapt();
}
///
/// 删除区域从业人员
///
///
///
public async Task DeletePractitionerAsync(IList id)
{
await _practitionerRepository.Updateable()
.SetColumns(m => m.IsDeleted, true)
.Where(m => id.Contains(m.Id))
.ExecuteCommandAsync();
}
#endregion
}