using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Linq.Dynamic.Core; using System.Threading.Tasks; using System.Web.Mvc; using Abp.Application.Services.Dto; using Abp.Auditing; using Abp.Authorization; using Abp.Domain.Repositories; using Abp.Json; using Abp.Runtime.Caching; using Abp.Timing; using IwbZero.Auditing; using IwbZero.AppServiceBase; using IwbZero.Helper; using JetBrains.Annotations; using ShwasherSys.Authorization.Permissions; using ShwasherSys.BaseSysInfo; using ShwasherSys.Common; using ShwasherSys.CustomerInfo; using ShwasherSys.Invoice.Dto; using ShwasherSys.OrderSendInfo; using ShwasherSys.OrderSendInfo.Dto; using ShwasherSys.EntityFramework; using ShwasherSys.ProductStoreInfo.Dto; namespace ShwasherSys.Invoice { [AbpAuthorize] public class StatementBillAppService : IwbZeroAsyncCrudAppService, IStatementBillAppService { public StatementBillAppService( ICacheManager cacheManager, IRepository repository, IRepository viewOrderSendRepository, ISqlExecuter sqlExecuter, IRepository orderSendRepository,IRepository viewStatementBillRepository, IRepository customerRepository) : base(repository, "StatementBillNo") { ViewOrderSendRepository = viewOrderSendRepository; CacheManager = cacheManager; SqlExecuter = sqlExecuter; _viewStatementBillRepository = viewStatementBillRepository; CustomerRepository = customerRepository; OrderSendRepository = orderSendRepository; } protected ISqlExecuter SqlExecuter; private readonly IRepository _viewStatementBillRepository; protected override bool KeyIsAuto { get; set; } = false; protected IRepository ViewOrderSendRepository { get; } protected IRepository OrderSendRepository { get; } protected IRepository CustomerRepository { get; } #region GetSelect [DisableAuditing] public override async Task> GetSelectList() { var list = await Repository.GetAllListAsync(); var sList = new List {new SelectListItem {Text = @"请选择...", Value = "", Selected = true}}; foreach (var l in list) { //sList.Add(new SelectListItem { Value = l.Id, Text = l. }); } return sList; } [DisableAuditing] public override async Task GetSelectStr() { var list = await Repository.GetAllListAsync(); string str = ""; foreach (var l in list) { //str += $""; } return str; } #endregion #region CURD [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBillCreate)] public override async Task Create(StatementBillCreateDto input) { string orderSendIds = input.OrderSendIds; //string lcSql = "update OrderSend set StatementBillNo='" + input.StatementBillNo + "' "; //lcSql += " where OrderSendId in(" + orderSendIds + ")"; //var sCount = SqlExecuter.Execute(lcSql); string lcSql = ""; var osi = Array.ConvertAll(orderSendIds.Split(','), int.Parse); for (int i = 0; i < osi.Length; i++) { lcSql += $"update OrderSend set StatementBillNo='{input.StatementBillNo}',StatementBillSort=${i + 1} where OrderSendId = ${osi[i]};\r\n "; } var sCount = SqlExecuter.Execute(lcSql); input.BillMan = AbpSession.UserName; input.StatementState = 0; await CreateEntity(input); await CurrentUnitOfWork.SaveChangesAsync(); } [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)] public override async Task Update(StatementBillUpdateDto input) { await UpdateEntity(input); } [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBillUpdateDesc), AuditLog("修改对账单备注")] public async Task UpdateDesc(StatementBillUpdateDescDto input) { var entity = Repository.Get(input.Id); entity.Description = input.Description; this.Logger.Info("修改对账单备注!" + entity.ToJsonString()); await Repository.UpdateAsync(entity); } [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBillDelete)] public override async Task Delete(EntityDto input) { var entity = Repository.Get(input.Id); var orderSends = OrderSendRepository.GetAllList(i => i.StatementBillNo == entity.StatementBillNo); foreach (var send in orderSends) { send.StatementBillNo = ""; send.TimeLastMod = Clock.Now; send.UserIDLastMod = AbpSession.UserName; OrderSendRepository.Update(send); } //BusinessLogTypeEnum.OrderSend.WriteLog(BusinessLogRepository, "撤销对账单", $"对账单{entity.ToJsonString()}撤销"); //this.Logger.Info("撤销发票!" + entity.ToString()); await Repository.DeleteAsync(input.Id); } [DisableAuditing] [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)] public override async Task> GetAll(IwbPagedRequestDto input) { var query = CreateFilteredQuery(input); var customers = CustomerRepository.GetAll(); var result = query.Join(customers, s => s.CustomerId, c => c.Id, (s, c) => new StatementBillDto(){CustomerId=s.CustomerId,CustomerName=c.CustomerName,BillMan = s.BillMan,Description=s.Description,Id=s.Id, OrderStickBillNo=s.OrderStickBillNo,StatementBillNo=s.StatementBillNo, StatementState = s.StatementState, CreationTime=s.CreationTime }); result = ApplyFilter(result, input); var totalCount = await AsyncQueryableExecuter.CountAsync(result); result = result.OrderByDescending(i => i.CreationTime); result = result.Skip(input.SkipCount).Take(input.MaxResultCount); var dtoList = new PagedResultDto(totalCount, result.ToList()); return dtoList; } #region GetEntity/Dto /// /// 查询实体Dto /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)] public override async Task GetDto(EntityDto input) { var entity = await GetEntity(input); return MapToEntityDto(entity); } /// /// 查询实体Dto /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)] public override async Task GetDtoById(int id) { var entity = await GetEntityById(id); return MapToEntityDto(entity); } /// /// 查询实体Dto(需指明自定义字段) /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)] public override async Task GetDtoByNo(string no) { var entity = await GetEntityByNo(no); return MapToEntityDto(entity); } /// /// 查询实体 /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)] public override async Task GetEntity(EntityDto input) { var entity = await GetEntityById(input.Id); return entity; } /// /// 查询实体 /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)] public override async Task GetEntityById(int id) { return await Repository.FirstOrDefaultAsync(a=>a.Id==id); } /// /// 查询实体(需指明自定义字段) /// /// /// [DisableAuditing] [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill)] public override async Task GetEntityByNo(string no) { //CheckGetPermission(); if (string.IsNullOrEmpty(KeyFiledName)) { ThrowError("NoKeyFieldName"); } return await base.GetEntityByNo(no); } #endregion #region Hide ///// ///// 根据给定的创建 过滤查询. ///// ///// The input. //protected override IQueryable CreateFilteredQuery(IwbPagedRequestDto input) //{ // var query = Repository.GetAll(); // var pagedInput = input as IIwbPagedRequest; // if (pagedInput == null) // { // return query; // } // if (!string.IsNullOrEmpty(pagedInput.KeyWords)) // { // object keyWords = pagedInput.KeyWords; // LambdaObject obj = new LambdaObject() // { // FieldType = (LambdaFieldType)pagedInput.FieldType, // FieldName = pagedInput.KeyField, // FieldValue = keyWords, // ExpType = (LambdaExpType)pagedInput.ExpType // }; // var exp = obj.GetExp(); // query = exp != null ? query.Where(exp) : query; // } // if (pagedInput.SearchList != null && pagedInput.SearchList.Count > 0) // { // List objList = new List(); // foreach (var o in pagedInput.SearchList) // { // if (string.IsNullOrEmpty(o.KeyWords)) // continue; // object keyWords = o.KeyWords; // objList.Add(new LambdaObject // { // FieldType = (LambdaFieldType)o.FieldType, // FieldName = o.KeyField, // FieldValue = keyWords, // ExpType = (LambdaExpType)o.ExpType // }); // } // var exp = objList.GetExp(); // query = exp != null ? query.Where(exp) : query; // } // return query; //} //protected override IQueryable ApplySorting(IQueryable query, IwbPagedRequestDto input) //{ // return query.OrderBy(a => a.No); //} //protected override IQueryable ApplyPaging(IQueryable query, IwbPagedRequestDto input) //{ // if (input is IPagedResultRequest pagedInput) // { // return query.Skip(pagedInput.SkipCount).Take(pagedInput.MaxResultCount); // } // return query; //} #endregion #endregion [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill), AuditLog("查询出待生成对账单的客户列表记录")] public List GetHasSendOrderCustomer() { List listItems = new List(); var queryAllList = ViewOrderSendRepository.GetAll().Where(i => (i.OrderSendBillNo != null && i.OrderSendBillNo != "") && i.CustomerId != null&& (i.StatementBillNo == null || i.StatementBillNo == "")).Select(i => i.CustomerId).Distinct(); foreach (var send in queryAllList) { listItems.Add(new SelectListItem() { Text = send, Value = send }); } return listItems; } [AbpAuthorize(PermissionNames.PagesInvoiceInfoStatementBill), AuditLog("根据客户编号,查询出待生成对账单的发货明细")] public async Task> GetOrderSendByCustomerId(QuerySendDto input) { var queryAllList = ViewOrderSendRepository.GetAll().Where(i => i.CustomerId == input.CustomerId && (i.OrderSendBillNo != null && i.OrderSendBillNo != "") && (i.StatementBillNo == ""|| i.StatementBillNo == null));//.OrderByDescending(i=>i.SendActionDate).ThenBy(i=>i.ProductName); if (input.SendDateStart != null && input.SendDateStart > DateTime.MinValue) { queryAllList = queryAllList.Where(i => i.SendActionDate >= input.SendDateStart); } if (input.SendDateEnd != null && input.SendDateEnd > DateTime.MinValue) { queryAllList = queryAllList.Where(i => i.SendActionDate <= input.SendDateEnd); } //queryAllList = queryAllList.OrderByDescending(i => i.SendActionDate).ThenBy(i => i.ProductName); queryAllList = queryAllList.OrderBy(i => i.SendActionDate).ThenBy(i => i.ProductName); //queryAllList = queryAllList.OrderByDescending(i => i.CustomerId).ThenBy(i => i.SendBillSort); return await AsyncQueryableExecuter.ToListAsync(queryAllList); } public async Task> QueryStatisticStatementBillItems(QueryStatementBillReportDto input) { var startDate = input.Year.GetDateByType(input.Month, out var endDate, out var dateStr); //查询外购的排产单进行统计(不是新建状态的) var query = _viewStatementBillRepository.GetAll().Where(a => a.CreationTime >= startDate && a.CreationTime < endDate); if (!string.IsNullOrEmpty(input.CustomerId)) { query = query.Where(i =>i.CustomerId==input.CustomerId); } var items = await query.OrderByDescending(i => i.CreationTime).ToListAsync(); return items; } } }