XiangTan_DM/DM_Weight/ViewModels/InvoiceInWindowViewModel.cs

449 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.select;
using DM_Weight.util;
using log4net;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Markup;
namespace DM_Weight.ViewModels
{
public class InvoiceInWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
{
private readonly ILog logger = LogManager.GetLogger(typeof(InvoiceInWindowViewModel));
private int _pageNum = 1;
public int PageNum
{
get => _pageNum;
set
{
SetProperty(ref _pageNum, value);
RequestData();
}
}
private int _pageCount = 1;
public int PageCount
{
get => _pageCount;
set
{
SetProperty(ref _pageCount, value);
}
}
private int _pageSize = 8;
public int PageSize
{
get => _pageSize;
set
{
SetProperty(ref _pageSize, value);
}
}
private int _totalCount = 0;
public int TotalCount
{
get => _totalCount;
set
{
SetProperty(ref _totalCount, value);
}
}
IDialogService _dialogService;
IEventAggregator _eventAggregator;
private DelegateCommand _rowSelected;
public DelegateCommand RowSelected => _rowSelected ??= new DelegateCommand(GetChannelByInvoice);
public InvoiceInWindowViewModel(IDialogService DialogService, IEventAggregator eventAggregator)
{
_dialogService = DialogService;
_eventAggregator = eventAggregator;
}
public static List<OrderTakeSelect> StaticOrderTakeSelects = new()
{
new OrderTakeSelect
{
Code = "invoiceNo",
Name = "凭证单号"
}
};
private List<OrderTakeSelect> _orderTakeSelects = StaticOrderTakeSelects;
public List<OrderTakeSelect> OrderTakeSelects
{
get { return _orderTakeSelects; }
set
{
SetProperty(ref _orderTakeSelects, value);
}
}
private OrderTakeSelect _selectedItem = StaticOrderTakeSelects[0];
/// <summary>
/// 查询条件 查询字段
/// </summary>
public OrderTakeSelect SelectedItem
{
get { return _selectedItem; }
set
{
SetProperty(ref _selectedItem, value);
RequestData();
}
}
private Invoice? _selectedInvoice;
public Invoice? SelectedInvoice
{
get { return _selectedInvoice; }
set
{
SetProperty(ref _selectedInvoice, value);
//OpenOrderDialog();
}
}
private string _orderDate = DateTime.Now.ToString("yyyy-MM-dd");
/// <summary>
/// 查询条件 处方日期
/// </summary>
public string OrderDate
{
get { return _orderDate; }
set
{
if (!String.IsNullOrEmpty(value))
{
SetProperty(ref _orderDate, DateTime.Parse(value).ToString("yyyy-MM-dd"));
}
else
{
SetProperty(ref _orderDate, value);
}
RequestData();
}
}
private string? _searchValue;
/// <summary>
/// 查询条件 查询字段值
/// </summary>
public string? SearchValue
{
get { return _searchValue; }
set
{
SetProperty(ref _searchValue, value);
RequestData();
}
}
private List<Invoice> _invoices = new();
public List<Invoice> Invoices { get { return _invoices; } set { SetProperty(ref _invoices, value); } }
private List<InOutInvoice> _inOutInvoices = new();
public List<InOutInvoice> InOutInvoices { get { return _inOutInvoices; } set { SetProperty(ref _inOutInvoices, value); } }
public bool KeepAlive => false;
private List<ChannelStock> _channelStocks = new();
public List<ChannelStock> ChannelStocks { get { return _channelStocks; } set { SetProperty(ref _channelStocks, value); } }
private List<ChannelStock> _addChannels = new();
public List<ChannelStock> AddChannels { get { return _addChannels; } set { SetProperty(ref _addChannels, value); } }
public void GetChannelByInvoice()
{
ChannelStocks.Clear();
InOutInvoices.Clear();
List<ChannelStock> i = new List<ChannelStock>();
//string strMsg = string.Empty;
if (SelectedInvoice != null)
{
var invoices = SqlSugarHelper.Db.Queryable<InOutInvoice>()
.LeftJoin(
SqlSugarHelper.Db.Queryable<ChannelStock>()
.Where(cs => cs.DrawerType == 1)
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
.GroupBy(cs => cs.DrugId), (i, t) => i.DrugId == t.DrugId
)
.Includes<DrugInfo>(i => i.DrugInfo)
.Where(i => i.InvoiceNo == SelectedInvoice.InvoiceNo && i.Status == 1 && i.Type == 2 && i.CancelFlag == 0)
.MergeTable().GroupBy(i => new { i.DrugId, i.DrugManuNo })
.Select(i => new InOutInvoice
{
DrugId = i.DrugId,
DrugEffDate = i.DrugEffDate,
DrugManuNo = i.DrugManuNo,
quantity = SqlFunc.AggregateSum(i.quantity),
DrugInfo=i.DrugInfo
})
.ToList();
//.Select((i, t) => new InOutInvoice
// {
// DrugId = i.DrugId,
// DrugEffDate = i.DrugEffDate,
// DrugManuNo = i.DrugManuNo,
// quantity = i.quantity,
// DrugInfo = i.DrugInfo
// }).ToList();
foreach (var invoice in invoices)
{
List<ChannelStock> q = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Includes<DrugInfo>(cs => cs.DrugInfo)
.WhereIF(!string.IsNullOrEmpty(invoice.DrugEffDate), cs => cs.EffDate.Equals(invoice.DrugEffDate.Substring(0, 10)))
.WhereIF(!string.IsNullOrEmpty(invoice.DrugManuNo), cs => cs.ManuNo.Equals(invoice.DrugManuNo))
.Where(cs => cs.DrugId == invoice.DrugId)
.Where(cs => cs.DrawerType == 1)
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
.OrderBy(cs => cs.DrugId)
.OrderBy(cs => cs.DrawerNo)
.OrderBy(cs => cs.ColNo)
.ToList();
if (q.Count <= 0)
{
//strMsg += $"药品{invoice.DrugInfo.DrugName}未绑定;";
ChannelStock cs = new ChannelStock();
cs.ColNo = -1;
cs.EffDate = invoice.DrugEffDate;
cs.ManuNo = invoice.DrugManuNo;
cs.DrugInfo = invoice.DrugInfo;
cs.Quantity = 0;
q.Add(cs);
}
for (int j = 0; j < q.Count; j++)
{
ChannelStock item = q[j];
if (j == 0)
{
invoice.quantity = invoice.Units == "盒" ? invoice.quantity * invoice.DrugInfo.ConvertRatio : invoice.quantity;
item.AddQuantity = invoice.quantity;
}
item.Invoice = invoice;
i.Add(item);
}
InOutInvoice copy = TransExpV2<InOutInvoice, InOutInvoice>.Trans(invoice);
InOutInvoices.Add(copy);
}
}
ICollectionView vw = CollectionViewSource.GetDefaultView(i);
vw.GroupDescriptions.Add(new PropertyGroupDescription("Invoice"));
ChannelStocks = i;
//if (ChannelStocks == null || ChannelStocks.Count <= 0)
//{
// AlertMsg alertMsg = new AlertMsg
// {
// Message = "调拨单内药品未绑定,请先绑定",
// Type = MsgType.ERROR,
// };
// _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
// return;
//}
//if (strMsg.Length > 0)
//{
// AlertMsg alertMsg = new AlertMsg
// {
// Message = strMsg,
// Type = MsgType.ERROR,
// };
// _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
// return;
//}
}
public DelegateCommand OpenInvoiceAdd
{
get => new DelegateCommand(() =>
{
bool flag = true;
//for (int i = 0; i < InOutInvoices.Count; i++)
//{
// InOutInvoice invoices = InOutInvoices[i];
// if (invoices.quantity != ChannelStocks.FindAll(it => it.DrugId == invoices.DrugId).Sum(it => it.AddQuantity))
// {
// flag = false;
// break;
// }
//}
if (flag)
{
AddChannels = ChannelStocks.FindAll(it => it.AddQuantity != 0 && it.Location != "未绑定");
if (AddChannels == null || AddChannels.Count <= 0)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "药品未绑定或未输入数量",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
AddChannels.Sort((a, b) =>
{
if ((a.DrawerNo - b.DrawerNo) == 0)
{
return a.ColNo - b.ColNo;
}
return a.DrawerNo - b.DrawerNo;
});
OpenOrderDialog();
}
//else
//{
// AlertMsg alertMsg = new AlertMsg
// {
// Message = "库位添加数量小于应添加数量",
// Type = MsgType.ERROR,
// };
// _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
//}
}, () => SelectedInvoice != null).ObservesProperty(() => SelectedInvoice);
}
public async void OpenOrderDialog()
{
if (SelectedInvoice != null && SelectedInvoice.Status == 0)
{
// 此处延时1毫秒等待页面渲染
await Task.Delay(TimeSpan.FromMilliseconds(1));
DialogParameters dialogParameters = new DialogParameters();
dialogParameters.Add("invoice", SelectedInvoice);
dialogParameters.Add("ChannelStocks", AddChannels);
DialogServiceExtensions.ShowDialogHost(_dialogService, "InvoiceAddDialog", dialogParameters, DoDialogResult, "RootDialog");
}
}
public DelegateCommand QueryCommand
{
get => new DelegateCommand(() =>
{
RequestData();
});
}
private void DoDialogResult(IDialogResult dialogResult)
{
// 委托 被动执行 被子窗口执行
// dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态
//if (dialogResult.Result == ButtonResult.OK)
{
SelectedInvoice = null;
RequestData();
}
//MessageBox.Show("返回值:" + dialogResult.Result.ToString());
}
//这个方法用于拦截请求,continuationCallback(true)就是不拦截continuationCallback(false)拦截本次操作
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
continuationCallback(true);
}
public void RequestData()
{
Invoices.Clear();
int totalCount = 0;
var sb = new StringBuilder();
sb.Append("select i.invoice_no as InvoiceNo, i.invoice_date as InvoiceDate, COUNT(i.id) as `Count`, SUM(IF(i.units='盒',i.quantity*dr.convert_ratio,i.quantity)) as quantity, p1.pharmacy_name as PharmacyName1, p2.pharmacy_name as PharmacyName2 from in_out_invoice i");
sb.Append(" inner join drug_info dr on i.drug_id=dr.drug_id ");
sb.Append(" left join ( select c.drug_id as drug_id from channel_stock c where c.machine_id = '" + (ConfigurationManager.AppSettings["machineId"] ?? "DM1") + "' group by c.drug_id ) di on di.drug_id = i.drug_id");
sb.Append(" left join pharmacy_info p1 on p1.pharmacy = i.in_pharmacy_id");
sb.Append(" left join pharmacy_info p2 on p2.pharmacy = i.out_pharmacy_id");
sb.Append(" where i.status=@Status ");
sb.Append(" and i.type=@type ");
sb.Append(" and i.cancel_flag=@CancelFlag ");
if (OrderDate != null)
{
sb.Append(" and DATE_FORMAT(i.invoice_date,'%Y-%m-%d') = @CreateTime ");
}
if (!String.IsNullOrEmpty(SearchValue))
{
sb.Append(" and i.invoice_no = @InvoiceNo ");
}
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["storage"]))
{
sb.Append(" and i.in_pharmacy_id = @OutPharmacyId ");
}
sb.Append(" group by i.invoice_no");
sb.Append(" order by i.invoice_date ");
Invoices = SqlSugarHelper.Db.SqlQueryable<dynamic>(sb.ToString())
.AddParameters(new
{
Status = 1,
type = 2,
CancelFlag = 0,
CreateTime = OrderDate,
InvoiceNo = SearchValue,
OutPharmacyId = ConfigurationManager.AppSettings["storage"]
})
.Select(it => new Invoice())
.Select("*")
.ToPageList(PageNum, PageSize, ref totalCount);
TotalCount = totalCount;
PageCount = (int)Math.Ceiling((double)TotalCount / PageSize);
}
//接收导航传过来的参数 现在是在此处初始化了表格数据
public void OnNavigatedTo(NavigationContext navigationContext)
{
RequestData();
}
//每次导航的时候该实列用不用重新创建true是不重新创建,false是重新创建
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//这个方法用于拦截请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
}
}