コード例 #1
0
        /// <summary>
        /// DT_DEVICE_FILEテーブルからDtDeviceFileを削除する
        /// </summary>
        /// <param name="sid">削除するデータのSID</param>
        /// <returns>削除したデータ</returns>
        public DtDeviceFile DeleteDtDeviceFile(long sid)
        {
            DtDeviceFile model = null;

            try
            {
                _logger.Enter($"{nameof(sid)}={sid}");

                DBAccessor.Models.DtDeviceFile entity = new DBAccessor.Models.DtDeviceFile()
                {
                    Sid = sid
                };
                _dbPolly.Execute(() =>
                {
                    using (DBAccessor.Models.RmsDbContext db = new DBAccessor.Models.RmsDbContext(_appSettings))
                    {
                        db.DtDeviceFile.Attach(entity);
                        db.DtDeviceFile.Remove(entity);

                        if (db.SaveChanges() > 0)
                        {
                            model = entity.ToModel();
                        }
                    }
                });

                return(model);
            }
            catch (ValidationException e)
            {
                throw new RmsParameterException(e.ValidationResult.ErrorMessage, e);
            }
            catch (Exception e)
            {
                throw new RmsException("DT_DEVICE_FILEテーブルのDeleteに失敗しました。", e);
            }
            finally
            {
                _logger.LeaveJson("{0}", model);
            }
        }
コード例 #2
0
        /// <summary>
        /// DT_DEVICE_FILEテーブルからDtDeviceFileを取得する
        /// </summary>
        /// <param name="sid">取得するデータのSID</param>
        /// <returns>取得したデータ</returns>
        public DtDeviceFile ReadDtDeviceFile(long sid)
        {
            DtDeviceFile model = null;

            try
            {
                _logger.Enter($"{nameof(sid)}={sid}");

                DBAccessor.Models.DtDeviceFile entity = null;
                _dbPolly.Execute(() =>
                {
                    using (DBAccessor.Models.RmsDbContext db = new DBAccessor.Models.RmsDbContext(_appSettings))
                    {
                        entity = db.DtDeviceFile.Include(x => x.DeviceS).Include(x => x.DtDeviceFileAttribute).FirstOrDefault(x => x.Sid == sid);
                    }
                });

                if (entity != null)
                {
                    model = entity.ToModel();
                }

                return(model);
            }
            catch (ValidationException e)
            {
                throw new RmsParameterException(e.ValidationResult.ErrorMessage, e);
            }
            catch (Exception e)
            {
                throw new RmsException("DT_DELIVERY_FILEテーブルのSelectに失敗しました。", e);
            }
            finally
            {
                _logger.LeaveJson("{0}", model);
            }
        }
