Example #1
0
        public static bool TryParse(string xml, out CheckItemCollection item)
        {
            bool parsed = false;
            item = new CheckItemCollection();

            try
            {
                XElement element = XElement.Parse(xml);

                foreach (XElement xItem in element.Elements("item"))
                {
                    ICheckItemBase cItem = new CheckItem();
                    (cItem as CheckItem).Name = xItem.Attribute("name").Value;
                    (cItem as CheckItem).Standard = xItem.Attribute("standard").Value;
                    (cItem as CheckItem).Unit = xItem.Attribute("unit").Value;
                    (cItem as CheckItem).Value = xItem.Attribute("value").Value;
                    (cItem as CheckItem).ValueType = StringToCheckItemValueType(xItem.Attribute("type").Value);

                    item.Items.Add((cItem as CheckItem));

                    // 把item放到接口列表
                    int index;
                    Int32.TryParse(xItem.Attribute("index") == null ? "0" : xItem.Attribute("index").Value, out index);
                    cItem.Index = index;
                    item.CheckItems.Add(cItem);
                }

                foreach (XElement xGroup in element.Elements("group"))
                {
                    ICheckItemBase cGroup = new CheckItemGroup();

                    cGroup.Name = xGroup.Attribute("name").Value;
                    cGroup.Unit = xGroup.Attribute("unit").Value;

                    foreach (XElement xItem in xGroup.Elements("item"))
                    {
                        CheckItem cItem = new CheckItem();
                        cItem.Name = xItem.Attribute("name").Value;
                        cItem.Standard = xItem.Attribute("standard").Value;
                        cItem.Unit = xItem.Attribute("unit").Value;
                        cItem.Value = xItem.Attribute("value").Value;
                        cItem.ValueType = StringToCheckItemValueType(xItem.Attribute("type").Value);

                        (cGroup as CheckItemGroup).Items.Add(cItem);
                    }

                    item.Groups.Add((cGroup as CheckItemGroup));

                    // 把item放到接口列表
                    int index;
                    Int32.TryParse(xGroup.Attribute("index") == null ? "0" : xGroup.Attribute("index").Value, out index);
                    cGroup.Index = index;
                    item.CheckItems.Add(cGroup);
                }

                item.Sort();

                parsed = true;
            }
            catch (Exception ex)
            {
                throw ex;
                parsed = false;
                item = null;
            }

            return parsed;
        }
