Newer
Older
GHFX_REFACTOR / EMS_Forms / Form_EditTask.cs
wxn on 28 Nov 2016 28 KB 管线定位
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.OracleClient;
using GeoScene.Data;
using GeoScene.Globe;

namespace Cyberpipe.EMS_Forms
{
    public partial class Form_EditTask : Office2007Form
    {
        private GSOGlobeControl globeControl1;
        public static List<string> pipeLists = new List<string>();
        public static List<string> typeLists = new List<string>();
        public static List<string> checkLists = new List<string>();
        OracleConnection conn = null;


        public static List<string> roadList = new List<string>();

        private int currentpage = 1;
        private int lastpage = 1;
        private int pagesize = 6;

        public Form_EditTask(GSOGlobeControl globeControl1)
        {
            
            this.globeControl1 = globeControl1;
            globeControl1.TrackPolygonEndEvent += new TrackPolygonEndEventHandler(TrackPolygonEndEvent);
            InitializeComponent();
        }

        void TrackPolygonEndEvent(object sender, TrackPolygonEndEventArgs e)
        {
            if (globeControl1.Globe.Action == EnumAction3D.TrackPolygon && e.Polygon != null)
            {
                Show();
                GSOFeature f2 = new GSOFeature();
                f2.Geometry = e.Polygon;
                f2.Geometry.AltitudeMode = EnumAltitudeMode.Absolute;
                globeControl1.Globe.MemoryLayer.AddFeature(f2);
                f2.Geometry.MoveZ(3);
                List<String> listBSQ = new List<String>();
                
                GSOFeatures bsqFeatures = PolygonIntersectAnalysis(e.Polygon, "标识器");
                for (int i = 0; i < bsqFeatures.Length; i++)
                {
                    GSOFeature f = bsqFeatures[i];
                    if (f != null)
                    {
                        listBSQ.Add(f.Name);
                    }

                }
                if (listBSQ.Count > 0 && listBSQ.Count <1000)
                {
                    addGridByMarkIds(listBSQ);
                    
                }
                else if (listBSQ.Count >= 1000)
                {
                    dataGridView_mark.Rows.Clear();
                    MessageBox.Show("所选区域内标识器数量超过上限1000!", "提示");
                }
                else
                {
                    dataGridView_mark.Rows.Clear();
                    MessageBox.Show("所选区域内没有标识器!", "提示");
                }                
                globeControl1.Globe.Action = EnumAction3D.ActionNull;
                globeControl1.Globe.ClearMeasure();
                globeControl1.Globe.ClearAnalysis();
                globeControl1.Globe.MemoryLayer.RemoveAllFeature();
                globeControl1.Refresh();

            }
        }
        private GSOFeatures PolygonIntersectAnalysis(GSOGeoPolygon3D polygon, string pipelinetype)
        {
            GSOLayer layer = globeControl1.Globe.Layers.GetLayerByCaption(pipelinetype);
           
            GSOFeatureLayer flayer = layer as GSOFeatureLayer;
            if (flayer == null) return null;
            GSOFeatures feats = polygon == null ? flayer.GetAllFeatures() : flayer.FindFeaturesInPolygon(polygon, false);
            return feats;
        }

