二开 (代码)工作流审核后 套打导出PDF并发送邮件原创
金蝶云社区-霹雳太史侯
霹雳太史侯
8人赞赏了该文章 542次浏览 未经作者许可,禁止转载编辑于2023年07月28日 16:20:33
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;

using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.BOS;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.App.Core;
using System.IO;
using Kingdee.BOS.App.Data;

namespace JIANGHUI.TESTPROJECT
{
    [Description("销售订单-工作流审批 套打导出PDF并发送邮件"), HotUpdate]
    
    public class PDF_TEST : AbstractOperationServicePlugIn
    {
        private static string TargetFormId = "SAL_SaleOrder";//FORM

        private static string TemplateId = "cd4b02ea-df80-486d-afc3-fb7660c083c0";//打印模板标识
        public string filePath = "";
        string docno = string.Empty;
        string FID = string.Empty;

        public override void EndOperationTransaction(EndOperationTransactionArgs e)//绑定审核
        {
            base.EndOperationTransaction(e);
            

            foreach (DynamicObject entity in e.DataEntitys)
            {
                FID = entity["ID"].ToString();
                docno = entity["BillNo"].ToString();
              
                string sql = "/*dialect*/SELECT A.FCREATORID FROM T_SAL_ORDER A WHERE A.FBILLNO='"+ docno+"'";
                var res = DBUtils.ExecuteDynamicObject(this.Context,sql);
                long cjrid = Convert.ToInt64(res[0]["FCREATORID"]);
                string fileName = ExportTargetBill(TargetFormId, FID, TemplateId);
                string url = System.Web.HttpContext.Current.Request.Url.Scheme + "://" +
                         System.Web.HttpContext.Current.Request.Url.Authority + "/K3Cloud/" + KeyConst.TEMPFILEPATH +
                         "/" + fileName;
                filePath = PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, fileName);
                sendMail(cjrid, filePath);

            }
        }


        public void sendMail(long  userid,string filepath)
        {
            var sendMailService = new SendMailService();
            //SYS 100781
            FormMetadata userOB = MetaDataServiceHelper.Load(base.Context, "BOS_User", true) as FormMetadata;
            var userobj = BusinessDataServiceHelper.LoadSingle(base.Context, 100781, userOB.BusinessInfo.GetDynamicObjectType(), null);
            

            //获取邮件服务器信息  收件人信息 获取创建人/获取当前登录用户
            var emailInfo = sendMailService.GetEmailMessageInfoByUserId(base.Context, 100781);
            //var emailInfo = sendMailService.GetEmailMessageInfoByUserId(base.Context, userid);
            //设置邮件标题
            emailInfo.Subject = "销售订单审批";

            //设置邮件内容
            emailInfo.Body = "各位好: 销售订单已审批,请安排后续工作!";





            //设置邮件收件人
            emailInfo.To = new List<string>();
            emailInfo.To.Add("8888@qq.com");

            //设置邮件抄送人
            emailInfo.Cc = new List<string>();
            emailInfo.Cc.Add("8888@qq.com");

            //设置邮件附件
           
            if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(filePath))
            {
                emailInfo.Attachments = new List<string>();
                emailInfo.Attachments.Add(filePath);
            }
            else
            {
                emailInfo.Body += "\r\n本次操作未查询到满足条件的数据,无引出文件。";
            }

            MailUtils.Sendmail(emailInfo);

        }




        /// <summary>
        /// 套打导出指定单据
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="billId"></param>
        /// <param name="templateId"></param>
        /// <returns></returns>
        private string ExportTargetBill(string formId, string billId, string templateId)
        {
            IDynamicFormView dynamicFormView = CreateView(this.Context, formId, billId); //此行为代码模拟打开凭证界面
            if (dynamicFormView == null)
                return null;
            try
            {
                return ExportTargetBill(dynamicFormView, formId, billId, templateId);
            }
            finally
            {
                dynamicFormView.Close();
            }
        }

        private string ExportTargetBill(IDynamicFormView dynamicFormView, string formId, string billId, string templateId)
        {
            Kingdee.BOS.Core.Import.IImportView importView = dynamicFormView as Kingdee.BOS.Core.Import.IImportView;
            IDynamicFormViewService viewService = dynamicFormView as IDynamicFormViewService;
            if (importView == null || viewService == null)
                return null;
            try
            {
                importView.AddViewSession();

                List<string> billIds = new List<string>() { billId };
                List<string> templateIds = new List<string>() { templateId };

                PrintExportInfo pExInfo = new PrintExportInfo();
                pExInfo.PageId = dynamicFormView.PageId;
                pExInfo.FormId = formId;
                pExInfo.BillIds = billIds; //单据内码
                pExInfo.TemplateIds = templateIds; //套打模板ID
                pExInfo.FileType = ExportFileType.PDF; //文件格式
                pExInfo.ExportType = ExportType.ByPage; //导出格式
                //string fileName = Guid.NewGuid().ToString() + ".PDF";
                string fileName = docno+"_"+DateTime.Now.ToString("yyyy-MM-dd") + ".PDF";
                string temppath = PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, fileName);
                pExInfo.FilePath = temppath; //文件输出路径
                viewService.ExportNotePrint(pExInfo);
                return fileName;
            }
            finally
            {
                importView.RemoveViewSession();
            }
        }

        #region 创建服务端视图(引入视图)
        /// <summary>
        /// 构建表单打开参数
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="metadata"></param>
        /// <param name="pkValue"></param>
        /// <returns></returns>
        private BillOpenParameter CreateOpenParameter(Context ctx, FormMetadata metadata, string pkValue)
        {
            var form = metadata.BusinessInfo.GetForm();
            BillOpenParameter openPara = new BillOpenParameter(form.Id, metadata.GetLayoutInfo().Id);
            openPara.Context = ctx;
            openPara.PageId = Guid.NewGuid().ToString();
            openPara.Status = OperationStatus.VIEW;
            openPara.CreateFrom = CreateFrom.Default;
            openPara.DefaultBillTypeId = string.Empty;
            openPara.PkValue = pkValue;
            openPara.FormMetaData = metadata;
            openPara.SetCustomParameter(Kingdee.BOS.Core.FormConst.PlugIns, form.CreateFormPlugIns());
            openPara.ServiceName = form.FormServiceName;
            return openPara;
        }

        /// <summary>
        /// 创建视图
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId"></param>
        /// <param name="pkValue"></param>
        /// <returns></returns>
        private IDynamicFormView CreateView(Context ctx, string formId, string pkValue)
        {
            FormMetadata metadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
            if (metadata == null)
                return null;
            var openParameter = CreateOpenParameter(ctx, metadata, pkValue);
            var provider = metadata.BusinessInfo.GetForm().GetFormServiceProvider();

            string importViewClass = "Kingdee.BOS.Web.Import.ImportBillView,Kingdee.BOS.Web";
            Type type = Type.GetType(importViewClass);
            IDynamicFormViewService billView = (IDynamicFormViewService)Activator.CreateInstance(type);
            billView.Initialize(openParameter, provider);
            billView.LoadData();
            return (IDynamicFormView)billView;
        }
        #endregion

    }





}

图标赞 8
8人点赞
还没有人点赞,快来当第一个点赞的人吧!
图标打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!