步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

284 lines
11 KiB

using BBWY.Client.APIServices;
2 years ago
using BBWY.Client.Helpers;
using BBWY.Client.Models;
2 years ago
using BBWY.Client.Views.ServiceOrder;
using BBWY.Common.Extensions;
2 years ago
using BBWY.Common.Http;
using BBWY.Common.Models;
2 years ago
using BBWY.Controls;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2 years ago
using System.IO;
using System.Linq;
2 years ago
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace BBWY.Client.ViewModels
{
public class ServiceOrderViewModel : BaseVM, IDenpendency
{
private ServiceOrderService serviceOrderService;
2 years ago
private IHttpClientFactory httpClientFactory;
private bool isLoading;
private ServiceOrderState? serviceOrderState;
private ReturnDirection? returnDirection;
private long serviceOrderCount;
private int pageIndex;
private int pageSize;
private string orderId;
private string spu;
private string sku;
private string serviceId;
private DateTime? startDate;
private DateTime? endDate;
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
public ServiceOrderState? ServiceOrderState { get => serviceOrderState; set { Set(ref serviceOrderState, value); } }
public ReturnDirection? ReturnDirection { get => returnDirection; set { Set(ref returnDirection, value); } }
public ICommand SetServiceOrderStateCommand { get; set; }
public ICommand SetReturnDirectionCommand { get; set; }
public ICommand OnPageIndexChangedCommand { get; set; }
public ICommand SetSearchDateCommand { get; set; }
public ICommand SearchServiceOrderCommand { get; set; }
2 years ago
public ICommand NavigateToDetailCommand { get; set; }
public ICommand PreviewImgCommand { get; set; }
2 years ago
public ICommand EditServiceOrderCommand { get; set; }
public IList<ServiceOrder> ServiceOrderList { get; set; }
public long ServiceOrderCount { get => serviceOrderCount; set { Set(ref serviceOrderCount, value); } }
public int PageIndex { get => pageIndex; set { Set(ref pageIndex, value); } }
public int PageSize { get => pageSize; set { Set(ref pageSize, value); } }
public string OrderId { get => orderId; set { Set(ref orderId, value); } }
public string Spu { get => spu; set { Set(ref spu, value); } }
public string Sku { get => sku; set { Set(ref sku, value); } }
public string ServiceId { get => serviceId; set { Set(ref serviceId, value); } }
public DateTime? StartDate { get => startDate; set { Set(ref startDate, value); } }
public DateTime? EndDate { get => endDate; set { Set(ref endDate, value); } }
public GlobalContext GlobalContext { get; set; }
2 years ago
public ServiceOrderViewModel(ServiceOrderService serviceOrderService, GlobalContext globalContext, IHttpClientFactory httpClientFactory)
{
2 years ago
this.httpClientFactory = httpClientFactory;
this.serviceOrderService = serviceOrderService;
SetServiceOrderStateCommand = new RelayCommand<ServiceOrderState?>(SetServiceOrderState);
SetReturnDirectionCommand = new RelayCommand<ReturnDirection?>(SetReturnDirection);
CopyTextCommand = new RelayCommand<string>(s => { try { Clipboard.SetText(s); } catch (Exception ex) { } });
SetSearchDateCommand = new RelayCommand<int>(SetSearchDate);
2 years ago
SearchServiceOrderCommand = new RelayCommand(InitQueryServiceOrder);
NavigateToDetailCommand = new RelayCommand<string>(NavigateToDetail);
OnPageIndexChangedCommand = new RelayCommand<PageArgs>(OnPageIndexChanged);
PreviewImgCommand = new RelayCommand<string>(PreviewImg);
2 years ago
EditServiceOrderCommand = new RelayCommand<ServiceOrder>(OpenEditServiceOrder);
ServiceOrderList = new ObservableCollection<ServiceOrder>() { new ServiceOrder(), new ServiceOrder(), new ServiceOrder() };
PageSize = 10;
GlobalContext = globalContext;
EndDate = DateTime.Now.Date;
StartDate = DateTime.Now.Date.AddDays(-15);
2 years ago
InitQueryServiceOrder();
}
2 years ago
private void InitQueryServiceOrder()
{
PageIndex = 1;
Task.Factory.StartNew(() => QueryServiceOrder(PageIndex));
}
private void SetSearchDate(int d)
{
EndDate = d == 1 ? DateTime.Now.Date.AddDays(-1) : DateTime.Now;
StartDate = DateTime.Now.Date.AddDays(d * -1);
2 years ago
InitQueryServiceOrder();
}
private void SetServiceOrderState(ServiceOrderState? state)
{
this.ServiceOrderState = state;
//query
2 years ago
InitQueryServiceOrder();
}
private void SetReturnDirection(ReturnDirection? returnDirection)
{
this.ReturnDirection = returnDirection;
//query
2 years ago
InitQueryServiceOrder();
}
private void QueryServiceOrder(int pageIndex)
{
IsLoading = true;
var response = serviceOrderService.GetList(OrderId, Sku, Spu, ServiceId, GlobalContext.User.Shop.ShopId.ToString(), ServiceOrderState, ReturnDirection, pageIndex, PageSize, StartDate, EndDate);
if (!response.Success)
{
IsLoading = false;
App.Current.Dispatcher.Invoke(() =>
{
MessageBox.Show(response.Msg, "提示");
return;
});
}
IsLoading = false;
ServiceOrderCount = response.Data.Count;
2 years ago
App.Current.Dispatcher.Invoke(() => ServiceOrderList.Clear());
if (response.Data.Items == null || response.Data.Items.Count() == 0)
return;
var list = response.Data.Items.Map<IList<ServiceOrder>>();
App.Current.Dispatcher.Invoke(() =>
{
foreach (var s in list)
{
2 years ago
#region Test
//转换ImageName
2 years ago
//if (int.Parse(s.ServiceId) % 2 == 0)
//{
// s.ProductPackage = ProductPackage.新; s.ImageName = "20230317071208762563,8d58b491-7859-4187-9f43-4fd177a0f25f,b0df0763-9cf4-40ca-a1fc-57695e4b8d33";
//}
2 years ago
#endregion
2 years ago
s.Init();
ServiceOrderList.Add(s);
}
});
}
2 years ago
private void NavigateToDetail(string serviceId)
{
var url = $"https://sh.shop.jd.com/afs/detail/waitReceive?afsApplyId=undefined&afsServiceId={serviceId}";
try
{
//System.Diagnostics.Process.Start("explorer.exe", url);
ShellExecuteHelper.ShellExecute(IntPtr.Zero, "open", url, string.Empty, string.Empty, ShellExecuteHelper.ShowCommands.SW_SHOWNORMAL);
}
catch (Exception ex)
{
Clipboard.SetText(url);
MessageBox.Show($"{ex.Message}\r\n调用浏览器失败,网页链接已复制到剪切板,请手动打开浏览器访问", "提示");
}
}
private void OnPageIndexChanged(PageArgs pageArgs)
{
Task.Factory.StartNew(() => QueryServiceOrder(pageArgs.PageIndex));
}
private void PreviewImg(string thumbnailImg)
{
var fullUrl = thumbnailImg.Substring(0, thumbnailImg.IndexOf("?"));
var fileName = fullUrl.Substring(fullUrl.LastIndexOf("/") + 1);
var localPath = Path.Combine(Path.GetTempPath(), fileName);
if (!File.Exists(localPath))
{
IsLoading = true;
var downloader = new HttpDownloader(httpClientFactory);
downloader.OnDownloadComplated += (s, e) =>
{
IsLoading = false;
if (e.Error != null)
{
App.Current.Dispatcher.Invoke(() => MessageBox.Show(e.Error.Message));
return;
}
CallWindowsPhoto(localPath);
2 years ago
};
2 years ago
Task.Factory.StartNew(() => downloader.DownloadFile(fullUrl, Path.GetTempPath(), fileName, null));
}
else
{
CallWindowsPhoto(localPath);
}
}
private void CallWindowsPhoto(string path)
{
//建立新的系统进程
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
//设置图片的真实路径和文件名
process.StartInfo.FileName = path;
//设置进程运行参数,这里以最大化窗口方法显示图片。
process.StartInfo.Arguments = "rundl132.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
//此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true
process.StartInfo.UseShellExecute = true;
//此处可以更改进程所打开窗体的显示样式,可以不设
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start();
process.Close();
}
catch (Exception ex)
{
App.Current.Dispatcher.Invoke(() => MessageBox.Show($"打开照片查看器失败 {ex.Message}"));
}
}
2 years ago
private void OpenEditServiceOrder(ServiceOrder serviceOrder)
{
var w = new EditServiceOrder(serviceOrder);
var r = w.ShowDialog();
if (r == true)
{
2 years ago
IsLoading = true;
Task.Factory.StartNew(() => RefreshServiceOrder(serviceOrder));
}
}
private void RefreshServiceOrder(long servicePId)
{
var order = ServiceOrderList.FirstOrDefault(s => s.Id == servicePId);
RefreshServiceOrder(order);
}
private void RefreshServiceOrder(ServiceOrder serviceOrder)
{
var serviceOrderResponse = serviceOrderService.GetList(string.Empty, string.Empty, string.Empty, serviceOrder.ServiceId, serviceOrder.ShopId, null, null, 1, 1, null, null);
IsLoading = false;
if (!serviceOrderResponse.Success)
{
Application.Current.Dispatcher.Invoke(() => MessageBox.Show(serviceOrderResponse.Msg, "刷新服务单"));
return;
2 years ago
}
2 years ago
var newServiceOrder = serviceOrderResponse.Data.Items.FirstOrDefault().Map<ServiceOrder>();
newServiceOrder.Init();
Application.Current.Dispatcher.Invoke(() =>
{
var orderIndex = ServiceOrderList.IndexOf(serviceOrder);
ServiceOrderList.Remove(serviceOrder);
ServiceOrderList.Insert(orderIndex, newServiceOrder);
});
2 years ago
}
}
}