コード例 #3
0
        /// <summary>
        /// 引数に指定したDtDeviceFileをDT_DEVICE_FILEテーブルへ登録する
        /// 既にレコードが存在する場合は登録ではなく当該レコードの更新処理を行う
        /// また当該レコードに紐づいたDT_DEVICE_FILE_ATTRIBUTEテーブルの更新を行う
        /// </summary>
        /// <param name="inData">登録するデータ</param>
        /// <returns>処理結果</returns>
        public DtDeviceFile CreateOrUpdateDtDeviceFile(DtDeviceFile inData)
        {
            DtDeviceFile model = null;

            // 更新日時
            // DtDeviceFileAttribute更新時に明示的に日時データを渡す必要があるため、
            // レコード作成および更新日時はすべて明示的に設定する
            var now = _timePrivder.UtcNow;

            try
            {
                _logger.EnterJson("{0}", inData);

                _dbPolly.Execute(() =>
                {
                    using (DBAccessor.Models.RmsDbContext db = new DBAccessor.Models.RmsDbContext(_appSettings))
                        using (var tran = db.Database.BeginTransaction())
                        {
                            string container = inData.Container;
                            string filePath  = inData.FilePath;

                            var deviceFileModel = db.DtDeviceFile.FirstOrDefault(
                                x => x.Container.Equals(container, StringComparison.OrdinalIgnoreCase) &&
                                x.FilePath.Equals(filePath, StringComparison.OrdinalIgnoreCase));

                            DBAccessor.Models.DtDeviceFile entity;

                            if (deviceFileModel == null || deviceFileModel.Sid == 0)
                            {
                                inData.CreateDatetime = now;
                                inData.UpdateDatetime = now;
                                entity = new DBAccessor.Models.DtDeviceFile(inData);

                                // コンテナ名とファイルパスをキーにして見つからなければレコード追加
                                entity = db.DtDeviceFile.Add(entity).Entity;
                            }
                            else
                            {
                                // 端末ファイル更新
                                deviceFileModel.SourceEquipmentUid = inData.SourceEquipmentUid;
                                deviceFileModel.CollectDatetime    = inData.CollectDatetime;
                                deviceFileModel.UpdateDatetime     = now;

                                entity = deviceFileModel;

                                // コンテナ名とファイルパスをキーにしてレコードが見つかった場合は、
                                // 端末ファイルデータおよび紐づく端末ファイル属性レコードの更新を行う
                                // 端末ファイル属性については、以下の処理を行う
                                // ・同じNameを持つレコードが存在しない場合はレコード追加
                                // ・同じNameを持つレコードが存在する場合はレコードを更新
                                // ・既に存在するレコードのNameが追加するレコードの中に存在しない場合は、当該レコードを削除

                                // 端末ファイル属性
                                var inDeviceFileAttributeModel     = inData.DtDeviceFileAttribute;
                                var actualDeviceFileAttributeModel = db.DtDeviceFileAttribute.Where(x => x.DeviceFileSid == deviceFileModel.Sid);

                                // 端末ファイル属性テーブルに存在するが、追加するレコードの中に存在しないものは削除する
                                foreach (var attr in actualDeviceFileAttributeModel)
                                {
                                    var inAttrData = inDeviceFileAttributeModel.FirstOrDefault(x => x.Name.Equals(attr.Name, StringComparison.OrdinalIgnoreCase));
                                    if (inAttrData == null)
                                    {
                                        db.DtDeviceFileAttribute.Remove(attr);
                                    }
                                }

                                // 端末ファイル属性テーブルに存在しないものは追加、存在するものは更新
                                foreach (var attr in inDeviceFileAttributeModel)
                                {
                                    var found = actualDeviceFileAttributeModel.FirstOrDefault(x => x.Name.Equals(attr.Name, StringComparison.OrdinalIgnoreCase));
                                    DBAccessor.Models.DtDeviceFileAttribute deviceFileAttributeEntity;

                                    if (found == null || found.Sid == 0)
                                    {
                                        attr.DeviceFileSid        = deviceFileModel.Sid;
                                        attr.CreateDatetime       = now;
                                        attr.UpdateDatetime       = now;
                                        deviceFileAttributeEntity = new DBAccessor.Models.DtDeviceFileAttribute(attr);

                                        // レコード追加
                                        _ = db.DtDeviceFileAttribute.Add(deviceFileAttributeEntity).Entity;
                                    }
                                    else
                                    {
                                        // レコード更新
                                        found.Value          = attr.Value;
                                        found.UpdateDatetime = now;
                                    }
                                }
                            }

                            db.SaveChanges();
                            model = entity.ToModel();

                            // トランザクション終了
                            tran.Commit();
                        }
                });

                return(model);
            }
            catch (ValidationException e)
            {
                throw new RmsParameterException(e.ValidationResult.ErrorMessage, e);
            }
            catch (Exception e)
            {
                throw new RmsException("DT_DEVICE_FILEテーブルへのInsertに失敗しました。", e);
            }
            finally
            {
                _logger.LeaveJson("{0}", model);
            }
        }