        private void Form_EditTask_Load(object sender, EventArgs e)
        {
            try
            {
                conn = OledbHelper.sqlConnection();
                string com = "select * from PATROLER";
                conn.Open();
                OracleCommand cmd = new OracleCommand(com, conn);
                OracleDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    String name = Convert.ToString(dr["USERNAME"]);
                    comboBox_userName.Items.Add(name);

                }                

                comboBox_road.Items.Clear();
                roadList = new List<string>();
                string com1 = "select distinct 所属道路 from 标识器";
                OracleCommand cmd1 = new OracleCommand(com1, conn);
                OracleDataReader dr1 = cmd1.ExecuteReader();
                while (dr1.Read())
                {
                    String name = Convert.ToString(dr1["所属道路"]);
                    if (!name.Equals(""))
                    {
                        roadList.Add(name);
                        comboBox_road.Items.Add(name);
                    }
                }

                conn.Close();

                bool flag2 = false;
                foreach (PipelineType pipeLineType in Utility.listPipelineType)
                {
                    foreach (string pipe in pipeLists)
                    {
                        if (pipe != pipeLineType.type) continue;
                        flag2 = true;
                        break;
                    }
                    if (!flag2 && pipeLineType.type != "标识器")
                    {
                        pipeLists.Add(pipeLineType.type);
                    }
                    flag2 = false;
                }
                comboBox_pipeType.Items.Add("全部管线");
                for (int n = 0; n < pipeLists.Count; n++)
                {
                    comboBox_pipeType.Items.Add(pipeLists[n]);
                }
                //if (dataGridView_mark.Rows.Count < 1)
                //{
                //    return;
                //}
                CheckBox ckBox = new CheckBox();
                ckBox.Text = "全选";
                ckBox.Checked = false;
                Rectangle rect =dataGridView_mark.GetCellDisplayRectangle(0, -1, true);
                //Point oPoint = new Point();
                //oPoint.X = rect.Location.X + (rect.Width) / 2;
                //oPoint.Y = rect.Location.Y ;
                //ckBox.Location = oPoint;

                ckBox.Size = new Size(dataGridView_mark.Columns[0].Width, 18);
                ckBox.Location = rect.Location;
                ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);


