Пример #1
0
        public async Task <int> AddAsync(LogAddModel mLog)
        {
            var location = _context.Locations.FirstOrDefault(a => a.LocationId == mLog.LocationId);

            if (location == null)
            {
                throw new ExpectException("Could not find Location data which LocationId equal to " + mLog.LocationId);
            }

            var logDescription = _context.LogDescriptions.FirstOrDefault(a => a.LogDescriptionId == mLog.LogDescriptionId);

            if (logDescription == null)
            {
                throw new ExpectException("Could not find LogDescription data which LogDescriptionId equal to " + mLog.LogDescriptionId);
            }

            var user = _loginUser.GetLoginUserInfo();

            var model = new Log
            {
                Comment          = mLog.Comment,
                LocationId       = mLog.LocationId,
                LogDescriptionId = mLog.LogDescriptionId,
                Creator          = user.UserName,
                CreateDate       = DateTime.Now
            };

            _context.Logs.Add(model);
            await _context.SaveChangesAsync();

            return(model.LogId);
        }
Пример #2
0
        public async Task <int> AddAsync(ProjectAddModel project)
        {
            var customer = _context.Customers.FirstOrDefault(c => c.CustomerId == project.CustomerId);

            if (customer == null)
            {
                throw new ExpectException("Could not find Customer data which CustomerId equal to " + project.CustomerId);
            }

            //ProjectNo must be unique
            var checkData = await _context.Projects.Where(p => p.ProjectNo == project.ProjectNo).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which ProjectNo equal to '" + project.ProjectNo + "' already exist in system");
            }

            //Get UserInfo
            var user = _loginUser.GetLoginUserInfo();

            var model = new Project
            {
                CustomerId  = project.CustomerId,
                ProjectName = project.ProjectName,
                ProjectNo   = project.ProjectNo,
                Creator     = user.UserName,
                CreateDate  = DateTime.Now
            };

            _context.Projects.Add(model);
            await _context.SaveChangesAsync();

            return(model.ProjectId);
        }
Пример #3
0
        public async Task <int> AddAsync(SceneAddModel scene)
        {
            var project = _context.Projects.FirstOrDefault(c => c.ProjectId == scene.ProjectId);

            if (project == null)
            {
                throw new ExpectException("Could not find Project data which ProjectId equal to " + scene.ProjectId);
            }

            //SceneName must be unique
            var checkData = await _context.Scenes.Where(s => s.SceneName == scene.SceneName).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which SceneName equal to '" + scene.SceneName + "' already exist in system");
            }


            //Get UserInfo
            var user  = _loginUser.GetLoginUserInfo();
            var model = new Scene
            {
                ProjectId  = scene.ProjectId,
                SceneName  = scene.SceneName,
                Enable     = scene.Enable,
                Creator    = user.UserName,
                CreateDate = DateTime.Now
            };

            _context.Scenes.Add(model);
            await _context.SaveChangesAsync();

            return(model.SceneId);
        }
Пример #4
0
        public async Task <int> AddAsync(CustomerAddModel customer)
        {
            //CustomerNo must be unique
            var checkData = await _context.Customers.Where(c => c.CustomerNo == customer.CustomerNo).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which CustomerNo equal to '" + customer.CustomerNo + "' already exist in system");
            }

            //Get UserInfo
            var user = _loginUser.GetLoginUserInfo();

            var model = new Customer
            {
                CustomerName = customer.CustomerName,
                CustomerNo   = customer.CustomerNo,
                Creator      = user.UserName,
                CreateDate   = DateTime.Now
            };

            _context.Customers.Add(model);

            await _context.SaveChangesAsync();

            return(model.CustomerId);
        }