Example #2
0
        public static bool TryParse(string xml, out CheckItemCollection item)
        {
            bool parsed = false;
            item = new CheckItemCollection();

            try
            {
                XElement element = XElement.Parse(xml);

                foreach (XElement xItem in element.Elements("item"))
                {
                    CheckItem cItem = new CheckItem();
                    cItem.Name = xItem.Attribute("name").Value;
                    cItem.Standard = xItem.Attribute("standard").Value;
                    cItem.Unit = xItem.Attribute("unit").Value;
                    cItem.Value = xItem.Attribute("value").Value;
                    cItem.ValueType = StringToCheckItemValueType(xItem.Attribute("type").Value);

                    item.Items.Add(cItem);
                }

                foreach (XElement xGroup in element.Elements("group"))
                {
                    CheckItemGroup cGroup = new CheckItemGroup();

                    cGroup.Name = xGroup.Attribute("name").Value;
                    cGroup.Unit = xGroup.Attribute("unit").Value;

                    foreach (XElement xItem in xGroup.Elements("item"))
                    {
                        CheckItem cItem = new CheckItem();
                        cItem.Name = xItem.Attribute("name").Value;
                        cItem.Standard = xItem.Attribute("standard").Value;
                        cItem.Unit = xItem.Attribute("unit").Value;
                        cItem.Value = xItem.Attribute("value").Value;
                        cItem.ValueType = StringToCheckItemValueType(xItem.Attribute("type").Value);

                        cGroup.Items.Add(cItem);
                    }

                    item.Groups.Add(cGroup);
                }

                parsed = true;
            }
            catch
            {
                parsed = false;
                item = null;
            }

            return parsed;
        }
        public void DownloadReportDoc(int id)
        {
            BusinessCheckNotice notice = repository.FindByNotCache(id);
            if (notice == null)
            {
                throw new HttpException(404, "商检任务单不存在");
            }

            notice.PipeType = RepositoryFactory.GetRepository<IPipeTypeRepository, PipeType>().FindBy(notice.PipeType.Code);

            if (notice.State != BusinessCheckNoticeState.ResultSubmitted &&
                notice.State != BusinessCheckNoticeState.Done)
            {
                Response.Write("暂无相关商检报告");
                return;
            }

            String templateDir = Server.MapPath("~/Report/Template/Income/");
            String templateFile = "";

            if (notice.PipeType.Screw.ToString() == "BC" || notice.PipeType.Screw.ToString() == "LC" || notice.PipeType.Screw.ToString() == "STC" || notice.PipeType.Screw.ToString() == "EUE" || notice.PipeType.Screw.ToString() == "NUE")
            {    

                templateFile = "检测报告.docx";
            }
            else
            {
                templateFile = "无图标检测报告.docx";
            }

            FileInfo fi = new FileInfo(templateDir + templateFile);
            if (!fi.Exists)
            {
                throw new FileNotFoundException("模板文件(" + templateFile + ")不存在,请联系系统管理员");
            }

            string desDir = Server.MapPath("~/Report/Output/Income/");
            string desFileName = string.Format("检测报告({0}){1}.docx", notice.Code, DateTime.Now.ToString("yyyyMMddHHmmssffff"));

            DirectoryInfo di = new DirectoryInfo(desDir);
            if (!di.Exists) { di.Create(); }
            fi.CopyTo(desDir + desFileName);

            #region Create Documnent

            using (WordprocessingDocument doc = WordprocessingDocument.Open(desDir + desFileName, true, new OpenSettings { AutoSave = true }))
            {
                Text t;

                //替换CQ
                string deletCQ = notice.DetectionCompany.Code;

                // 商检报告号
                IEnumerable<Text> texts = doc.MainDocumentPart.Document.Descendants<Text>().Where(text => text.Text == "ReportNum");
                foreach (Text reportnum in texts)
                {
                    reportnum.Text = notice.ReportNumber.Replace(deletCQ, "");
                }

                // 样品编号
                texts = doc.MainDocumentPart.Document.Descendants<Text>().Where(text => text.Text == "Code");
                string str = notice.Code.Replace(deletCQ, "").ToString();
                int nIndex;
                string wMin;
                string wMax;
                string sb;
                if (str != null)
                {
                    if (str.Length != 0)
                    {
                        if (str.IndexOf("-") != -1)
                        {
                            nIndex = str.IndexOf("-", 0, str.Length);
                            if (nIndex != 0)
                            {
                                wMin = str.Substring(0, nIndex);
                                wMax = str.Substring(nIndex + 1);
                                sb = wMin + "-" + notice.DetectionCompany.Code.ToString() + "-" + wMax;
                            }
                            else
                            {
                                sb = str;
                            }
                        }
                        else 
                        {
                            sb = str;
                        }
                    }
                    else
                    {
                        sb = str;
                    }
                }
                else
                {
                    sb = str;
                }
                foreach (Text code in texts)
                {
                    code.Text = sb;
                    //code.Text = notice.DetectionCompany.Code + "-" + notice.Code.Replace(deletCQ, "");
                }

                // 样品名称
                texts = doc.MainDocumentPart.Document.Descendants<Text>().Where(text => text.Text == "PipeTypeName");
                foreach (Text name in texts)
                {
                    name.Text = notice.PipeType.Name;
                }

                // 规格型号
                texts = doc.MainDocumentPart.Document.Descendants<Text>().Where(text => text.Text == "PipeType");
                foreach (Text pipetype in texts)
                {
                    pipetype.Text = string.Format("{0}x{1} {2} {3}", notice.PipeType.ExternalDiameter, notice.PipeType.Thickness, notice.PipeType.SteelLevel, notice.PipeType.Screw);
                }

                // 委托单位
                texts = doc.MainDocumentPart.Document.Descendants<Text>().Where(text => text.Text == "company");
                foreach (Text company in texts)
                {
                    company.Text = notice.DetectionCompany==null?"":notice.DetectionCompany.Entrustment;
                }

                #region 基础数据表格

                Table table = doc.MainDocumentPart.Document.Body.Elements<Table>().First();
                
                //样品编号
                TableRow row = table.Elements<TableRow>().ElementAt(0);
                t = row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                //t.Text = notice.DetectionCompany.Code + "-" + notice.Code.Replace(deletCQ, "");
                t.Text =sb;

                //合同号
                row = table.Elements<TableRow>().ElementAt(1);
                t = row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.DeliverNotice.CompanyContract;

                //样品数量
                row = table.Elements<TableRow>().ElementAt(2);
                t = row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.ReceiveCount + "根";

                //生产厂商
                row = table.Elements<TableRow>().ElementAt(3);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.DeliverNotice.Producer.Name;
                //抽样数量
                t = row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.SamplingCount + "根";

                //样品状态
                row = table.Elements<TableRow>().ElementAt(4);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.PipeState;
                //到货日期
                t = row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.FirstReciveDate.ToString("yyyy-MM-dd");

                //环境条件
                row = table.Elements<TableRow>().ElementAt(5);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.Environment;
                //检测日期
                t = row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.ReceiveTime.HasValue ? notice.ReceiveTime.Value.ToString("yyyy-MM-dd") : "/";

                //检测车间
                row = table.Elements<TableRow>().ElementAt(6);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.DetectionCompany.Name;
                //抽样人
                t = row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.CheckOff==null?"":notice.CheckOff.Name;
                //t.Text = notice.Receiver.Name + "/" + (notice.ReceiveTime.HasValue ? notice.ReceiveTime.Value.ToString("yyyy.MM.dd") : "");

                //检测项目
                row = table.Elements<TableRow>().ElementAt(7);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.PipeType.GetCheckItemsStringConclution();

                //检测依据
                row = table.Elements<TableRow>().ElementAt(8);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.CheckStandard;

                //抽样依据
                row = table.Elements<TableRow>().ElementAt(9);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.SamplingMethod;

                //检测工具
                row = table.Elements<TableRow>().ElementAt(10);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.CheckTools;
                
                //检测结论
                row = table.Elements<TableRow>().ElementAt(11);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.HandleConclusion;
                //报告日期
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().Last().Elements<Run>().First().Elements<Text>().Last();
                t.Text = "报告日期:"+(notice.HandleDate.HasValue ? notice.HandleDate.Value.ToString("yyyy年MM月dd日") : "");

                //备注
                row = table.Elements<TableRow>().ElementAt(12);
                t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                t.Text = notice.Comment == null ? "/" : notice.Comment;

                 #region 主检、审核、批准
                //row = table.Elements<TableRow>().ElementAt(13);
                //t = row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();

                //if (notice.MainChecker != null && !string.IsNullOrEmpty(notice.MainChecker.SignPath) &&
                //    AppendImage(row.Elements<TableCell>().ElementAt(1).Elements<Paragraph>().First().Elements<Run>().First(),
                //        Server.MapPath(notice.MainChecker.SignPath)))
                //{
                //    t.Remove();
                //}
                //else
                //{
                //    t.Text = notice.MainChecker == null ? "" : notice.MainChecker.Name;
                //}

                //t = row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                //if (notice.Verifier != null && !string.IsNullOrEmpty(notice.Verifier.SignPath) &&
                //    AppendImage(row.Elements<TableCell>().ElementAt(3).Elements<Paragraph>().First().Elements<Run>().First(),
                //        Server.MapPath(notice.Verifier.SignPath)))
                //{
                //    t.Remove();
                //}
                //else
                //{
                //    t.Text = notice.Verifier == null ? "" : notice.Verifier.Name;
                //}

                //t = row.Elements<TableCell>().ElementAt(5).Elements<Paragraph>().First().Elements<Run>().First().Elements<Text>().First();
                //if (notice.Approver != null && !string.IsNullOrEmpty(notice.Approver.SignPath) &&
                //    AppendImage(row.Elements<TableCell>().ElementAt(5).Elements<Paragraph>().First().Elements<Run>().First(),
                //        Server.MapPath(notice.Approver.SignPath)))
                //{
                //    t.Remove();
                //}
                //else
                //{
                //    t.Text = notice.Approver == null ? "" : notice.Approver.Name;
                //}
                #endregion

                #endregion

                #region 检测结果表格

                TableCell cell = null;
                //TableCellProperties cellProperty = null;

                PipeType pt = notice.PipeType;

                #region 检测数据

                int i = 0;
                int recordsPageCount = notice.SamplingCount.Value / 20;
                if (notice.SamplingCount.Value % 20 != 0) { recordsPageCount += 1; }

                recordsPageCount += 1;

                texts = doc.MainDocumentPart.Document.Descendants<Text>().Where(text => text.Text == "Count");
                foreach (Text text in texts)
                {
                    text.Text = recordsPageCount.ToString();
                }

                doc.MainDocumentPart.Document.Body.Elements<Paragraph>().Last().Remove();

                foreach (PipeBCRecord record in RepositoryFactory.GetRepository<IPipeBCRecordRepository, PipeBCRecord>().FindBy(notice))
                {
                    if (i % 20 == 0) // 每页20条
                    {
                        if (i != 0)
                        {
                            doc.MainDocumentPart.Document.Body.AppendChild<Paragraph>(GenerateBreakPageParagraph());
                        }

                        doc.MainDocumentPart.Document.Body.AppendChild<Paragraph>(GenerateReportDataHeadParagraph());
                        doc.MainDocumentPart.Document.Body.AppendChild<Paragraph>(GenerateReportDataSecondHeadParagraph(notice.ReportNumber,
                            string.Format("{0}x{1} {2} {3}", notice.PipeType.ExternalDiameter, notice.PipeType.Thickness, notice.PipeType.SteelLevel, notice.PipeType.Screw),
                            i / 20 + 1 + 1, recordsPageCount));

                        table = GenerateBasiceReportDataTable();

                        #region 表头

                        row = table.Elements<TableRow>().First();
                        row.Elements<TableCell>().First().Remove();

                        cell = BuildCell("抽样号", 1, new VerticalMerge { Val = MergedCellValues.Restart }, true);

                        //cellProperty = cell.Elements<TableCellProperties>().First();
                        //cellProperty.Append(new TableCellWidth { Width = "40", Type = TableWidthUnitValues.Pct });
                        row.AppendChild<TableCell>(cell);

                        TableRow row2 = new TableRow();
                        row2.Append(BuildCell(string.Empty, 1, new VerticalMerge()));

                        TableRow row3 = new TableRow();
                        //TableCellProperties cellProperty3 = new TableCellProperties();
                        //cellProperty3.Append(new TableCellWidth { Width = "40", Type = TableWidthUnitValues.Dxa });
                        row3.Append(BuildCell("标准范围"));
                        //row3.Elements<TableCell>().ElementAt(0).Append(cellProperty3);

                        if (pt.OtherCheckItems != null)
                        {
                            foreach (ICheckItemBase item in pt.OtherCheckItems.CheckItems)
                            {
                                if (item is CheckItem)
                                {
                                        row.Append(BuildCell((item as CheckItem).Name.Replace(" ","") + (string.IsNullOrEmpty((item as CheckItem).Unit) ? "" : " (" + (item as CheckItem).Unit + ")"), 1, new VerticalMerge { Val = MergedCellValues.Restart }, true));
                                        row2.Append(BuildCell(string.Empty, 1, new VerticalMerge(), true));
                                        row3.Append(BuildCell((item as CheckItem).Standard == "" ? "/" : (item as CheckItem).Standard));
                                }
                                else if (item is CheckItemGroup)
                                {
                                    row.Append(BuildCell((item as CheckItemGroup).Name.Replace(" ", "") + (string.IsNullOrEmpty((item as CheckItemGroup).Unit) ? "" : " (" + (item as CheckItemGroup).Unit + ")"), (item as CheckItemGroup).Items.Count, null, true));
                                    foreach (CheckItem ci in (item as CheckItemGroup).Items)
                                    {
                                        row2.Append(BuildCell(ci.Name.Replace(" ", "") + (string.IsNullOrEmpty(ci.Unit) ? "" : "(" + ci.Unit + ")"), 1, null, true));
                                        row3.Append(BuildCell(ci.Standard==""?"/":ci.Standard));
                                    }
                                }
                            }                            
                        }

                        table.AppendChild<TableRow>(row2);
                        table.AppendChild<TableRow>(row3);
                        

                        doc.MainDocumentPart.Document.Body.AppendChild<Table>(table);
                        table = doc.MainDocumentPart.Document.Body.Elements<Table>().Last();

                        #endregion
                    }

                    TableRow rw = new TableRow();

                    TableCell cellNo = BuildCell((++i).ToString());
                    //TableCellProperties cellNoProperty = new TableCellProperties();
                    //cellNoProperty.Append(new TableCellWidth { Width = "40", Type = TableWidthUnitValues.Dxa });
                    //cellNo.Append(cellNoProperty);

                    rw.AppendChild<TableCell>(cellNo);

                    if (pt.OtherCheckItems != null)
                    {
                        if (record.OtherCheckItems == null) { record.OtherCheckItems = new CheckItemCollection(); }

                        foreach (ICheckItemBase item in pt.OtherCheckItems.CheckItems)
                        {
                            if (item is CheckItem)
                            {
                                string text = string.Empty;
                                CheckItem rci = record.OtherCheckItems.Items.FirstOrDefault(ci => ci.Name == item.Name);
                                if (rci == null)
                                {
                                    rci = new CheckItem { Name = item.Name, ValueType = (item as CheckItem).ValueType };
                                }

                                if (rci.ValueType == CheckItemValueType.Boolean)
                                {
                                    //text = rci.Value == "true" ? " 符合 " : (rci.Value == "false" ? "不符合" : "/");
                                    if (rci.Name == "结论")
                                    {
                                        text = rci.Value == "true" ? "合格" : "不合格";
                                    }
                                    else if(rci.Name=="通径")
                                    {
                                        text = rci.Value == "true" ? "通过" : "不通过";
                                    }
                                    else
                                    {
                                        text = rci.Value == "true" ? " 符合 " : "不符合";
                                    }
                                }
                                else
                                {
                                    text = rci.Value == "" ? "/" : rci.Value;
                                }

                                rw.AppendChild<TableCell>(BuildCell(text));
                            }
                            else if (item is CheckItemGroup)
                            {
                                CheckItemGroup rcg = record.OtherCheckItems.Groups.FirstOrDefault(g => g.Name == (item as CheckItemGroup).Name);
                                if (rcg == null)
                                {
                                    rcg = new CheckItemGroup();

                                    for (int j = 0; j < (item as CheckItemGroup).Items.Count; j++)
                                    {
                                        rw.AppendChild<TableCell>(BuildCell("/"));
                                    }
                                }
                                else
                                {
                                    foreach (CheckItem innerItem in (item as CheckItemGroup).Items)
                                    {
                                        string text = string.Empty;
                                        CheckItem rci = rcg.Items.FirstOrDefault(ci => ci.Name == innerItem.Name);
                                        if (rci == null)
                                        {
                                            rci = new CheckItem { Name = item.Name, ValueType = innerItem.ValueType };
                                        }

                                        if (rci.ValueType == CheckItemValueType.Boolean)
                                        {
                                            //text = rci.Value == "true" ? "符合" : (rci.Value == "false" ? "不符合" : "/");
                                            text = rci.Value == "true" ? "符合" : "不符合";

                                        }
                                        else
                                        {
                                            text = rci.Value == "" ? "/" : rci.Value;
                                        }

                                        rw.AppendChild<TableCell>(BuildCell(text));
                                    }
                                }
                            }
                        }                        
                    }

                    table.AppendChild<TableRow>(rw);
                }

                //录入、核对
                doc.MainDocumentPart.Document.Body.AppendChild<Paragraph>(GenerateReportDataFooterParagraph(
                    notice.Checker == null ? "" : notice.Checker.Name, notice.CheckOff==null?"":notice.CheckOff.Name));
                    //notice.Checker == null ? "" : notice.Checker.Name,
                    //notice.Verifier == null ? "" : notice.Verifier.Name));

                #endregion

                #endregion

                doc.Close();
            }

            #endregion

            if (Request.Browser.Browser == "IE")
            {
                Response.AddHeader("Content-Disposition", "Attachment;FileName=" + HttpUtility.UrlEncode(desFileName, Encoding.UTF8));
            }
            else
            {
                Response.AddHeader("Content-Disposition", "Attachment;FileName=" + desFileName);
            }
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/vnd.ms-word.document.12";
            Response.WriteFile(desDir + desFileName);
            Response.End();

            fi = new FileInfo(desDir + desFileName);
            fi.Delete();
        }