/// <summary>
        /// 打印施工合同,根据word模板
        /// </summary>
        /// <param name="prjid"></param>
        /// <returns></returns>
        public virtual ActionResult PrintContract(int prjid,int contracttype)
        {
            var contract = Repository.SingleOrDefault<Contract>(x => x.ProjectId == prjid);
            decimal contractamount =0;
             _erpcode = contract.Project.ErpCode.Trim();
             _prjname = contract.Project.ProjectName;
            var relatecontracts = Repository.All<Contract>().Where(x => x.Project.ErpCode.Trim().Equals(_erpcode));
            var relatesubcontracts = Repository.All<SubContract>().Where(x => x.Project.ErpCode.Trim().Equals(_erpcode));
            var sumamount = relatecontracts.Sum(x => x.ContractAmount);
            var sumacfee = relatecontracts.Sum(x => x.AcwmFee);
            var acwmfee = "";
            var strAdd = StrAdd2;
            // 分账号合同 按照封账号计算合同金额 文明措施费
            if (contracttype==0)
            {
                contractamount = contract.ContractAmount.HasValue ? contract.ContractAmount.Value : 0;
                if (contract.AcwmFee.HasValue && contract.AcwmFee.Value > 0)
                { acwmfee = string.Format("包括安措及文明施工费{0}元,", contract.AcwmFee); }
            }
            //总账号合同,汇总所有分账号合同金额
            if (contracttype == 1)
            {
                if (relatecontracts.Count() > 1)
                {
                    var rearchar = new char[2];
                    rearchar[0] = '(';
                    rearchar[1] = '(';

                    var startindex = _prjname.LastIndexOfAny(rearchar);
                    if (startindex > 1)
                    _prjname = _prjname.Remove(startindex);
                }
                contractamount = sumamount.HasValue ? sumamount.Value:0;
                if (sumacfee > 0)
                {
                   // acwmfee = string.Format("包括安措及文明施工费{0}元,", sumacfee);
                   acwmfee = Rmb.CmycurD(sumacfee.Value);
                }
            }

            var planstart = contract.PlanStartDate.HasValue ? contract.PlanStartDate.Value.ToString("yyyy 年 MM 月 d 日") : "";
            var planend = contract.PlanEndDate.HasValue ? contract.PlanEndDate.Value.ToString("yyyy 年 MM 月 d 日") : "";
            var signon = contract.SignOn.HasValue ? contract.SignOn.Value.ToString("yyyy 年 MM 月 d 日") : DateTime.Now.ToString("yyyy 年 MM 月 d 日");
            var address = contract.FirstCompany.Address;
            var contactPhone = contract.FirstCompany.ContactPhone;
            var companyname = contract.FirstCompany.Name;
            var contractNo = contract.ContractNo;
            if (_erpcode.StartsWith("31") || _erpcode.StartsWith("32"))
                strAdd = StrAdd1;
            Document doc;
            var templatedoc = HostingEnvironment.MapPath("~/App_Data/");
            //if (contract.Project.ErpCode.StartsWith("1")) // 如果是基建项目则选择不同格式的模板
            //{
            //    templatedoc = templatedoc + "contract2.doc";
            //    doc = new Document(templatedoc);
            //    doc.MailMerge.Execute(new[] { "PrjName", "YiFang", "ContractAmount", "PlanStart", "PlanEnd", "SignOn", "ACWMFee" },
            //                          new object[] { prjName, companyname, Rmb.CmycurD(contractamount), planstart, planend, signon, acwmfee });
            //}
            //else
            //{
                switch (contracttype)
                {
                    case 1:
                        templatedoc = templatedoc + "营销合同.doc";
                        break;
                    default:
                        templatedoc = templatedoc + "contractsingle.doc";
                        break;
                }
                doc = new Document(templatedoc);
                if (contracttype == 1)
                {
                    var subProjects = new SubProjectList();
                    var subcontracts = new SubContractinfoList();
                    foreach (var relatecontract in relatecontracts)
                    {
                        subProjects.Add(new SubProject(relatecontract.Project.ErpCode, relatecontract.Project.ProjectName,
                            relatecontract.ContractAmount.HasValue ? relatecontract.ContractAmount.Value.ToString() : ""));
                    }
                    var i = 1;
                    foreach (var relatesubcontract in relatesubcontracts)
                    {
                        subcontracts.Add(new SubContractinfo(relatesubcontract.Project.ErpCode, relatesubcontract.Project.ProjectName,
                            relatesubcontract.PartnerCompany.Name,i));
                        i = i + 1;
                    }

                    var subProjectMailMergeDataSource = new SubProjectMailMergeDataSource(subProjects);
                    var mergdatasource2 = new SubContractMailMergeDataSource(subcontracts);

                    doc.MailMerge.ExecuteWithRegions(subProjectMailMergeDataSource);
                    doc.MailMerge.ExecuteWithRegions(mergdatasource2);
                }

                 doc.MailMerge.Execute(new[] {"JiaFang","ContractNo", "PrjName", "YiFang","PrjAccount","ContractAmount",
                     "AcwmFee",  "PlanStart", "PlanEnd", "SignOn","Address1", "Address2", "TelPhone1","Telphone2" },
                    new object[]{JiaFang,  contractNo, _prjname,  companyname, _erpcode,Rmb.CmycurD(contractamount),
                        acwmfee, planstart,planend,signon,Address1,address,Phone1,contactPhone});
            //}
            var output = new MemoryStream();
            doc.Save(output,SaveFormat.Doc);
            return File(output.ToArray(), "application/msword", Url.Encode(contract.Project.ProjectName + "施工承包合同.doc"));
        }
 public SubContractMailMergeDataSource(SubContractinfoList subcontracts)
 {
     mSubcontracts = subcontracts;
     // When the data source is initialized, it must be positioned before the first record.
     mRecordIndex = -1;
 }