public WorkshopDto Get(EntityDto <int> input)
        {
            var query = _workshopRepository.GetAllIncluding(w => w.Factory)
                        .Include(w => w.Factory.City).Where(w => w.Id == input.Id);
            var entity = query.FirstOrDefault();

            if (entity.IsNullOrDeleted())
            {
                throw new ApplicationException("该设备不存在或已被删除");
            }
            return(ObjectMapper.Map <WorkshopDto>(entity));
        }
Esempio n. 2
0
        public DeviceDto Create(CreateDeviceDto input)
        {
            var query = _deviceRepository.GetAllIncluding()
                        .Where(d => d.HardwareId == input.HardwareId || d.DeviceName == input.DeviceName);

            if ((query.Any()) && (query.FirstOrDefault().IsDeleted == true))
            {
                var entity = query.FirstOrDefault();
                entity.IsDeleted = false;
                var result_old = _deviceRepository.Update(entity);
                CurrentUnitOfWork.SaveChanges();
                return(ObjectMapper.Map <DeviceDto>(result_old));
            }
            if (query.Any())
            {
                throw new ApplicationException("设备已存在");
            }

            var workshopQuery = _workshopRepository.GetAllIncluding().Where(w => w.WorkshopName == input.WorkshopName)
                                .Where(w => w.Factory.FactoryName == input.FactoryName)
                                .Where(w => w.Factory.City.CityName == input.CityName);
            var workshop = workshopQuery.FirstOrDefault();

            if (workshop == null)
            {
                throw new ApplicationException("Workshop不存在");
            }

            var gatewayQuery = _gatewayRepository.GetAllIncluding().Where(g => g.Workshop.WorkshopName == input.WorkshopName)
                               .Where(g => g.Workshop.Factory.FactoryName == input.FactoryName)
                               .Where(g => g.Workshop.Factory.City.CityName == input.CityName)
                               .Where(g => g.HardwareId == input.HardwareId || g.GatewayName == input.GatewayName);
            var gateway = gatewayQuery.FirstOrDefault();

            if (gateway == null)
            {
                throw new ApplicationException("网关不存在");
            }

            var deviceTypeQuery = _deviceTypeRepository.GetAll().Where(dt => dt.TypeName == input.DeviceTypeName);
            var deviceType      = deviceTypeQuery.FirstOrDefault();

            if (deviceType == null)
            {
                throw new ApplicationException("设备类型不存在");
            }
            var device = ObjectMapper.Map <Device>(input);

            device.Gateway    = gateway;
            device.DeviceType = deviceType;
            var result = _deviceRepository.Insert(device);

            CurrentUnitOfWork.SaveChanges();
            return(ObjectMapper.Map <DeviceDto>(result));
        }
        public GatewayDto Create(CreateGatewayDto input)
        {
            var query       = _gatewayRepository.GetAllIncluding().Where(g => g.HardwareId == input.HardwareId || g.GatewayName == input.GatewayName);
            var gateway_old = query.FirstOrDefault();

            if ((query.Any()) && (query.FirstOrDefault().IsDeleted == true))
            {
                gateway_old.IsDeleted = false;
                var result_old = _gatewayRepository.Update(gateway_old);
                CurrentUnitOfWork.SaveChanges();
                return(ObjectMapper.Map <GatewayDto>(result_old));
            }

            if (query.Any() && gateway_old.IsDeleted == false)
            {
                throw new ApplicationException("网关已存在");
            }

            var workshopQuery = _workshopRepository.GetAllIncluding().Where(w => w.WorkshopName == input.WorkshopName)
                                .Where(w => w.Factory.FactoryName == input.FactoryName)
                                .Where(w => w.Factory.City.CityName == input.CityName);
            var workshop = workshopQuery.FirstOrDefault();

            if (workshop == null)
            {
                throw new ApplicationException("Workshop不存在");
            }

            var gatewayTypeQuery = _gatewayTypeRepository.GetAll().Where(gt => gt.TypeName == input.GatewayTypeName);
            var gatewayType      = gatewayTypeQuery.FirstOrDefault();

            if (gatewayType == null)
            {
                throw new ApplicationException("网关类型不存在");
            }
            var gateway = ObjectMapper.Map <Gateway>(input);

            gateway.Workshop    = workshop;
            gateway.GatewayType = gatewayType;
            var result = _gatewayRepository.Insert(gateway);

            CurrentUnitOfWork.SaveChanges();
            return(ObjectMapper.Map <GatewayDto>(result));
        }