using MESClassLibrary.BLL.Log;
using MESClassLibrary.DAL.Keep;
using MESClassLibrary.EFModel;
using MESClassLibrary.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace MESClassLibrary.BLL.Keep
{
    public class KeepRecordBLL
    {
        KeepRecordDAL dal = new KeepRecordDAL();
        BBMPTEntities ef = new BBMPTEntities();
        BasicBLL<tb_KeepRecord> db = new BasicBLL<tb_KeepRecord>();

        public string GetDate(string VersionID, string KeepTime, string DeviceID, string KeepLevelID)
        {
            try
            {
                string str = "";
                string CheckUserID = "";
                string ConfirmUserID = "";
                string ErrMes = "";
                BasicBLL<tb_KeepItemsVersion> db = new BasicBLL<tb_KeepItemsVersion>();
                string jsonStr = "[]";
                List<tb_KeepItemsVersion> list = null;

                list = db.SearchInfoByKey("IsUseing", 1).Where(p => p.VersionID.Equals(VersionID)).ToList();
                if (list.Count > 0)
                {
                    BasicBLL<tb_KeepItems> CheckItem_db = new BasicBLL<tb_KeepItems>();
                    var CheckItemList = CheckItem_db.SearchAllInfo().ToList();

                    BasicBLL<tb_KeepLevel> KeepLevel_db = new BasicBLL<tb_KeepLevel>();
                    var KeepLevelList = KeepLevel_db.SearchAllInfo().ToList();

                    string[] arr_id = list.Select(s => s.ID).ToArray();
                    BasicBLL<tb_KeepRecord> CommonlyInspectionRecord_db = new BasicBLL<tb_KeepRecord>();
                    var _info = CommonlyInspectionRecord_db.SearchInfoByKey("CheckDate", Convert.ToDateTime(KeepTime)).Where(p => arr_id.Contains(p.KeepItemsVersionID)).ToList();
                    if (_info.Count > 0)
                    {
                        #region 修改
                        List<KeepItemsVersionModel> CommonlyInspectionList = new List<KeepItemsVersionModel>();
                        foreach (var item in list)
                        {
                            KeepItemsVersionModel dm = Tool.Mapper<KeepItemsVersionModel, tb_KeepItemsVersion>(item);
                            dm.KeepItems = CheckItemList.FirstOrDefault(p => p.ID.Equals(item.KeepItemsID)).KeepItems;
                            CommonlyInspectionList.Add(dm);
                        }
                        str = "<table border='1' width='900' cellspacing='0' cellpadding='2' id='KeepItemsTable' name='1'>";
                        str += "<tr>";
                        str += "<td width='200'>保养内容</td>";
                        str += "<td width='200'>检查结果</td>";
                        str += "</tr>";

                        for (int j = 0; j < CommonlyInspectionList.Count; j++)
                        {
                            str += "<tr>";
                            str += "<td>" + CommonlyInspectionList[j].KeepItems + "</td>";

                            var R_info = _info.FirstOrDefault(p => p.KeepItemsVersionID == CommonlyInspectionList[j].ID);
                            CheckUserID = R_info.CheckUser;
                            ConfirmUserID = R_info.ConfirmUser;
                            //ErrMes = R_info.ErrMes;
                            switch (R_info.Result)
                            {
                                case "0":
                                    str += "<td><select class=\"easyui-combobox\" name='" + R_info.ID + "'><option value='1'>良好</option><option value='0' selected = 'selected'>故障</option></select></td>";
                                    break;
                                case "1":
                                    str += "<td><select class=\"easyui-combobox\" name='" + R_info.ID + "'><option value='1' selected = 'selected'>良好</option><option value='0'>故障</option></select></td>";
                                    break;
                            }
                            str += "</tr>";
                        }
                        //str += "<tr>";
                        //str += "<td width='200'>异常情况描述</td>";
                        //str += "<td><input class='easyui-textbox' id='ErrMes' value='" + ErrMes + "'/></td>";
                        //str += "</tr>";
                        str += "</table>";
                        str += "<input id='CheckUserID' type='text' hidden='hidden' value='" + CheckUserID + "'/>";
                        str += "<input id='ConfirmUserID' type='text' hidden='hidden' value='" + ConfirmUserID + "'/>";
                        str += "<input id='KeepItemsFlag' type='text' hidden='hidden' value='1'/>";

                        #endregion
                    }
                    else
                    {
                        #region 新增
                        List<KeepItemsVersionModel> CommonlyInspectionList = new List<KeepItemsVersionModel>();
                        foreach (var item in list)
                        {
                            KeepItemsVersionModel dm = Tool.Mapper<KeepItemsVersionModel, tb_KeepItemsVersion>(item);
                            dm.KeepItems = CheckItemList.FirstOrDefault(p => p.ID.Equals(item.KeepItemsID)).KeepItems;
                            CommonlyInspectionList.Add(dm);
                        }
                        str = "<table border='1' width='900' cellspacing='0' cellpadding='2' id='KeepItemsTable'  name='0'>";
                        str += "<tr>";
                        str += "<td width='200'>保养内容</td>";
                        str += "<td width='200'>检查结果</td>";
                        str += "</tr>";
                        for (int j = 0; j < CommonlyInspectionList.Count; j++)
                        {
                            str += "<tr>";
                            str += "<td>" + CommonlyInspectionList[j].KeepItems + "</td>";
                            str += "<td><select class=\"easyui-combobox\" name='" + CommonlyInspectionList[j].ID + "'>" +
                                "<option value='1'>良好</option>" +
                                "<option value='0'>故障</option>" +
                                "</select>" +
                                "</td>";
                            str += "</tr>";
                        }
                        //str += "<tr>";
                        //str += "<td width='200'>异常情况描述</td>";
                        //str += "<td><input class='easyui-textbox' id='ErrMes' value=''/></td>";
                        //str += "</tr>";
                        str += "</table>";
                        str += "<input id='KeepItemsFlag' type='text' hidden='hidden' value='0'/>";
                        #endregion
                    }
                }
                return str;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return "";
            }

        }

        public bool AddInfo(string CommonlyInspection, string CheckUserID, string ConfirmUserID, string UserID, string CheckDate, string KeepPlanID)
        {
            try
            {
                BasicBLL<tb_KeepRecord> Recorddb = new BasicBLL<tb_KeepRecord>();
                DateTime dtime = DateTime.Now;
                DateTime checktime = Convert.ToDateTime(CheckDate);
                string[] arr = CommonlyInspection.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < arr.Length - 1; i++)
                {
                    int index = arr[i].IndexOf(":");
                    tb_KeepRecord md = new tb_KeepRecord();
                    md.ID = Guid.NewGuid().ToString();
                    md.KeepPlanID = KeepPlanID;
                    md.KeepItemsVersionID = arr[i].Substring(0, index);
                    md.Result = arr[i].Substring(index + 1, arr[i].Length - index - 1);
                    md.CheckUser = CheckUserID;
                    md.ConfirmUser = CheckUserID;
                    //md.CreateUserID = UserID;
                    md.CheckDate = checktime;
                    //md.CheckType = CheckType;
                    //md.ErrMes = ErrMes;
                    md.ConfirmUserDate = dtime;
                    md.CreateTime = dtime;
                    //md.IsUseing = 1;
                    Recorddb.AddInfo(md);
                }
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }

        }
        public bool UpdateInfo(string CommonlyInspection, string CheckUserID, string ConfirmUserID, string UserID, string CheckDate, string KeepPlanID)
        {
            try
            {
                string[] proNames = new string[6];
                proNames[0] = "Result";
                proNames[1] = "CheckUser";
                proNames[2] = "CheckDate";
                proNames[3] = "ConfirmUser";
                proNames[4] = "ConfirmUserDate";
                //proNames[5] = "UpdateUserID";
                proNames[5] = "UpdateTime";
                //proNames[7] = "CheckType";
                //proNames[8] = "ErrMes";

                BasicBLL<tb_KeepRecord> Recorddb = new BasicBLL<tb_KeepRecord>();
                DateTime dtime = DateTime.Now;
                DateTime checktime = Convert.ToDateTime(CheckDate);
                string[] arr = CommonlyInspection.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < arr.Length - 3; i++)
                {
                    int index = arr[i].IndexOf(":");
                    tb_KeepRecord md = new tb_KeepRecord();
                    md.ID = arr[i].Substring(0, index);
                    md.Result = arr[i].Substring(index + 1, arr[i].Length - index - 1);
                    md.CheckUser = CheckUserID;
                    md.CheckDate = checktime;
                    //md.CheckType = CheckType;
                    //md.ErrMes = ErrMes;
                    md.ConfirmUser = ConfirmUserID;
                    md.ConfirmUserDate = dtime;
                    //md.UpdateUserID = UserID;
                    md.UpdateTime = dtime;
                    //必填字段初始化,如果不需要更新必填字段则设置为空即可,时间类型无需初始化
                    //如果没有初始化必填字段,更新会报错
                    //md.CreateUserID = "";
                    md.KeepItemsVersionID = "";
                    Recorddb.UpdateInfo(md, proNames);
                }
                return true;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }

        }


        public string GetReportDate(string PlanID)
        {
            try
            {
                string str = "";
                str = "<table border='1' cellspacing='0' cellpadding='5' id='InspectionContentTable' name='1' width='100%'>";
                str += "<tr>";
                str += "<td width='50'>序号</td>";
                str += "<td width='250'>设备名称</td>";
                str += "<td width='400'>保养内容</td>";
                str += "<td width='100'>结果</td>";
                str += "</tr>";
                DataTable dt = dal.SearhByKeepPlanID(PlanID);
                if (dt != null && dt.Rows.Count > 0)
                {
                    IList<KeepRecordModel> list = Tool.ConvertTo<KeepRecordModel>(dt);
                    List<string> deviceNameList = list.GroupBy(p => p.DeviceName).Select(p => p.Key).ToList();
                    int n = 1;
                    foreach (var item in deviceNameList)
                    {
                        List<KeepRecordModel> recordList = list.Where(p => p.DeviceName != null && p.DeviceName.Equals(item)).ToList();

                        List<string> VersionList = recordList.GroupBy(p => p.Version).Select(p => p.Key).ToList();
                        if (VersionList.Count > 1)
                        {
                            string version = VersionList.OrderByDescending(p => p).FirstOrDefault();
                            recordList = recordList.Where(p => p.KeepLevel != null && p.Version.Equals(version)).ToList();

                        }

                        int k = 1;
                        foreach (var itemRecord in recordList)
                        {
                            if (k == 1)
                            {
                                str += "<tr>";
                                str += "<td width='50' rowspan='" + recordList.Count + "'>" + n.ToString() + "</td>";
                                str += "<td width='250' rowspan='" + recordList.Count + "'>" + item + "</td>";
                                str += "<td width='400'>" + itemRecord.KeepItems + "</td>";
                                string r = itemRecord.result == "1" ? "X" : "√";
                                str += "<td width='100'>" + r + "</td>";
                                str += "</tr>";
                            }
                            else
                            {
                                str += "<tr>";
                                str += "<td width='400'>" + itemRecord.KeepItems + "</td>";
                                string r = itemRecord.result == "1" ? "X" : "√";
                                str += "<td width='100'>" + r + "</td>";
                                str += "</tr>";
                            }

                            k++;
                        }
                        n++;
                    }


                }
                str += "</table>";

                return str;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return "";
            }

        }

    }
}