using BBWY.Client.APIServices; using BBWY.Client.Models; using BBWY.Common.Models; using BBWY.Controls; using GalaSoft.MvvmLight.Command; using Microsoft.Win32; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace BBWY.Client.ViewModels { public class ExportOrderSkuViewModel : BaseVM, IDenpendency { private DateTime startDate; private DateTime endDate; private GlobalContext globalContext; private KVModel selectedPlatform; private int pageIndex; private int pageSize; private long totalCount; private bool isLoading; private bool isShowAll_Shop; private bool isShowAll_Department; private OrderService orderService; public IList DepartmentList { get; set; } public IList ShopList { get; set; } public IList PlatformList { get; set; } public IList ExportSkuList { get; set; } public DateTime StartDate { get => startDate; set { Set(ref startDate, value); } } public DateTime EndDate { get => endDate; set { Set(ref endDate, value); } } public KVModel SelectedPlatform { get => selectedPlatform; set { Set(ref selectedPlatform, value); } } public ICommand SearchCommand { get; set; } public ICommand ExportCommand { get; set; } public ICommand OrderPageIndexChangedCommand { get; set; } public int PageIndex { get => pageIndex; set { Set(ref pageIndex, value); } } public int PageSize { get => pageSize; set { Set(ref pageSize, value); } } public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } } public long TotalCount { get => totalCount; set { Set(ref totalCount, value); } } public bool IsShowAll_Shop { get => isShowAll_Shop; set { Set(ref isShowAll_Shop, value); } } public bool IsShowAll_Department { get => isShowAll_Department; set { Set(ref isShowAll_Department, value); } } public ExportOrderSkuViewModel(GlobalContext globalContext, OrderService orderService) { this.orderService = orderService; DepartmentList = new ObservableCollection(); ShopList = new ObservableCollection(); PlatformList = new ObservableCollection(); ExportSkuList = new ObservableCollection(); SearchCommand = new RelayCommand(Search); ExportCommand = new RelayCommand(Export); OrderPageIndexChangedCommand = new RelayCommand(p => { //this.PageIndex = p.PageIndex; Task.Factory.StartNew(() => SearchCore(p.PageIndex)); }); var platformEnumValues = Enum.GetValues(typeof(Platform)); foreach (var ev in platformEnumValues) { var p = (Platform)ev; PlatformList.Add(new KVModel() { Key = p.ToString(), Value = ((int)p).ToString() }); } IsShowAll_Department = IsShowAll_Shop = true; this.globalContext = globalContext; LoadDepartment(); StartDate = DateTime.Now.Date; EndDate = DateTime.Now.Date; PageIndex = 1; PageSize = 20; } private void LoadDepartment() { var dlist = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(globalContext.User.DepartmentList)); foreach (var d in dlist) { d.IsSelected = false; DepartmentList.Add(d); d.OnIsSelectedChanged = OnDeparmentSelectionChanged; } ShopList.Clear(); } private void OnDeparmentSelectionChanged() { ShopList.Clear(); foreach (var d in DepartmentList) { if (!d.IsSelected) continue; foreach (var s in d.ShopList) { s.OnIsSelectedChanged = OnShopSelectionChanged; s.IsSelected = false; ShopList.Add(s); } } IsShowAll_Department = !DepartmentList.Any(d => d.IsSelected); } private void OnShopSelectionChanged() { IsShowAll_Shop = !ShopList.Any(s => s.IsSelected); } private void Search() { PageIndex = 1; Task.Factory.StartNew(() => SearchCore(PageIndex)); } private void SearchCore(int pageIndex) { IsLoading = true; int? purchasePlatform = null; if (SelectedPlatform != null) purchasePlatform = int.Parse(SelectedPlatform.Value); List shopIds = new List(); if (ShopList.Count() > 0) { if (ShopList.Any(s => s.IsSelected)) shopIds.AddRange(ShopList.Where(s => s.IsSelected).Select(s => s.ShopId)); else shopIds.AddRange(ShopList.Select(s => s.ShopId)); } var response = orderService.QueryOrderSkuList(StartDate, EndDate, purchasePlatform, shopIds, pageIndex, PageSize); IsLoading = false; TotalCount = 0; if (!response.Success) { App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "提示")); return; } TotalCount = response.Data.Count; App.Current.Dispatcher.Invoke(() => { ExportSkuList.Clear(); foreach (var item in response.Data.ItemList) ExportSkuList.Add(item); }); } private void Export() { var sfd = new SaveFileDialog() { Filter = "csv files(*.csv)|*.csv" }; if (sfd.ShowDialog() != true) return; var ssaveFileName = sfd.FileName; int? purchasePlatform = null; if (SelectedPlatform != null) purchasePlatform = int.Parse(SelectedPlatform.Value); IsLoading = true; Task.Factory.StartNew(() => orderService.ExportOrderSkuList(StartDate, EndDate, purchasePlatform, ShopList.Where(s => s.IsSelected).Select(s => s.ShopId).ToList())).ContinueWith(t => { var response = t.Result; if (!response.Success) { IsLoading = false; App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "提示")); return; } try { var list = response.Data.Select(x => x.ToString()).ToList(); list.Insert(0, "开始时间,店铺名称,订单Id,订单状态,SPU,SKU,仓储类型,采购成本(货款+运费),平台优惠,商家优惠,单价,数量,商品标题"); System.IO.File.WriteAllLines(ssaveFileName, list, Encoding.UTF8); App.Current.Dispatcher.Invoke(() => MessageBox.Show("导出完成", "导出")); } catch (Exception ex) { App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "导出")); return; } finally { IsLoading = false; } }); } } }