using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; using Microsoft.EntityFrameworkCore; using NPOI.Util; using Shentun.Peis.CardBills; using Shentun.Peis.CardTypes; using Shentun.Peis.Enums; using Shentun.Peis.MedicalConclusionTypes; using Shentun.Peis.Models; using Shentun.Peis.PatientRegisters; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.Abp.Identity; using Volo.Abp.ObjectMapping; namespace Shentun.Peis.CardRegisters { [ApiExplorerSettings(GroupName = "Work")] [Authorize] public class CardRegisterAppService : ApplicationService { private readonly IRepository _cardRegisterRepository; private readonly IRepository _cardBillRepository; private readonly IRepository _userRepository; private readonly IRepository _organizationUnitRepository; private readonly CardRegisterManager _manager; private readonly CardBillManager _cardBillManager; private readonly IRepository _cardTypeRepository; private readonly CacheService _cacheService; private readonly IRepository _payModeRepository; public CardRegisterAppService( IRepository cardRegisterRepository, IRepository cardBillRepository, IRepository userRepository, IRepository organizationUnitRepository, CardRegisterManager manager, CardBillManager cardBillManager, IRepository cardTypeRepository, CacheService cacheService, IRepository payModeRepository) { this._cardRegisterRepository = cardRegisterRepository; this._cardBillRepository = cardBillRepository; this._userRepository = userRepository; this._organizationUnitRepository = organizationUnitRepository; this._manager = manager; this._cardBillManager = cardBillManager; _cardTypeRepository = cardTypeRepository; _cacheService = cacheService; _payModeRepository = payModeRepository; } ///// ///// 查询会员卡列表 带联合搜索条件 ///// ///// ///// //[HttpPost("api/app/cardregister/getcardregisterlist")] //public async Task> GetCardRegisterListAsync(GetCardRegisterListDto input) //{ // if (input != null) // { // var cardRegisterList = (await _cardRegisterRepository.GetDbSetAsync()) // .Include(x => x.CardType).AsEnumerable(); // if (input.CardModeId != null) // { // cardRegisterList = cardRegisterList.Where(m => m.CardType.CardModeId == input.CardModeId); // } // if (input.CardTypeId != null && input.CardTypeId != Guid.Empty) // { // cardRegisterList = cardRegisterList.Where(m => m.CardTypeId == input.CardTypeId); // } // if (!string.IsNullOrEmpty(input.CardNo)) // { // cardRegisterList = cardRegisterList.Where(m => m.CardNo == input.CardNo); // } // if (!string.IsNullOrEmpty(input.IdNo)) // { // cardRegisterList = cardRegisterList.Where(m => m.IdNo == input.IdNo); // } // if (!string.IsNullOrEmpty(input.CustomerName)) // { // cardRegisterList = cardRegisterList.Where(m => !string.IsNullOrEmpty(m.CustomerName) && m.CustomerName.Contains(input.CustomerName)); // } // if (input.IsActive != null) // { // cardRegisterList = cardRegisterList.Where(m => m.IsActive == input.IsActive); // } // if (!string.IsNullOrEmpty(input.Phone)) // { // cardRegisterList = cardRegisterList.Where(m => (!string.IsNullOrEmpty(m.Telephone) && m.Telephone.Contains(input.Phone)) || (!string.IsNullOrEmpty(m.MobileTelephone) && m.MobileTelephone.Contains(input.Phone))); // } // if (!string.IsNullOrEmpty(input.StartDate) && !string.IsNullOrEmpty(input.EndDate)) // { // if (input.DateType != null) // { // if (input.DateType == '1') // { // cardRegisterList = cardRegisterList.Where(m => m.CreationTime >= Convert.ToDateTime(input.StartDate) && // m.CreationTime < Convert.ToDateTime(input.EndDate).AddDays(1)); // } // else if (input.DateType == '2') // { // cardRegisterList = cardRegisterList.Where(m => m.ExpiryDate != null && m.ExpiryDate >= Convert.ToDateTime(input.StartDate) && // m.ExpiryDate < Convert.ToDateTime(input.EndDate).AddDays(1)); // } // } // } // var organizationUnitQueryable = await _organizationUnitRepository.GetQueryableAsync(); // var userQueryable = await _userRepository.GetQueryableAsync(); // var entlist = from a in cardRegisterList // join b in userQueryable on a.CreatorId equals b.Id into bb // from ab in bb.DefaultIfEmpty() // join c in userQueryable on a.LastModifierId equals c.Id into cc // from ac in cc.DefaultIfEmpty() // join d in organizationUnitQueryable on a.MedicalCenterId equals d.Id into dd // from ad in dd.DefaultIfEmpty() // select new { a, CreatorName = ab != null ? ab.Surname : "", LastModifierName = ac != null ? ac.Surname : "", OrganizationUnitName = ad != null ? ad.DisplayName : "" }; // var entdtolist = entlist.Select(s => new CardRegisterDto // { // CardBalance = s.a.CardBalance, // IsActive = s.a.IsActive, // CardModeId = s.a.CardType.CardModeId, // CardNo = s.a.CardNo, // CardPassword = s.a.CardPassword, // CardTypeId = s.a.CardTypeId, // CardTypeName = s.a.CardType.DisplayName, // CreationTime = s.a.CreationTime, // CreatorId = s.a.CreatorId, // Discount = s.a.Discount, // ExpiryDate = DataHelper.ConversionDateToString(s.a.ExpiryDate), // Id = s.a.Id, // IdNo = s.a.IdNo, // LastModificationTime = s.a.LastModificationTime, // LastModifierId = s.a.LastModifierId, // CustomerName = s.a.CustomerName, // MobileTelephone = s.a.MobileTelephone, // Remark = s.a.Remark, // Telephone = s.a.Telephone, // CreatorName = s.CreatorName, // LastModifierName = s.LastModifierName, // OrganizationUnitName = s.OrganizationUnitName // }).ToList(); // return entdtolist; // } // else // { // throw new UserFriendlyException("参数有误"); // } //} /// /// 查询会员卡列表 带联合搜索条件 /// /// /// [HttpPost("api/app/cardregister/getcardregisterlist")] public async Task> GetCardRegisterListAsync(GetCardRegisterListDto input) { if (input != null) { var cardRegisterList = from cardRegister in await _cardRegisterRepository.GetQueryableAsync() join cardType in await _cardTypeRepository.GetQueryableAsync() on cardRegister.CardTypeId equals cardType.Id join organizationUnit in await _organizationUnitRepository.GetQueryableAsync() on cardRegister.MedicalCenterId equals organizationUnit.Id select new { cardRegister, cardType, organizationUnitName = organizationUnit.DisplayName }; if (input.CardModeId != null) { cardRegisterList = cardRegisterList.Where(m => m.cardType.CardModeId == input.CardModeId); } if (input.CardTypeId != null && input.CardTypeId != Guid.Empty) { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.CardTypeId == input.CardTypeId); } if (!string.IsNullOrEmpty(input.CardNo)) { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.CardNo.ToUpper() == input.CardNo.ToUpper()); } if (!string.IsNullOrEmpty(input.IdNo)) { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.IdNo == input.IdNo); } if (!string.IsNullOrEmpty(input.CustomerName)) { cardRegisterList = cardRegisterList.Where(m => !string.IsNullOrEmpty(m.cardRegister.CustomerName) && m.cardRegister.CustomerName.Contains(input.CustomerName)); } if (input.IsActive != null) { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.IsActive == input.IsActive); } if (!string.IsNullOrEmpty(input.Phone)) { cardRegisterList = cardRegisterList.Where(m => (!string.IsNullOrEmpty(m.cardRegister.Telephone) && m.cardRegister.Telephone.Contains(input.Phone)) || (!string.IsNullOrEmpty(m.cardRegister.MobileTelephone) && m.cardRegister.MobileTelephone.Contains(input.Phone))); } if (!string.IsNullOrEmpty(input.StartDate) && !string.IsNullOrEmpty(input.EndDate)) { if (input.DateType != null) { if (input.DateType == '1') { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.CreationTime >= Convert.ToDateTime(input.StartDate) && m.cardRegister.CreationTime < Convert.ToDateTime(input.EndDate).AddDays(1)); } else if (input.DateType == '2') { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.ExpiryDate >= Convert.ToDateTime(input.StartDate) && m.cardRegister.ExpiryDate < Convert.ToDateTime(input.EndDate).AddDays(1)); } } } int totalCount = cardRegisterList.Count(); cardRegisterList = cardRegisterList.OrderBy(o => o.cardType.CardModeId).ThenBy(o => o.cardRegister.Id).Skip(input.SkipCount * input.MaxResultCount).Take(input.MaxResultCount); var entDtoList = cardRegisterList.ToList().Select(s => new CardRegisterDto { CardBalance = s.cardRegister.CardBalance, IsActive = s.cardRegister.IsActive, CardModeId = s.cardType.CardModeId, CardNo = s.cardRegister.CardNo, CardPassword = s.cardRegister.CardPassword, CardTypeId = s.cardRegister.CardTypeId, CardTypeName = s.cardType.DisplayName, CreationTime = s.cardRegister.CreationTime, CreatorId = s.cardRegister.CreatorId, Discount = s.cardRegister.Discount, ExpiryDate = DataHelper.ConversionDateToString(s.cardRegister.ExpiryDate), Id = s.cardRegister.Id, IdNo = s.cardRegister.IdNo, LastModificationTime = s.cardRegister.LastModificationTime, LastModifierId = s.cardRegister.LastModifierId, CustomerName = s.cardRegister.CustomerName, MobileTelephone = s.cardRegister.MobileTelephone, Remark = s.cardRegister.Remark, Telephone = s.cardRegister.Telephone, CreatorName = _cacheService.GetSurnameAsync(s.cardRegister.CreatorId).GetAwaiter().GetResult(), LastModifierName = _cacheService.GetSurnameAsync(s.cardRegister.LastModifierId).GetAwaiter().GetResult(), OrganizationUnitName = s.organizationUnitName, MedicalCenterId = s.cardRegister.MedicalCenterId }).ToList(); return new PagedResultDto(totalCount, entDtoList); } else { throw new UserFriendlyException("参数有误"); } } /// /// 收费时检索会员卡信息 /// /// /// [HttpPost("api/app/CardRegister/GetCardRegisterCharge")] public async Task> GetCardRegisterChargeAsync(GetCardRegisterChargeInputDto input) { var cardRegisterList = from cardRegister in await _cardRegisterRepository.GetQueryableAsync() join cardType in await _cardTypeRepository.GetQueryableAsync() on cardRegister.CardTypeId equals cardType.Id where cardRegister.IsActive == 'Y' select new { cardRegister, cardType }; if (input.CardModeId != null) { cardRegisterList = cardRegisterList.Where(m => m.cardType.CardModeId == input.CardModeId); } if (!string.IsNullOrEmpty(input.CardNo)) { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.CardNo.ToUpper() == input.CardNo.ToUpper()); } if (!string.IsNullOrEmpty(input.IdNo)) { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.IdNo == input.IdNo); } if (!string.IsNullOrEmpty(input.CustomerName)) { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.CustomerName == input.CustomerName); } if (!string.IsNullOrEmpty(input.Phone)) { cardRegisterList = cardRegisterList.Where(m => m.cardRegister.Telephone == input.Phone || m.cardRegister.MobileTelephone == input.Phone); } var entDtoList = cardRegisterList.ToList().Select(s => new GetCardRegisterChargeDto { CardBalance = s.cardRegister.CardBalance, CardModeId = s.cardType.CardModeId, CardNo = s.cardRegister.CardNo, CardTypeName = s.cardType.DisplayName, Discount = s.cardRegister.Discount, ExpiryDate = DataHelper.ConversionDateToString(s.cardRegister.ExpiryDate), Id = s.cardRegister.Id, IdNo = s.cardRegister.IdNo, CustomerName = s.cardRegister.CustomerName, MobileTelephone = s.cardRegister.MobileTelephone, Remark = s.cardRegister.Remark, Telephone = s.cardRegister.Telephone }).ToList(); return entDtoList; } /// /// 根据ID查实体内容 /// /// /// [HttpGet("api/app/cardregister/get")] public async Task GetAsync(Guid id) { var userList = await _userRepository.GetListAsync(); var entity = await _cardRegisterRepository.GetAsync(id); var entityDto = ObjectMapper.Map(entity); entityDto.CreatorName = EntityHelper.GetSurnameNoSql(userList, entityDto.CreatorId); entityDto.LastModifierName = EntityHelper.GetSurnameNoSql(userList, entityDto.LastModifierId); return entityDto; } /// /// 创建 /// /// /// [HttpPost("api/app/cardregister/create")] public async Task CreateAsync(CreateCardRegisterDto input) { Guid cardRegisterId = GuidGenerator.Create(); var createEntity = ObjectMapper.Map(input); var entity = await _manager.CreateAsync(createEntity, cardRegisterId); entity = await _cardRegisterRepository.InsertAsync(entity); var dto = ObjectMapper.Map(entity); return dto; } /// /// 修改 /// /// /// /// [HttpPost("api/app/cardregister/update")] public async Task UpdateAsync(Guid id, UpdateCardRegisterDto input) { var entity = await _cardRegisterRepository.GetAsync(id); var sourceEntity = ObjectMapper.Map(input); _manager.UpdateAsync(sourceEntity, entity); entity = await _cardRegisterRepository.UpdateAsync(entity); return ObjectMapper.Map(entity); } /// /// 删除 /// /// /// [HttpPost("api/app/cardregister/delete")] public Task DeleteAsync(Guid id) { throw new Exception("卡禁止删除"); } /// /// 更新启用状态 /// /// /// /// [HttpPost("api/app/cardregister/updateactive")] public async Task UpdateActive(Guid id, UpdateCardRegisterActiveDto updateCardRegisterActiveDto) { Check.NotNull(updateCardRegisterActiveDto, nameof(updateCardRegisterActiveDto)); var entity = await _cardRegisterRepository.GetAsync(id); await _manager.UpdateActive(entity, updateCardRegisterActiveDto.IsActive); await _cardRegisterRepository.UpdateAsync(entity); } /* /// /// 会员卡充值 delete by zhh 2024-03-17 /// /// [HttpPost("api/app/cardregister/cardregisterrecharge")] public async Task CardRegisterRechargeAsync(CardRegisterRechargeDto input) { if (input != null) { var cardRegisterEnt = await _cardRegisterRepository.FindAsync(m => m.Id == input.CardRegisterId); if (cardRegisterEnt != null) { cardRegisterEnt.CardBalance += input.RechargeAmount; await _cardRegisterRepository.UpdateAsync(cardRegisterEnt); //增加充值记录 var cardBill = new CardBill(GuidGenerator.Create()) { BillFlag = '2', BillMoney = input.RechargeAmount, CardRegisterId = input.CardRegisterId, PayModeId = input.PayModeId }; cardBill = _cardBillManager.CreateAsync(cardBill); await _cardBillRepository.InsertAsync(cardBill); } else { throw new UserFriendlyException("会员卡不存在"); } } else { throw new UserFriendlyException("参数有误"); } } */ /// /// 会员卡充值、退费 /// /// [HttpPost("api/app/cardregister/addcardbill")] public async Task AddCardBill(CreateCardBillDto createCardBillDto) { DataHelper.CheckEntityIsNull(createCardBillDto); //Check.NotNull(createCardBillDto, "CreateCardBillDto"); var cardRegister = await _cardRegisterRepository.GetAsync(createCardBillDto.CardRegisterId); var cardBill = _manager.CreateCardBill(cardRegister, createCardBillDto.PayModeId, createCardBillDto.BillFlag, createCardBillDto.BillMoney); await _cardRegisterRepository.UpdateAsync(cardRegister); await _cardBillRepository.InsertAsync(cardBill); } /// /// excel文档创建会员卡 /// /// [HttpPost("api/app/CardRegister/BatchCreateCardRegisterByExcel")] public async Task BatchCreateCardRegisterByExcelAsync(BatchCreateCardRegisterByExcelInputDto input) { var cardTypeEntity = await _cardTypeRepository.FirstOrDefaultAsync(f => f.DisplayName == input.CardTypeName.Trim()); if (cardTypeEntity == null) { throw new UserFriendlyException($"当前数据卡类型不存在,卡号:{input.CardNo},姓名:{input.CustomerName}"); } Guid cardRegisterId = GuidGenerator.Create(); var cardRegisterEntity = new CardRegister { CardNo = input.CardNo, CardPassword = "", CardTypeId = cardTypeEntity.Id, CustomerName = input.CustomerName, Discount = input.Discount, ExpiryDate = DateTime.Parse(input.ExpiryDate), IdNo = input.IdNo, IsActive = 'Y', MobileTelephone = input.MobileTelephone, Remark = input.Remark, Telephone = input.Telephone, MedicalCenterId = input.MedicalCenterId }; cardRegisterEntity = await _manager.CreateAsync(cardRegisterEntity, cardRegisterId); if (input.CardBalance > 0) { if (string.IsNullOrWhiteSpace(input.PayModeName)) { throw new UserFriendlyException("充值支付方式不能为空"); } var payModeEntity = await _payModeRepository.FirstOrDefaultAsync(f => f.DisplayName == input.PayModeName.Trim()); if (payModeEntity == null) { throw new UserFriendlyException($"当前数据充值支付方式不存在,卡号:{input.CardNo},姓名:{input.CustomerName}"); } var cardBillEntity = _manager.CreateCardBill(cardRegisterEntity, payModeEntity.Id, CardBillFlag.Charge, input.CardBalance); await _cardRegisterRepository.InsertAsync(cardRegisterEntity); await _cardBillRepository.InsertAsync(cardBillEntity); } else { await _cardRegisterRepository.InsertAsync(cardRegisterEntity); } } } }