HuNan_DM/DM_Weight/ViewModels/InvoiceInNewWindow2ViewMode...

627 lines
28 KiB
C#
Raw Permalink Normal View History

using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
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.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
using System.Windows.Data;
using Unity;
namespace DM_Weight.ViewModels
{
public class InvoiceInNewWindow2ViewModel : 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);
}
}
private int _status = 0;
public int Status { get => _status; set => SetProperty(ref _status, value); }
IDialogService _dialogService;
IEventAggregator _eventAggregator;
private DelegateCommand _rowSelected;
public DelegateCommand RowSelected => _rowSelected ??= new DelegateCommand(GetChannelByInvoice);
//private SqlSugarScope SqlSugarHelper.Db;
private PortUtil _portUtil;
public InvoiceInNewWindow2ViewModel(PortUtil portUtil, IDialogService DialogService, IEventAggregator eventAggregator, SqlSugarScope sqlSugarScope)
{
_portUtil = portUtil;
_dialogService = DialogService;
_eventAggregator = eventAggregator;
//this.SqlSugarHelper.Db = sqlSugarScope;
}
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 => true;
private ObservableCollection<ChannelList>? _channelLsts = new();
public ObservableCollection<ChannelList>? ChannelLsts
{
get => _channelLsts;
set => SetProperty(ref _channelLsts, value);
}
private List<ChannelStock> _channelStocks = new List<ChannelStock>();
public List<ChannelStock> ChannelStocks
{
get => _channelStocks;
set => SetProperty(ref _channelStocks, value);
}
private IEnumerable<IGrouping<int, ChannelStock>> enumerable;
private IEnumerator<IGrouping<int, ChannelStock>> enumerator;
private string WindowName = "InvoiceAddWindow";
//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();
ChannelLsts.Clear();
InOutInvoices.Clear();
List<ChannelStock> iChannelStock = new List<ChannelStock>();
if (SelectedInvoice != null)
{
string strSql = @"SELECT sum(Quantity) AS SumQuantity, COUNT(ID) AS CountNum,INVOICE_NO AS InvoiceNo,drug_id AS DrugId,QUANTITY AS quantity,drug_manu_no AS drugManuNo FROM IN_OUT_INVOICE WHERE INVOICE_NO=@INVOICE_NO GROUP BY INVOICE_NO,DRUG_ID";
var invoices = SqlSugarHelper.Db.SqlQueryable<InOutInvoice>(strSql)
.AddParameters(new
{
INVOICE_NO = SelectedInvoice.InvoiceNo
})
.Select(it => new InOutInvoice())
.Select("*").ToList();
//if(iList!=null&&iList.Count>0)
//{
// for (int i = 0; i < iList.Count; i++)
// {
// if (iList[i].Count>1)
// {
// for (int j = 0; j < iList[i].Count; j++)
// {
// }
// }
// }
//}
//var invoices = SqlSugarHelper.Db.Queryable<InOutInvoice>()
// .Includes<DrugInfo>(i => i.DrugInfo)
// .InnerJoin(SqlSugarHelper.Db.Queryable<ChannelList>().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)
// .ToList();
for (int i = 0; i < invoices.Count; i++)
{
List<ChannelList> queryData = SqlSugarHelper.Db.Queryable<ChannelList>()
.Includes(cl => cl.Drug, di => di.DrugManuNos)
.Includes(cl => cl.channelStocks, dr => dr.DrugInfo, d => d.DrugManuNos)
.Where(cl => cl.DrugId == invoices[i].DrugId)
.Where(cl => cl.DrawerType == 1)
.Where(cl => cl.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
//.Where(cl => cl.DrugId != null)
//.WhereIF(!string.IsNullOrEmpty(invoices[i].DrugManuNo), cl => cl.drugManuNo.Equals(invoices[i].DrugManuNo))
.OrderBy(cl => cl.ColNo)
.ToList();
List<ChannelList> channelLst = queryData.Select(cl =>
{
cl.channelStocks = cl.channelStocks.Select(cs =>
{
cs.drugManuNo = cl.Drug.DrugManuNos.Find(it => it.ManuNo.Equals(cs.ManuNo));
return cs;
}).ToList();
return cl;
}).OrderBy(cl => cl.ColNo).ToList();
channelLst.ForEach(cl => cl.channelStocks.ForEach(cs => cs.DrugInfo = cl.Drug));
if (channelLst.Count > 0)
{
if (channelLst[0].channelStocks.Count > 0)
{
//for (int j = 0; j < channelLst[0].channelStocks.Count; j++)
{
var invoicesManuNo = SqlSugarHelper.Db.Queryable<InOutInvoice>()
.Where(iManuNo => iManuNo.InvoiceNo == invoices[i].InvoiceNo && iManuNo.DrugId == invoices[i].DrugId).ToList();
for (int k = 0; k < invoicesManuNo.Count; k++)
{
List<ChannelStock> stockLst = channelLst[0].channelStocks.Where(cs => cs.ManuNo == invoicesManuNo[k].DrugManuNo).ToList();
if (stockLst != null && stockLst.Count > 0)
{
channelLst[0].channelStocks.Where(cs => cs.ManuNo == invoicesManuNo[k].DrugManuNo).ToList()[0].AddQuantity = invoicesManuNo[k].quantity; //.AddQuantity = invoicesManuNo[k].quantity;
}
else
{
ChannelStock cls = new ChannelStock();
DrugManuNo drugManu = new DrugManuNo();
cls.DrugInfo = channelLst[0].Drug;
List<DrugManuNo> drugManuNoList = channelLst[0].Drug.DrugManuNos;// SqlSugarHelper.Db.Queryable<DrugManuNo>().Where(dmn=>dmn.DrugId== invoicesManuNo[k].DrugId).ToList();
//if (channelLst[0].channelStocks.Count > 0)
//{
// //channelLst[0].channelStocks[0].AddQuantity = invoices[i].quantity;
// cls.drugManuNo = channelLst[0].channelStocks[0].drugManuNo;
//}
//else
{
//drugManu = cls.DrugInfo.DrugManuNos[0];
if (drugManuNoList != null&& drugManuNoList.Count>0)
{
cls.DrugInfo.DrugManuNos = drugManuNoList;
cls.drugManuNo = drugManuNoList.Where(dmn => dmn.ManuNo == invoicesManuNo[k].DrugManuNo).First();
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "该药品下没有批次,请先添加批次",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
}
cls.Chnguid = null;
cls.MachineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1";
cls.DrawerNo = channelLst[0].DrawerNo;
cls.BoardType = channelLst[0].BoardType;
cls.DrawerType = channelLst[0].DrawerType;
cls.ColNo = channelLst[0].ColNo;
cls.DrugId = channelLst[0].DrugId;
cls.AddQuantity = invoicesManuNo[k].quantity;
cls.ManuNo = invoicesManuNo[k].DrugManuNo;
channelLst[0].channelStocks.Add(cls);
}
}
}
}
else
{
List<ChannelStock> CSList = new List<ChannelStock>();
if (!string.IsNullOrEmpty(invoices[i].DrugManuNo))
{
//List<ChannelStock> stockLst = channelLst[i].channelStocks.Where(cs => cs.ManuNo == invoices[i].DrugManuNo).ToList();
//if (stockLst != null && stockLst.Count > 0)
//{
// channelLst[i].channelStocks[i].AddQuantity = invoices[i].quantity;
//}
//else
{
ChannelStock cls = new ChannelStock();
DrugManuNo drugManu = new DrugManuNo();
cls.DrugInfo = channelLst[0].Drug;
if (channelLst[0].channelStocks.Count > 0)
{
channelLst[0].channelStocks[0].AddQuantity = invoices[i].quantity;
cls.drugManuNo = channelLst[0].channelStocks[0].drugManuNo;
}
else
{
//drugManu= cls.DrugInfo.DrugManuNos[0];
if (cls.DrugInfo.DrugManuNos.Count > 0)
{
//cls.drugManuNo = cls.DrugInfo.DrugManuNos[0];
cls.drugManuNo = cls.DrugInfo.DrugManuNos.Where(dm => dm.ManuNo == invoices[i].DrugManuNo).First();
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "该药品下没有批次,请先添加批次",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
}
cls.Chnguid = channelLst[0].Id;
cls.MachineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1";
cls.DrawerNo = channelLst[0].DrawerNo;
cls.BoardType = channelLst[0].BoardType;
cls.DrawerType = channelLst[0].DrawerType;
cls.ColNo = channelLst[0].ColNo;
cls.DrugId = channelLst[0].DrugId;
cls.AddQuantity = invoices[i].quantity;
List<ChannelStock> stockList = new List<ChannelStock>();
stockList.AddRange(channelLst[0].channelStocks);
stockList.Add(cls);
channelLst[0].channelStocks = stockList;
}
}
}
}
else
{
//药品未绑定库位,需要先绑药
AlertMsg alertMsg = new AlertMsg
{
Message = "药品未绑定库位,请先绑定库位",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
InOutInvoice copy = TransExpV2<InOutInvoice, InOutInvoice>.Trans(invoices[i]);
InOutInvoices.Add(copy);
ChannelLsts.AddRange(channelLst);
}
}
}
public DelegateCommand OpenInvoiceAdd
{
get => new DelegateCommand(() =>
{
Status = 1;
bool flag = true;
var list = ChannelLsts.GroupBy(cl => cl.DrugId).ToList();
if (list != null && list.Count == InOutInvoices.Count)
{
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))
if (invoices.SumQuantity != ChannelLsts[i].channelStocks.FindAll(it => it.DrugId == invoices.DrugId).Sum(it => it.AddQuantity))
{
flag = false;
break;
}
}
}
if (flag)
{
ChannelStocks.Clear();
foreach (ChannelList lst in ChannelLsts)
{
ChannelStocks.AddRange(lst.channelStocks);
}
AddChannels = ChannelStocks.FindAll(it => it.AddQuantity != 0);
//AddChannels = ChannelLsts[i].channelStocks.FindAll(it => it.AddQuantity != 0);
if (AddChannels.Count > 0)
{
for (int i = 0; i < AddChannels.Count; i++)
{
AddChannels[i].ManuNo = AddChannels[i].drugManuNo.ManuNo;
AddChannels[i].EffDate = AddChannels[i].drugManuNo.EffDate;
}
AddChannels.Sort((a, b) =>
{
if ((a.DrawerNo - b.DrawerNo) == 0)
{
return a.ColNo - b.ColNo;
}
return a.DrawerNo - b.DrawerNo;
});
}
//enumerable = ChannelStocks.GroupBy(cs => cs.DrawerNo, cs => cs);
//enumerator = enumerable.GetEnumerator();
//enumerator.MoveNext();
//OpenOneByOne();
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(i.quantity) as quantity, p1.pharmacy_name as PharmacyName1, p2.pharmacy_name as PharmacyName2 from in_out_invoice i");
sb.Append(" SELECT count(1) as Count, i.InvoiceNo, i.InvoiceDate, sum(i.quantity) as quantity, p1.pharmacy_name as PharmacyName1, p2.pharmacy_name PharmacyName2 from ");
sb.Append(" (SELECT drug_id,in_pharmacy_id,out_pharmacy_id, invoice_no as InvoiceNo, DATE_FORMAT(Invoice_Date,'%Y-%m-%d') as InvoiceDate, SUM(quantity) as quantity ");
sb.Append(" FROM in_out_invoice where status=@Status and type!=@type and cancel_flag=@CancelFlag GROUP BY invoice_no,drug_id) i ");
sb.Append(" inner join ( select c.drug_id as drug_id from channel_list 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_id = i.in_pharmacy_id");
sb.Append(" left join pharmacy_info p2 on p2.pharmacy_id = i.out_pharmacy_id where 1=1");
//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 i.InvoiceDate = @CreateTime ");
}
if (!String.IsNullOrEmpty(SearchValue))
{
sb.Append(" and i.InvoiceNo = @InvoiceNo ");
}
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["storage"]))
{
sb.Append(" and i.in_pharmacy_id = @OutPharmacyId ");
}
//sb.Append(" group by i.invoice_no");
sb.Append(" GROUP BY i.InvoiceNo order by i.InvoiceDate ");
Invoices = SqlSugarHelper.Db.SqlQueryable<dynamic>(sb.ToString())
.AddParameters(new
{
Status = 0,
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 AddAction(ChannelList channelLS)
{
if (channelLS != null)
{
ChannelStock cls = new ChannelStock();
DrugManuNo drugManu = new DrugManuNo();
cls.DrugInfo = channelLS.Drug;
if (channelLS.channelStocks.Count > 0)
{
cls.drugManuNo = channelLS.channelStocks[0].drugManuNo;
}
else
{
//drugManu= cls.DrugInfo.DrugManuNos[0];
if (cls.DrugInfo.DrugManuNos.Count > 0)
{
cls.drugManuNo = cls.DrugInfo.DrugManuNos[0];
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "该药品下没有批次,请先添加批次",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
}
//cls.Id = "";
cls.Chnguid = channelLS.Id;
cls.MachineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1";
cls.DrawerNo = channelLS.DrawerNo;
cls.BoardType = channelLS.BoardType;
cls.DrawerType = channelLS.DrawerType;
cls.ColNo = channelLS.ColNo;
cls.DrugId = channelLS.DrugId;
cls.AddQuantity = 0;
List<ChannelStock> stockList = new List<ChannelStock>();
stockList.AddRange(channelLS.channelStocks);
stockList.Add(cls);
channelLS.channelStocks = stockList;
}
}
//接收导航传过来的参数 现在是在此处初始化了表格数据
public void OnNavigatedTo(NavigationContext navigationContext)
{
_eventAggregator.GetEvent<AddDrugEvent>().Subscribe(AddAction);
RequestData();
}
//每次导航的时候该实列用不用重新创建true是不重新创建,false是重新创建
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//这个方法用于拦截请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
_eventAggregator.GetEvent<AddDrugEvent>().Unsubscribe(AddAction);
}
}
}