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

namespace Cyberpipe.EMS_Forms
{
    public partial class Form_OnlineTask : Office2007Form
    {
        private GSOGlobeControl globeControl1;
//        DataTable dt = new DataTable();
        private int currentpage = 1;
        private int lastpage = 1;
        private int pagesize = 11;

        public static bool IS_OPEN = false;

        public Form_OnlineTask(GSOGlobeControl globeControl1)
        {
            this.globeControl1 = globeControl1;
            globeControl1.Globe.MemoryLayer.ObjectMinVisiblePixelSize = -1;
            InitializeComponent();
        }

        private void Form_OnlineTask_Load(object sender, EventArgs e)
        {
            try
            {
                dateTime_begin.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                dateTime_end.Value = DateTime.Now;
                string sql = "select t.username from patroler t";
                using (OracleDataReader reader = OracleUtils.ExecuteReader(OracleUtils.ConnectionString, CommandType.Text, sql))
                {
                    while (reader.Read())
                    {
                        comboBox_userName.Items.Add(reader[0].ToString());
                    }
                }
                reloadGrid(currentpage);
                IS_OPEN = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form_OnlineTask_FormClosing(object sender, FormClosingEventArgs e)
        {
            globeControl1.Globe.MemoryLayer.RemoveAllFeature();
            IS_OPEN = false;
        }
        private void showThisForm()
        {
            Show();
        }

        //标签信息
        public class listID
        {
            public string MarkId;
            public int check = 0;
            public double x;
            public double y;
            public string checktime = "标签未定位";
            public long t = 0;
        }

        private void dataGridView_OnlineTask_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {                
                if (dataGridView_OnlineTask.Columns[e.ColumnIndex].HeaderText.Equals("任务信息"))
                {
                    try
                    {
                        int dbID = 0;
                        dbID = Convert.ToInt32(dataGridView_OnlineTask.Rows[e.RowIndex].Cells["任务编号"].Value);
                        string sql = "select * from MARK_TASK where TASK_ID ='" + dbID + "' order by DBID";
                        DataTable taskInfoDt  = OledbHelper.ExecuteDataTable(sql);
                        if (taskInfoDt.Rows.Count == 0)
                        {
                            MessageBox.Show("没有相应详细任务信息!", "提示");
                            return;
                        }
                        Hide();
                        flyToTask(e.RowIndex);
                        Form_TaskInfo p = new Form_TaskInfo(globeControl1, taskInfoDt);
                        p.showParentForm += new Form_TaskInfo.Handle(showThisForm);//改变值的事件  
                        p.Show();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        private void flyToTask(int rowIndex)
        {
            int dbID = 0;
            dbID = Convert.ToInt32(dataGridView_OnlineTask.Rows[rowIndex].Cells["任务编号"].Value);
            string userName = Convert.ToString(dataGridView_OnlineTask.Rows[rowIndex].Cells["任务执行人员"].Value);
            DateTime t = new DateTime();
            try
            {
                OracleConnection conn = null;
                List<listID> listIDs = new List<listID>();
                conn = OledbHelper.sqlConnection();
                conn.Open();
                OracleCommand cmd = new OracleCommand("select * from MARK_TASK where TASK_ID ='" + dbID + "'", conn);
                OracleDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    listID li = new listID();
                    li.MarkId = Convert.ToString(dr["MARK_ID"]);

                    if (Convert.ToInt32(dr["IS_CHECKED"]) == 1)
                    {
                        //Datatime
                        li.check = Convert.ToInt32(dr["IS_CHECKED"]);
                        li.checktime = Convert.ToString(dr["CHECKTIME"]);
                        t = (DateTime)dr["CHECKTIME"];
                        li.t = t.Year * 10000000000 + t.Day * 1000000 + t.Month * 100000000 + t.Hour * 10000 + t.Minute * 100 + t.Second;
                    }
                    listIDs.Add(li);
                }
                dr.Close();
                conn.Close();
                conn = OledbHelper.sqlConnection();
                conn.Open();
                cmd = new OracleCommand("select * from 标识器", conn);
                dr = cmd.ExecuteReader();

                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 = 558443.622;
                double y = 3451941.845;

                GSOGeoModel model = new GSOGeoModel();

                string filepath;

                GSOPoint3d p = new GSOPoint3d();
                GSOPoint3d po = new GSOPoint3d();
                GSOPoint3ds pnts = new GSOPoint3ds();
                GSOGeoPolyline3D line = new GSOGeoPolyline3D();

                while (dr.Read())
                {
                    string id = Convert.ToString(dr["编号"]);////
                    for (int i = 0; i < listIDs.Count; i++)
                    {
                        if (id != Convert.ToString(listIDs[i].MarkId)) continue;
                        x = Convert.ToDouble(dr["X坐标"]);
                        y = Convert.ToDouble(dr["Y坐标"]);
                        GSOPoint2d pt2d = new GSOPoint2d(y, x);
                        GSOPoint2d result = GSOProjectManager.Inverse(pt2d, ID);
                        p.X = result.X;
                        p.Y = result.Y;
                        p.Z = 1.5;
                        if (Convert.ToInt16(listIDs[i].check) == 1)
                        {
                            filepath = Application.StartupPath + "/EMSgcm/小旗子/lvqi/lvqi.gcm";
                            model.FilePath = filepath;
                        }
                        if (Convert.ToInt16(listIDs[i].check) == 0)
                        {
                            filepath = Application.StartupPath + "/EMSgcm/小旗子/hongqi/hongqi.gcm";

                            model.FilePath = filepath;
                        }

                        model.Position = p;
                        model.AltitudeMode = EnumAltitudeMode.Absolute;
                        GSOFeature f = new GSOFeature();
                        f.Geometry = model;
                        f.Name = "标识器:"+id;
                        GSOFeature newFeature = globeControl1.Globe.MemoryLayer.AddFeature(f);
                        GSOLabel label = new GSOLabel();
                        label.Text = "任务工单号:" + dbID + "\r\n" +
                                     "管线定位员:" + userName + "\r\n" +
                                     "定位时间:" + listIDs[i].checktime;
                        label.Style = new GSOLabelStyle();
                        label.Style.Opaque = 0.8; //设置标注的透明度,取值区间是0-1  
                        label.Style.TracktionLineType = EnumTracktionLineType.Solid;
                        label.Style.TextStyle.FontHeight = 10;
                        label.Style.TextStyle.FontName = "宋体";
                        label.Style.TractionLineEndPos = new GSOPoint2d(80, 60);
                        label.Style.BackBeginColor = Color.Cyan;
                        label.Style.BackEndColor = Color.White;
                        newFeature.Label = label;
                        listIDs[i].x = p.X;
                        listIDs[i].y = p.Y;
                    }
                }
                dr.Close();
                conn.Close();

                //获取定位人员位置
                conn = OledbHelper.sqlConnection();
                conn.Open();
                cmd = new OracleCommand("select * from POSITION", conn);
                dr = cmd.ExecuteReader();
                int num = 0;
                while (dr.Read())
                {
                    //int num1 = Convert.ToInt32(dr["DBID"]);
                    //if (num < num1)
                    //{
                    po.X = Convert.ToDouble(dr["LONGITUDE"]);
                    po.Y = Convert.ToDouble(dr["LATITUDE"]);
                    po.Z = 2.5;
                    break;
                    //}
                }
                dr.Close();
                conn.Close();
                pnts.Add(po);

                model = new GSOGeoModel();

                filepath = Application.StartupPath + "/EMSgcm/hat/hat.gcm";

                model.FilePath = filepath;
                model.Position = po;
                model.AltitudeMode = EnumAltitudeMode.Absolute;
                GSOFeature f1 = new GSOFeature();
                f1.Geometry = model;
                double s = 0.1;
                f1.Geometry.Scale(s, s, s);
                GSOFeature newFeature1 = globeControl1.Globe.MemoryLayer.AddFeature(f1);

                GSOLabel label1 = new GSOLabel();
                label1.Text = "定位员:" + userName;
                label1.Style = new GSOLabelStyle();
                label1.Style.Opaque = 0.8; //设置标注的透明度,取值区间是0-1  
                label1.Style.TracktionLineType = EnumTracktionLineType.Solid;
                label1.Style.TextStyle.FontHeight = 20;
                label1.Style.TextStyle.FontName = "黑体";
                label1.Style.TractionLineEndPos = new GSOPoint2d(80, 60);
                newFeature1.Label = label1;

                // 按扫描时间对任务点进行排序
                int m = 0;
                long time = 0;
                for (int ii = 0; ii < listIDs.Count; ii++)
                {
                    for (int k = ii; k < listIDs.Count; k++)
                    {
                        if (time < listIDs[k].t)
                        {
                            time = listIDs[k].t;
                            m = k;
                        }
                    }

                    if (listIDs[m].check == 1)
                    {
                        p.X = listIDs[m].x;
                        p.Y = listIDs[m].y;
                        p.Z = 2.5;
                        pnts.Add(p);

                        time = listIDs[ii].t;
                        listIDs[ii].t = listIDs[m].t;
                        listIDs[m].t = time;

                        int i = 0;
                        i = listIDs[ii].check;
                        listIDs[ii].check = listIDs[m].check;
                        listIDs[m].check = i;

                        x = listIDs[ii].x;
                        listIDs[ii].x = listIDs[m].x;
                        listIDs[m].x = x;

                        y = listIDs[ii].y;
                        listIDs[ii].y = listIDs[m].y;
                        listIDs[m].y = y;
                    }
                    time = 0;
                }
                if (pnts.Count != 0)
                {
                    line.AddPart(pnts); //把节点添加到线上
                    line.AltitudeMode = EnumAltitudeMode.Absolute;
                    GSOSimpleLineStyle3D style1 = new GSOSimpleLineStyle3D(); //创建线的风格    
                    //style1.LineColor = Color.FromArgb(250, 255, 0, 255);
                    style1.LineColor = Color.Red;
                    style1.LineWidth = 3; //设置线的宽度为3
                    style1.VertexVisible = true; //显示线的节点

                    line.Style = style1; //把风格添加到线上
                    GSOFeature f2 = new GSOFeature();

                    f2.Geometry = line; //把线对象添加到几何对象上                
                    f2.Name = "轨迹"; //设置几何对象的名称
                    f2.SetFieldValue("description", "这是线的属性"); //设置几何对象的字段值
                    GSOFeature newFeature2 = globeControl1.Globe.MemoryLayer.AddFeature(f2);
                }

                EnumAltitudeMode altmode = EnumAltitudeMode.Absolute;
                globeControl1.Globe.FlyToPointSpeed = 10000000;
                globeControl1.Globe.FlyToPosition(po, altmode);
                globeControl1.Refresh();

            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                //conn.Close();
            }
        }


        private void reloadGrid(int pageIndex)
        {
            string sqlcount = "select count(*) from taskinfo t where TASKSTATE='开始任务'";
            string sqlrows = "select * from (select rownum as rowno,dbid," +
                                       "deploytime," +
                                       "descirption," +
                                       "finishtime," +
                                       "street," +
                                       "taskstate," +
                                       "username," +
                                       "'查看' as btn " +
                                  "from taskinfo where TASKSTATE='开始任务'";

            if (!String.IsNullOrEmpty(comboBox_userName.Text.Trim()))
            {
                sqlcount += " and username='" + comboBox_userName.Text.Trim() + "' ";
                sqlrows += " and username='" + comboBox_userName.Text.Trim() + "' ";
            }

            if (dateTime_begin.Value != null)
            {
                sqlcount += "and deploytime>=to_date('" + dateTime_begin.Value.ToShortDateString() + "','yyyy-MM-dd') ";
                sqlrows += "and deploytime>=to_date('" + dateTime_begin.Value.ToShortDateString() + "','yyyy-MM-dd') ";
            }

            if (dateTime_end.Value != null)
            {
                DateTime time = dateTime_end.Value;
                time = time.AddDays(1);
                sqlcount += "and deploytime<=to_date('" + time.ToShortDateString() + "','yyyy-MM-dd') ";
                sqlrows += "and deploytime<=to_date('" + time.ToShortDateString() + "','yyyy-MM-dd') ";
            }

            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_OnlineTask.DataSource = table;
            //dataGridView_OnlineTask.Columns[8].Visible = false;
        }

        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 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 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 button_search_Click(object sender, EventArgs e)
        {
            try
            {
                currentpage = 1;
                reloadGrid(currentpage);
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询失败:" + ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


    }
}