public ResponseView Add(CasMapperView casMapper)
        {
            var repo = ServiceLocator.Current.GetInstance<ICasMapperRepository>();
            var result = new ResponseView();

            try
            {
                var model = _adapter.Adapt<CasMapper>(casMapper);
                repo.Add(model);
                repo.Context.Commit();
                return result;
            }
            catch (Exception e)
            {
                result.Fault();
                return result;
            }
        }
        public ResponseView Add(StorageView storage)
        {
            var model = _adapter.Adapt<Storage>(storage);

            var repo = ServiceLocator.Current.GetInstance<IStorageRepository>();

            var result = new ResponseView();

            try
            {
                repo.Add(model);
                repo.Context.Commit();
                return result;
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "添加仓库失败";
                return result;
            }
        }
 public ResponseView SubmitFeedback(IEnumerable<SubscriptionForFeedbackView> subscriptions, string loginName, string feedbackState, string remark)
 {
     //回告意见
     var sugg = feedbackState.ConvertToBool();
     //订单ID
     var ids = subscriptions.Select(t => t.SubscriptionId.ConvertToGuid());
     //回告状态
     FeedbackState state = FeedbackState.未征订;
     Enum.TryParse(feedbackState, out state);
     //返回消息
     var result = new ResponseView();
     //创建回告
     var feedback = SubscriptionService.CreateFeedback(loginName, state, remark);
     //修改订单,增加回告
     var subs = _subscriptionRepo.Find(t => ids.Contains(t.ID));
     foreach (var item in subs)
     {
         //添加回告
         item.Feedback = feedback;
         //修改订单状态
         item.SubscriptionState = state;
         _subscriptionRepo.Modify(item);
     }
     //_subscriptionRepo.Modify(
     //    t => ids.Contains(t.ID),
     //    d => new Subscription { Feedback = feedback }
     //    );
     try
     {
         _subscriptionRepo.Context.Commit();
     }
     catch (Exception e)
     {
         result.IsSuccess = false;
         result.Message = "回告失败";
     }
     return result;
 }
        public ResponseView SubmitDeclarationApproval(IEnumerable<DeclarationForApprovalView> declarations, string loginName, string suggestion, string remark)
        {
            //数据类型转换
            var sugg = suggestion.ConvertToBool();
            //审核人姓名与学院
            var user = new TbmisUserAppl(loginName).GetUser();
            var division = user.SchoolName;
            var auditor = user.TbmisUserName;

            //CUD仓储
            var repo = ServiceLocator.Current.GetInstance<IDeclarationRepository>();
            //操作响应类
            var result = new ResponseView();

            //处理审核记录
            //foreach (var item in declarations)
            //{
            //    var id = item.DeclarationId.ConvertToInt();
            //    var declaration = repo.First(t => t.DeclarationId == id);
            //    Domain.ApprovalService.CreateApproval<DeclarationApproval>(declaration, division, auditor, sugg, remark);
            //}
            //保存到db
            try
            {
                repo.Context.Commit();
                return result;
            }
            catch (Exception)
            {
                result.IsSuccess = false;
                result.Message = "提交审核失败";
                return result;
            }
        }
        public ResponseView SubmitSubscriptions(string booksellerId, string spareCount, IEnumerable<SubscriptionForSubmitView> subscriptions)
        {
            //类型转换
            var sellerId = booksellerId.ConvertToGuid();
            var spare = spareCount.ConvertToInt();
            var ids = subscriptions.Select(t => t.SubscriptionId.ConvertToGuid());
            //操作响应类
            var result = new ResponseView();
            //保存至DB
            try
            {
                //修改订单
                _subscriptionRepo.Modify(
                    t => ids.Contains(t.ID),
                    s => new Subscription
                    {
                        Bookseller_Id = sellerId,
                        SpareCount = spare,
                        SubscriptionState = FeedbackState.征订中
                    });

                //删除订单
                //RemoveNotSubscription();
                //写入DB
                _subscriptionRepo.Context.Commit();
            }
            catch (Exception e)
            {
                var msg = e.Message;
                result.IsSuccess = false;
                result.Message = "征订失败";
            }
            return result;
        }
        public ResponseView RemoveSubscriptions(IEnumerable<SubscriptionForFeedbackView> subscriptions)
        {
            //取订单ID
            var ids = subscriptions.Select(d => d.SubscriptionId.ConvertToGuid());
            //操作响应类
            var result = new ResponseView();

            //批量删除
            try
            {
                _subscriptionRepo.Remove(t => ids.Contains(t.ID));
                _subscriptionRepo.Context.Commit();
            }
            catch (Exception e)
            {
                var msg = e.Message;
                result.IsSuccess = false;
                result.Message = "征订失败";
            }
            return result;
        }
 public ResponseView GetStudentNameByStudentNum(string studentNum)
 {
     var name = new StudentAppl().GetNameByNum(studentNum);
     var result = new ResponseView();
     if (name.Contains("没有该学生"))
     {
         result.IsSuccess = false;
     }
     result.Message = name;
     return result;
 }
        public ResponseView SubmitInStock(InventoryView inventory, string instockCount, string loginName)
        {
            var count = instockCount.ConvertToInt();
            var person = new TbmisUserAppl(loginName).GetUser().TbmisUserName;
            var inve = _adapter.Adapt<Inventory>(inventory);
            var inveNew = new Inventory();
            //var inveNew = inve;
            var repo = ServiceLocator.Current.GetInstance<IInventoryRepository>();
            var result = new ResponseView();

            //是否已存在库存
            if (inve.ID != null && inve.ID != Guid.Empty)
            {
                //取出库存
                inveNew = repo.First(t => t.ID == inve.ID);
                //更新架位号
                inveNew.ShelfNumber = inve.ShelfNumber;
            }
            else
            {
                inveNew = inve;
            }

            //修改库存数量
            inveNew.InventoryCount += count;
            //创建库存异动记录
            InventoryService.CreateStockRecord<InStockRecord>(inveNew, person, count);

            try
            {
                if (inveNew.ID == null || inve.ID == Guid.Empty)
                {
                    repo.Add(inveNew);
                }
                else
                {
                    repo.Modify(inveNew);
                }

                repo.Context.Commit();
                return result;
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "入库操作失败";
                return result;
            }
        }
        public ResponseView Modify(StorageView storage)
        {
            var model = _adapter.Adapt<Storage>(storage);

            var repo = ServiceLocator.Current.GetInstance<IStorageRepository>();

            var stor = repo.First(t => t.ID == model.ID);

            stor.Name = model.Name;
            stor.Address = model.Address;

            var result = new ResponseView();

            try
            {
                repo.Context.Commit();
                return result;
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "修改仓库失败";
                return result;
            }
        }
        public ResponseView Remove(IEnumerable<StorageView> storages)
        {
            var models = _adapter.Adapt<Storage>(storages);

            var repo = ServiceLocator.Current.GetInstance<IStorageRepository>();

            var result = new ResponseView();

            try
            {
                foreach (var item in models)
                {
                    repo.Remove(item);
                }
                repo.Context.Commit();
                return result;
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "删除仓库失败";
                return result;
            }
        }
        public ResponseView SubmitTeacherDeclaration(DeclarationView view)
        {
            //类型转换
            var textId = view.TextbookId.ConvertToGuid();
            var teacId = new TbmisUserAppl(view.Declarant).GetUser().TbmisUserId;
            var count = view.DeclarationCount.ConvertToInt();
            //当前学期
            var term = new TermAppl().GetMaxTerm().YearTerm;
            //操作响应类
            var result = new ResponseView();

            try
            {
                ////CUD仓储
                //var repo = ServiceLocator.Current.GetInstance<ITeacherDeclarationRepository>();
                ////创建申报
                //var declaration = Domain.DeclarationService.CreateDeclaration<TeacherDeclaration>(term, view.TeachingTaskNums.First(), textId, teacId, view.Mobile, view.Telephone, count, view.NotNeedTextbook);
                ////保存
                //repo.Add(declaration);
                ////提交到数据库
                //repo.Context.Commit();
                return result;
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "提交教师用书申报失败";
                return result;
            }
        }
        //public ResponseView SubmitStudentDeclaration(IEnumerable<TeachingTaskView> teachingTaskViews, string textbookId, string loginName, string mobile, string telephone, string declarationCount)
        public ResponseView SubmitStudentDeclaration(DeclarationView view)
        {
            //数据类型转换

            var count = view.DeclarationCount.ConvertToInt();
            //取对象
            var term = new TermAppl().GetMaxTerm().YearTerm;  //学期
            var teacId = new TbmisUserAppl(view.Declarant).GetUser().TbmisUserId;  //教师ID
            var teacher = _teacherRepo.First(t => t.ID == teacId);  //教师

            var repo = ServiceLocator.Current.GetInstance<IStudentDeclarationRepository>();//CUD专用仓储

            //重载构造函数参数的写法,使得仓储共用工作单元,实现事务
            //配置文件中要添加name为Update的IUnitOfWork的Mapto

            //var declRepo = ServiceLocator.Current.GetInstance<IDeclarationRepository>(new ParameterOverrides()
            //        {
            //            {"unitOfWork", ServiceLocator.Current.GetInstance<IUnitOfWork>("Update")}
            //        });
            //var subRepo = ServiceLocator.Current.GetInstance<ISubscriptionRepository>(new ParameterOverrides()
            //        {
            //            {"unitOfWork", ServiceLocator.Current.GetInstance<IUnitOfWork>("Update")}
            //        });

            //操作响应类
            var result = new ResponseView();
            //错误消息
            var faultMessage = string.Empty;
            //返回消息
            var messageForResponse = new StringBuilder();
            //成功数量
            var successCount = 0;

            //需要教材
            //if (!view.NotNeedTextbook)
            //{
            //    //取教材
            //    var textId = view.TextbookId.ConvertToGuid();
            //    var book = _textbookRepo.First(t => t.ID == textId);  //教材

            //    //为教学任务中的每个学生班级创建用书申报
            //    foreach (var item in view.TeachingTaskNums)
            //    {
            //        //取教学任务
            //        var task = _teachingTaskRepo.First(t => t.TeachingTaskNum == item);

            //        //创建学生用书申报
            //        var declaration = Domain.DeclarationService.CreateDeclaration<StudentDeclaration>(term, item, textId, teacId, view.Mobile, view.Telephone, count, view.NotNeedTextbook);

            //        //取出教学任务中的学生班级,依次处理
            //        foreach (var proClass in task.ProfessionalClasses)
            //        {
            //            //检查班级是否满足申报条件
            //            if (Domain.DeclarationService.CanDeclaration(proClass, book, ref faultMessage))
            //            {
            //                //创建申报班级
            //                Domain.DeclarationService.CreateDeclarationClass(declaration, proClass);
            //            }
            //            else
            //            {
            //                //记录出错信息
            //                messageForResponse.Append(faultMessage);
            //                faultMessage = string.Empty;
            //            }
            //        }

            //        //检查可申报该教材的班级数量
            //        if (declaration.DeclarationClasses.Count() == task.ProfessionalClasses.Count())
            //        {
            //            //将申报添加到仓储
            //            repo.Add(declaration);
            //            successCount++;
            //        }
            //    }

            //}
            //else    //不需要教材
            //{
            //    foreach (var item in view.TeachingTaskNums)
            //    {
            //        //创建学生用书申报
            //        var declaration = Domain.DeclarationService.CreateDeclaration<StudentDeclaration>(term, item, null, teacId, view.Mobile, view.Telephone, count, view.NotNeedTextbook);

            //        //将申报添加到仓储
            //        repo.Add(declaration);
            //        successCount++;
            //    }
            //}
            try
            {
                //将申报保存到数据库
                repo.Context.Commit();
                //返回消息
                result.Message = string.Format("共{0}个教学班,成功{1}个教学班",
                    view.TeachingTaskNums.Count(),
                    successCount
                    );
                //如果没有全部成功提交,增加错误消息
                if (view.TeachingTaskNums.Count() != successCount)
                {
                    result.Message += string.Format(",失败{0}个教学班{1}失败原因:{2}",
                        view.TeachingTaskNums.Count() - successCount,
                        " ",
                        messageForResponse
                        );
                }

                return result;
            }
            catch (Exception e)
            {
                result.IsSuccess = false;
                result.Message = "提交学生用书申报失败";
                return result;
            }
        }
        /// <summary>
        /// 退还班级教材
        /// </summary>
        /// <param name="classId"></param>
        /// <param name="releaseRecordIds"></param>
        /// <returns></returns>
        public ResponseView DropClassBookSubmit(string classId, IEnumerable<string> releaseRecordIds)
        {
            //数据类型转换
            Guid cId = classId.ConvertToGuid();
            var term = new TermAppl().GetCurrentTerm().ToString();
            //根据发放记录Id,当前学期,取该班级每本教材的退还数
            var dorpBookCountForClass = new List<Domain.Models.StudentReleaseRecord>();
            foreach (string recordId in releaseRecordIds)
            {
                var rId = recordId.ConvertToGuid();
                var classRel = _studentReleaseRecordRepo.Find(s => s.Class_Id == cId && s.ID == rId).GroupBy(s => new
                {
                    s.Textbook_Id,
                    s.ID
                }).Select(d => new StudentReleaseRecord()
                    {
                        ID = d.Key.ID,
                        Textbook_Id = d.Key.Textbook_Id,
                        ReleaseCount = d.Sum(s => s.ReleaseCount)
                    });

                dorpBookCountForClass.AddRange(classRel);
            }

            //根据发放记录Id,取对应的库存变更记录Id
            var stockRecordIds = new List<Domain.Models.StudentReleaseRecord>();

            foreach (string releaseRecordId in releaseRecordIds)
            {
                var recordId = releaseRecordId.ConvertToGuid();
                var stockRecordId = _studentReleaseRecordRepo.First(s => s.ID == recordId);

                stockRecordIds.Add(stockRecordId);
            }
            //根据库存变更记录Id,取库存Id
            var inventoryIds = new List<Domain.Models.InStockRecord>();
            foreach (StudentReleaseRecord id in stockRecordIds)
            {
                var inventoryId = _inStockRecordRepo.First(i => i.ID == id.StockRecord_Id);

                inventoryIds.Add(inventoryId);
            }

            var result = new ResponseView();
            var message = string.Empty;
            var messageForResponse = string.Empty;
            var releaseRecordCount = 0;

            releaseRecordCount += releaseRecordIds.Count();
            var successCount = 0;

            //删除库存变记录
            foreach (Domain.Models.StudentReleaseRecord stockRecordId in stockRecordIds)
            {
                if (stockRecordId.StockRecord_Id != null)
                {
                    var stoockRecord = Domain.ReleaseBookService.CreateStockRecord(stockRecordId.StockRecord_Id.Value);
                    _inStockRecordRepo.Remove(stoockRecord);
                    _inStockRecordRepo.Context.Commit();
                }
            }

            //修改库存数量
            foreach (InStockRecord view in inventoryIds)
            {
                var releaseCount = dorpBookCountForClass.First(d => d.StockRecord_Id == view.ID).ReleaseCount;
                var inventory = Domain.ReleaseBookService.CreatInventoryAtDrop(view.Inventory_Id,
                                                                        releaseCount);
                _inventoryRepo.Modify(inventory);
                _inventoryRepo.Context.Commit();
            }

            //删除班级发放记录
            foreach (string releaseRecordId in releaseRecordIds)
            {
                var claID = classId.ConvertToGuid();
                var studentRecord =
                    Domain.ReleaseBookService.CreateClassDropReleaseRecord(
                        releaseRecordId.ConvertToGuid(), claID);
                _studentReleaseRecordRepo.Remove(studentRecord);

                _studentReleaseRecordRepo.Context.Commit();
                successCount++;
            }

            return result;
        }
        public ResponseView Import(IEnumerable<CasMapperView> casMappers)
        {
            var repo = ServiceLocator.Current.GetInstance<ICasMapperRepository>();
            var result = new ResponseView();

            try
            {
                var models = _adapter.Adapt<CasMapper>(casMappers);
                foreach (var item in models)
                {
                    repo.Add(item);
                }
                repo.Context.Commit();
                return result;
            }
            catch (Exception e)
            {
                result.Fault();
                return result;
            }
        }
        public ResponseView SubmitFeedbackApproval(IEnumerable<FeedbackForApprovalView> feedbacks, string loginName, string suggestion, string remark)
        {
            //审核意见
            var sugg = suggestion.ConvertToBool();
            //审核部门,审核人
            var user = new TbmisUserAppl(loginName).GetUser();
            var division = user.SchoolName;
            var auditor = user.TbmisUserName;
            //回告ID
            var ids = feedbacks.Select(t => t.FeedbackId.ConvertToGuid());
            //消息类
            var result = new ResponseView();
            //保存到db
            try
            {
                //处理审核记录
                var backs = _repo.Find(t => ids.Contains(t.ID)).ToList();
                backs.ForEach(t =>
                    {
                        Domain.ApprovalService.CreateApproval<FeedbackApproval>(t, division, auditor, sugg, remark);
                    });

                _repo.Context.Commit();
            }
            catch (Exception)
            {
                result.IsSuccess = false;
                result.Message = "提交审核失败";
            }
            return result;
        }
        /// <summary>
        /// 退还学生教材
        /// </summary>
        /// <param name="releaseRecordeIds"></param>
        /// <param name="studentId"></param>
        /// <returns></returns>
        public ResponseView DropStudenBookSubmit(IEnumerable<string> releaseRecordeIds, string studentId)
        {
            //数据类型转换
            Guid stID = studentId.ConvertToGuid();
            var term = new TermAppl().GetCurrentTerm().ToString();
            //根据学生Id,发放记录Id,取该生每本教材的退还数
            var dorpBookCountForStudent = new List<StudentReleaseRecord>();
            foreach (string recordId in releaseRecordeIds)
            {
                var rId = recordId.ConvertToGuid();
                var studentRel = _studentReleaseRecordRepo.First(s => s.Student_Id == stID && s.ID == rId);
                dorpBookCountForStudent.Add(studentRel);
            }

            //根据发放记录Id,取对应的库存变更记录Id
            var stockRecordIds = new List<StudentReleaseRecord>();

            foreach (string recordId in releaseRecordeIds)
            {
                var rId = recordId.ConvertToGuid();
                var stockRecordId = _studentReleaseRecordRepo.First(s => s.ID == rId);

                stockRecordIds.Add(stockRecordId);
            }
            //根据库存变更记录Id,取库存Id
            var inventoryIds = new List<Domain.Models.InStockRecord>();
            foreach (StudentReleaseRecord id in stockRecordIds)
            {
                var inventoryId = _inStockRecordRepo.First(i => i.ID == id.StockRecord_Id);

                inventoryIds.Add(inventoryId);
            }

            var result = new ResponseView();
            var message = string.Empty;
            var messageForResponse = string.Empty;
            var releaseRecordCount = 0;

            releaseRecordCount += dorpBookCountForStudent.Count();
            var successCount = 0;

            //删除库存变记录
            foreach (StudentReleaseRecord stockRecordId in stockRecordIds)
            {
                if (stockRecordId.StockRecord_Id != null)
                {
                    var stoockRecord = Domain.ReleaseBookService.CreateStockRecord(stockRecordId.StockRecord_Id.Value);
                    _inStockRecordRepo.Remove(stoockRecord);
                    _inStockRecordRepo.Context.Commit();
                }
            }

            //修改库存数量
            foreach (InStockRecord view in inventoryIds)
            {
                var releaseCount = dorpBookCountForStudent.First(d => d.StockRecord_Id == view.ID).ReleaseCount;
                var inventory = Domain.ReleaseBookService.CreatInventoryAtDrop(view.Inventory_Id,
                                                                        releaseCount);
                _inventoryRepo.Modify(inventory);
                _inventoryRepo.Context.Commit();
            }

            //删除学生发放记录
            foreach (StudentReleaseRecord view in dorpBookCountForStudent)
            {
                var studentRecord =
                    Domain.ReleaseBookService.CreateStudentDropReleaseRecord(
                        view.ID, stID);
                _studentReleaseRecordRepo.Remove(studentRecord);

                _studentReleaseRecordRepo.Context.Commit();
                successCount++;
            }

            return result;
        }
        public ResponseView Remove(IEnumerable<TextbookForQueryView> textbooks)
        {
            var repo = ServiceLocator.Current.GetInstance<ITextbookRepository>();
            var result = new ResponseView();

            var books = _adapter.Adapt<Textbook>(textbooks);

            try
            {
                foreach (var item in books)
                {
                    repo.Remove(item);
                }

                repo.Context.Commit();
                return result;
            }
            catch (Exception)
            {
                result.IsSuccess = false;
                result.Message = "删除图书失败";
                return result;
            }
        }