using Maticsoft.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PMSUI.Model
{
    public class StationInformationBLL
    {
        public static DataSet GetList(string strWhere)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"Select * from StationInformation");

            if (strWhere.Trim() != "")
            {
                sql.Append($" where {strWhere}");
            }

            return DbHelperSQL.Query(sql.ToString());
        }

        /// <summary>
        /// 获取对应得库位状态
        /// </summary>
        /// <param name="type"></param>
        /// <param name="locationCode"></param>
        /// <returns></returns>
        public static int GetStatus(int id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"Select * from StationInformation  where ID=" + id + "");

            DataTable dt1 = DbHelperSQL.Query(sql.ToString()).Tables[0];

            return int.Parse(dt1.Rows[0]["Status"].ToString());

        }

        /// <summary>
        /// 获取对应得物料信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="locationCode"></param>
        /// <returns></returns>
        public static string GetMaterials(int id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"Select * from StationInformation  where ID=" + id + "");

            DataTable dt1 = DbHelperSQL.Query(sql.ToString()).Tables[0];

            return dt1.Rows[0]["Materials"].ToString();

        }

        //public static Tuple<int, bool> Add(Model.TaskListTable model)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("insert into TaskListTable ");
        //    strSql.Append("  ([TaskStatus],[AGVID],[TaskType],[UpPos] ,[DownPos],[GoDownPos],[BackToUpPos],[TaskPriority],[CalledTime],[StartTime],[EndTime]) ");
        //    strSql.Append($" values ('{model.TaskStatus}','{model.AGVID}','{model.TaskType}','{model.UpPos}','{model.DownPos}','{model.GoDownPos}','{model.BackToUpPos}','{model.TaskPriority}','{model.CalledTime}','{model.StartTime}','{model.EndTime}')");

        //    int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
        //    if (rows > 0)
        //    {
        //        string str = "SELECT IDENT_CURRENT('TaskListTable')";
        //        DataTable d = DbHelperSQL.Query(str?.ToString()).Tables[0];
        //        int i = int.Parse(d?.Rows[0][0].ToString());
        //        return Tuple.Create(i, true);
        //    }
        //    else
        //    {
        //        return Tuple.Create(-1, false);
        //    }
        //}


        public static Tuple<int, bool> GetID(string name)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"Select * from StationInformation  where Name='{name}'");

            DataTable dt1 = DbHelperSQL.Query(sql.ToString()).Tables[0];
            if (dt1.Rows.Count > 0)
            {
                string str = "SELECT IDENT_CURRENT('StationInformation')";
                DataTable d = DbHelperSQL.Query(str?.ToString()).Tables[0];
                int i = int.Parse(d?.Rows[0][0].ToString());
                return Tuple.Create(i, true);
            }
            else
            {
                return Tuple.Create(-1, false);
            }
        }

        public static bool Update(int id, int stattus)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"update StationInformation set Status='{stattus}' where ID={stattus}");
            int row = DbHelperSQL.ExecuteSql(sql.ToString());
            if (row > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool Update(int id, int stattus, string materitals)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"update StationInformation set Status={stattus} ");
            if (materitals != "")
            {
                sql.Append($" , Materials='{materitals}' ");
            }
            sql.Append($"  where ID={id}");
            int row = DbHelperSQL.ExecuteSql(sql.ToString());
            if (row > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool Update(int id, int x, int y)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"update StationInformation set LocationX={x} , LocationY ={y} where ID={id}");
            int row = DbHelperSQL.ExecuteSql(sql.ToString());
            if (row > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool Update(StationInformation model)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"update StationInformation set Name='{model.Name}',Materials='{model.Materials}',Status={model.Status},Station={model.Station}, JobIndex={model.JobIndex} , Width ={model.Width}, Height ={model.Height}, LocationX ={model.LocationX}, LocationY ={model.LocationY} where ID={model.ID}");
            return DbHelperSQL.ExecuteSql(sql.ToString()) > 0 ? true : false;
        }

        public static bool Delete(int id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"delete StationInformation where ID={id}");
            int row = DbHelperSQL.ExecuteSql(sql.ToString());
            if (row > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool Add(StationInformation model)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append($"INSERT INTO StationInformation([Name],[Materials],[Status],[Station],[JobIndex],[Width],[Height],[LocationX],[LocationY]) ");
            sql.Append($" VALUES('{model.Name}','{model.Materials}',{model.Status},{model.Station},{model.JobIndex},{model.Width},{model.Height},{model.LocationX},{model.LocationY})");
            return DbHelperSQL.ExecuteSql(sql.ToString()) > 0 ? true : false;
        }
    }
}
