using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Quartz.Impl.AdoJobStore.Common;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VolPro.Core.DBManager;
using VolPro.Core.DbSqlSugar;
using VolPro.Core.EFDbContext;
using VolPro.Core.Extensions;
using VolPro.Entity.DomainModels;
namespace VolPro.Core.Print
{
public class PrintCustom : PrintFilter
{
public PrintCustom() { }
///
/// 主表查询
///
///
///
///
///
public override ISugarQueryable Query(ISugarQueryable query, PrintQuery parms) where T : class
{
//判断是哪张主表自定义过滤条件
if (typeof(T).Name == typeof(Demo_Order).Name)
{
var orderQuery = ((ISugarQueryable)query);
return orderQuery
//这里where可以写自定义条件
.Where(x => true) as ISugarQueryable;
}
return query;
}
///
/// 明细表查询
///
///
///
///
///
public override ISugarQueryable QueryDetail(ISugarQueryable query, PrintQuery parms) where Detail : class
{
//判断是哪张明细表自定义过滤条件
if (typeof(Detail).Name == typeof(Demo_OrderList).Name)
{
var orderQuery = ((ISugarQueryable)query);
return orderQuery
//这里where可以写自定义条件
.Where(x => true) as ISugarQueryable;
}
return query;
}
///
/// 对返回的结果自定义处理
///
///
///
///
///
///
public override List> QueryResult(
List> result,
PrintQuery parms,
BaseDbContext dbContext)
{
if (result.Count == 0) return result;
//判断表,自定义返回数据
if (typeof(T).Name == typeof(Demo_Order).Name)
{
//判断是哪个打印模板,然后自定义返回数据
if (parms.TemplateName == "订单管理主从明细表打印")
{
//返回DemoOrder表自定义配置
SetDemoOrderValue(result, parms, dbContext);
}
}
//告警统计数据
if (typeof(T).Name == typeof(Warehouse_Alert).Name)
{
//日报打印模板处理
if (parms.TemplateName == "统计日报")
{
//返回Alert表自定义配置
SetAlertValue(result, parms, dbContext);
}
}
return result;
}
///
/// 返回Alert表自定义配置
///
///
///
///
private void SetAlertValue(
List> result,
PrintQuery parms,
BaseDbContext dbContext)
{
//给明细表设置合计
//var data = dbContext.Set()
//设置自定义返回的字段(模板设计页面需要定义:单价合计、数量合计两个字段)
//result[0]["单价合计"] = data?.单价合计 ?? 0;
//result[0]["数量合计"] = data?.数量合计 ?? 0;
//result[0]这里还可以自定义其他字段设置值与模板设计页面定义的字段一致即可
var dic = new Dictionary();
dic.Add("单位名称", "1#仓库");
dic.Add("上报日期", DateTime.Now.ToString("D"));
dic.Add("截止时间", DateTime.Now.ToString("T"));
//parms.Ids是勾选的主表主键id
var options = parms.Options.DeserializeObject>();
List ids = parms.Ids.Select(s => s.GetGuid()).ToList();
if (options["month"] == null)
{
//自定义查询返回表格数据
var data = dbContext.Set().Where(x => ids.Contains(x.AlertId))
.Select(s => new
{
时间 = s.CreateDate,
告警区域 = s.Area,
事件描述 = s.Message,
处理结果 = s.Result,
负责人 = s.ChargePerson,
结果确认 = s.CheckResult,
确认人 = s.CheckPerson,
确认时间 = s.CheckTime
}).ToList();
dic.Add("告警记录", data);
}
else
{
//按月份查询返回表格数据
var dateStart = DateTime.Parse(options["month"][0], null, DateTimeStyles.RoundtripKind).ToLocalTime();
var dateEnd = DateTime.Parse(options["month"][1], null, DateTimeStyles.RoundtripKind).ToLocalTime();
var data = dbContext.Set()
.Where(x => x.CreateDate>=dateStart && x.CreateDate<=dateEnd)
.Select(s => new
{
时间 = s.CreateDate,
告警区域 = s.Area,
事件描述 = s.Message,
处理结果 = s.Result,
负责人 = s.ChargePerson,
结果确认 = s.CheckResult,
确认人 = s.CheckPerson,
确认时间 = s.CheckTime
}).ToList();
dic.Add("告警记录", data);
}
//返回库房管理系统的数据
var warehouseManageTable = dbContext.Set()
.Where(x => x.Type == "库房管理系统")//这里写条件
.Select(s => new
{
状态 = s.Status,//[名称]与[编号]是打印板自定义表格的里面输入的字段名
项目 = s.Title,
当日情况 = s.Situation,
正常值 = s.ReferenceValue,
结论 = s.Result
}).ToList();
var dicTable1 = new Dictionary();
dic.Add("库房管理系统", warehouseManageTable);
//返回钥匙管控系统的数据
var keyManageTable = dbContext.Set()
.Where(x => x.Type == "钥匙管控系统")//这里写条件
.Select(s => new
{
状态 = s.Status,//[名称]与[编号]是打印板自定义表格的里面输入的字段名
项目 = s.Title,
当日情况 = s.Situation,
正常值 = s.ReferenceValue,
结论 = s.Result
}).ToList();
var dicTable2 = new Dictionary();
dic.Add("钥匙管控系统", keyManageTable);
//返回门禁系统的数据
var doorManageTable = dbContext.Set()
.Where(x => x.Type == "门禁系统")//这里写条件
.Select(s => new
{
状态 = s.Status,//[名称]与[编号]是打印板自定义表格的里面输入的字段名
项目 = s.Title,
当日情况 = s.Situation,
正常值 = s.ReferenceValue,
结论 = s.Result
}).ToList();
var dicTable3 = new Dictionary();
dic.Add("门禁系统", doorManageTable);
//返回视频监控系统的数据
var cameraManageTable = dbContext.Set()
.Where(x => x.Type == "视频监控系统")//这里写条件
.Select(s => new
{
状态 = s.Status,//[名称]与[编号]是打印板自定义表格的里面输入的字段名
项目 = s.Title,
当日情况 = s.Situation,
正常值 = s.ReferenceValue,
结论 = s.Result
}).ToList();
var dicTable4 = new Dictionary();
dic.Add("视频监控系统", cameraManageTable);
result.Clear();
result.Add(dic);
}
///
/// 返回DemoOrder表自定义配置
///
///
///
///
private void SetDemoOrderValue(
List> result,
PrintQuery parms,
BaseDbContext dbContext)
{
//给明细表设置合计
var data = dbContext.Set()
//根据主表id查询返回明细表合计
.Where(x => x.Order_Id == parms.Ids[0].GetGuid())
.Select(s => new
{
单价合计 = SqlFunc.AggregateSum(s.Price),
数量合计 = SqlFunc.AggregateSum(s.Qty),
}).FirstOrDefault();
//设置自定义返回的字段(模板设计页面需要定义:单价合计、数量合计两个字段)
result[0]["单价合计"] = data?.单价合计 ?? 0;
result[0]["数量合计"] = data?.数量合计 ?? 0;
//result[0]这里还可以自定义其他字段设置值与模板设计页面定义的字段一致即可
////例如:再返回一些自定义的表格数据
//var otherTable = dbContext.Set()
// .Where(x => true)//这里写条件
// .Select(s => new
// {
// 名称 = s.ProductName,//[名称]与[编号]是打印板自定义表格的里面输入的字段名
// 编号 = s.ProductCode
// }).ToList();
//result[0]["字段名"] = otherTable;
}
}
}