Ejemplo n.º 1
0
        public JsonResult Save_(ManipulationLibraryModel ManiModel, List <ManipulationEquipmentModel> TMUModel, bool isUseMachine, List <FileUploadModel> FileModel)
        {
            ResponseBase responseResult;

            try
            {
                ManiModel.ActionUser = UserContext.UserID;
                responseResult       = BLLManipulationLibrary.Instance.InsertOrUpdate(ManiModel, TMUModel, FileModel);

                if (!responseResult.IsSuccess)
                {
                    JsonDataResult.Result = "ERROR";
                    JsonDataResult.ErrorMessages.AddRange(responseResult.Errors);
                }
                else
                {
                    JsonDataResult.Result = "OK";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(Json(JsonDataResult));
        }
Ejemplo n.º 2
0
        public ResponseBase InsertOrUpdate(ManipulationLibraryModel model, List <ManipulationEquipmentModel> TMUModel, List <FileUploadModel> fileModel)
        {
            try
            {
                using (db = new IEDEntities())
                {
                    var result = new ResponseBase();
                    T_ManipulationLibrary   mani          = null;
                    T_ManipulationEquipment maniEquipment = null;
                    T_ManipulationFile      maniFile      = null;
                    T_File file = null;
                    if (CheckExists(model.Name.Trim().ToUpper(), model.Id, true))
                    {
                        result.IsSuccess = false;
                        result.Errors.Add(new Error()
                        {
                            MemberName = "Insert  ", Message = "Tên Thao Tác này đã tồn tại . Vui lòng chọn lại Tên khác !."
                        });
                        return(result);
                    }
                    if (!string.IsNullOrEmpty(model.Code))
                    {
                        if (CheckExists(model.Code.Trim().ToUpper(), model.Id, false))
                        {
                            result.IsSuccess = false;
                            result.Errors.Add(new Error()
                            {
                                MemberName = "Insert  ", Message = "Mã Thao Tác này đã tồn tại này. Vui lòng chọn lại Mã khác !."
                            });
                            return(result);
                        }
                    }
                    if (model.Id == 0)
                    {
                        #region add
                        mani                    = new T_ManipulationLibrary();
                        mani.Id                 = model.Id;
                        mani.Name               = model.Name;
                        mani.Code               = model.Code;
                        mani.Description        = model.Description;
                        mani.ManipulationTypeId = model.ManipulationTypeId;
                        mani.StandardTMU        = Math.Round(model.StandardTMU, 2);
                        mani.UserTMU            = Math.Round(model.UserTMU, 2);
                        if (model.Distance == 0)
                        {
                            mani.EquipmentTypeId = null;
                            mani.StopPrecisionId = null;
                            mani.ApplyPressureId = null;
                            mani.NatureCutsId    = null;
                            mani.Distance        = null;
                        }
                        else
                        {
                            mani.EquipmentTypeId = model.EquipmentTypeId;
                            mani.ApplyPressureId = model.ApplyPressureId;
                            mani.NatureCutsId    = model.NatureCutsId;
                            mani.StopPrecisionId = model.StopPrecisionId;
                            mani.Distance        = model.Distance;
                        }
                        mani.CreatedUser = model.ActionUser;
                        mani.CreatedDate = DateTime.Now;


                        if (TMUModel != null && TMUModel.Count > 0)
                        {
                            mani.T_ManipulationEquipment = new System.Collections.ObjectModel.Collection <T_ManipulationEquipment>();
                            foreach (var item in TMUModel)
                            {
                                maniEquipment = new T_ManipulationEquipment();
                                Parse.CopyObject(item, ref maniEquipment);
                                maniEquipment.T_ManipulationLibrary = mani;
                                maniEquipment.CreatedUser           = mani.CreatedUser;
                                maniEquipment.CreatedDate           = mani.CreatedDate;
                                mani.T_ManipulationEquipment.Add(maniEquipment);
                            }
                        }

                        if (fileModel != null && fileModel.Count > 0)
                        {
                            mani.T_ManipulationFile = new System.Collections.ObjectModel.Collection <T_ManipulationFile>();
                            foreach (var item in fileModel)
                            {
                                // add file
                                file = new T_File();
                                Parse.CopyObject(item, ref file);
                                file.T_ManipulationFile = new System.Collections.ObjectModel.Collection <T_ManipulationFile>();
                                file.CreatedUser        = mani.CreatedUser;
                                file.CreatedDate        = mani.CreatedDate;


                                // add commodity file
                                maniFile = new T_ManipulationFile();
                                maniFile.T_ManipulationLibrary = mani;
                                //maniFile.FileId = file.Id;
                                maniFile.CreatedUser = mani.CreatedUser;
                                maniFile.CreatedDate = mani.CreatedDate;
                                maniFile.T_File      = file;
                                mani.T_ManipulationFile.Add(maniFile);
                                file.T_ManipulationFile.Add(maniFile);
                                db.T_File.Add(file);
                            }
                        }

                        db.T_ManipulationLibrary.Add(mani);
                        #endregion
                    }
                    else
                    {
                        #region Update
                        mani = db.T_ManipulationLibrary.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id);
                        if (mani == null)
                        {
                            result.IsSuccess = false;
                            result.Errors.Add(new Error()
                            {
                                MemberName = "Update  ", Message = "Dữ liệu bạn đang thao tác đã bị xóa hoặc không tồn tại. Vui lòng kiểm tra lại !."
                            });
                            return(result);
                        }
                        else
                        {
                            mani.Name               = model.Name;
                            mani.Code               = model.Code;
                            mani.Description        = model.Description;
                            mani.ManipulationTypeId = model.ManipulationTypeId;
                            mani.StandardTMU        = Math.Round(model.StandardTMU, 2);
                            mani.UserTMU            = Math.Round(model.UserTMU, 2);
                            //if (maniModel.Distance == null)
                            //{
                            //    mani.EquipmentTypeId = null;
                            //    mani.StopPrecisionId = null;
                            //    mani.ApplyPressureId = null;
                            //    mani.NatureCutsId = null;
                            //    mani.Distance = null;
                            //}
                            //else
                            //{
                            mani.EquipmentTypeId = model.EquipmentTypeId;
                            mani.ApplyPressureId = model.ApplyPressureId;
                            mani.NatureCutsId    = model.NatureCutsId;
                            mani.StopPrecisionId = model.StopPrecisionId;
                            mani.Distance        = model.Distance;
                            //}
                            mani.UpdatedUser = model.ActionUser;
                            mani.UpdatedDate = DateTime.Now;

                            //remove old
                            if (model.isListMachineTMUChange)
                            {
                                var maniEquipOld = db.T_ManipulationEquipment.Where(x => !x.IsDeleted && x.ManipulationId == model.Id);
                                if (maniEquipOld != null && maniEquipOld.Count() > 0)
                                {
                                    foreach (var item in maniEquipOld)
                                    {
                                        item.IsDeleted   = true;
                                        item.DeletedUser = mani.UpdatedUser;
                                        item.DeletedDate = mani.UpdatedDate;
                                    }
                                }
                                // add new
                                if (TMUModel != null && TMUModel.Count() > 0)
                                {
                                    foreach (var item in TMUModel)
                                    {
                                        maniEquipment = new T_ManipulationEquipment();
                                        Parse.CopyObject(item, ref maniEquipment);
                                        maniEquipment.ManipulationId = mani.Id;
                                        maniEquipment.CreatedUser    = mani.UpdatedUser ?? 0;
                                        maniEquipment.CreatedDate    = mani.UpdatedDate ?? DateTime.Now;
                                        db.T_ManipulationEquipment.Add(maniEquipment);
                                    }
                                }
                            }

                            #region File
                            if (model.isListAttachFileChange)
                            {
                                var maniFiles = db.T_ManipulationFile.Where(x => !x.IsDeleted && x.ManipulationId == mani.Id);
                                if (maniFiles != null && maniFiles.Count() > 0)
                                {
                                    foreach (var item in maniFiles)
                                    {
                                        item.IsDeleted   = true;
                                        item.DeletedUser = mani.UpdatedUser;
                                        item.DeletedDate = mani.UpdatedDate;
                                    }
                                }

                                if (fileModel != null && fileModel.Count > 0)
                                {
                                    // mani.T_ManipulationFile = new System.Collections.ObjectModel.Collection<T_ManipulationFile>();
                                    foreach (var item in fileModel)
                                    {
                                        // add file
                                        file = new T_File();
                                        Parse.CopyObject(item, ref file);
                                        file.T_ManipulationFile = new System.Collections.ObjectModel.Collection <T_ManipulationFile>();
                                        file.CreatedUser        = mani.UpdatedUser ?? 0;
                                        file.CreatedDate        = mani.UpdatedDate ?? DateTime.Now;


                                        // add commodity file
                                        maniFile = new T_ManipulationFile();
                                        //  maniFile.T_ManipulationLibrary = mani;
                                        maniFile.ManipulationId = model.Id;
                                        maniFile.CreatedUser    = mani.UpdatedUser ?? 0;
                                        maniFile.CreatedDate    = mani.UpdatedDate ?? DateTime.Now;
                                        maniFile.T_File         = file;
                                        //mani.T_ManipulationFile.Add(maniFile);
                                        file.T_ManipulationFile.Add(maniFile);
                                        db.T_File.Add(file);
                                    }
                                }
                            }
                            #endregion
                            #endregion
                        }
                    }
                    db.SaveChanges();
                    result.IsSuccess = true;
                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }