using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Report;
using DM_Weight.util;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace DM_Weight.ViewModels
{
public class AccountWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
{
public static AccountWindowViewModel vm;
//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 DateTime? _startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
private DateTime? nowDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
public DateTime? StartDate
{
get => _startDate;
set
{
if (value != null)
{
SetProperty(ref _startDate, new DateTime(value?.Year ?? 0, value?.Month ?? 0, value?.Day ?? 0));
}
else
{
SetProperty(ref _startDate, value);
}
RequestData();
}
}
private DateTime? _endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
public DateTime? EndDate
{
get => _endDate;
set
{
if (value != null)
{
TimeSpan ershisi = new TimeSpan(23, 59, 59);
SetProperty(ref _endDate, new DateTime(value?.Year ?? 0, value?.Month ?? 0, value?.Day ?? 0, 23, 59, 59));
}
else
{
SetProperty(ref _endDate, value);
}
RequestData();
}
}
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
///
/// 账册
///
private List _accountList = new();
public List AccountList
{
get => _accountList;
set => SetProperty(ref _accountList, value);
}
///
/// 药品
///
private List? _drugInfos;
public List? DrugInfos
{
get => _drugInfos;
set => SetProperty(ref _drugInfos, value);
}
private DrugInfo? _drugInfo;
public DrugInfo? DrugInfo
{
get => _drugInfo;
set
{
SetProperty(ref _drugInfo, value);
//if (_drugInfo != null)
//{
//DrugManuNos = _drugInfo.DrugManuNos.OrderByDescending(dm => dm.ManuNo).ToList();
//}
RequestData();
}
}
///
/// 账册类型1入2出3总结存
///
private List _accountTypeList = new List() {
new AccountType
{
AccountTypeName="借入",
AccountTypeValue=1
},
new AccountType
{
AccountTypeName="发出",
AccountTypeValue=2
},
new AccountType
{
AccountTypeName="日结存",
AccountTypeValue=3
},
new AccountType
{
AccountTypeName="总结存",
AccountTypeValue=4
}
};
public List? AccountTypeList
{
get => _accountTypeList;
set
{
SetProperty(ref _accountTypeList, value);
RequestData();
}
}
private AccountType? _accountType;
public AccountType AccountType
{
get => _accountType;
set
{
SetProperty(ref _accountType, value);
RequestData();
}
}
/////
///// 药品类型
/////
//private List _drugTypeList = new List()
//{
// new DrugType{ TypeValue=1,TypeName="精一"},
// new DrugType{TypeValue=2,TypeName="精二"}
//};
//public List? DrugTypeList
//{
// get => _drugTypeList;
// set
// {
// SetProperty(ref _drugTypeList, value);
// }
//}
//private DrugType? _drugType;
//public DrugType? _DrugType
//{
// get => _drugType; set
// {
// SetProperty(ref _drugType, value);
// RequestData();
// }
//}
//MachineRecordService _machineRecordService;
IEventAggregator _eventAggregator;
////private SqlSugarScope SqlSugarHelper.Db;
public AccountWindowViewModel(IEventAggregator eventAggregator)
{
//_machineRecordService = machineRecord;
////this.SqlSugarHelper.Db = sqlSugarScope;
_eventAggregator = eventAggregator;
vm = this;
}
void RequestData()
{
int totalCount = 0;
AccountList = ReportAccountBook(StartDate, EndDate, AccountType == null ? 0 : AccountType.AccountTypeValue, DrugInfo == null ? "" : DrugInfo.DrugId.ToString() ?? "");
//for (int i = 0; i < accountList.Count; i++)
//{
// if (accountList[i].DrugInfo is null)
// {
// DrugInfo drug= new DrugInfo();
// drug.DrugId = accountList[i].DrugId;
// drug.DrugName = accountList[i].DrugName;
// drug.DrugSpec = accountList[i].DrugSpec;
// drug.Dosage= accountList[i].Dosage;
// drug.PackUnit= accountList[i].PackUnit;
// drug.Manufactory= accountList[i].Manufactory;
// accountList[i].DrugInfo= drug;
// }
//}
//ICollectionView vw = CollectionViewSource.GetDefaultView(accountList);
//vw.GroupDescriptions.Add(new PropertyGroupDescription("DrugInfo"));
}
private void GetAllDrugInfos()
{
var list = SqlSugarHelper.Db.Queryable().Includes(di => di.DrugManuNos).OrderBy(di => di.DrugId).ToList();
DrugInfos = list;
DrugInfo = list[0];
}
public long CurrentTimeMillis()
{
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
///
/// 导出发药登记表
///
public DelegateCommand DownloadOrderUser
{
get => new DelegateCommand(() =>
{
GridReportUtil.OrderUseReport(StartDate, EndDate, DrugInfo == null ? "" : DrugInfo.DrugId.ToString() ?? "");
});
}
///
/// 导出账册
///
public DelegateCommand DownloadAccountBook
{
get => new DelegateCommand(() =>
{
GridReportUtil.PrintReportAccountBook(StartDate, EndDate, AccountType == null ? 0 : AccountType.AccountTypeValue, DrugInfo == null ? "" : DrugInfo.DrugId.ToString() ?? "");
});
}
///
/// 刷新
///
public DelegateCommand Query
{
get => new DelegateCommand(() =>
{
RequestData();
});
}
public bool KeepAlive => true;
public void UpdateComboBoxItems(string text)
{
string str = @"SELECT d.drug_id,d.py_code,d.drug_barcode,d.drug_name,d.drug_brand_name,d.drug_spec,d.dosage,d.pack_unit,
d.manufactory,d.max_stock FROM `drug_info` d";
if (string.IsNullOrEmpty(text))
{
DrugInfos = SqlSugarHelper.Db.SqlQueryable(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
return;
}
if (DrugInfos != null)
{
DrugInfos.Clear();
}
//DrugInfos = SqlSugarHelper.Db.SqlQueryable(str).Where(di => di.DrugName.Contains(text) || di.PyCode.Contains(text) || di.DrugId.Contains(text)).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
//药品信息
GetAllDrugInfos();
//查询表格数据
RequestData();
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
private List ReportAccountBook(DateTime? startDate, DateTime? endDate, int type, string drug_id = "")
{
DateTime? p_startDate = startDate ?? Convert.ToDateTime("2010-1-1");
DateTime? p_endDate = endDate ?? DateTime.Now.AddDays(1);
string p_machine_id = (ConfigurationManager.AppSettings["machineId"] ?? "DM3");
string SQL = $@" SELECT ac.create_date as CreateDate, ac.TYPE,
if(ac.type in(1,2),0,ac.yesterday_quantity) as YQuantity,if(ac.type in(3,4),0,ac.add_quantity) as AddQuantity,if(ac.type in(3,4),0,ac.out_quantity) as OutQuantity,
if(ac.type in(1,2),0,ac.manu_stock) as ManuStock,ac.total_stock, -- if(ac.type in(1,2),0,ac.total_stock) as TotalStock,
ac.invoice_no as InvoiceNo, ac.manu_no as ManuNo,ac.eff_date as EffDate,di.drug_id,di.drug_name as DrugName,di.drug_spec as DrugSpec,di.manufactory as Manufactory,di.pack_unit,di.dosage,u1.user_name as OperatorName,u2.user_name as ReviewerName
FROM account_book_g2 ac left join drug_info di on ac.drug_id=di.drug_id left join user_list u1 on ac.user_id1=u1.id left join user_list u2 on ac.user_id2=u2.id
WHERE ac.machine_id='{p_machine_id}' and create_time>'{p_startDate}' AND create_time<'{p_endDate}' ";
if (!string.IsNullOrEmpty(drug_id))
{
SQL += " AND ac.drug_id='" + drug_id + "' ";
}
if (type > 0)
{
if (type == 1)
{
SQL += " AND ac.add_quantity>0 ";
}
if (type == 2)
{
SQL += " AND ac.out_quantity>0 ";
}
if (type == 3)
{
SQL += " AND type=3 ";
}
if (type == 4)
{
SQL += " AND type=4 ";
}
}
SQL += " ORDER BY ac.create_date desc,ac.drug_id";
//ChannelStocks = q.Select(it => { it.CheckQuantity = it.Quantity; return it; }).ToList();
List accountList = SqlSugarHelper.Db.SqlQueryable(SQL)
//.AddParameters(new
//{
// machineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1"
//})
//.Select(it => new { o = new AccountModel(), i = new DrugInfo() })
//.Select(it=>new AccountModel())
//.Select("*") //让上面一行不生成sql
.ToList();
//List accountList=new List();
return accountList;
}
}
}