FrmOneway.cs 6.5 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OneWay
{

    public partial class FrmOneway : Form
    {
        List<Way> wayUPs = new List<Way>();
        List<Way> wayDowns = new List<Way>();
        List<Way> wayLefts = new List<Way>();
        List<Way> wayRights = new List<Way>();
        Way up = new Way();
        Way down = new Way();
        Way left = new Way();
        Way right = new Way();
        List<string[]> rows = new List<string[]>();
        string linefilePath = @"tbl_landmarks.csv";
        static void WriteCsv(string baseDir, string filePath, List<string[]> rows)
        {
            if (!Directory.Exists(baseDir))
                Directory.CreateDirectory(baseDir);
            using (var writer = new StreamWriter(baseDir + filePath))
            {
                foreach (var row in rows)
                {
                    writer.WriteLine(string.Join(",", row));
                }
            }
        }
        static List<string[]> ReadCsv(string filePath)
        {
            List<string[]> rows = new List<string[]>();

            using (var reader = new StreamReader(filePath))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split(',');
                    rows.Add(values);
                }
            }

            return rows;
        }
        string baseDir = "./";
        public FrmOneway(string baseDir)
        {
            InitializeComponent();
            texUP.Text = ConfigAppSettings.GetValue("Oneway_Up", "", "单向道设置-上方");
            texDown.Text = ConfigAppSettings.GetValue("Oneway_Down", "", "单向道设置-下方");
            texLeft.Text = ConfigAppSettings.GetValue("Oneway_Left", "", "单向道设置-左侧");
            texRight.Text = ConfigAppSettings.GetValue("Oneway_Right", "", "单向道设置-右侧");
            this.baseDir = baseDir;
            rows = ReadCsv(baseDir + "/" + linefilePath);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string input = texUP.Text.Trim();
            string[] str = input.Split(';');
            if (input.Length > 0)
                foreach (string str2 in str)
                {
                    if (str2.Length > 0)
                    {
                        Way temp = new Way();
                        temp.UP = str2.Split(',').ToList();
                        wayUPs.Add(temp);
                    }
                }

            input = texDown.Text.Trim();
            str = input.Split(';');
            if (input.Length > 0)
                foreach (string str2 in str)
                {
                    if (str2.Length > 0)
                    {
                        Way temp = new Way();
                        temp.DOWN = str2.Split(',').ToList();
                        wayDowns.Add(temp);
                    }
                }

            input = texLeft.Text.Trim();
            str = input.Split(';');
            if (input.Length > 0)
                foreach (string str2 in str)
                {
                    if (str2.Length > 0)
                    {
                        Way temp = new Way();
                        temp.LEFT = str2.Split(',').ToList();
                        wayLefts.Add(temp);
                    }
                }

            input = texRight.Text.Trim();
            str = input.Split(';');
            if (input.Length > 0)
                foreach (string str2 in str)
                {
                    if (str2.Length > 0)
                    {
                        Way temp = new Way();
                        temp.RIGHT = str2.Split(',').ToList();
                        wayRights.Add(temp);
                    }
                }


            if (wayUPs.Count > 0)
                foreach (var oneway in wayUPs)
                {
                    for (int i = 1; i < oneway.UP.Count; i++)
                    {
                        string[] row = rows.First(m => m[0].Trim() == oneway.UP[i].ToString());
                        if (row != null)
                        {
                            row[6] = "";
                        }
                    }

                }
            if (wayDowns.Count > 0)
                foreach (var oneway in wayDowns)
                {
                    for (int i = 1; i < oneway.DOWN.Count; i++)
                    {
                        string[] row = rows.First(m => m[0].Trim() == oneway.DOWN[i].ToString());
                        if (row != null)
                        {
                            row[4] = "";
                        }
                    }

                }
            if (wayLefts.Count > 0)
                foreach (var oneway in wayLefts)
                {
                    for (int i = 1; i < oneway.LEFT.Count; i++)
                    {
                        string[] row = rows.First(m => m[0].Trim() == oneway.LEFT[i].ToString());
                        if (row != null)
                        {
                            row[5] = "";
                        }
                    }

                }
            if (wayRights.Count > 0)
                foreach (var oneway in wayRights)
                {
                    for (int i = 1; i < oneway.RIGHT.Count; i++)
                    {
                        string[] row = rows.First(m => m[0].Trim() == oneway.RIGHT[i].ToString());
                        if (row != null)
                        {
                            row[7] = "";
                        }
                    }

                }

            WriteCsv(baseDir + "/output/", linefilePath, rows);
            ConfigAppSettings.SaveValue("Oneway_Up", texUP.Text, "单向道设置-上方");
            ConfigAppSettings.SaveValue("Oneway_Down", texDown.Text, "单向道设置-下方");
            ConfigAppSettings.SaveValue("Oneway_Left", texLeft.Text, "单向道设置-左侧");
            ConfigAppSettings.SaveValue("Oneway_Right", texRight.Text, "单向道设置-右侧");
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }
    }
    class Way
    {
        public List<string> UP { get; set; }
        public List<string> DOWN { get; set; }
        public List<string> LEFT { get; set; }
        public List<string> RIGHT { get; set; }

    }
}