# 自定义打印内容(打印合计、明细表合计、打印表格合计配置)

1、主从明细表、一对多打印默认都没有合计,需要单独配置字段与后台返回
2、这里只是以自定义返回合计举例,其他自定义的内容(返回自定义字段或者其他自定义表格)操作一致

# 自定义打印页面内容

An image

# 自定义打印模板配置

An image

# 后台代码配置
/************在PrintCustom.cs中实现QueryResult方法,并根据条件判断调用自定义方法**************/

/// <summary>
/// 对返回的结果自定义处理
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="Detail"></typeparam>
/// <param name="result"></param>
/// <param name="parms"></param>
/// <returns></returns>
public override List<Dictionary<string, object>> QueryResult<T, Detail>(
    List<Dictionary<string, object>> result,
    PrintQuery parms,
    BaseDbContext dbContext)
{
    if (result.Count == 0) return result;
    
    //自定义处理,并调用下面自定义方法SetDemoOrderValue
    //判断表,自定义返回数据
    if (typeof(T).Name == typeof(Demo_Order).Name)
    {
        //判断是哪个打印模板,然后自定义返回数据
        if (parms.TemplateName == "订单管理主从明细表打印")
        {
            //返回DemoOrder表自定义配置
            SetDemoOrderValue(result, parms, dbContext);
        }
    }
    return result;
}



/// <summary>
/// 这个方法SetDemoOrderValue是自定义的: 返回DemoOrder表自定义配置(返回合计内容)
/// </summary>
/// <param name="result"></param>
/// <param name="parms"></param>
/// <param name="dbContext"></param>
private void SetDemoOrderValue(
    List<Dictionary<string, object>> result,
    PrintQuery parms,
    BaseDbContext dbContext)
{
    //从数据库统计明细表合计
    var data = dbContext.Set<Demo_OrderList>()
            //根据主表id查询返回明细表合计
            .Where(x => x.Order_Id == parms.Ids[0].GetGuid())
            .GroupBy(x => true)
            .Select(s => new
            {
                单价合计 = s.Sum(c => c.Price),
                数量合计 = s.Sum(c => c.Qty)
            }).FirstOrDefault();

    //设置自定义返回的字段(模板设计页面需要定义:单价合计、数量合计两个字段)
    //注意,这里【单价合计】名字与第二张图的第4步字段名必须一致
    result[0]["单价合计"] = data?.单价合计 ?? 0;
    result[0]["数量合计"] = data?.数量合计 ?? 0;

    //result[0]这里还可以自定义其他字段设置值与模板设计页面定义的字段一致即可

    ////例如:再返回一些自定义的表格数据
    //var otherTable = dbContext.Set<Demo_Product>()
    //    .Where(x => true)//这里写条件
    //    .Select(s => new
    //    {
    //        名称 = s.ProductName,//[名称]与[编号]是打印板自定义表格的里面输入的字段名
    //        编号 = s.ProductCode
    //    }).ToList();

    //result[0]["字段名"] = otherTable;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

# 打印回调

用户只要点击了打印按钮就会回调,监听不了打印成功还是失败

# 1、打印配置设置回调接口地址

填写回调的接口地址

An image

# 2、接口实现

以Sys_Region表为例,在表的控制器上写更新方法,接口名与上面的配置的接口要一致


    public partial class Sys_RegionController
    {
        private readonly ISys_RegionService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ISys_RegionRepository _regionRepository;
        [ActivatorUtilitiesConstructor]
        public Sys_RegionController(
            ISys_RegionService service,
            IHttpContextAccessor httpContextAccessor,
            ISys_RegionRepository regionRepository
        )
        : base(service)
        {
            _service = service;
            _regionRepository = regionRepository;
            _httpContextAccessor = httpContextAccessor;
        }
        [Route("printCallback"), HttpPost]
        public IActionResult PrintCallback([FromBody] int[] ids)
        {
            //这里自己写脚本更新
            //1、EF写法
            string sql = " update Sys_Region set 更新的字段=值 where 主键字段 in @ids";
            _regionRepository.DapperContext.ExcuteNonQuery(sql, new { ids });


            //2、sqlsugar写法
            //string sql = " update Sys_Region set 更新的字段=值 where 主键字段 in (@ids)";
            //var sugarParameter = new SugarParameter("@ids", "张三");
            //_regionRepository.SqlSugarClient.Ado.ExecuteCommand(sql, sugarParameter);

            return Content("更新成功");
        }
     }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37