using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.Model;
using MESClassLibrary.DAL.BasicInfo;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using MESClassLibrary.EFModel;

namespace MESClassLibrary.BLL.BasicInfo
{
    public class OperatorBLL
    {
        BasicBLL<tb_Operator> db = new BasicBLL<tb_Operator>();

        OperatorDAL dal = new OperatorDAL();

        public DataTable SearchInfoByName(string OperatorName, string StationID)
        {
            try
            {
                return dal.SearchInfoByName(OperatorName, StationID);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }


        public DataTable SearchInfoByNameAndPsw(string OperatorName, string StationID, string psw)
        {
            try
            {
                return dal.SearchInfoByNameAndPsw(OperatorName, StationID, psw);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        /// <summary>
        /// 查询信息
        /// </summary>
        /// <returns></returns>
        public string SearchInfo(string page, string pagesize, string OperatorName, string StationID)
        {
            try
            {
                string jsonStr = "[]";
                int total = 0;//总行数
                List<tb_Operator> list = db.SearchAllInfo();

                if (!String.IsNullOrEmpty(StationID))
                {
                    list = list.Where(p => p.StationID.Equals(StationID)).ToList();//按条件分页查询
                }

                if (!String.IsNullOrEmpty(OperatorName))
                {
                    list = list.Where(p => p.OperatorName.Contains(OperatorName)).ToList();//按条件分页查询
                }

                if (list.Count > 0)
                {
                    total = list.Count;
                    int Skipcount = (Convert.ToInt32(page) - 1) * Convert.ToInt32(pagesize);
                    list = list.Skip(Skipcount).Take(Convert.ToInt32(pagesize)).ToList();

                    #region 联查
                    List<OperatorModel> StationList = new List<OperatorModel>();
                    BasicBLL<tb_Station> pl_db = new BasicBLL<tb_Station>();
                    var pl_list = pl_db.SearchAllInfo().ToList();
                    foreach (var item in list)
                    {
                        OperatorModel dm = Tool.Mapper<OperatorModel, tb_Operator>(item);
                        var info = pl_list.FirstOrDefault(p => p.StationID == item.StationID);
                        if (info != null)
                        {
                            dm.StationNo = info.StationNo;
                        }
                        StationList.Add(dm);
                    }
                    #endregion

                    JsonDataModel<OperatorModel> md = new JsonDataModel<OperatorModel>();
                    md.total = total.ToString();
                    md.rows = StationList;
                    jsonStr = JSONTools.ScriptSerialize<JsonDataModel<OperatorModel>>(md);
                }
                return jsonStr;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return "";
            }


        }
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="md">生产线模型对象</param>
        /// <returns></returns>
        public bool AddInfo(tb_Operator md)
        {
            try
            {
                var list = db.SearchInfoByKey("OperatorNo", md.OperatorNo);//判断是否有重复数据
                if (list != null)
                {
                    if (list.Where(p => p.OperatorID != md.OperatorID).Count() > 0)
                    {
                        return false;
                    }

                }

                return db.AddInfo(md);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }

        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="md">生产线模型对象</param>
        /// <returns></returns>
        public bool UpdateInfo(tb_Operator md)
        {
            try
            {
                var list = db.SearchAllInfo().Where(p => p.OperatorNo == md.OperatorNo && p.OperatorID != md.OperatorID).ToList();//判断是否有重复数据
                if (list.Count > 0)
                {
                    return false;
                }

                //初始化要更新的字段
                string[] proNames = new string[5];
                proNames[0] = "StationID";
                proNames[1] = "OperatorNo";
                proNames[2] = "OperatorName";
                proNames[3] = "OperatorPsw";
                proNames[4] = "Des";

                //必填字段初始化,如果不需要更新必填字段则设置为空即可,时间类型无需初始化
                //如果没有初始化必填字段,更新会报错
                //md.Des = "";

                return db.UpdateInfo(md, proNames);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }
        /// 删除生产线信息
        public bool DelInfo(tb_Operator md)
        {
            try
            {
                return db.DelInfo(md);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }
    }
}