/// <summary>
 /// 根据报告号获取附件
 /// </summary>
 /// <param name="reportId">报告号</param>
 /// <returns>附件列表</returns>
 public List<Attachment> GetAttachmentById(Guid reportId)
 {
     using (IUnitOfWork unitwork = MSSqlHelper.DataContext())
     {
         IAttachmentRepository reportAttachRep = new AttachmentRepository(unitwork);
         List<Attachment> attaches = reportAttachRep.GetByMasterNR(reportId.ToString());
         return attaches;
     }
 }
 public bool DelAttachmentById(int attachId, string filePath)
 {
     using (IUnitOfWork unitwork = MSSqlHelper.DataContext())
     {
         try
         {
             IAttachmentRepository attachRep = new AttachmentRepository(unitwork);
             attachRep.DeleteById(attachId);
             string p = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Settings.Default.FileAttachmentPath);
             string path = Path.Combine(p, filePath);
             File.Delete(path);
             unitwork.Submit();
         }
         catch (Exception e) {
             Console.WriteLine(e.Message);
             return false;
         }
         return true;
     }
 }
 /// <summary>
 /// 根据模具号、操作员号、起止日期、页码信息获得模具放行信息
 /// </summary>
 /// <param name="moldNR">模具号</param>
 /// <param name="pageIndex">页码</param>
 /// <param name="pageSize">页码数量</param>
 /// <returns>模具放行信息列表</returns>
 public List<MoldReleaseInfo> GetMoldReleaseInfoByMoldNRInPage(string moldNR, int pageIndex, int pageSize, string operatorId, DateTime? startDate, DateTime? endDate)
 {
     using (IUnitOfWork unitwork = MSSqlHelper.DataContext())
     {
         IReportRepository reportRep = new ReportRepository(unitwork);
         List<ReportView> reports = reportRep.GetReportViewByMoldNR(moldNR, pageIndex, pageSize, operatorId, startDate, endDate);
         IAttachmentRepository attachRep = new AttachmentRepository(unitwork);
         List<MoldReleaseInfo> moldReleaseInfos = new List<MoldReleaseInfo>();
         foreach (ReportView r in reports)
         {
             MoldReleaseInfo moldReleaseInfo = new MoldReleaseInfo()
             {
                 TesterName = r.Name,
                 TesterNR = r.OperatorID,
                 Date = r.Date,
                 TargetNR = r.MoldID,
                 ReportType = r.ReportType,
                 ReportTypeCN = r.ReportTypeCN,
                 Attach = attachRep.GetByMasterNR(r.ReportId.ToString())
             };
             moldReleaseInfos.Add(moldReleaseInfo);
         }
         return moldReleaseInfos;
     }
 }
        /// <summary>
        /// 根据模具号获得模具基本信息
        /// </summary>
        /// <param name="moldNR">模具号</param>
        /// <returns>模具基本信息</returns>
        public MoldBaseInfo GetMoldBaseInfoByNR(string moldNR)
        {
            using (IUnitOfWork unitwork = MSSqlHelper.DataContext())
            {
                IMoldRepository moldRepostitory = new MoldRepository(unitwork);
                IAttachmentRepository attachRep = new AttachmentRepository(unitwork);
                IPositionRepository posiRep = new PositionRepository(unitwork);

                MoldView m = moldRepostitory.GetMoldViewByMoldNR(moldNR);
                if (m != null)
                {
                    MoldBaseInfo mb = new MoldBaseInfo()
                    {
                        MoldNR = m.MoldNR,
                        Name = m.Name,
                        Type = m.TypeName,
                        Position = posiRep.GetByFacilictyNR(moldNR).PositionNR,
                        Producer = m.Producer,
                        Material = m.Material,
                        Weight = m.Weight == null ? string.Empty : m.Weight.ToString(),
                        State = m.State,
                        StateCN = m.StateCN,
                        ProjectId = m.ProjectID,
                        ProjectName = m.ProjectName,
                        Attach = attachRep.GetByMasterNR(moldNR)
                    };
                    return mb;
                }
                return null;
            }
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="files">文件列表</param>
        /// <param name="masterNR">附主号</param>
        /// <returns>上传信息</returns>
        public Message FileUpLoad(FileUP[] files,string masterNR)
        {
            try
            {
                if (files != null && files.Length > 0)
                {
                    using (IUnitOfWork unitwork = MSSqlHelper.DataContext())
                    {
                        string p = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Settings.Default.FileAttachmentPath);
                        if (!Directory.Exists(p))
                        {
                            Directory.CreateDirectory(p);
                        }
                        List<Attachment> reportAttaches = new List<Attachment>();
                        string type = string.Empty;
                        const int bufferLength = 4096;

                        foreach (FileUP file in files)
                        {
                            // type = file.Name.Substring(file.Name.LastIndexOf('.'), file.Name.Length - file.Name.LastIndexOf('.'));
                            string servername = GuidUtil.GenerateGUID().ToString() + file.FileType;

                            string filePath = Path.Combine(p, servername);

                            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
                            {
                                using (Stream stream = new MemoryStream(file.Data))
                                {
                                    byte[] buffer = new byte[bufferLength];
                                    int readcount = 0;
                                    do
                                    {
                                        readcount = stream.Read(buffer, 0, bufferLength);
                                        if (readcount > 0)
                                        {
                                            fs.Write(buffer, 0, readcount);
                                        }
                                    } while (readcount > 0);
                                }
                            }

                            Attachment attachment = new Attachment()
                            {
                                MasterNR = masterNR,
                                Name = file.Name,
                                Path = servername,
                                Date = DateTime.Now
                            };
                            reportAttaches.Add(attachment);
                        }

                        IAttachmentRepository attachRep = new AttachmentRepository(unitwork);
                        attachRep.Add(reportAttaches);
                        unitwork.Submit();

                        return new Message() { MsgType = MsgType.OK, Content = "文件上传成功!" };
                    }
                }
                return new Message() { MsgType = MsgType.Warn, Content = "文件为空!" };
            }
            catch(Exception ex)
            {
                LogUtil.log.Error(ex.ToString());
                return new Message() { MsgType = MsgType.Error, Content = "请核实所填数据的准确性" };
            }
        }