                dataGridView_mark.Controls.Add(ckBox);


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());

            }            
        }

        private void Form_EditTask_FormClosing(object sender, FormClosingEventArgs e)
        {

            globeControl1.Globe.MemoryLayer.RemoveAllFeature();
            globeControl1.TrackPolygonEndEvent -= new TrackPolygonEndEventHandler(TrackPolygonEndEvent);
        }

        private void comboBox_road_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox_pipeType.Items.Clear();
            comboBox_pipeType.Items.Add("全部管线");
            for (int n = 0; n < pipeLists.Count; n++)
            {
                comboBox_pipeType.Items.Add(pipeLists[n]);
            }
            comboBox_appendant.Items.Clear();
        }

        private void comboBox_pipeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox_appendant.Items.Clear();

            bool flag1 = false;
            if (comboBox_pipeType.Text != "" && comboBox_appendant.Text == "")
            {
                if (comboBox_pipeType.Text == "全部管线")
                {
                    for (int i = 0; i < Utility.listPipelineType.Count; i++)
                    {
                        for (int lisnum = 0; lisnum < typeLists.Count; lisnum++)
                        {
                            if (typeLists[lisnum] != Utility.listPipelineType[i].name) continue;
                            flag1 = true;
                            break;
                        }
                        if (!flag1 && Utility.listPipelineType[i].name != "标识器")
                        {
                            string ls = Utility.listPipelineType[i].name;
                            typeLists.Add(ls);

                        }
                        flag1 = false;
                    }
                    comboBox_appendant.Items.Add("全部附属物和特征点");
                    for (int i = 0; i < typeLists.Count; i++)
                    {
                        comboBox_appendant.Items.Add(typeLists[i]);
                    }
                }
                if (comboBox_pipeType.Text.Equals("全部管线")) return;
                {
                    comboBox_appendant.Items.Add("全部附属物和特征点");
                    for (int i = 0; i < Utility.listPipelineType.Count; i++)
                    {
                        if (Utility.listPipelineType[i].type == comboBox_pipeType.Text)
                        {
                            if (Utility.listPipelineType[i].type == Utility.listPipelineType[i].name)
                                comboBox_appendant.Items.Add(Utility.listPipelineType[i].name + "管段");
                            if (Utility.listPipelineType[i].type != Utility.listPipelineType[i].name)
                                comboBox_appendant.Items.Add(Utility.listPipelineType[i].name);
                        }
                    }
                }
            }
            else
            {
                if (comboBox_pipeType.Text == "")
                    comboBox_appendant.Text = "";
            }
        }

        private void comboBox_appendant_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (comboBox_road.Text == "")
                {
                    MessageBox.Show("请选择需定位管线所在道路区域!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    return;
                }
                if (comboBox_pipeType.Text == "")
                {
                    MessageBox.Show("请选择需定位管线类别!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    return;
                }
                if (comboBox_appendant.Text == "")
                {
                    MessageBox.Show("请选择需定位管线附属物或特征点名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    return;
                }
                currentpage = 1;
                reloadGrid(currentpage);  
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ckBox_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView_mark.Rows.Count; i++)
            {
                dataGridView_mark.Rows[i].Cells[0].Value = ((CheckBox)sender).Checked;
            }
            dataGridView_mark.EndEdit();
        }
        private void dataGridView_mark_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DataGridView.HitTestInfo hittestinfo = dataGridView_mark.HitTest(e.X, e.Y);
                if (hittestinfo.RowIndex > -1)
                {
                    string markId = "";

                    markId = Convert.ToString(dataGridView_mark.Rows[hittestinfo.RowIndex].Cells["标识编号"].Value);
                    int ID = GSOProjectManager.AddProject("+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=499885 +y_0=6 +a=6378140 +b=6356755.288157528 +units=m +no_defs");
                    double x = Convert.ToDouble(dataGridView_mark.Rows[hittestinfo.RowIndex].Cells["X"].Value);
                    double y = Convert.ToDouble(dataGridView_mark.Rows[hittestinfo.RowIndex].Cells["Y"].Value);


                    GSOFeatures features = globeControl1.Globe.Layers.GetLayerByCaption("标识器").GetFeatureByName(markId, true);
                    if (features.Length > 0)
                    {
                        GSOPoint3d p = new GSOPoint3d();
                        GSOPoint2d pt2d = new GSOPoint2d(y, x);
                        GSOPoint2d result = GSOProjectManager.Inverse(pt2d, ID);
                        p.X = result.X;
                        p.Y = result.Y;

                        p.X = ((GSOGeoModel)(features[0].Geometry)).PositionX;
                        p.Y = ((GSOGeoModel)(features[0].Geometry)).PositionY; ;
                        p.Z = 2;
                        //GSOGeoModel model = new GSOGeoModel();
                        //string filepath = Application.StartupPath + "/EMSgcm/小旗子/hongqi/hongqi.gcm";

                        //model.FilePath = filepath;
                        //model.Position = p;
                        //model.AltitudeMode = EnumAltitudeMode.Absolute;
                        //GSOFeature f = new GSOFeature();
                        //f.Geometry = model;
                        //GSOFeature newFeature = globeControl1.Globe.MemoryLayer.AddFeature(f);


                        GSOLabel label = new GSOLabel();
                        label.Text = "标识器位置";

                        label.Style = new GSOLabelStyle();
                        label.Style.Opaque = 0.8; //设置标注的透明度,取值区间是0-1   
                        label.Style.TracktionLineType = EnumTracktionLineType.Solid;
                        label.Style.TextStyle.FontHeight = 20;
                        label.Style.TextStyle.FontName = "黑体";
                        label.Style.TractionLineEndPos = new GSOPoint2d(80, 60);
                        features[0].Label = label;

                        GSOFeature f = new GSOFeature();
                        f.Geometry = features[0].Geometry;
                        f.Label = label;
                        GSOFeature newFeature = globeControl1.Globe.MemoryLayer.AddFeature(f);
                        globeControl1.Globe.MemoryLayer.ObjectMinVisiblePixelSize = -1;
                        globeControl1.Globe.MemoryLayer.ObjectMaxVisibleDistance = -1;

                        globeControl1.Globe.JumpToFeature(newFeature, 10);

                    }
                    else
                    {
                        MessageBox.Show("未找到对象!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            string strUserName = comboBox_userName.Text.ToString();
            string strStreetName = comboBox_road.Text.ToString();
            //任务描述
            string strTask = richTextBox1.Text.ToString();
            if (strUserName == "")
            {
                MessageBox.Show("定位人员姓名不能为空", "提示");
                return;
            }

            //if (strStreetName == "")
            //{
            //    MessageBox.Show("任务路段不能为空", "提示");
            //    return;
            //}            
            checkLists.Clear();
            for (int n = 0; n < dataGridView_mark.Rows.Count; n++)
            {
                bool i = Convert.ToBoolean(dataGridView_mark.Rows[n].Cells[0].Value);
                if (i)
                {
                    checkLists.Add(Convert.ToString(dataGridView_mark.Rows[n].Cells["标识编号"].Value));
                }
            }
            if (checkLists.Count <= 0)
            {
                MessageBox.Show("请选择定位点", "提示");
                return;
            }          



            /*************************************************************************************/
            //添加任务表单
            try
            {
                DateTime current = new DateTime();
                current = DateTime.Now;
                int task_number = current.Year * 10000 + current.Month * 100 + current.Day;
                task_number = createTaskNumber(Convert.ToString(task_number));

                int r = (int)MessageBox.Show("是否要创建工单号为" + task_number + "的工单?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (r == 1)
                {
                    ////获取 PATROLER_ID
                    long userunm = 0;
                    conn = OledbHelper.sqlConnection();
                    conn.Open();
                    OracleCommand cmd5 = new OracleCommand("select * from PATROLER", conn);
                    OracleDataReader dr5 = cmd5.ExecuteReader();
                    while (dr5.Read())
                    {
                        String str = Convert.ToString(dr5["USERNAME"]);
                        if (str == strUserName)
                        {
                            userunm = Convert.ToInt32(dr5["DBID"]);
                            break;
                        }
                    }
                    dr5.Close();

                    string dateString = current.ToString();
                    string sql1 = "insert into TASKINFO (DBID,TASKSTATE,USERNAME,STREET,DEPLOYTIME,DESCIRPTION,PATROLER_ID)VALUES('" +
                        task_number + "','下发任务','" + strUserName + "','" + strStreetName +
                        "',to_date('" + dateString + "', 'yyyy-MM-dd hh24:mi:ss'),'" + strTask + "','" + 
                        userunm + "')";

                    int var = OledbHelper.sqlExecuteNonQuery(sql1);

                    /////////////////////// 
                    /*********/
                    //新建 任务工单 标识器内容
                    if (var!=-1)
                    {
                        for (int j = 0; j < checkLists.Count; j++)
                        {
                            string sql = "insert into MARK_TASK (MARK_ID,TASK_ID,IS_CHECKED)VALUES('" + checkLists[j] + "','" + task_number + "','0')";
                            var = OledbHelper.sqlExecuteNonQuery(sql);

                        }
                    }

                    if (var != -1)
                        MessageBox.Show("工单" + task_number + "创建成功!", "提示");
                    else
                    {
                        MessageBox.Show("工单" + task_number + "创建失败!", "提示");
                        return;
                    }
                        
                    conn.Close();
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            Close();
        }

        private int createTaskNumber(string number)
        {
            int taskNumber = 0;
            List<int> numList = new List<int>();
            List<int> numList1 = new List<int>();
            OracleConnection conn = new OracleConnection();
            conn = OledbHelper.sqlConnection();
            conn.Open();
            string sql = "select * from TASKINFO where DBID like '%"+number+"%'";
            OracleCommand cmd = new OracleCommand(sql, conn);
            OracleDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                numList.Add(Convert.ToInt32(dr["DBID"]));
            }
            if (numList.Count == 0)
            {
                taskNumber = Convert.ToInt32(number) * 100;
            }
            else
            {
                taskNumber = numList.Max() + 1;
            }
            return taskNumber;
        }

        private void reloadGrid(int pageIndex)
        {
            dataGridView_mark.Rows.Clear();

            conn = OledbHelper.sqlConnection();
            conn.Open();
            OracleCommand cmd;
            string street = comboBox_road.Text;
            cmd = new OracleCommand("select * from 标识器 where 所属道路 ='" + street+"'", conn);
            OracleDataReader dr = cmd.ExecuteReader();

            bool b = false;
            while (dr.Read())
            {
                string str = Convert.ToString(dr["对象类型"]);
                string code = Convert.ToString(dr["编号"]);
                string str1 = Convert.ToString(dr["对象编号"]);
                //string bsd_str = Convert.ToString(dr["是否关键点"]);
                double x = Convert.ToDouble(dr["X坐标"]);
                double y = Convert.ToDouble(dr["Y坐标"]);
                if (code == "BSQ_LDL_133") {
                    code = "BSQ_LDL_133";
                }
                string[] codeStr = str.Split(',');
                string[] codeStr1 = str1.Split(',');
                int length = 0;
                if (codeStr.Length <= codeStr1.Length)
                {
                    length = codeStr.Length;
                }
                else
                {
                    length = codeStr1.Length;
                }
                for (int n = 0; n < length; n++)
                {
                    for (int i = 0; i < Utility.listPipelineType.Count; i++)
                    {
                        if (Utility.listPipelineType[i].code == codeStr[n])
                        {
                            if (comboBox_pipeType.Text == "全部管线" || Utility.listPipelineType[i].type == comboBox_pipeType.Text)
                            {
                                if (Utility.listPipelineType[i].name == comboBox_appendant.Text || comboBox_appendant.Text == "全部附属物和特征点")
                                {
                                    object[] newrow = new object[] { b, codeStr1[n], Utility.listPipelineType[i].type, Utility.listPipelineType[i].name, code, x, y };
                                    dataGridView_mark.Rows.Add(newrow);
                                }
                            }
                        }
                    }
                }
            }
            conn.Close();
            //分页
            //string sqlcount = "select count(*) from 标识器 t where 1=1";
            //string sqlrows = "select * from (select rownum as rowno," +
            //                           "所属道路," +
            //                           "对象名称," +
            //                           "编号," +
            //                           "对象编号," +
            //                           "X坐标," +
            //                           "Y坐标," +
            //                           "'false' as btn " +
            //                      "from 标识器 where 1=1 ";

            //sqlrows += " and rownum<=" + (pagesize * pageIndex) + ") table_alias where table_alias.rowno>=" + ((pageIndex - 1) * pagesize + 1);

            //int rows = int.Parse(OracleUtils.ExecuteScalar(OracleUtils.ConnectionString, CommandType.Text, sqlcount).ToString());
            //int pages = 0;
            //if (rows % pagesize == 0)
            //{
            //    pages = rows / pagesize;
            //}
            //else
            //{
            //    pages = rows / pagesize + 1;
            //}

            //lastpage = pages;

            //lab_page_msg.Text = "共" + rows + "条记录,每页" + pagesize + "条,第" + pageIndex + "页,共" + pages + "页";
            //combo_page_num.Items.Clear();
            //for (int i = 1; i <= pages; i++)
            //{
            //    combo_page_num.Items.Add(new ComboBoxItem(i.ToString(), i.ToString()));
            //}
            //if (currentpage == 1)
            //{
            //    btn_page_first.Enabled = false;
            //    btn_page_pre.Enabled = false;
            //    btn_page_last.Enabled = true;
            //    btn_page_next.Enabled = true;
            //}
            //else if (currentpage == lastpage)
            //{
            //    btn_page_first.Enabled = true;
            //    btn_page_pre.Enabled = true;
            //    btn_page_last.Enabled = false;
            //    btn_page_next.Enabled = false;
            //}
            //else
            //{
            //    btn_page_first.Enabled = true;
            //    btn_page_pre.Enabled = true;
            //    btn_page_last.Enabled = true;
            //    btn_page_next.Enabled = true;
            //}

            //DataTable table = OracleUtils.ExecuteDataset(OracleUtils.ConnectionString, CommandType.Text, sqlrows).Tables[0];
            //dataGridView_mark.DataSource = table;
            //if (table.Rows.Count == 0)
            //{
            //    MessageBox.Show("没有找到任何数据!", "提示");
            //}
        }

        private void addGridByMarkIds(List<String> MarkIds)
        {
            dataGridView_mark.Rows.Clear();

            conn = OledbHelper.sqlConnection();
            conn.Open();
            OracleCommand cmd;
            string inParams = "";
            for(int i = 0; i< MarkIds.Count ; i++)
            {
                inParams += "'"+MarkIds[i]+"'";
                if (i != MarkIds.Count - 1)
                {
                    inParams += ",";
                }
            }
            cmd = new OracleCommand("select * from 标识器 where 编号 in(" + inParams + ")", conn);
            OracleDataReader dr = cmd.ExecuteReader();

            bool b = false;
            while (dr.Read())
            {
                string str = Convert.ToString(dr["对象类型"]);
                string code = Convert.ToString(dr["编号"]);
                string str1 = Convert.ToString(dr["对象编号"]);
                //string bsd_str = Convert.ToString(dr["是否关键点"]);
                double x = Convert.ToDouble(dr["X坐标"]);
                double y = Convert.ToDouble(dr["Y坐标"]);

                string[] codeStr = str.Split(',');
                string[] codeStr1 = str1.Split(',');
                int length = 0;
                if (codeStr.Length <= codeStr1.Length)
                {
                    length = codeStr.Length;
                }
                else
                {
                    length = codeStr1.Length;
                }
                for (int n = 0; n < length; n++)
                {
                    for (int i = 0; i < Utility.listPipelineType.Count; i++)
                    {
                        if (Utility.listPipelineType[i].code == codeStr[n])
                        {
                            object[] newrow = new object[] { b, codeStr1[n], Utility.listPipelineType[i].type, Utility.listPipelineType[i].name, code, x, y };
                            dataGridView_mark.Rows.Add(newrow);
                        }
                    }
                }
            }
            conn.Close();

        }

        private void btn_page_first_Click(object sender, EventArgs e)
        {
            currentpage = 1;
            reloadGrid(currentpage);  
        }

        private void btn_page_pre_Click(object sender, EventArgs e)
        {
            currentpage--;
            if (currentpage < 1)
            {
                currentpage = 1;
            }
            reloadGrid(currentpage);
        }

        private void combo_page_num_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(combo_page_num.Text))
            {
                currentpage = int.Parse(combo_page_num.Text);
                reloadGrid(currentpage);
            }  
        }

        private void btn_page_next_Click(object sender, EventArgs e)
        {
            currentpage++;
            if (currentpage > lastpage)
            {
                currentpage = lastpage;
            }
            reloadGrid(currentpage);
        }

        private void btn_page_last_Click(object sender, EventArgs e)
        {
            currentpage = lastpage;
            reloadGrid(currentpage);
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            globeControl1.Globe.Action = EnumAction3D.TrackPolygon;
            Visible = false;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab.Text=="按道路选择")
            {
                dataGridView_mark.Rows.Clear();
                for (int i = 0; i < roadList.Count; i++)
                {
                    comboBox_road.Items.Add(roadList[i]);
                }                
            }
            else {
                dataGridView_mark.Rows.Clear();
                comboBox_road.Items.Clear();
                comboBox_pipeType.Items.Clear();
                comboBox_appendant.Items.Clear();
            }
        }
    }
}