using BBWY.Client.APIServices; using BBWY.Client.Models; using BBWY.Client.Views.BillCorrection; using BBWY.Common.Models; using BBWY.Common.Trigger; using GalaSoft.MvvmLight.Command; using Microsoft.Win32; using Newtonsoft.Json; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using BBWY.Common.Extensions; namespace BBWY.Client.ViewModels { public class BillCorrectionViewModel : BaseVM, IDenpendency { private BillCorrectionService billCorrectionService; private string searchShopKeyWord; private DelayTrigger delayTrigger; private bool isLoading; private GlobalContext globalContext; public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } /// /// 销售运费快递账单文件列表 /// public ObservableCollection SaleFreightBillFileList { get; set; } /// /// 销售运费账单列表 /// public List SaleFreightBillList { get; set; } public IList ShopList { get; set; } /// /// 店铺搜索关键词 /// public string SearchShopKeyWord { get => searchShopKeyWord; set { if (Set(ref searchShopKeyWord, value)) { delayTrigger.SetKey(value); } } } /// /// 导入快递账单 /// public ICommand ImportSaleFreightBillCommand { get; set; } /// /// 查询订单 /// public ICommand SearchBillCorrectionOrderCommand { get; set; } /// /// 矫正费用 /// public ICommand CorrectCommand { get; set; } public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } } public IList OrderList { get; set; } public BillCorrectionViewModel(GlobalContext globalContext, BillCorrectionService billCorrectionService) { this.billCorrectionService = billCorrectionService; this.globalContext = globalContext; var shopList = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(globalContext.User.ShopList)); this.ShopList = new ObservableCollection(shopList); SaleFreightBillFileList = new ObservableCollection(); SaleFreightBillList = new List(); StartDate = DateTime.Now.Date.AddDays((DateTime.Now.Day - 1) * -1).AddMonths(-1); EndDate = StartDate.AddMonths(1).AddDays(-1); delayTrigger = new DelayTrigger(500); delayTrigger.OnExecute = OnSearchShopKeyWordChanged; OrderList = new ObservableCollection(); ImportSaleFreightBillCommand = new RelayCommand(ImportSaleFreightBill); SearchBillCorrectionOrderCommand = new RelayCommand(SearchBillCorrectionOrder); CorrectCommand = new RelayCommand(Correct); } private void OnSearchShopKeyWordChanged(string key) { foreach (var s in this.ShopList) s.IsSelected = false; App.Current.Dispatcher.Invoke(() => { this.ShopList.Clear(); var keyWordShopList = string.IsNullOrEmpty(key) ? globalContext.User.ShopList : globalContext.User.ShopList.Where(s => s.ShopName.Contains(key)); if (keyWordShopList.Count() > 0) { foreach (var shop in keyWordShopList) ShopList.Add(shop); } }); } private void ImportSaleFreightBill(string expressName) { var ofd = new OpenFileDialog() { Filter = "excel文件|*.xlsx;*.xls" }; if (ofd.ShowDialog() != true) return; var fileName = ofd.FileName.Substring(ofd.FileName.LastIndexOf("\\") + 1); var filePath = ofd.FileName; if (SaleFreightBillFileList.Contains(fileName)) { MessageBox.Show("请勿重复导入快递账单", "导入快递账单"); return; } IWorkbook xbook = null; try { using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { if (filePath.EndsWith(".xls")) xbook = new HSSFWorkbook(fs); else if (filePath.EndsWith(".xlsx")) xbook = new XSSFWorkbook(fs); } using (xbook) { IList billModelList = null; if (expressName == "YT") { var basicAmountW = new YTBasicAmount(); if (basicAmountW.ShowDialog() != true) return; var basicAmount = basicAmountW.BasicAmount; billModelList = LoadYTSaleBillFile(xbook, fileName, basicAmount); } else if (expressName == "YZ") { billModelList = LoadYZSaleBillFile(xbook, fileName); } else if (expressName == "JD") { billModelList = LoadJDSaleBillFile(xbook, fileName); } if (billModelList != null && billModelList.Count() > 0) { SaleFreightBillList.AddRange(billModelList); SaleFreightBillFileList.Add(fileName); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "导入账单失败提示"); return; } } /// /// 读取圆通运费账单 /// /// /// /// 基础面单价格 /// /// private IList LoadYTSaleBillFile(IWorkbook xbook, string belongFileName, decimal basicSaleFreight) { var sheet = xbook.GetSheetAt(0); var waybillNoCellTitle = sheet.GetRow(0).GetCell(1); if (waybillNoCellTitle == null || waybillNoCellTitle.StringCellValue != "运单号码") throw new Exception("验证圆通快递账单失败-未读取到运单号码"); var saleExpressFreightCellTitle = sheet.GetRow(0).GetCell(5); if (saleExpressFreightCellTitle == null || saleExpressFreightCellTitle.StringCellValue != "补差") throw new Exception("验证圆通快递账单失败-未读取到补差"); var rowCount = sheet.LastRowNum; IList list = new List(); for (var i = 1; i < rowCount; i++) { var row = sheet.GetRow(i); if (row == null) break; var waybillNoCell = row.GetCell(1); if (string.IsNullOrEmpty(waybillNoCell.StringCellValue)) break; var saleExpressFreightCell = row.GetCell(5); list.Add(new BillModel() { Amount = Convert.ToDecimal(saleExpressFreightCell.NumericCellValue) + basicSaleFreight, BillNo = waybillNoCell.StringCellValue, BillType = BillCorrectionType.销售运费账单, BelongFileName = belongFileName }); } return list; } /// /// 读取邮政运费账单 /// /// /// /// /// private IList LoadYZSaleBillFile(IWorkbook xbook, string belongFileName) { var sheet = xbook.GetSheetAt(0); var waybillNoCellTitle = sheet.GetRow(0).GetCell(2); if (waybillNoCellTitle == null || waybillNoCellTitle.StringCellValue != "邮件号") throw new Exception("验证邮政快递账单失败-未读取到邮件号"); var saleExpressFreightCellTitle = sheet.GetRow(0).GetCell(8); if (saleExpressFreightCellTitle == null || saleExpressFreightCellTitle.StringCellValue != "总邮资") throw new Exception("验证邮政快递账单失败-未读取到总邮资"); var rowCount = sheet.LastRowNum; IList list = new List(); for (var i = 1; i < rowCount; i++) { var row = sheet.GetRow(i); if (row == null) break; var waybillNoCell = row.GetCell(2); if (string.IsNullOrEmpty(waybillNoCell.StringCellValue)) break; var saleExpressFreightCell = row.GetCell(8); list.Add(new BillModel() { Amount = Convert.ToDecimal(saleExpressFreightCell.NumericCellValue), BillNo = waybillNoCell.StringCellValue, BillType = BillCorrectionType.销售运费账单, BelongFileName = belongFileName }); } return list; } /// /// 读取JD运费账单 /// /// /// /// private IList LoadJDSaleBillFile(IWorkbook xbook, string belongFileName) { var sheet = xbook.GetSheetAt(1); if (sheet == null) throw new Exception("验证JD快递账单失败-未读取到sheet1"); var waybillNoCellTitle = sheet.GetRow(0).GetCell(2); if (waybillNoCellTitle == null || waybillNoCellTitle.StringCellValue != "运单号") throw new Exception("验证JD快递账单失败-未读取到运单号"); var saleExpressFreightCellTitle = sheet.GetRow(0).GetCell(35); if (saleExpressFreightCellTitle == null || saleExpressFreightCellTitle.StringCellValue != "结算金额") throw new Exception("验证JD快递账单失败-未读取到结算金额"); var rowCount = sheet.LastRowNum; IList list = new List(); for (var i = 1; i < rowCount; i++) { var row = sheet.GetRow(i); if (row == null) break; var waybillNoCell = row.GetCell(2); if (string.IsNullOrEmpty(waybillNoCell.StringCellValue)) break; var saleExpressFreightCell = row.GetCell(35); var b = list.FirstOrDefault(b => b.BillNo == waybillNoCell.StringCellValue); if (b == null) { b = new BillModel() { BelongFileName = belongFileName, BillNo = waybillNoCell.StringCellValue, BillType = BillCorrectionType.销售运费账单 }; list.Add(b); } b.Amount += Convert.ToDecimal(saleExpressFreightCell.NumericCellValue); } return list; } /// /// 查询 /// private void SearchBillCorrectionOrder() { IsLoading = true; OrderList.Clear(); var shopIds = ShopList.Where(s => s.IsSelected).Select(s => s.ShopId).ToList(); Task.Factory.StartNew(() => billCorrectionService.GetBillCorrectionOrderList(shopIds, StartDate, EndDate)) .ContinueWith(t => { IsLoading = false; var response = t.Result; if (!response.Success) { App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "提示")); return; } if (response.Data == null || response.Data.Count() == 0) return; var list = response.Data.Map>(); App.Current.Dispatcher.Invoke(() => { foreach (var order in list) { //if (string.IsNullOrEmpty(order.WaybillNo)) // continue; //var billModel = SaleFreightBillList.FirstOrDefault(b => b.BillNo == order.WaybillNo); //if (billModel == null) // continue; //if (billModel.Amount != order.DeliveryExpressFreight) //{ // order.NewDeliveryExpressFreight = billModel.Amount; // order.ChangedContent = $"销售运费 {order.DeliveryExpressFreight} -> {order.NewDeliveryExpressFreight}"; //} OrderList.Add(order); } }); }); } /// /// 矫正费用 /// private void Correct() { } } }