HuNan_DM/DM_Weight/ViewModels/MachineRecordWindowViewMode...

303 lines
9.2 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 Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DM_Weight.Models;
using DM_Weight.Port;
using DM_Weight.Report;
using DM_Weight.util;
using gregn6Lib;
using Newtonsoft.Json;
using System.IO;
using System.Configuration;
using System.Reflection.PortableExecutable;
using DM_Weight.msg;
using Prism.Events;
namespace DM_Weight.ViewModels
{
public class MachineRecordWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
{
public static MachineRecordWindowViewModel vm;
IEventAggregator _eventAggregator;
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);
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 int _type;
public int Type
{
get { return _type; }
set
{
SetProperty(ref _type, value);
}
}
private DrugInfo? _drugInfo;
public DrugInfo? DrugInfo
{
get => _drugInfo;
set
{
SetProperty(ref _drugInfo, value);
RequestData();
}
}
private List<DrugInfo>? _drugInfos;
public List<DrugInfo>? DrugInfos
{
get => _drugInfos;
set => SetProperty(ref _drugInfos, value);
}
private List<MachineRecord>? machineRecords;
public List<MachineRecord>? MachineRecords
{
get { return machineRecords; }
set { SetProperty(ref machineRecords, value); }
}
public MachineRecordWindowViewModel(IEventAggregator eventAggregator)
{
vm = this;
_eventAggregator = eventAggregator;
}
public bool KeepAlive => false;
public DelegateCommand Query
{
get => new DelegateCommand(() =>
{
RequestData();
});
}
public DelegateCommand Download
{
get => new DelegateCommand(() =>
{
GridReportUtil.PrintReportMechineRecord(Type, StartDate, EndDate);
});
}
public DelegateCommand DownloadAccountBook
{
get => new DelegateCommand(() =>
{
GridReportUtil.PrintReportAccountBook(StartDate, EndDate, DrugInfo==null?"":DrugInfo.DrugId);
});
}
public DelegateCommand SaveCommand
{
get => new DelegateCommand(() =>
{
//保存修改的凭证号
UpdatPZHSave();
});
}
private void UpdatPZHSave()
{
var f = SqlSugarHelper.Db.UseTran(() =>
{
for (int i = 0; i < MachineRecords.Count; i++)
{
// 更新数据 库存信息
SqlSugarHelper.Db.Updateable(new MachineRecord()
{
Id = MachineRecords[i].Id,
InvoiceId = MachineRecords[i].InvoiceId
}).UpdateColumns(it => new { it.InvoiceId }).WhereColumns(it => new { it.Id }).ExecuteCommand();
}
return true;
});
if (f.Data)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "保存成功",
Type = MsgType.SUCCESS
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
if (!f.IsSuccess)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "操作失败!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
void ReportInitialize()
{
}
//这个方法用于拦截请求,continuationCallback(true)就是不拦截continuationCallback(false)拦截本次操作
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
continuationCallback(true);
}
//接收导航传过来的参数
public void OnNavigatedTo(NavigationContext navigationContext)
{
//取出Type决定页面显示内容
Type = navigationContext.Parameters.GetValue<int>("Type");
//查询表格数据
RequestData();
GetAllDrugInfos();
}
void RequestData()
{
int totalCount = 0;
string machineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1";
MachineRecords = SqlSugarHelper.Db.Queryable<MachineRecord>()
.Includes<DrugInfo>(mr => mr.DrugInfo)
.Includes<UserList>(mr => mr.User)
.Where(mr => mr.MachineId == machineId)
.WhereIF(Type == 3, (mr) => new int[] { 31, 32 }.Contains(mr.Type))
.WhereIF(Type != 3, (mr) => mr.Type == Type)
.WhereIF(StartDate != null, (mr) => mr.OperationTime > StartDate)
.WhereIF(EndDate != null, (mr) => mr.OperationTime < EndDate)
.WhereIF(DrugInfo != null, (mr) => mr.DrugId == DrugInfo.DrugId)
//.Select(mr => mr)
.ToPageList(PageNum, PageSize, ref totalCount);
//.ToList();
TotalCount = totalCount;
}
//每次导航的时候该实列用不用重新创建true是不重新创建,false是重新创建
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//这个方法用于拦截请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
private void GetAllDrugInfos()
{
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,CONCAT(drug_name,' ',drug_spec)as drug_name_spec FROM `drug_info` d";
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
}
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,CONCAT(drug_name,' ',drug_spec)as drug_name_spec FROM `drug_info` d";
if (string.IsNullOrEmpty(text))
{
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
return;
}
if (DrugInfos != null)
{
DrugInfos.Clear();
}
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).Where(di => di.DrugName.Contains(text) || di.PyCode.Contains(text) || di.DrugId.Contains(text)).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
}
}
}