Пример #5
0
        public async Task <int> AddAsync(SceneSegmentAddModel mSceneSegment)
        {
            var scene = _context.Scenes.FirstOrDefault(c => c.SceneId == mSceneSegment.SceneId);

            if (scene == null)
            {
                throw new ExpectException("Could not find Scene data which SceneId equal to " + mSceneSegment.SceneId);
            }

            //SequenceNo must be unique
            var checkData = await _context.SceneSegments.Where(s => s.SequenceNo == mSceneSegment.SequenceNo).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which SequenceNo equal to '" + mSceneSegment.SequenceNo + "' already exist in system");
            }


            //Get UserInfo
            var user  = _loginUser.GetLoginUserInfo();
            var model = new SceneSegment
            {
                SceneId    = mSceneSegment.SceneId,
                SequenceNo = mSceneSegment.SequenceNo,
                StartTime  = mSceneSegment.StartTime,
                Volumn     = mSceneSegment.Volumn,
                Creator    = user.UserName,
                CreateDate = DateTime.Now
            };

            _context.SceneSegments.Add(model);
            await _context.SaveChangesAsync();

            return(model.SceneSegmentId);
        }
Пример #6
0
        public async Task <int> AddAsync(HolidayAddModel mHoliday)
        {
            var project = _context.Projects.FirstOrDefault(c => c.ProjectId == mHoliday.ProjectId);

            if (project == null)
            {
                throw new ExpectException("Could not find Project data which ProjectId equal to " + mHoliday.ProjectId);
            }

            //Get UserInfo
            var user = _loginUser.GetLoginUserInfo();

            var model = new Holiday
            {
                ProjectId  = mHoliday.ProjectId,
                Day        = mHoliday.Day,
                Creator    = user.UserName,
                CreateDate = DateTime.Now
            };

            _context.Holidays.Add(model);
            await _context.SaveChangesAsync();

            return(model.HolidayId);
        }
Пример #7
0
        public async Task <int> AddAsync(AlarmAddModel mAlarm)
        {
            var location = _context.Locations.FirstOrDefault(a => a.LocationId == mAlarm.LocationId);

            if (location == null)
            {
                throw new ExpectException("Could not find Location data which LocationId equal to " + mAlarm.LocationId);
            }

            var alarmMessage = _context.AlarmMessages.FirstOrDefault(a => a.AlarmMessageId == mAlarm.AlarmMessageId);

            if (alarmMessage == null)
            {
                throw new ExpectException("Could not find AlarmMessage data which AlarmMessageId equal to " + mAlarm.AlarmMessageId);
            }


            var model = new Alarm
            {
                AlarmMessageId = mAlarm.AlarmMessageId,
                LocationId     = mAlarm.LocationId,
                CreateDate     = DateTime.Now
            };

            _context.Alarms.Add(model);
            await _context.SaveChangesAsync();

            if (!string.IsNullOrEmpty(mAlarm.Email))
            {
                //Send Mail
                MailSend mailSend = new MailSend();
                mailSend.To.Add(new MailboxAddress("", mAlarm.Email));
                mailSend.Subject = "AlarmMessage";

                var builder = new BodyBuilder();

                // Set the plain-text version of the message text
                builder.HtmlBody = string.Format(@"
<!DOCTYPE html><html><head><meta charset='utf - 8'><title>MailAlarm</title><style></style></head><body id='preview'>
<p><font style='font-weight:bold;'>Location Information</font></p>
<p>DeviceSerialNo:{0}</p>
<p><font style='font-weight:bold;'>Alarm Message</font></p>
<p>ErrorCode:{1}</p>
<p>Message:{2}</p>
<br/>
<br/>
<br/>
--This is a system email 
</ body ></ html > ", location.DeviceSerialNo, alarmMessage.ErrorCode, alarmMessage.Message);


                mailSend.Body = builder.ToMessageBody();
                await mailSend.SendAsync();
            }


            return(model.AlarmId);
        }
Пример #8
0
        public async Task <int> AddAsync(DeviceAddModel mDevice)
        {
            var model = new Device
            {
                Voltage  = mDevice.Voltage,
                Diameter = mDevice.Diameter,
                Torque   = mDevice.Torque,
            };

            _context.Devices.Add(model);

            await _context.SaveChangesAsync();

            return(model.DeviceId);
        }
Пример #9
0
        public async Task <int> AddAsync(GroupAddModel group)
        {
            //Chech whether the Foreign key ProjectId data exist
            var project = _context.Projects.FirstOrDefault(p => p.ProjectId == group.ProjectId);

            if (project == null)
            {
                throw new ExpectException("Could not find Project data which ProjectId equal to " + group.ProjectId);
            }

            //If SceneId not null,check whether corresponding Scenes data existed
            if (group.SceneId != null)
            {
                var scene = _context.Scenes.FirstOrDefault(p => p.SceneId == group.SceneId);
                if (scene == null)
                {
                    throw new ExpectException("Could not find Scenes data which SceneId equal to " + group.SceneId);
                }
            }

            //GroupName must be unique
            var checkData = await _context.Groups.Where(g => g.GroupName == group.GroupName).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which GroupName equal to '" + group.GroupName + "' already exist in system.");
            }

            //Get UserInfo
            var user = _loginUser.GetLoginUserInfo();

            var model = new Group
            {
                GroupName  = group.GroupName,
                ProjectId  = group.ProjectId,
                SceneId    = group.SceneId,
                Creator    = user.UserName,
                CreateDate = DateTime.Now
            };

            _context.Groups.Add(model);
            await _context.SaveChangesAsync();

            return(model.GroupId);
        }
