Newer
Older
Correlator / Correlator / ServiceImpl / SoundSpeedDataService.cs
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using Correlator.Model;
using Correlator.Service;
using Correlator.Util;
using Newtonsoft.Json;
using MessageBox = HandyControl.Controls.MessageBox;

namespace Correlator.ServiceImpl
{
    public class SoundSpeedDataService : ISoundSpeedDataService
    {
        public ObservableCollection<MaterialVelocity> GetVelocityCollection(string materialName)
        {
            var velocityCollection = new ObservableCollection<MaterialVelocity>();
            using (var manager = new DataBaseManager())
            {
                var velocities = manager
                    .Table<MaterialVelocity>()
                    .Where(x => x.MaterialName == materialName)
                    .ToList();
                foreach (var model in velocities)
                {
                    velocityCollection.Add(model);
                }
            }

            return velocityCollection;
        }

        public void DeleteVelocity(MaterialVelocity velocity)
        {
            using (var manager = new DataBaseManager())
            {
                manager.Delete(velocity);
            }
        }

        public void UpdatePipeMaterial(string materialName, MaterialVelocity velocity)
        {
            using (var manager = new DataBaseManager())
            {
                var materialModel = manager
                    .Table<PipeMaterialModel>()
                    .FirstOrDefault(x => x.ChineseMaterial == materialName);
                materialModel.SoundSpeed = velocity.Velocity;

                "SoundSpeedDataService".WriteLog("正在修改的管材是:" + JsonConvert.SerializeObject(materialModel));

                manager.Update(materialModel);
            }
        }

        public void InsertVelocity(string materialName, string minDiameter, string maxDiameter, string soundSpeed)
        {
            if (string.IsNullOrEmpty(minDiameter) || string.IsNullOrEmpty(maxDiameter) ||
                string.IsNullOrEmpty(soundSpeed))
            {
                MessageBox.Show("不允许为空", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!Regex.IsMatch(minDiameter, "^(([1-9]\\d{0,3})|10000|0)$"))
            {
                MessageBox.Show("管径输入错误", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!Regex.IsMatch(maxDiameter, "^(([1-9]\\d{0,3})|10000|0)$"))
            {
                MessageBox.Show("管径输入错误", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (int.Parse(minDiameter) > int.Parse(maxDiameter))
            {
                MessageBox.Show("管径下限不能大于管径上限", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!Regex.IsMatch(soundSpeed, "^(([1-9]\\d{0,3})|10000|0)$"))
            {
                MessageBox.Show("速度输入错误", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            using (var manager = new DataBaseManager())
            {
                var velocity = new MaterialVelocity
                {
                    MaterialName = materialName,
                    LowDiameter = Convert.ToInt32(minDiameter),
                    HighDiameter = Convert.ToInt32(maxDiameter),
                    Velocity = Convert.ToInt32(soundSpeed)
                };
                manager.Insert(velocity);
            }
        }

        public void UpdateVelocity(MaterialVelocity velocity, string minDiameter, string maxDiameter, string soundSpeed)
        {
            if (string.IsNullOrEmpty(minDiameter) || string.IsNullOrEmpty(maxDiameter) ||
                string.IsNullOrEmpty(soundSpeed))
            {
                MessageBox.Show("不允许为空", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!Regex.IsMatch(minDiameter, "^(([1-9]\\d{0,3})|10000|0)$"))
            {
                MessageBox.Show("管径输入错误", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!Regex.IsMatch(maxDiameter, "^(([1-9]\\d{0,3})|10000|0)$"))
            {
                MessageBox.Show("管径输入错误", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (int.Parse(minDiameter) > int.Parse(maxDiameter))
            {
                MessageBox.Show("管径下限不能大于管径上限", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            if (!Regex.IsMatch(soundSpeed, "^(([1-9]\\d{0,3})|10000|0)$"))
            {
                MessageBox.Show("速度输入错误", "温馨提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            using (var manager = new DataBaseManager())
            {
                velocity.LowDiameter = Convert.ToInt32(minDiameter);
                velocity.HighDiameter = Convert.ToInt32(maxDiameter);
                velocity.Velocity = Convert.ToInt32(soundSpeed);

                "SoundSpeedDataService".WriteLog("正在修改的管材是:" + JsonConvert.SerializeObject(velocity));

                manager.Update(velocity);
            }
        }

        public int GetSoundVelocity(string material, int pipeDiameter)
        {
            using (var manager = new DataBaseManager())
            {
                //多条件查询
                var materialVelocities = manager
                    .Table<MaterialVelocity>()
                    .Where(mv =>
                        mv.MaterialName == material &&
                        mv.LowDiameter <= pipeDiameter &&
                        mv.HighDiameter >= pipeDiameter
                    );
                if (materialVelocities.Any())
                {
                    return materialVelocities.Select(mv => mv.Velocity).FirstOrDefault();
                }

                return 0;
            }
        }
    }
}