2024-12-03 13:22:42 +08:00
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 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 > ( ) ;
2024-12-19 19:55:35 +08:00
//string strMsg = string.Empty;
2024-12-03 13:22:42 +08:00
if ( SelectedInvoice ! = null )
{
var invoices = SqlSugarHelper . Db . Queryable < InOutInvoice > ( )
. Includes < DrugInfo > ( i = > i . DrugInfo )
2024-12-19 19:55:35 +08:00
. 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 )
. Where ( i = > i . InvoiceNo = = SelectedInvoice . InvoiceNo & & i . Status = = 1 & & i . Type = = 2 & & i . CancelFlag = = 0 )
2024-12-03 13:22:42 +08:00
. ToList ( ) ;
foreach ( var invoice in invoices )
{
List < ChannelStock > q = SqlSugarHelper . Db . Queryable < ChannelStock > ( )
. Includes < DrugInfo > ( cs = > cs . DrugInfo )
2024-12-19 19:55:35 +08:00
. WhereIF ( ! string . IsNullOrEmpty ( invoice . DrugEffDate ) , cs = > cs . EffDate . Equals ( invoice . DrugEffDate . Substring ( 0 , 10 ) ) )
2024-12-03 13:22:42 +08:00
. 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 ( ) ;
2024-12-19 19:55:35 +08:00
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 + + )
2024-12-03 13:22:42 +08:00
{
ChannelStock item = q [ j ] ;
2024-12-19 19:55:35 +08:00
if ( j = = 0 )
2024-12-03 13:22:42 +08:00
{
2024-12-19 19:55:35 +08:00
invoice . quantity = invoice . Units = = "盒" ? invoice . quantity * invoice . DrugInfo . ConvertRatio : invoice . quantity ;
2024-12-03 13:22:42 +08:00
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 ;
2024-12-19 19:55:35 +08:00
//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;
//}
2024-12-03 13:22:42 +08:00
}
public DelegateCommand OpenInvoiceAdd
{
get = > new DelegateCommand ( ( ) = >
{
bool flag = true ;
2024-12-19 19:55:35 +08:00
//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;
// }
//}
2024-12-03 13:22:42 +08:00
if ( flag )
{
2024-12-19 19:55:35 +08:00
AddChannels = ChannelStocks . FindAll ( it = > it . AddQuantity ! = 0 & & it . Location ! = "未绑定" ) ;
2024-12-03 13:22:42 +08:00
AddChannels . Sort ( ( a , b ) = >
{
if ( ( a . DrawerNo - b . DrawerNo ) = = 0 )
{
return a . ColNo - b . ColNo ;
}
return a . DrawerNo - b . DrawerNo ;
} ) ;
OpenOrderDialog ( ) ;
}
2024-12-19 19:55:35 +08:00
//else
//{
// AlertMsg alertMsg = new AlertMsg
// {
// Message = "库位添加数量小于应添加数量",
// Type = MsgType.ERROR,
// };
// _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
//}
} , ( ) = > SelectedInvoice ! = null ) . ObservesProperty ( ( ) = > SelectedInvoice ) ;
2024-12-03 13:22:42 +08:00
}
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 ( ) ;
2024-12-19 19:55:35 +08:00
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" ) ;
2024-12-03 13:22:42 +08:00
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 " ) ;
2024-12-19 19:55:35 +08:00
sb . Append ( " and i.type=@type " ) ;
2024-12-03 13:22:42 +08:00
sb . Append ( " and i.cancel_flag=@CancelFlag " ) ;
if ( OrderDate ! = null )
{
2024-12-19 19:55:35 +08:00
sb . Append ( " and DATE_FORMAT(i.invoice_date,'%Y-%m-%d') = @CreateTime " ) ;
2024-12-03 13:22:42 +08:00
}
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
{
2024-12-19 19:55:35 +08:00
Status = 1 ,
2024-12-03 13:22:42 +08:00
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 )
{
}
}
}