Пример #10
0
        public async Task <int> AddAsync(UserGroupAddModel mUserGroup)
        {
            var group = _context.Groups.FirstOrDefault(c => c.GroupId == mUserGroup.GroupId);

            if (group == null)
            {
                throw new ExpectException("Could not find Group data which GroupId equal to " + mUserGroup.GroupId);
            }

            var user = _context.Users.FirstOrDefault(c => c.Id == mUserGroup.UserId);

            if (user == null)
            {
                throw new ExpectException("Could not find User data which UserId equal to '" + mUserGroup.UserId + "'");
            }

            //GroupId and UserId must be unique
            var checkData = await _context.UserGroups
                            .Where(c => c.GroupId == mUserGroup.GroupId &&
                                   c.UserId == mUserGroup.UserId).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("There is already exist data which GroupId equal to "
                                          + mUserGroup.GroupId + " and UserId equal to '" + mUserGroup.UserId + "'");
            }


            var model = new UserGroup
            {
                GroupId = mUserGroup.GroupId,
                UserId  = mUserGroup.UserId
            };

            _context.UserGroups.Add(model);
            await _context.SaveChangesAsync();

            return(model.UserGroupId);
        }
Пример #11
0
        public async Task <int> AddAsync(GroupLocationAddModel mGroupLocation)
        {
            var group = _context.Groups.FirstOrDefault(c => c.GroupId == mGroupLocation.GroupId);

            if (group == null)
            {
                throw new ExpectException("Could not find Group data which GroupId equal to " + mGroupLocation.GroupId);
            }

            var location = _context.Locations.FirstOrDefault(c => c.LocationId == mGroupLocation.LocationId);

            if (location == null)
            {
                throw new ExpectException("Could not find Location data which LocationId equal to " + mGroupLocation.LocationId);
            }

            //GroupId and LocationId must be unique
            var checkData = await _context.GroupLocations
                            .Where(c => c.GroupId == mGroupLocation.GroupId &&
                                   c.LocationId == mGroupLocation.LocationId).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("There is already exist data which GroupId equal to "
                                          + mGroupLocation.GroupId + " and LocationId equal to " + mGroupLocation.LocationId);
            }


            var model = new GroupLocation
            {
                GroupId    = mGroupLocation.GroupId,
                LocationId = mGroupLocation.LocationId
            };

            _context.GroupLocations.Add(model);
            await _context.SaveChangesAsync();

            return(model.GroupLocationId);
        }
        public async Task <int> AddAsync(LogDescriptionAddModel mLogDescription)
        {
            //DescriptionCode must be unique
            var checkData = await _context.LogDescriptions
                            .Where(c => c.DescriptionCode == mLogDescription.DescriptionCode).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which DescriptioncODE equal to " + mLogDescription.DescriptionCode + " already exist in system");
            }


            var model = new LogDescription
            {
                DescriptionCode = mLogDescription.DescriptionCode,
                Description     = mLogDescription.Description
            };

            _context.LogDescriptions.Add(model);
            await _context.SaveChangesAsync();

            return(model.LogDescriptionId);
        }
