|
|
using AutoMapper.Internal.Mappers;using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Mvc;using Microsoft.EntityFrameworkCore;using Shentun.Peis.CustomerOrgGroupDetails;using Shentun.Peis.CustomerOrgGroups;using Shentun.Peis.CustomerOrgRegisters;using Shentun.Peis.CustomerOrgs;using Shentun.Peis.HelperDto;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 Xceed.Document.NET;
namespace Shentun.Peis.CustomerOrgGroups{
/// <summary>
/// 团检分组
/// </summary>
[ApiExplorerSettings(GroupName = "Work")] [Authorize] public class CustomerOrgGroupAppService : CrudAppService< CustomerOrgGroup, //The Book entity
CustomerOrgGroupDto, //Used to show books
Guid, //Primary key of the book entity
PagedAndSortedResultRequestDto, //Used for paging/sorting
CreateCustomerOrgGroupDto, UpdateCustomerOrgGroupDto> { private readonly IRepository<CustomerOrgRegister, Guid> _customerOrgRegisterRepository; private readonly ICustomerOrgGroupDetailRepository _customerOrgGroupDetailRepository; private readonly IRepository<IdentityUser, Guid> _userRepository; private readonly CustomerOrgGroupManager _manager; private readonly CacheService _cacheService; private readonly CustomerOrgGroupDetailManager _customerOrgGroupDetailManager; public CustomerOrgGroupAppService( IRepository<CustomerOrgGroup, Guid> repository, IRepository<CustomerOrgRegister, Guid> customerOrgRegisterRepository, ICustomerOrgGroupDetailRepository customerOrgGroupDetailRepository, IRepository<IdentityUser, Guid> userRepository, CustomerOrgGroupManager manager, CacheService cacheService, CustomerOrgGroupDetailManager customerOrgGroupDetailManager) : base(repository) { this._customerOrgRegisterRepository = customerOrgRegisterRepository; _customerOrgGroupDetailRepository = customerOrgGroupDetailRepository; _userRepository = userRepository; _manager = manager; _cacheService = cacheService; _customerOrgGroupDetailManager = customerOrgGroupDetailManager; } /// <summary>
/// 获取通过主键
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override async Task<CustomerOrgGroupDto> GetAsync(Guid id) { var entityDto = await base.GetAsync(id); entityDto.CreatorName = _cacheService.GetSurnameAsync(entityDto.CreatorId).Result; entityDto.LastModifierName = _cacheService.GetSurnameAsync(entityDto.LastModifierId).Result;
return entityDto;
}
/// <summary>
/// 获取列表 团检分组
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[RemoteService(false)] public override async Task<PagedResultDto<CustomerOrgGroupDto>> GetListAsync(PagedAndSortedResultRequestDto input) { return await base.GetListAsync(input); }
[RemoteService(false)] public override Task<CustomerOrgGroupDto> CreateAsync(CreateCustomerOrgGroupDto input) { return base.CreateAsync(input); }
/// <summary>
/// 获取列表 团检分组 可以带名字 次数 单位ID 检查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("api/app/customerorggroup/getlistinfilter")] public async Task<List<CustomerOrgGroupOrCustomerOrgDto>> GetListInFilterAsync(GetListDto input) {
var oldlist = (await Repository.GetQueryableAsync()). Include(x => x.CustomerOrgRegister). Include(x => x.CustomerOrgRegister.CustomerOrg).AsEnumerable();
if (!string.IsNullOrEmpty(input.Filter)) oldlist = oldlist.Where(m => m.DisplayName.Contains(input.Filter));
if (input.CustomerOrgId != null && input.CustomerOrgId != Guid.Empty) oldlist = oldlist.Where(m => m.CustomerOrgRegister.CustomerOrg.Id == input.CustomerOrgId);
if (input.CustomerOrgRegisterId != null && input.CustomerOrgRegisterId != Guid.Empty) oldlist = oldlist.Where(m => m.CustomerOrgRegisterId == input.CustomerOrgRegisterId);
if (input.MedicalTimes != null) oldlist = oldlist.Where(m => m.CustomerOrgRegister.MedicalTimes == input.MedicalTimes);
var entdto = oldlist.OrderBy(o => o.CustomerOrgRegister.CustomerOrg.DisplayOrder) .ThenBy(o => o.CustomerOrgRegister.MedicalTimes) .ThenBy(o => o.DisplayOrder) .Select(s => new CustomerOrgGroupOrCustomerOrgDto { CreationTime = s.CreationTime, CreatorId = s.CreatorId, DisplayName = s.DisplayName, DisplayOrder = s.DisplayOrder, Id = s.Id, LastModificationTime = s.LastModificationTime, LastModifierId = s.LastModifierId, Price = s.Price, ForSexId = s.ForSexId, Remark = s.Remark, MaritalStatusId = s.MaritalStatusId, JobTitle = s.JobTitle, JobPost = s.JobPost, AgeUpperLimit = s.AgeUpperLimit, AgeLowerLimit = s.AgeLowerLimit, CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result, LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result, CustomerOrgId = s.CustomerOrgRegister.CustomerOrgId, CustomerOrgName = s.CustomerOrgRegister.CustomerOrg.DisplayName, MedicalTimes = s.CustomerOrgRegister.MedicalTimes, CustomerOrgRegisterId = s.CustomerOrgRegisterId, CanAddMoney = s.CanAddMoney }).OrderBy(m => m.DisplayOrder).ToList();
return entdto;
}
[HttpPost("api/app/CustomerOrgGroup/GetListForPatentRegisterByFilter")] public async Task<List<CustomerOrgGroupForPatientRegisterDto>> GetListForPatentRegisterByFilterAsync(GetListDto input) {
var oldlist = (await Repository.GetQueryableAsync()). Include(x => x.CustomerOrgRegister). Include(x => x.CustomerOrgRegister.CustomerOrg).AsEnumerable();
if (!string.IsNullOrEmpty(input.Filter)) oldlist = oldlist.Where(m => m.DisplayName.Contains(input.Filter));
if (input.CustomerOrgId != null && input.CustomerOrgId != Guid.Empty) oldlist = oldlist.Where(m => m.CustomerOrgRegister.CustomerOrg.Id == input.CustomerOrgId);
if (input.CustomerOrgRegisterId != null && input.CustomerOrgRegisterId != Guid.Empty) oldlist = oldlist.Where(m => m.CustomerOrgRegisterId == input.CustomerOrgRegisterId);
if (input.MedicalTimes != null) oldlist = oldlist.Where(m => m.CustomerOrgRegister.MedicalTimes == input.MedicalTimes);
var entdto = oldlist.OrderBy(o => o.CustomerOrgRegister.CustomerOrg.DisplayOrder) .ThenBy(o => o.CustomerOrgRegister.MedicalTimes) .ThenBy(o => o.DisplayOrder) .Select(s => new CustomerOrgGroupForPatientRegisterDto { Id = s.Id, DisplayName = s.DisplayName, DisplayOrder = s.DisplayOrder, Price = s.Price, ForSexId = s.ForSexId, MaritalStatusId = s.MaritalStatusId, JobTitle = s.JobTitle, JobPost = s.JobPost, AgeUpperLimit = s.AgeUpperLimit, AgeLowerLimit = s.AgeLowerLimit, CustomerOrgRegisterId = s.CustomerOrgRegisterId }).OrderBy(m => m.DisplayOrder).ToList();
return entdto;
}
/// <summary>
/// 创建单位分组
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("api/customerorggroup/createcustomerorggroupincustomerorgregister")] public async Task<CustomerOrgGroupDto> CreateCustomerOrgGroupInCustomerOrgRegisterAsync(CreateCustomerOrgGroupInCustomerOrgRegisterDto input) {
var createEntity = ObjectMapper.Map<CreateCustomerOrgGroupDto, CustomerOrgGroup>(input); var entity = await _manager.CreateAsync(input.IsMaxMedicalTimes, input.CustomerOrgRegisterId, input.CustomerOrgId, createEntity);
//检查体检次数是否完成
await _manager.CheckCustomerOrgRegisterStatus(entity.CustomerOrgRegisterId);
entity = await Repository.InsertAsync(entity); var dto = ObjectMapper.Map<CustomerOrgGroup, CustomerOrgGroupDto>(entity); return dto; } [HttpPost("api/app/CustomerOrgGroup/CreateCustomerOrgGroupWithDetail")] public async Task<CustomerOrgGroupDto> CreateCustomerOrgGroupWithDetailAsync(CreateCustomerOrgGroupWithDetailDto input) {
var createEntity = ObjectMapper.Map<CreateCustomerOrgGroupDto, CustomerOrgGroup>(input); var entity = await _manager.CreateAsync(input.IsMaxMedicalTimes, input.CustomerOrgRegisterId, input.CustomerOrgId, createEntity);
//检查体检次数是否完成
await _manager.CheckCustomerOrgRegisterStatus(entity.CustomerOrgRegisterId);
entity = await Repository.InsertAsync(entity);
//插入明细
if (input.Details.Any()) { var createEntityList = ObjectMapper.Map<List<CreateCustomerOrgGroupDetail_Detail>, List<CustomerOrgGroupDetail>>(input.Details); var list = new List<CustomerOrgGroupDetail>(); foreach (var item in createEntityList) { item.CustomerOrgGroupId = entity.Id; var customerOrgGroupDetail = _customerOrgGroupDetailManager.CreateAsync(item); list.Add(customerOrgGroupDetail); } var sumMonkey = list.Sum(o => o.Price * o.Amount); entity.Price = sumMonkey; entity = await Repository.InsertAsync(entity);
await _customerOrgGroupDetailRepository.InsertManyAsync(list); }
var dto = ObjectMapper.Map<CustomerOrgGroup, CustomerOrgGroupDto>(entity); return dto; }
/// <summary>
/// 更新
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<CustomerOrgGroupDto> UpdateAsync(Guid id, UpdateCustomerOrgGroupDto input) { var entity = await Repository.GetAsync(id); var sourceEntity = ObjectMapper.Map<UpdateCustomerOrgGroupDto, CustomerOrgGroup>(input);
//检查体检次数是否完成
await _manager.CheckCustomerOrgRegisterStatus(entity.CustomerOrgRegisterId);
await _manager.UpdateAsync(sourceEntity, entity); //修改明细价格,只修改价格不为0的
var detailList = (await _customerOrgGroupDetailRepository.GetQueryableAsync()). Where(o => o.CustomerOrgGroupId == id && o.Price != 0).ToList();
if (detailList.Any()) { var sumMoney = detailList.Sum(o => o.Amount * o.Price); if (entity.Price == 0) { foreach (var item in detailList) { item.Price = 0; } } else { //按原来的比例重新计算
if (entity.Price != sumMoney) { var percentage = entity.Price / sumMoney; foreach (var item in detailList) { item.Price = Math.Round(percentage * item.Price, 2); } //自动分摊小数
var newSumMoney = detailList.Sum(o => o.Amount * o.Price);
var differenceMoney = entity.Price - newSumMoney; if (differenceMoney != 0) { foreach (var item in detailList) { //不允许算出负数,并且必须能反算出正确的价格
if ((item.Price * item.Amount + differenceMoney) == (item.Price + Math.Round((differenceMoney / item.Amount), 2)) * item.Amount && (item.Price + Math.Round((differenceMoney / item.Amount), 2)) > 0) { item.Price = item.Price + Math.Round((differenceMoney / item.Amount), 2); break; }
} } differenceMoney = entity.Price - detailList.Sum(o => o.Amount * o.Price); if (differenceMoney != 0) { throw new UserFriendlyException("无法反算出组合项目价格"); } } } }
entity = await Repository.UpdateAsync(entity); if (detailList.Count() > 0) { await _customerOrgGroupDetailRepository.UpdateManyAsync(detailList); } return ObjectMapper.Map<CustomerOrgGroup, CustomerOrgGroupDto>(entity); } /// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override async Task DeleteAsync(Guid id) { await _manager.CheckAndDeleteAsync(id); }
/// <summary>
/// 修改排序 置顶,置底
/// </summary>
/// <param name="id">需要修改的ID</param>
/// <param name="SortType">修改方式:1 置顶 2 置底</param>
/// <returns></returns>
[HttpPut("api/app/customerorggroup/updatemanysort")] public async Task UpdateManySortAsync(Guid id, int SortType) { await _manager.UpdateManySortAsync(id, SortType); }
/// <summary>
/// 修改排序 拖拽
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPut("api/app/customerorggroup/updatesortmany")] public async Task UpdateSortManyAsync(UpdateSortManyDto input) { await _manager.UpdateSortManyAsync(input); }
/// <summary>
/// 获取体检分组列表 按单位ID检索 查询最后一次
/// </summary>
/// <param name="CustomerOrgId"></param>
/// <returns></returns>
public async Task<List<CustomerOrgGroupDto>> GetListInCustomerOrgIdAsync(Guid CustomerOrgId) { var CustomerOrgRegisterId = (await _customerOrgRegisterRepository.GetListAsync(m => m.CustomerOrgId == CustomerOrgId)) .OrderByDescending(x => x.MedicalTimes).Select(s => s.Id).FirstOrDefault(); if (CustomerOrgRegisterId != Guid.Empty) { var entlist = await Repository.GetListAsync(m => m.CustomerOrgRegisterId == CustomerOrgRegisterId); //var userList = await _userRepository.GetListAsync();
var entdto = entlist.Select(s => new CustomerOrgGroupDto { CreationTime = s.CreationTime, CreatorId = s.CreatorId, DisplayName = s.DisplayName, DisplayOrder = s.DisplayOrder, Id = s.Id, LastModificationTime = s.LastModificationTime, LastModifierId = s.LastModifierId, Price = s.Price, ForSexId = s.ForSexId, Remark = s.Remark, MaritalStatusId = s.MaritalStatusId, JobTitle = s.JobTitle, JobPost = s.JobPost, AgeUpperLimit = s.AgeUpperLimit, AgeLowerLimit = s.AgeLowerLimit, CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result, LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result, CustomerOrgRegisterId = CustomerOrgRegisterId }).ToList();
return entdto; } return new List<CustomerOrgGroupDto>(); } /// <summary>
/// 获取体检分组列表 按单位登记ID
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPut("api/app/CustomerOrgGroup/GetListByCustomerOrgRegisterId")] public async Task<List<CustomerOrgGroupDto>> GetListByCustomerOrgRegisterIdAsync(CustomerOrgRegisterIdInputDto input) {
var entlist = await Repository.GetListAsync(m => m.CustomerOrgRegisterId == input.CustomerOrgRegisterId); var entdto = entlist.Select(s => new CustomerOrgGroupDto { CreationTime = s.CreationTime, CreatorId = s.CreatorId, DisplayName = s.DisplayName, DisplayOrder = s.DisplayOrder, Id = s.Id, LastModificationTime = s.LastModificationTime, LastModifierId = s.LastModifierId, Price = s.Price, ForSexId = s.ForSexId, Remark = s.Remark, MaritalStatusId = s.MaritalStatusId, JobTitle = s.JobTitle, JobPost = s.JobPost, AgeUpperLimit = s.AgeUpperLimit, AgeLowerLimit = s.AgeLowerLimit, CreatorName = _cacheService.GetSurnameAsync(s.CreatorId).Result, LastModifierName = _cacheService.GetSurnameAsync(s.LastModifierId).Result, CustomerOrgRegisterId = input.CustomerOrgRegisterId }).ToList();
return entdto;
return new List<CustomerOrgGroupDto>(); } /// <summary>
/// 复制上次分组
/// </summary>
/// <returns></returns>
[HttpPost("api/app/CustomerOrgGroup/CopyLastGrouping")] public async Task CopyLastGroupingAsync(CustomerOrgRegisterIdInputDto input) { if (input == null) throw new UserFriendlyException("请求参数有误"); var customerOrgRegisterEnt = await _customerOrgRegisterRepository.GetAsync(input.CustomerOrgRegisterId); if (customerOrgRegisterEnt == null) throw new UserFriendlyException("单位体检次数不存在"); if (customerOrgRegisterEnt.IsComplete == 'Y') throw new UserFriendlyException("该单位体检次数已完成,无法复制");
var prevCustomerOrgRegister = (await _customerOrgRegisterRepository.GetQueryableAsync()) .Where(m => m.CustomerOrgId == customerOrgRegisterEnt.CustomerOrgId && m.MedicalTimes < customerOrgRegisterEnt.MedicalTimes) .OrderByDescending(o => o.MedicalTimes).FirstOrDefault();
if (prevCustomerOrgRegister != null) { #region 创建分组、明细
//上一次体检次数的分组
var prevCustomerOrgGroupList = (await Repository.GetQueryableAsync()).Where(m => m.CustomerOrgRegisterId == prevCustomerOrgRegister.Id);
foreach (var group in prevCustomerOrgGroupList) { Guid customerOrgGroupId = GuidGenerator.Create(); var newCustomerOrgGroup = new CustomerOrgGroup(customerOrgGroupId) { AgeLowerLimit = group.AgeLowerLimit, AgeUpperLimit = group.AgeUpperLimit, CustomerOrgRegisterId = customerOrgRegisterEnt.Id, DisplayName = group.DisplayName, DisplayOrder = group.DisplayOrder, ForSexId = group.ForSexId, JobPost = group.JobPost, JobTitle = group.JobTitle, MaritalStatusId = group.MaritalStatusId, Price = group.Price, Remark = group.Remark };
await Repository.InsertAsync(newCustomerOrgGroup, true);
#region 创建分组明细
var prevCustomerOrgGroupDetailList = (await _customerOrgGroupDetailRepository.GetQueryableAsync()).Where(m => m.CustomerOrgGroupId == group.Id); foreach (var customerOrgGroupDetail in prevCustomerOrgGroupDetailList) { var newcustomerOrgGroupDetail = new CustomerOrgGroupDetail { Amount = customerOrgGroupDetail.Amount, AsbitemId = customerOrgGroupDetail.AsbitemId, CustomerOrgGroupId = customerOrgGroupId, Price = customerOrgGroupDetail.Price };
await _customerOrgGroupDetailRepository.InsertAsync(newcustomerOrgGroupDetail); }
#endregion
}
#endregion
} else { throw new UserFriendlyException("上一次单位体检次数不存在"); } } }}
|