using Foresight;
using Foresight.Data;
using Foresight.Fleet.Services.Asset;
using Foresight.Fleet.Services.JobSite;
using Foresight.ServiceModel;
using IronIntel.Contractor.Machines;
using IronIntel.Contractor.MapView;
using IronIntel.Contractor.Shape;
using IronIntel.Services.Business.Admin;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace IronIntel.Contractor.JobSites
{
    public class JobSitesManagement
    {
        private const string MachineFields = "{0}MACHINEID,{0}MACHINENAME,{0}MAKEID,{0}MODELID,{0}TYPEID,{0}MACHINEICONID,{0}DEVICEID,{0}VIN,{0}MAKEYEAR,{0}NOTES,{0}STATUS,{0}CONTRACTORID,{0}DEALERID,{0}UID,{0}ADDEDON,{0}CUR_LONGITUDE,{0}CUR_LATITUDE,{0}LOCDATE_UTC,{0}ENGINEHOURS,{0}HOURSDATE_UTC,{0}DATASOURCE,{0}HIDE,{0}FUEL_CONSUMED,{0}FUEL_UNITS,{0}FUEL_DATE,{0}ODOMETER,{0}ODODATE_UTC,{0}ODOMETERUOM,{0}FUELCOST,{0}FUELCOSTUOM,{0}MACHINERATE,{0}WORKTYPE,{0}RETIREMENTHOURS,{0}RETIREMENTODO,{0}ALTITUDE,{0}ALTITUDEUNITS,{0}IDLEHOURSUTC,{0}IDLEHOURS,{0}LOADCOUNTUTC,{0}LOADCOUNT,{0}PAYLOADTOTALUTC,{0}PAYLOADTOTAL,{0}PAYLOADTOTALUNITS,{0}DEFREMAININGUTC,{0}DEFREMAINING,{0}FUELREMAININGUTC,{0}FUELREMAININGPERCENT,{0}MACHINENAME2,{0}ONROAD,{0}LEASESTART,{0}LEASEEND,{0}LEASEHOURS,{0}UNDERCARRIAGEHOURS,{0}ODOSTART2,{0}ISDELETED,{0}DELETEDDATE,{0}ODOSTART2DATASOURCE,{0}LOCDATASOURCE,{0}HOURSDATASOURCE,{0}FUELDATASOURCE,{0}AQUISITIONTYPE,{0}ICONFILENAME,{0}STARTINGENGINEHOURS,{0}DISTANCECALCBY,{0}TELEMATICSENABLED,{0}COSTCENTER,{0}EQCLASS,{0}DESCRIPTION";


        public static JobSiteViewItem GetJobSiteByID(string jobsiteid)
        {
            const string sql = @"select JOBSITEID,JOBSITENAME,LATITUDE,LONGITUDE,RADIUS,RADUIS_UOM,CONTRACTORID,COLOR,NOTES,STARTDATE,ENDDATE,POLYGON,BASEONMACHINEID,ISDELETED from JOBSITES where JOBSITEID={0}";

            FISqlConnection db = SystemParams.GetDbInstance();
            DataTable dt = db.GetDataTableBySQL(sql, jobsiteid);

            JobSiteViewItem result = null;
            if (dt.Rows.Count > 0)
            {
                result = ConvertToJobSiteViewItem(dt.Rows[0]);
                MachineViewItem[] msiary = GetJobSiteMachines(Convert.ToInt64(jobsiteid));
                if (msiary.Count() > 0)
                {
                    result.Machines = msiary.OrderBy((m) => m.VIN).ToArray();
                }
                else
                {
                    result.Machines = null;
                }
            }
            return result;
        }


        public static void RefreshJobsiteAssets(string sessionid, long jobsiteid)
        {
            System.Threading.ThreadPool.QueueUserWorkItem((object state) =>
            {
                try
                {
                    FleetServiceClientHelper.CreateClient<JobSiteProvider>(sessionid).RefreshAllJobSites(SystemParams.CompanyID, jobsiteid);
                }
                catch { }
            }, null);
        }

        public static void DeleteJobSites(long jobsiteid)
        {
            const string SQL = "delete from JOBSITES where JOBSITEID={0}"
                             + " delete from JOBSITETYPE where JOBSITEID={0}"
                             + " delete from JOBSITEMACHINES where JOBSITEID={0}"
                             + " delete from RELATIONSHIP where RELATIONSHIPTYPEID='ContactJobsite' and RELATEDID={0}";
            FIDbAccess db = SystemParams.GetDbInstance();
            db.ExecSQL(SQL, jobsiteid);
        }


        public static void AddMachinesToJobSite(JobSiteViewItem jobsite)
        {
            if (jobsite != null)
            {
                const string SQL_t = @"if exists(select 1 from JOBSITEMACHINES where JOBSITEID={0} and MACHINEID={1}) update JOBSITEMACHINES set ONSITE={3} where JOBSITEID={0} and MACHINEID = {1} else insert into JOBSITEMACHINES(JOBSITEID,MACHINEID,VIN,ADDEDON,ONSITE) values({0},{1},{2},getutcdate(),{3})";
                const string SQL_upt = "update JOBSITEMACHINES set ONSITE=0 where JOBSITEID!={0} and MACHINEID={1}";
                const string SQL_del = "select MACHINEID from JOBSITEMACHINES where JOBSITEID={0} ";
                const string SQL_OUT = "exec pro_removejobsitemachines {0},{1},{2},{3},{4},{5},{6},{7}";

                string ad = string.Empty;
                if (jobsite.Machines != null && jobsite.Machines.Length > 0)
                {
                    string machineids = string.Join(",", jobsite.Machines.Select(m => m.ID));
                    ad = "  and MACHINEID not in(" + machineids + ")";
                }

                using (FISqlTransaction tran = new FISqlTransaction(SystemParams.DataDbConnectionString))
                {
                    DataTable dt = tran.GetDataTableBySQL(SQL_del + ad, jobsite.ID);
                    foreach (DataRow dr in dt.Rows)
                    {
                        string mid = FIDbAccess.GetFieldString(dr["MACHINEID"], "");
                        tran.ExecSQL(SQL_OUT, mid, jobsite.ID, DateTime.Now.ToUniversalTime(), null, null, null, null, "ManualRemove");
                    }

                    if (jobsite.Machines != null && jobsite.Machines.Length > 0)
                    {
                        foreach (MachineViewItem mac in jobsite.Machines)
                        {
                            if (mac.OnSite)
                            {
                                tran.ExecSQL(SQL_upt, jobsite.ID, mac.ID);
                            }

                            tran.ExecSQL(SQL_t, jobsite.ID, mac.ID, mac.VIN, mac.OnSite ? 1 : 0);
                        }
                    }
                    tran.Commit();
                }
            }

        }

        public static void AddMachineOnSiteToJobSite(FISqlConnection db, long machineid, string vin, int jobsiteid)
        {
            const string SQL_t = @"if exists(select 1 from JOBSITEMACHINES where JOBSITEID={0} and MACHINEID={1}) update JOBSITEMACHINES set ONSITE={3} 
                where JOBSITEID={0} and MACHINEID = {1} else insert into JOBSITEMACHINES(JOBSITEID,MACHINEID,VIN,ADDEDON,ONSITE) values({0},{1},{2},getutcdate(),{3})";
            const string SQL_upt = "update JOBSITEMACHINES set ONSITE=0 where JOBSITEID!={0} and MACHINEID={1}";

            if (db == null)
                db = SystemParams.GetDbInstance();

            db.ExecSQL(SQL_upt, jobsiteid, machineid);
            if (jobsiteid > 0)
                db.ExecSQL(SQL_t, jobsiteid, machineid, vin, 1);
        }



        private static JobSiteViewItem ConvertToJobSiteViewItem(DataRow dr)
        {
            JobSiteViewItem js = new JobSiteViewItem();
            long JobSiteId = FIDbAccess.GetFieldInt(dr["JOBSITEID"], 0);
            js.ID = FIDbAccess.GetFieldInt(dr["JOBSITEID"], 0);
            js.Name = FIDbAccess.GetFieldString(dr["JOBSITENAME"], string.Empty);
            js.Latitude = FIDbAccess.GetFieldDouble(dr["LATITUDE"], 0);
            js.Longitude = FIDbAccess.GetFieldDouble(dr["LONGITUDE"], 0);
            js.Radius = FIDbAccess.GetFieldDouble(dr["RADIUS"], 0);
            js.Radius_UOM = FIDbAccess.GetFieldString(dr["RADUIS_UOM"], string.Empty);
            if (string.IsNullOrWhiteSpace(js.Radius_UOM))
                js.Radius_UOM = "Mile";
            js.ContractorID = FIDbAccess.GetFieldString(dr["CONTRACTORID"], string.Empty);
            js.ColorString = FIDbAccess.GetFieldString(dr["COLOR"], string.Empty);
            System.Drawing.Color color = System.Drawing.Color.Orange;
            try
            {
                color = System.Drawing.ColorTranslator.FromHtml(js.ColorString);
            }
            catch
            {
            }
            js.Color = new IIColor() { Alpha = color.A, Red = color.R, Green = color.G, Blue = color.B };

            js.Notes = FIDbAccess.GetFieldString(dr["NOTES"], string.Empty);
            js.StartDate = FIDbAccess.GetFieldDateTime(dr["STARTDATE"], DateTime.MinValue);
            js.EndDate = FIDbAccess.GetFieldDateTime(dr["ENDDATE"], DateTime.MinValue);
            string polygon = FIDbAccess.GetFieldString(dr["POLYGON"], string.Empty);
            js.Polygon = ConvertPolygonToPointItem(polygon);
            js.BaseOnMachineID = FIDbAccess.GetFieldInt(dr["BASEONMACHINEID"], 0);
            js.IsDeleted = FIDbAccess.GetFieldInt(dr["ISDELETED"], 0) == 1;
            if (js.IsDeleted)
                js.Name = js.Name + " - Deleted";
            return js;
        }

        private static PostionItem[] ConvertPolygonToPointItem(string polygon)
        {
            if (string.IsNullOrWhiteSpace(polygon))
                return null;

            List<PostionItem> list = new List<PostionItem>();
            var polygons = polygon.Split(';');
            foreach (var py in polygons)
            {
                PostionItem pi = new PostionItem();
                var sap = py.Split(',');
                pi.Latitude = Convert.ToDouble(sap[0]);
                pi.Longitude = Convert.ToDouble(sap[1]);
                list.Add(pi);
            }
            return list.ToArray();
        }

        private static string ConvertPointItemToPolygon(PostionItem[] points)
        {
            string polygon = string.Empty;
            if (points == null || points.Length < 0)
            {
                return polygon;
            }
            foreach (var pi in points)
            {
                if (string.IsNullOrWhiteSpace(polygon))
                    polygon += pi.Latitude + "," + pi.Longitude;
                else
                    polygon += ";" + pi.Latitude + "," + pi.Longitude;
            }
            return polygon;
        }


        private static MachineViewItem[] GetJobSiteMachines(long JobSiteId)
        {
            const string sql_j = @"select jm.MACHINEID,ISNULL(ONSITE,0) ONSITE,m.VIN,m.MACHINENAME,m.MACHINENAME2,m.TYPEID from JOBSITEMACHINES jm left join machines m on jm.MACHINEID=m.MACHINEID where JOBSITEID={0}";
            FISqlConnection db = SystemParams.GetDbInstance();
            DataTable dt = null;
            dt = db.GetDataTableBySQL(sql_j, JobSiteId);

            if (dt.Rows.Count == 0)
            {
                return new MachineViewItem[0];
            }
            List<MachineViewItem> list = new List<MachineViewItem>();
            foreach (DataRow dr in dt.Rows)
            {
                MachineViewItem mi = new MachineViewItem();
                mi.ID = FIDbAccess.GetFieldInt64(dr["MACHINEID"], 0);
                mi.OnSite = FIDbAccess.GetFieldInt(dr["ONSITE"], 0) == 1;
                mi.Name = FIDbAccess.GetFieldString(dr["MACHINENAME"], string.Empty);
                mi.Name2 = FIDbAccess.GetFieldString(dr["MACHINENAME2"], string.Empty);
                mi.VIN = FIDbAccess.GetFieldString(dr["VIN"], string.Empty); ;
                mi.TypeID = FIDbAccess.GetFieldInt(dr["TYPEID"], 0);
                list.Add(mi);
            }
            return list.ToArray();
        }

        /// <summary>
        /// 获取机器OnSite状态的JobSite
        /// </summary>
        /// <returns></returns>
        public static Dictionary<int, int> GetOnSiteMahcines(FISqlConnection db)
        {
            const string SQL_J = "select JOBSITEID,MACHINEID from JOBSITEMACHINES where ONSITE=1";

            if (db == null)
                db = SystemParams.GetDbInstance();
            DataTable tb = db.GetDataTableBySQL(SQL_J);
            Dictionary<int, int> result = new Dictionary<int, int>();

            foreach (DataRow dr in tb.Rows)
            {
                int machineid = FIDbAccess.GetFieldInt(dr["MACHINEID"], 0);
                int jobsiteid = FIDbAccess.GetFieldInt(dr["JOBSITEID"], 0);
                result[machineid] = jobsiteid;
            }
            return result;
        }

        /// <summary>
        /// 获取机器OnSite状态的JobSite
        /// </summary>
        /// <param name="db"></param>
        /// <param name="machineid"></param>
        /// <returns></returns>
        public static int GetOnSiteByMachineID(FISqlConnection db, long machineid)
        {
            const string SQL_J = "select JOBSITEID from JOBSITEMACHINES where ONSITE=1 and MACHINEID={0}";

            if (db == null)
                db = SystemParams.GetDbInstance();
            DataTable tb = db.GetDataTableBySQL(SQL_J, machineid);
            int jobsiteid = 0;
            if (tb.Rows.Count > 0)
                jobsiteid = FIDbAccess.GetFieldInt(tb.Rows[0]["JOBSITEID"], 0);

            return jobsiteid;
        }

        #region Machines

        public static AvailableMachines GetMachineViewItemByType(string sessionid, string jobsiteid, string typeid, string searchText, string useriid)
        {
            AvailableMachines result = new AvailableMachines();
            string SQL_J = @"select m.*,js.JOBSITEID,js.JOBSITENAME,js.LATITUDE,js.LONGITUDE,jsm.ADDEDON as JSMADDEDON,ONSITE from MACHINES m
              left join JOBSITEMACHINES as jsm on m.MACHINEID = jsm.MACHINEID and jsm.ONSITE=1
              left join JOBSITES as js on js.JOBSITEID= jsm.JOBSITEID";

            const string SQL_L = @"select AssetId as MACHINEID,LATITUDE,LONGITUDE,AsofTime as ASOFTIME_UTC,MOVESTATUS,HEADING, Speed,SpeedUnits,PostedSpeedLimit,SpeedLimitUnits,Street from AssetLocation where IsPrimary=1";

            if (!string.IsNullOrWhiteSpace(typeid))
            {
                Regex r = new Regex(@"[a-zA-Z]");
                if (r.IsMatch(typeid))
                    return result;
                SQL_J += " and m.TYPEID=" + typeid + "";
            }

            FIDbAccess db = SystemParams.GetDbInstance();
            DataTable tb = db.GetDataTableBySQL(SQL_J);
            if (tb.Rows.Count == 0)
            {
                result.Assigned = new MachineViewItem[0];
                result.Unassigned = new MachineViewItem[0];
                return result;
            }

            double timeadjust = SystemParams.GetHoursOffset();
            DataTable tbl = null;
            string dbString2 = SystemParams.GetIronIntelReportDataDbString();
            if (!string.IsNullOrWhiteSpace(dbString2))
            {
                var db2 = new FISqlConnection(dbString2);
                tbl = db2.GetDataTableBySQL(SQL_L);
            }

            Dictionary<long, LocationViewItem> machineLocations = new Dictionary<long, LocationViewItem>();
            if (tbl != null && tbl.Rows.Count > 0)
            {
                foreach (DataRow dr in tbl.Rows)
                {
                    long mID = FIDbAccess.GetFieldInt64(dr["MACHINEID"], 0);
                    var l = new LocationViewItem();
                    l.Latitude = FIDbAccess.GetFieldDouble(dr["LATITUDE"], 0);
                    l.Longitude = FIDbAccess.GetFieldDouble(dr["LONGITUDE"], 0);
                    l.LocationTime = FIDbAccess.GetFieldDateTime(dr["ASOFTIME_UTC"], DateTime.MinValue);
                    if (l.LocationTime != DateTime.MinValue)
                        l.LocationTime = l.LocationTime.AddHours(timeadjust);

                    machineLocations[mID] = l;
                }
            }

            long[] availableAssetsids = null;
            var user = Users.UserManagement.GetUserByIID(useriid);
            if (user.UserType < Users.UserTypes.Admin)
                availableAssetsids = FleetServiceClientHelper.CreateClient<AssetQueryClient>(sessionid).GetAvailableAssetsForUsers(SystemParams.CompanyID, useriid);

            MachineManagement.RefreshBaseData();
            MachineMake[] makes = MachineManagement.GetMachineMakes();
            MachineModel[] models = MachineManagement.GetMachineModels();
            MachineType[] types = MachineManagement.GetMachineTypes();

            List<MachineViewItem> assigned = new List<MachineViewItem>();
            List<MachineViewItem> unassigned = new List<MachineViewItem>();
            JobSiteViewItem jobsite = GetJobSiteByID(jobsiteid);
            if (tb.Rows.Count > 0)
            {
                foreach (DataRow dr in tb.Rows)
                {
                    long mid = Convert.ToInt64(dr["MACHINEID"]);
                    if (user.UserType < Users.UserTypes.Admin && !availableAssetsids.Contains(mid))
                        continue;
                    MachineViewItem mi = ConvertToMachineViewItem(dr, makes, models, types, timeadjust);
                    if (machineLocations.ContainsKey(mi.ID))
                        mi.Location = machineLocations[mi.ID];

                    if (!string.IsNullOrWhiteSpace(searchText))
                    {
                        if (!Helper.Contains(mi.VIN, searchText)
                            && !Helper.Contains(mi.ID.ToString(), searchText)
                            && !Helper.Contains(mi.Name, searchText)
                            && !Helper.Contains(mi.Name2, searchText)
                            && !Helper.Contains(mi.Make, searchText)
                            && !Helper.Contains(mi.MachineType, searchText)
                            && !Helper.Contains(mi.Model, searchText))
                        {
                            continue;
                        }
                    }
                    mi.JobSiteName = FIDbAccess.GetFieldString(dr["JOBSITENAME"], string.Empty);
                    Postion mPoint = new Postion(mi.Location.Latitude, mi.Location.Longitude);
                    Postion jsPoint = new Postion(jobsite.Latitude, jobsite.Longitude);
                    mi.DistanceFromSite = Foresight.Haversine.GetDistanceKilometers(mPoint, jsPoint);

                    if (jobsite.Polygon != null && jobsite.Polygon.Length > 3)//多边形，计算机器是否在Jobsite内
                        mi.WithinSite = Haversine.IsInSide(new Postion(mi.Location.Latitude, mi.Location.Longitude), ConvertToPosition(jobsite.Polygon));//由于FICore.dll中Postion的Longtitude存在拼写错误，在前端容易引起混淆，使用加了PostionItem  


                    if (string.Compare(jobsite.Radius_UOM, "Kilometre", true) != 0)//转换
                        mi.DistanceFromSite = mi.DistanceFromSite * 0.6213712;
                    mi.DistanceFromSite = Math.Round(mi.DistanceFromSite, 2);

                    bool hide = FIDbAccess.GetFieldInt(dr["HIDE"], 0) == 1;
                    if (!hide && (dr["JOBSITEID"] is DBNull || string.IsNullOrWhiteSpace(dr["JOBSITEID"].ToString())))
                        unassigned.Add(mi);
                    else
                        assigned.Add(mi);
                }
            }
            if (jobsite.Radius > 0)
            {
                result.Assigned = assigned.OrderBy((m) => m.DistanceFromSite).ToArray();
                result.Unassigned = unassigned.OrderBy((m) => m.DistanceFromSite).ToArray();
            }
            else
            {
                result.Assigned = assigned.OrderBy((m) => !m.WithinSite).ThenBy((m) => m.DistanceFromSite).ToArray();
                result.Unassigned = unassigned.OrderBy((m) => !m.WithinSite).ThenBy((m) => m.DistanceFromSite).ToArray();
            }
            return result;
        }

        /// <summary>
        /// 获取可用于Jobsite 绑定的机器,Calamp和FI Tracker
        /// </summary>
        /// <param name="useriid"></param>
        /// <returns></returns>
        public static MachineViewItem[] GetBindingMachines(string sessionid, string useriid)
        {
            List<MachineViewItem> result = new List<MachineViewItem>();
            string SQL = @"select " + string.Format(MachineFields, "m.") + @" from MACHINES m order by m.MACHINENAME";

            const string SQL_L = @"select AssetId as MACHINEID,LATITUDE,LONGITUDE,AsofTime as ASOFTIME_UTC,MOVESTATUS,HEADING,Speed,SpeedUnits,PostedSpeedLimit,SpeedLimitUnits,Street from AssetLocation where IsPrimary=1 and Datasource in ('Calamp','FITRACKER')";

            FIDbAccess db = SystemParams.GetDbInstance();
            DataTable tb = db.GetDataTableBySQL(SQL, useriid);
            if (tb.Rows.Count == 0)
            {
                return new MachineViewItem[0];
            }

            double timeadjust = SystemParams.GetHoursOffset();
            DataTable tbl = null;
            string dbString2 = SystemParams.GetIronIntelReportDataDbString();
            if (!string.IsNullOrWhiteSpace(dbString2))
            {
                var db2 = new FISqlConnection(dbString2);
                tbl = db2.GetDataTableBySQL(SQL_L);
            }

            Dictionary<long, LocationViewItem> machineLocations = new Dictionary<long, LocationViewItem>();
            if (tbl != null && tbl.Rows.Count > 0)
            {
                foreach (DataRow dr in tbl.Rows)
                {
                    long mID = FIDbAccess.GetFieldInt64(dr["MACHINEID"], 0);
                    var l = new LocationViewItem();
                    l.Latitude = FIDbAccess.GetFieldDouble(dr["LATITUDE"], 0);
                    l.Longitude = FIDbAccess.GetFieldDouble(dr["LONGITUDE"], 0);
                    l.LocationTime = FIDbAccess.GetFieldDateTime(dr["ASOFTIME_UTC"], DateTime.MinValue);
                    if (l.LocationTime != DateTime.MinValue)
                        l.LocationTime = l.LocationTime.AddHours(timeadjust);

                    machineLocations[mID] = l;
                }
            }

            long[] availableAssetsids = null;
            var user = Users.UserManagement.GetUserByIID(useriid);
            if (user.UserType < Users.UserTypes.Admin)
                availableAssetsids = FleetServiceClientHelper.CreateClient<AssetQueryClient>(sessionid).GetAvailableAssetsForUsers(SystemParams.CompanyID, useriid);

            MachineManagement.RefreshBaseData();
            MachineMake[] makes = MachineManagement.GetMachineMakes();
            MachineModel[] models = MachineManagement.GetMachineModels();
            MachineType[] types = MachineManagement.GetMachineTypes();

            if (tb.Rows.Count > 0)
            {
                foreach (DataRow dr in tb.Rows)
                {
                    long mid = FIDbAccess.GetFieldInt64(dr["MACHINEID"], 0);
                    if (user.UserType < Users.UserTypes.Admin && !availableAssetsids.Contains(mid))
                        continue;
                    MachineViewItem mi = ConvertToMachineViewItem(dr, makes, models, types, timeadjust);
                    if (machineLocations.ContainsKey(mi.ID))
                    {
                        mi.Location = machineLocations[mi.ID];
                        result.Add(mi);
                    }
                }
            }
            return result.ToArray();
        }

        private static MachineViewItem ConvertToMachineViewItem(DataRow dr, MachineMake[] makes, MachineModel[] models, MachineType[] types, double timeadjust)
        {
            MachineViewItem mi = new MachineViewItem();
            mi.ID = FIDbAccess.GetFieldInt64(dr["MACHINEID"], 0);
            mi.Name = FIDbAccess.GetFieldString(dr["MACHINENAME"], string.Empty);
            mi.Name2 = FIDbAccess.GetFieldString(dr["MACHINENAME2"], string.Empty);
            mi.VIN = FIDbAccess.GetFieldString(dr["VIN"], string.Empty);
            mi.MakeYear = FIDbAccess.GetFieldInt(dr["MAKEYEAR"], 0);
            mi.EngineHours = FIDbAccess.GetFieldDouble(dr["ENGINEHOURS"], 0);
            mi.EngineHoursDate = FIDbAccess.GetFieldDateTime(dr["HOURSDATE_UTC"], DateTime.MinValue);
            if (mi.EngineHoursDate != DateTime.MinValue)
                mi.EngineHoursDate = mi.EngineHoursDate.AddHours(timeadjust);

            mi.Location = new LocationViewItem();
            mi.Location.Latitude = FIDbAccess.GetFieldDouble(dr["CUR_LATITUDE"], 0);
            mi.Location.Longitude = FIDbAccess.GetFieldDouble(dr["CUR_LONGITUDE"], 0);
            mi.Location.LocationTime = FIDbAccess.GetFieldDateTime(dr["LOCDATE_UTC"], DateTime.MinValue);
            if (mi.Location.LocationTime != DateTime.MinValue)
                mi.Location.LocationTime = mi.Location.LocationTime.AddHours(timeadjust);

            mi.Odometer = FIDbAccess.GetFieldDouble(dr["ODOMETER"], 0);
            mi.OdometerUOM = FIDbAccess.GetFieldString(dr["ODOMETERUOM"], string.Empty);

            int makeid = FIDbAccess.GetFieldInt(dr["MAKEID"], 0);
            MachineMake make = MachineManagement.GetMachineMake(makes, makeid);
            mi.Make = make == null ? string.Empty : make.Name;

            int modelid = FIDbAccess.GetFieldInt(dr["MODELID"], 0);
            MachineModel model = MachineManagement.GetMachineModel(models, modelid);
            mi.Model = model == null ? string.Empty : model.Name;

            int typeid = FIDbAccess.GetFieldInt(dr["TYPEID"], 0);
            mi.TypeID = typeid;
            MachineType mtype = MachineManagement.GetMachineType(types, typeid);
            if (mtype != null)
            {
                mi.MachineType = mtype.Name;
                mi.IconUrl = mtype.IconUrl;
            }
            else
            {
                mi.MachineType = string.Empty;
                mi.IconUrl = MachineManagement.DefaultMachineTypeIconUrl;
            }
            return mi;
        }

        private static Foresight.Postion[] ConvertToPosition(PostionItem[] polygon)
        {
            if ((polygon == null) || (polygon.Length == 0))
            {
                return new Foresight.Postion[0];
            }
            List<Foresight.Postion> ls = new List<Postion>(polygon.Length);
            foreach (var p in polygon)
            {
                ls.Add(new Postion(p.Latitude, p.Longitude));
            }
            return ls.ToArray();
        }

        #endregion

        public static MachineTypeItem[] GetMachineTypes()
        {
            List<MachineTypeItem> mTypes = new List<MachineTypeItem>();
            MachineType[] types = MachineManagement.GetMachineTypes();
            if (types != null)
            {
                foreach (var t in types)
                {
                    mTypes.Add(new MachineTypeItem() { ID = t.ID, Name = t.Name });
                }
            }
            return mTypes.ToArray();
        }

        private static Dictionary<long, List<string>> GetJobSiteTypes(FISqlConnection db)
        {

            const string SQL = "select JOBSITEID,JOBSITETYPE from JOBSITETYPE";

            Dictionary<long, List<string>> result = new Dictionary<long, List<string>>();
            if (db == null)
                db = SystemParams.GetDbInstance();
            DataTable tb = db.GetDataTableBySQL(SQL);

            foreach (DataRow dr in tb.Rows)
            {
                long jobsiteid = FIDbAccess.GetFieldInt64(dr["JOBSITEID"], 0);
                string type = FIDbAccess.GetFieldString(dr["JOBSITETYPE"], string.Empty);
                if (!result.ContainsKey(jobsiteid))
                    result[jobsiteid] = new List<string>();
                result[jobsiteid].Add(type);
            }
            return result;
        }

        private static void SaveJobSiteTypes(FIDbAccess db, long jobsiteid, string[] types)
        {
            const string SQL = "insert into JOBSITETYPE(JOBSITEID,JOBSITETYPE) values({0},{1})";
            const string SQL_DEL = "delete from JOBSITETYPE where JOBSITEID={0}";

            if (db == null)
                db = SystemParams.GetDbInstance();

            db.ExecSQL(SQL_DEL, jobsiteid);
            if (types != null)
            {
                foreach (string type in types)
                {
                    db.ExecSQL(SQL, jobsiteid, type);
                }
            }
        }
        public static JobSiteViewItem[] GetUserJobsites(string uid)
        {
            const string SQL = @"select a.JOBSITEID,JOBSITENAME,LATITUDE,LONGITUDE,RADIUS,RADUIS_UOM,b.CONTRACTORID,COLOR,NOTES,STARTDATE,ENDDATE,POLYGON, BASEONMACHINEID,ISDELETED from USERJOBSITEMAP a,JOBSITES b where a.JOBSITEID=b.JOBSITEID and a.USERIID={0}";

            FISqlConnection db = SystemParams.GetDbInstance();
            DataTable tb = db.GetDataTableBySQL(SQL, uid);
            if (tb.Rows.Count == 0)
            {
                return new JobSiteViewItem[0];
            }
            List<JobSiteViewItem> ls = new List<JobSiteViewItem>();
            foreach (DataRow dr in tb.Rows)
            {
                JobSiteViewItem js = ConvertToJobSiteViewItem(dr);
                ls.Add(js);
            }
            return ls.ToArray();

        }

        /// <summary>
        /// 从Shape文件导入Jobsite范围
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static MapPoint[] ImportJobsitePolygon(string filename, byte[] buffer)
        {
            Shape.Shape shape = new Shape.Shape();
            ShapeFileParser.ParseFromShapeFile(buffer, shape);
            if (shape.Polygons.Count > 0 && shape.Polygons[0].Rings.Count > 0)
            {
                return shape.Polygons[0].Rings[0].Points.ToArray();
            }
            return new MapPoint[0];
        }
    }
}