Пример #13
0
        public async Task <int> AddAsync(AlarmMessageAddModel mAlarmMessage)
        {
            //ErrorCode must be unique
            var checkData = await _context.AlarmMessages
                            .Where(c => c.ErrorCode == mAlarmMessage.ErrorCode).ToListAsync();

            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which ErrorCode equal to " + mAlarmMessage.ErrorCode + " already exist in system");
            }


            var model = new AlarmMessage
            {
                ErrorCode = mAlarmMessage.ErrorCode,
                Message   = mAlarmMessage.Message
            };

            _context.AlarmMessages.Add(model);
            await _context.SaveChangesAsync();

            return(model.AlarmMessageId);
        }
Пример #14
0
        public int Add(LocationAddModel mLocation)
        {
            var customer = _context.Projects.FirstOrDefault(l => l.ProjectId == mLocation.ProjectId);

            if (customer == null)
            {
                throw new ExpectException("Could not find Project data which ProjectId equal to " + mLocation.ProjectId);
            }

            var device = _context.Devices.FirstOrDefault(l => l.DeviceId == mLocation.DeviceId);

            if (device == null)
            {
                throw new ExpectException("Could not find Device data which DeviceId equal to " + mLocation.DeviceId);
            }

            //InstallationNumber must be unique
            var checkData = _context.Locations.Where(dl => dl.InstallationNumber == mLocation.InstallationNumber).ToList();

            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which InstallationNumber equal to '" + mLocation.InstallationNumber + "' already exist in system");
            }

            //DeviceSerialNo must be unique
            checkData = _context.Locations.Where(dl => dl.DeviceSerialNo == mLocation.DeviceSerialNo).ToList();
            if (checkData.Count > 0)
            {
                throw new ExpectException("The data which DeviceSerialNo equal to '" + mLocation.DeviceSerialNo + "' already exist in system");
            }

            //Check Orientation
            if (!Enum.IsDefined(typeof(Orientation), mLocation.Orientation))
            {
                throw new ExpectException("Invalid Orientation.You can get correct Orientation values from API");
            }

            //Check DeviceType
            if (!Enum.IsDefined(typeof(DeviceType), mLocation.DeviceType))
            {
                throw new ExpectException("Invalid DeviceType.You can get correct DeviceType values from API");
            }

            //Check CommMode
            if (!Enum.IsDefined(typeof(CommMode), mLocation.CommMode))
            {
                throw new ExpectException("Invalid CommMode.You can get correct CommMode values from API");
            }

            //Get UserInfo
            var user = _loginUser.GetLoginUserInfo();

            var model = new Location
            {
                Building             = mLocation.Building,
                CommAddress          = mLocation.CommAddress,
                CommMode             = mLocation.CommMode,
                CurrentPosition      = mLocation.CurrentPosition,
                Description          = mLocation.Description,
                DeviceSerialNo       = mLocation.DeviceSerialNo,
                DeviceId             = mLocation.DeviceId,
                DeviceType           = mLocation.DeviceType,
                FavorPositionFirst   = mLocation.FavorPositionFirst,
                FavorPositionrSecond = mLocation.FavorPositionrSecond,
                FavorPositionThird   = mLocation.FavorPositionThird,
                Floor = mLocation.Floor,
                InstallationNumber = mLocation.InstallationNumber,
                Orientation        = mLocation.Orientation,
                ProjectId          = mLocation.ProjectId,
                RoomNo             = mLocation.RoomNo,
                Creator            = user.UserName,
                CreateDate         = DateTime.Now
            };

            _context.Locations.Add(model);
            _context.SaveChangesAsync();
            return(model.LocationId);
        }