using DBUtility;
using foda.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace foda
{
    public class DF
    {
        #region tb_ZPPlan

        public static List<DPPM> GetZPP(string stationNo)
        {
            List<DPPM> list = new List<DPPM>();
            try
            {
                BBMPT1Entities db = new BBMPT1Entities();
                string sql = @"
                              select top 1 a.ID, b.StationNo, a.OrderNo, a.PartNo, 
                                     (select ProductName from tb_Product where PartNo = a.PartNo) PName,
		                             a.OrderCount,a.ProductCount,a.BadCount
                              from tb_ZPPlan a
                              left join tb_Station b
                              on a.StationID = b.StationID
                              where a.StationID = ( select StationID from tb_Station where StationNo = '" + stationNo + @"' )
                              and a.OrderDate = Convert(varchar(10),(select getdate()),120)
                              and a.IsFinish = 0
                              order by a.Item asc
                        ";
                list = db.Database.SqlQuery<DPPM>(sql, null).ToList();
                return list;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                return list;
            }
        }

        public static bool AddProductCount(tb_ZPPlan md)
        {
            BasicBLL<tb_ZPPlan> db = new BasicBLL<tb_ZPPlan>();
            try
            {
                string[] proNames = new string[1];
                proNames[0] = "ProductCount";

                return db.UpdateInfo(md, proNames);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                return false;
            }
        }

        public static bool AddBadCount(tb_ZPPlan md)
        {
            BasicBLL<tb_ZPPlan> db = new BasicBLL<tb_ZPPlan>();
            try
            {
                string[] proNames = new string[1];
                proNames[0] = "BadCount";

                return db.UpdateInfo(md, proNames);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                return false;
            }
        }

        public static bool PlanFinish(tb_ZPPlan md)
        {
            BasicBLL<tb_ZPPlan> db = new BasicBLL<tb_ZPPlan>();
            try
            {
                string[] proNames = new string[1];
                proNames[0] = "IsFinish";

                return db.UpdateInfo(md, proNames);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                return false;
            }
        }

        public static List<tb_PLC_Mould> GetPLCMould()
        {
            BasicBLL<tb_PLC_Mould> db = new BasicBLL<tb_PLC_Mould>();
            return db.SearchAllInfo();
        }

        public static List<tb_PLC_Break> GetPLCBreak()
        {
            BasicBLL<tb_PLC_Break> db = new BasicBLL<tb_PLC_Break>();
            return db.SearchAllInfo();
        }
        #endregion

        #region Common

        public static string GetProNameBySP(string stockNo, string PartNo)
        {
            string res = "";
            try
            {
                if (stockNo.Contains(",") || PartNo.Contains(","))
                {
                    //计划生产两个产品
                    string[] stocks = {"","" };
                    string[] parts = { "", "" };
                    if (stockNo.Contains(","))
                    {
                        stocks = stockNo.Split(',');
                    }
                    if (PartNo.Contains(","))
                    {
                        parts = PartNo.Split(',');
                    }
                    if (stocks.Length < 2)
                    {
                        stocks = new string[] { stocks[0], "" };
                    }
                    if (parts.Length < 2)
                    {
                        parts = new string[] { parts[0], "" };
                    }
                    string sql = @" select top 1 a.ProductName+','+b.ProductName as [Plan] from
										( select  ProductName  from tb_Product where partNo in ('" + parts[0] + @"','" + parts[1] + @"') ) a,
										( select  ProductName  from tb_Product where  partNo in ('" + parts[0] + @"','" + parts[1] + @"') ) b
										where a.ProductName != b.ProductName ";
                    object aa = SqlHelper.ExecuteScalar(SqlHelper.SqlConnString, CommandType.Text, sql, null);
                    if (aa != null)
                    {
                        res = aa.ToString();
                    }
                }
                else
                {
                    //计划生产一个产品
                    string sql = @" select top 1 ProductName as [Plan] from tb_Product where PartNo = '" + PartNo + "' ";
                    object aa = SqlHelper.ExecuteScalar(SqlHelper.SqlConnString, CommandType.Text, sql, null);
                    if (aa != null)
                    {
                        res = aa.ToString();
                    }
                }
                return res;
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                return res;
            }

        }

        #endregion
    }
}