using AngleSharp.Dom;
using Fw.Utility.UnifyResponse;
using Hotline.Api.Filter;
using Hotline.Application.Snapshot;
using Hotline.Caching.Interfaces;
using Hotline.File;
using Hotline.Orders;
using Hotline.Repository.SqlSugar.Snapshot;
using Hotline.Settings;
using Hotline.Share.Dtos;
using Hotline.Share.Dtos.Article;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Dtos.Settings;
using Hotline.Share.Dtos.Snapshot;
using Hotline.Share.Dtos.WebPortal;
using Hotline.Share.Enums.Order;
using Hotline.Share.Enums.Snapshot;
using Hotline.Share.Tools;
using Hotline.Snapshot;
using Hotline.Snapshot.IRepository;
using Hotline.ThirdAccountDomainServices.Interfaces;
using Hotline.Tools;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SharpCompress.Compressors.Xz;
using SqlSugar;
using SqlSugar.Extensions;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using XF.Domain.Authentications;
using XF.Domain.Exceptions;
using XF.Domain.Filters;
using XF.Domain.Repository;
namespace Hotline.Api.Controllers.Snapshot;
///
/// 随手拍接口
///
public class SnapshotController : BaseController
{
private readonly IRepository _orderRepository;
private readonly ISnapshotApplication _snapshotApplication;
private readonly ISystemAreaDomainService _systemAreaDomainService;
private readonly IIndustryRepository _industryRepository;
private readonly IOrderDomainService _orderDomainService;
private readonly IFileRepository _fileRepository;
private readonly IOrderSnapshotRepository _orderSnapshotRepository;
private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
private readonly ISessionContext _sessionContext;
private readonly IThirdAccountRepository _thirdAccountRepository;
private readonly ILogger _logger;
public SnapshotController(IRepository orderRepository, ISnapshotApplication snapshotApplication, ISystemAreaDomainService systemAreaDomainService, IIndustryRepository industryRepository, IOrderDomainService orderDomainService, IFileRepository fileRepository, IOrderSnapshotRepository orderSnapshotRepository, ISystemDicDataCacheManager systemDicDataCacheManager, ISessionContext sessionContext, IThirdAccountRepository thirdAccountRepository, ILogger logger)
{
_orderRepository = orderRepository;
_snapshotApplication = snapshotApplication;
_systemAreaDomainService = systemAreaDomainService;
_industryRepository = industryRepository;
_orderDomainService = orderDomainService;
_fileRepository = fileRepository;
_orderSnapshotRepository = orderSnapshotRepository;
_systemDicDataCacheManager = systemDicDataCacheManager;
_sessionContext = sessionContext;
_thirdAccountRepository = thirdAccountRepository;
_logger = logger;
}
///
/// 首页数据
///
///
[HttpGet("home")]
[AllowAnonymous]
public async Task GetHomePageAsync()
=> await _snapshotApplication.GetHomePageAsync();
///
/// 行业界面基础信息
///
/// 行业Id
///
[HttpGet("industry/base/{id}")]
[AllowAnonymous]
public async Task GetIndustryBaseAsync(string id)
=> await _snapshotApplication.GetIndustryBaseAsync(id, HttpContext.RequestAborted);
///
/// 添加随手拍工单
///
///
[HttpPost("order")]
[LogFilterAlpha("添加随手拍工单")]
public async Task AddOrderAsync([FromBody] AddSnapshotOrderInDto dto)
{
var ssp = _systemDicDataCacheManager.SourceChannel.FirstOrDefault(m => m.DicDataName == "随手拍")
?? throw UserFriendlyException.SameMessage("请添加[随手拍]来源.");
var order = dto.Adapt();
dto.ValidateObject();
var industry = await _industryRepository.GetAsync(dto.IndustryId, HttpContext.RequestAborted)
?? throw UserFriendlyException.SameMessage("行业不存在:" + dto.IndustryId);
order.AcceptTypeCode = industry.AcceptTypeCode;
order.AcceptType = industry.AcceptType;
order.FromGender = EGender.Unknown;
order.Title = dto.GetTitle(industry.IndustryType, industry.AcceptType);
order.Content = dto.GetContent(industry.IndustryType);
order.FromPhone = _sessionContext.Phone;
order.Contact = _sessionContext.Phone;
order.SourceChannel = ssp.DicDataName;
order.SourceChannelCode = ssp.DicDataValue;
order.InitId();
await _orderDomainService.AddAsync(order);
if (dto.Files.NotNullOrEmpty())
{
order.FileJson = await _fileRepository.AddFileAsync(dto.Files, order.Id, HttpContext.RequestAborted);
await _orderRepository.UpdateAsync(order);
}
var orderSnapshot = dto.Adapt();
orderSnapshot.Id = order.Id;
orderSnapshot.SnapshotUserId = _sessionContext.UserId;
orderSnapshot.IndustryId = dto.IndustryId;
orderSnapshot.IndustryName = industry.Name;
orderSnapshot.CompanyName = dto.CompanyName;
try
{
orderSnapshot.JobTypeName = _systemDicDataCacheManager.JobType.FirstOrDefault(m => m.DicDataValue == dto.JobType.ToString())?.DicDataName;
}
catch (Exception e)
{
_logger.LogError(e, $"添加随手拍工单, 获取JobTypeName异常. '{dto.JobType}'");
}
if (dto.StartWorkTime.NotNullOrEmpty()) orderSnapshot.StartWorkTime = dto.StartWorkTime.ObjToDate();
if (dto.EndWorkTime.NotNullOrEmpty()) orderSnapshot.EndWorkTime = dto.EndWorkTime.ObjToDate();
if (dto.Name.NotNullOrEmpty())
{
await _thirdAccountRepository.Updateable()
.SetColumns(m => m.UserName, dto.Name)
.Where(m => m.Id == _sessionContext.UserId)
.ExecuteCommandAsync(HttpContext.RequestAborted);
}
await _orderSnapshotRepository.AddAsync(orderSnapshot);
return order.Adapt();
}
///
/// 获取行业集合
///
///
[HttpGet("industry")]
[AllowAnonymous]
public async Task> GetIndustresAsync()
=> await _snapshotApplication.GetIndustresAsync();
///
/// 获取公开的工单集合
///
///
[HttpGet("order/published")]
[AllowAnonymous]
public async Task> GetPublishOrderAsync([FromQuery] OrderPublishInDto dto)
=> await _snapshotApplication.GetOrderPublishAsync(dto, HttpContext.RequestAborted);
///
/// 获取公开的工单详情
///
///
///
[HttpGet("order/published/{id}")]
[AllowAnonymous]
public async Task GetOrderPublishDetailAsync(string id)
=> await _snapshotApplication.GetOrderPublishDetailAsync(id, HttpContext.RequestAborted);
///
/// 获取小程序公告列表
///
///
///
[HttpGet("bulletions")]
[AllowAnonymous]
public async Task> QueryBulletinsAsync([FromQuery] BulletinInDto dto)
=> await _snapshotApplication.GetBulletinsAsync(dto, HttpContext.RequestAborted);
///
/// 获取小程序首页弹窗
///
///
[HttpGet("bulletions/popup")]
[AllowAnonymous]
public async Task GetBulletionPopupAsync()
=> await _snapshotApplication.GetBulletionPopupAsync(HttpContext.RequestAborted);
///
/// 公告详情
///
///
///
[HttpGet("bulletions/{id}")]
[AllowAnonymous]
public async Task QueryBulletionsDetailAsync(string id)
=> await _snapshotApplication.GetBulletinsDetailAsync(id);
///
/// 获取用户页面数据
///
///
[AllowAnonymous]
[HttpGet("user")]
public async Task GetUserInfo()
=> await _snapshotApplication.GetSnapshotUserInfoAsync();
///
/// 获取我提交的线索列表
///
///
///
[HttpGet("order")]
public async Task> QueryOrderListAsync([FromQuery] OrderInDto dto)
=> await _snapshotApplication.GetSnapshotOrdersAsync(dto, HttpContext.RequestAborted);
///
/// 获取我提交的线索详情
///
///
///
[HttpGet("order/{id}")]
public async Task QueryOrderListAsync(string id)
=> await _snapshotApplication.GetSnapshotOrderDetailAsync(id, HttpContext.RequestAborted);
///
/// 获取回访详情
///
///
///
[HttpGet("order/visit/{id}")]
public async Task> GetOrderVisitDetailAsync(string id)
=> await _snapshotApplication.GetOrderVisitDetailAsync(id);
///
/// 统计红包金额, 每月的总金额
///
///
///
[HttpGet("redpack")]
public async Task> QueryRedPackDateAsync([FromQuery] RedPackDateInDto dto)
=> await _snapshotApplication.GetRedPackDateAsync(dto, HttpContext.RequestAborted);
///
/// 用户领取红包总金额
///
///
[HttpGet("redpack/received")]
public async Task GetRedPackReceivedTotalAsync()
=> await _snapshotApplication.GetRedPackReceivedTotalAsync(HttpContext.RequestAborted);
///
/// 获取当月详细红包列表
///
///
///
[HttpGet("redpack/month")]
public async Task> QueryRedPackDateAsync([FromQuery] RedPacksInDto dto)
=> await _snapshotApplication.GetRedPacksAsync(dto, HttpContext.RequestAborted);
///
/// 获取随手拍电气焊动火作业待处理工单数量
/// TODO 条件 电气焊作业申报
///
///
[HttpGet("wait_accept_count")]
public async Task GetSnapshotWaitForAcceptCountAsync()
=> await _orderRepository
.CountAsync(m => m.SourceChannelCode == "ZGSSP" && m.Status == Share.Enums.Order.EOrderStatus.WaitForAccept);
///
/// 获取区域
///
///
[HttpGet("area/tree")]
[AllowAnonymous]
public async Task> GetAreaTreeAsync()
=> (await _systemAreaDomainService.GetAreaTree()).Adapt>();
///
/// 批量添加从业人员
///
///
///
[HttpPost("practitioner")]
public async Task AddPractitionerAsync([FromBody] IList dtos)
=> await _snapshotApplication.AddPractitionerAsync(dtos);
///
/// 获取从业人员详情
///
///
///
[HttpGet("practitioner/{id}")]
[AllowAnonymous]
public async Task GetPractitionerDetailAsync(string id)
=> await _snapshotApplication.GetPractitionerDetailAsync(id, HttpContext.RequestAborted);
///
/// 获取从业人员集合(每次随机)
///
///
///
[HttpGet("practitioner")]
[AllowAnonymous]
public async Task> GetPractitionerDetailAsync([FromQuery] PractitionerItemInDto dto)
=> await _snapshotApplication.GetPractitionerItemsAsync(dto, HttpContext.RequestAborted);
///
/// 志愿者上报
///
///
///
[HttpPost("report")]
[LogFilterAlpha("志愿者上报")]
public async Task AddVolunteerReportAsync([FromBody] AddVolunteerReportInDto dto)
=> await _snapshotApplication.AddVolunteerReportAsync(dto, HttpContext.RequestAborted);
///
/// 志愿者上报页面基础数据
///
/// 行业Id
///
[HttpGet("report/base")]
public Dictionary GetReportBaseAsync()
{
return new Dictionary
{
{ "jobType", _systemDicDataCacheManager.JobType },
};
}
///
/// 用户自己保存邀请码
///
///
[HttpPut("third/invitationcode")]
public async Task SaveInvitationCodeAsync([FromBody] SaveInvitationCodeInDto dto)
=> await _snapshotApplication.SaveInvitationCodeAsync(dto);
///
/// 网格员系统补推接口;
/// 根据工单No推送到网格员系统, 多个使用,号分隔
///
/// 工单No集合,多个使用 ',' 号分隔
///
[HttpPut("guider/nos")]
public async Task PostOrderGuiderSystemAsync([FromBody] string nos)
{
foreach (var no in nos.Split(','))
{
var orderId = await _orderRepository.Queryable()
.Where(m => m.No == no)
.Select(m => m.Id)
.FirstAsync() ?? throw UserFriendlyException.SameMessage("工单不存在");
await _snapshotApplication.PostOrderGuiderSystemAsync(orderId, HttpContext.RequestAborted);
}
}
#region 第三方系统
///
/// 接收网格员系统回调
///
///
[HttpPost("guider/reply")]
[LogFilter("网格员系统回调")]
[AllowAnonymous]
public async Task SaveGuiderSystemReplayAsync([FromBody] GuiderSystemInDto dto)
{
await _snapshotApplication.SaveGuiderSystemReplyAsync(dto, HttpContext.RequestAborted);
}
///
/// 电气焊申报系统申报工单查询
///
///
///
[HttpPost("order/declare")]
[AllowAnonymous]
public async Task GetOrderDeclareItemsAsync([FromBody] OrderDeclareItemsInDto dto)
{
var disposeDate = dto.JsonData.AcceptContent.DisposeDate.ObjToDate();
var industryId = await _industryRepository.Queryable()
.Where(m => m.IndustryType == EIndustryType.Declare)
.Select(m => m.Id)
.ToListAsync();
var query = _orderSnapshotRepository.Queryable()
.LeftJoin((snapshot, order) => order.Id == snapshot.Id)
.Where((snapshot, order) => industryId.Contains(snapshot.IndustryId))
.WhereIF(dto.JsonData.AcceptContent.Tyep == 2, (snapshot, order) => order.CreationTime.Date == disposeDate.Date)
.WhereIF(dto.JsonData.AcceptContent.Tyep == 1, (snapshot, order) => order.FiledTime != null && order.FiledTime.Value.Date == disposeDate.Date)
.Select((snapshot, order) => new OrderDeclareItemsOutDto
{
AreaCode = order.AreaCode,
AreaName = order.County,
EventType = 0,
WorkAddress = order.Address,
CreatedTime = order.CreationTime,
WorkType = snapshot.JobType.Value,
Source = 1,
WorkTimeStart = snapshot.StartWorkTime,
WorkTimeStop = snapshot.EndWorkTime,
OnsiteSituateDescription = order.Content,
Name = order.FromName,
Phone = order.FromPhone,
EventId = order.No,
PlaceType = snapshot.Workplace,
Compliance = 1,
CheckTime = order.RealCommunicationTime,
CheckUser = order.RealHandlerName,
CheckDept = order.FileUserOrgName,
CheckPhone = order.RealHandlerPhone,
LatLon = order.Latitude.ToString() + "," + order.Longitude.ToString(),
Title = order.Title,
CompanyType = snapshot.CompanyName,
OperationPlace = snapshot.WorkplaceName
});
var result = await query.ToListAsync();
return OpenResponse.Ok(new List>>()
{
new() { code = "1", msg = "ok", data = result }
});
}
#endregion
}