public PipeNeedNoticeItem(string key, PipeNeedNotice needNotice, Digger digger, PipeType pipeType)
     : base(key)
 {
     this.PipeNeedNotice = needNotice;
     this.Digger = digger;
     this.PipeType = pipeType;
 }
        public JsonResult Create(string Code, String Sender, String Verifier, String CompanyId, String Items)
        {
            #region Validation

            if (string.IsNullOrEmpty(CompanyId))
            {
                return Json(new { success = false, message = "未指定公司" });
            }

            if (string.IsNullOrEmpty(Code) || !new Regex("^[-|a-z|A-Z|0-9]+$").IsMatch(Code))
            {
                return Json(new { success = false, message = "编号格式不正确" });
            }

            if (String.IsNullOrEmpty(Items))
            {
                return Json(new { success = false, message = "请至少添加一条调拨项" });
            }

            DetectionCompany company = RepositoryFactory.GetRepository<IDetectionCompanyRepository, DetectionCompany>().FindBy(CompanyId);
            if (company == null)
            {
                return Json(new { success = false, message = "无法找到指定公司" });
            }

            #endregion

            PipeNeedNotice notice = new PipeNeedNotice(Guid.NewGuid().ToString("N"), Code, company, UserSession.OnlineAccount, DateTime.Now);
            notice.Sender = Sender;
            notice.Verifier = Verifier;
            repository.Add(notice);

            IPipeNeedNoticeItemRepository itemRepository = RepositoryFactory.GetRepository<IPipeNeedNoticeItemRepository, PipeNeedNoticeItem>(unitOfWork);
            IDiggerRepository diggerRepository = RepositoryFactory.GetRepository<IDiggerRepository, Digger>();
            IPipeTypeRepository ptRepository = RepositoryFactory.GetRepository<IPipeTypeRepository, PipeType>();

            IList<PDCPMS.DataContracts.NormalCheck.PipeNeedNoticeItem> itemContracts = new JavaScriptSerializer().Deserialize<IList<PDCPMS.DataContracts.NormalCheck.PipeNeedNoticeItem>>(Items);
            foreach (PDCPMS.DataContracts.NormalCheck.PipeNeedNoticeItem ic in itemContracts)
            {
                if (string.IsNullOrEmpty(ic.DiggerId))
                {
                    return Json(new { success = false, message = "调拨项数据不完善:井队数据缺失" });
                }

                Digger digger = diggerRepository.FindBy(ic.DiggerId);
                if (digger == null)
                {
                    return Json(new { success = false, message = "调拨项数据不完善:井队数据缺失" });
                }

                PipeType pt = ptRepository.FindBy(ic.PipeTypeId);
                if (pt == null)
                {
                    return Json(new { success = false, message = "调拨项数据不完善:管材型号数据缺失" });
                }

                if (ic.Length <= 0)
                {
                    return Json(new { success = false, message = "调拨项数据不完善:所需米数数据缺失" });
                }

                PipeNeedNoticeItem item = new PipeNeedNoticeItem(Guid.NewGuid().ToString("N"),
                    notice,
                    digger,
                    pt);
                item.Length = ic.Length;
                itemRepository.Add(item);
            }

            unitOfWork.Commit();

            TempData["HintMessage"] = new PDCPMS.Application.HintMessage { Content = "成功添加物资调拨单" };
            return Json(new { success = true, noticeId = notice.Key });
        }