using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
namespace BLL
{
    public class WeldReportService
    {
        public static Model.SGGLDB db = Funs.DB;
        /// 
        /// 根据检查编号查询焊接日报信息
        /// 
        /// 检查编号
        /// 焊接日报信息
        public static Model.BO_WeldReportMain GetWeldReportByDReportID(string dReportID)
        {
            return Funs.DB.BO_WeldReportMain.FirstOrDefault(x => x.DReportID == dReportID);
        }
        /// 
        /// 根据焊接日报主键获取焊接日报信息
        /// 
        /// 焊接日报编号
        /// 焊接日报信息
        public static bool IsExistDailyReportNO(string weldReportNo, string dReportID, string projectId)
        {
            var q = Funs.DB.BO_WeldReportMain.FirstOrDefault(x => x.JOT_DailyReportNo == weldReportNo && x.ProjectId == projectId && x.DReportID != dReportID);
            if (q != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 根据焊接日报主键获取焊接日报信息
        /// 
        /// 焊接日报编号
        /// 焊接日报信息
        public static bool IsExistDailyReportNO(string weldReportNo)
        {
            var q = from x in Funs.DB.BO_WeldReportMain where x.JOT_DailyReportNo == weldReportNo select x;
            if (q.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 增加焊接日报信息
        /// 
        /// 焊接日报实体
        public static void AddWeldReport(Model.BO_WeldReportMain weldReport)
        {
            using (var db = new Model.SGGLDB(Funs.ConnString))
            {
                Model.BO_WeldReportMain newWeldReport = new Model.BO_WeldReportMain();
                newWeldReport.DReportID = weldReport.DReportID;
                newWeldReport.ProjectId = weldReport.ProjectId;
                newWeldReport.InstallationId = weldReport.InstallationId;
                newWeldReport.UnitId = weldReport.UnitId;
                newWeldReport.JOT_WeldDate = weldReport.JOT_WeldDate;
                newWeldReport.JOT_DailyReportNo = weldReport.JOT_DailyReportNo;
                newWeldReport.CHT_Tabler = weldReport.CHT_Tabler;
                newWeldReport.CHT_TableDate = weldReport.CHT_TableDate;
                newWeldReport.JOT_Remark = weldReport.JOT_Remark;
                db.BO_WeldReportMain.InsertOnSubmit(newWeldReport);
                db.SubmitChanges();
            }
        }
        /// 
        /// 修改焊接日报信息
        /// 
        /// 焊接日报实体
        public static void UpdateWeldReport(Model.BO_WeldReportMain weldReport)
        {
            Model.SGGLDB db = Funs.DB;
            Model.BO_WeldReportMain newWeldReport = db.BO_WeldReportMain.First(e => e.DReportID == weldReport.DReportID);
            newWeldReport.UnitId = weldReport.UnitId;
            newWeldReport.InstallationId = weldReport.InstallationId;
            newWeldReport.JOT_WeldDate = weldReport.JOT_WeldDate;
            newWeldReport.JOT_DailyReportNo = weldReport.JOT_DailyReportNo;
            newWeldReport.CHT_Tabler = weldReport.CHT_Tabler;
            newWeldReport.CHT_TableDate = weldReport.CHT_TableDate;
            newWeldReport.JOT_Remark = weldReport.JOT_Remark;
            db.SubmitChanges();
        }
        /// 
        /// 根据焊接日报主键删除一个焊接日报信息
        /// 
        /// 焊接日报主键
        public static void DeleteWeldReportByDReportID(string dReportID)
        {
            Model.SGGLDB db = Funs.DB;
            Model.BO_WeldReportMain weldReport = db.BO_WeldReportMain.First(e => e.DReportID == dReportID);
            db.BO_WeldReportMain.DeleteOnSubmit(weldReport);
            db.SubmitChanges();
        }
        /// 
        /// 根据日报Id获取日报明细焊口信息
        /// 
        /// 日报id
        /// 
        public static List GetWeldReportItem(string dReportID)
        {
            return (from x in Funs.DB.View_JointInfo where x.DReportID == dReportID select x).ToList();
        }
        public static List GetDistinctWeldReportItem(string dReportID)
        {
           // return (from x in Funs.DB.View_JointInfo where x.DReportID == dReportID select x).ToList();
            List res = new List();
            var q = (from x in Funs.DB.PW_JointInfo where x.DReportID == dReportID orderby x.ISO_ID, x.JOT_JointNo select x).ToList();
            foreach (var item in q)
            {
                Model.View_JointInfo jot = new Model.View_JointInfo();
                jot.ISO_ID = item.ISO_ID;
                jot.JOT_ID = item.JOT_ID;
                var iso = Funs.DB.PW_IsoInfo.FirstOrDefault(x => x.ISO_ID == item.ISO_ID);
                if (iso != null)
                {
                    var detectionRate = Funs.DB.Base_DetectionRate.FirstOrDefault(x => x.DetectionRateId == iso.DetectionRateId);
                    if (detectionRate != null)
                    {
                        jot.NDTR_Name  = detectionRate.DetectionRateValue;
                    }
                   
                    jot.ISO_IsoNo = iso.ISO_IsoNo;
                    var area = Funs.DB.ProjectData_WorkArea.FirstOrDefault(x => x.WorkAreaId == iso.WorkAreaId);
                    if (area != null)
                    {
                        jot.WorkAreaCode = area.WorkAreaCode;
                    }
                }
                jot.JOT_JointNo = item.JOT_JointNo;
                jot.JOTY_ID = item.JOTY_ID;
                jot.WLO_Code = item.WLO_Code;
                jot.JOT_JointAttribute = item.JOT_JointAttribute;
                jot.JOT_Size = item.JOT_Size;
                jot.JOT_JointDesc = item.JOT_JointDesc;
                jot.JOT_Sch = item.JOT_Sch;
                jot.WME_ID = item.WME_ID;
                jot.JOT_HeartNo1 = item.JOT_HeartNo1;
                jot.JOT_HeartNo2 = item.JOT_HeartNo2;
                jot.JOT_BelongPipe = item.JOT_BelongPipe;
                jot.JOT_PrepareTemp = item.JOT_PrepareTemp;
                jot.IS_Proess = item.IS_Proess;
                jot.JOT_HotRpt = item.JOT_HotRpt;
                jot.JOT_Location = item.JOT_Location;
                jot.JOT_Dia = item.JOT_Dia;
                jot.JOT_DoneDin = item.JOT_DoneDin;
                jot.JOT_Electricity = item.JOT_Electricity;
                jot.JOT_Voltage = item.JOT_Voltage;
                jot.WeldingSpeed = item.WeldingSpeed;
                jot.ActualPrepareTemp = item.ActualPrepareTemp;
                jot.JOT_CellTemp = item.JOT_CellTemp;
                jot.JOT_LastTemp = item.JOT_LastTemp;
                var cellWelder = Funs.DB.BS_Welder.FirstOrDefault(x => x.WED_ID == item.JOT_CellWelder);
                if (cellWelder != null)
                {
                    jot.WED_Code1 = cellWelder.WED_Code;
                }
                var floorWelder = Funs.DB.BS_Welder.FirstOrDefault(x => x.WED_ID == item.JOT_FloorWelder);
                if (floorWelder != null)
                {
                    jot.WED_Code2 = floorWelder.WED_Code;
                }
                jot.DetectionTypeId = item.DetectionTypeId;
                jot.DetectionRateId = item.DetectionRateId;
                res.Add(jot);
            }
            return res;// (from x in Funs.DB.View_JointInfo where x.PW_PointID == PW_PointID && x.JOT_JointStatus != "104" orderby x.WorkAreaCode, x.ISO_IsoNo, x.JOT_JointNo select x).ToList();
        }
        /// 
        /// 根据单位Id获取焊接日报数
        /// 
        /// 
        /// 
        public static int GetWeldReportByUnitId(string unitId)
        {
            var q = (from x in Funs.DB.BO_WeldReportMain where x.UnitId == unitId select x).ToList();
            return q.Count();
        }
        /// 
        /// 根据装置Id获取焊接日报数
        /// 
        /// 
        /// 
        public static int GetWeldReportByInstallationId(string installationId)
        {
            var q = (from x in Funs.DB.BO_WeldReportMain where x.InstallationId == installationId select x).ToList();
            return q.Count();
        }
        /// 
        /// 获取日报
        /// 
        /// 
        public static ListItem[] GetWeldReportList()
        {
            var q = (from x in BLL.Funs.DB.BO_WeldReportMain orderby x.JOT_DailyReportNo select x).ToList();
            ListItem[] item = new ListItem[q.Count()];
            for (int i = 0; i < q.Count(); i++)
            {
                item[i] = new ListItem(q[i].JOT_DailyReportNo ?? "", q[i].DReportID.ToString());
            }
            return item;
        }
        /// 
        /// 查找后返回集合增加到列表集团中
        /// 
        /// 
        /// 
        public static List GetWeldReportAddItem(string hdItemsString)
        {
            List returnViewMatch = new List(); //= getWeldReportItem;
            if (!string.IsNullOrEmpty(hdItemsString))
            {
                List list = Funs.GetStrListByStr(hdItemsString, '#');
                if (list.Count() == 2)
                {
                    string CellWelderCode = string.Empty;
                    string FloorWelderCode = string.Empty;
                    string welderLists = list[0];
                    List welderIds = Funs.GetStrListByStr(welderLists, '|');
                    if (welderIds.Count() == 2)
                    {
                        var welderCell = BLL.WelderService.GetWelderById(welderIds[0]);
                        if (welderCell != null)
                        {
                            CellWelderCode = welderCell.WED_Code;
                        }
                        var welderFloor = BLL.WelderService.GetWelderById(welderIds[1]);
                        if (welderFloor != null)
                        {
                            FloorWelderCode = welderFloor.WED_Code;
                        }
                    }
                    string jotIdLists = list[1];
                    List jotIds = Funs.GetStrListByStr(jotIdLists, '|');
                    foreach (var jotItem in jotIds)
                    {
                        //if (returnViewMatch.FirstOrDefault(x => x.JOT_ID == jotItem) == null)
                        //{
                        var jotInfo = BLL.PW_JointInfoService.GetJointInfoByJotID(jotItem);
                        if (jotInfo != null)
                        {
                            Model.View_JointInfo newWeldReportItem = new Model.View_JointInfo();
                            newWeldReportItem.JOT_ID = jotInfo.JOT_ID;
                            newWeldReportItem.JOT_JointNo = jotInfo.JOT_JointNo;
                            var isoInfo = BLL.PW_IsoInfoService.GetIsoInfoByIsoInfoId(jotInfo.ISO_ID);
                            if (isoInfo != null)
                            {
                                newWeldReportItem.ISO_IsoNo = isoInfo.ISO_IsoNo;
                                newWeldReportItem.ISO_ID = isoInfo.ISO_ID;
                                //Model.ProjectData_WorkArea workArea = BLL.WorkAreaService.getWorkAreaByWorkAreaId(isoInfo.WorkAreaId);
                                Model.ProjectData_WorkArea workArea = Funs.DB.ProjectData_WorkArea.Where(x=>x.WorkAreaId==isoInfo.WorkAreaId).FirstOrDefault();
                                if (workArea != null)
                                {
                                    newWeldReportItem.WorkAreaCode = workArea.WorkAreaCode;
                                }
                                Model.Base_DetectionRate rate = BLL.Base_DetectionRateService.GetDetectionRateByDetectionRateId(isoInfo.DetectionRateId);
                                if (rate != null)
                                {
                                    newWeldReportItem.NDTR_Name = rate.DetectionRateValue;
                                }
                                //var rate = BLL.HJGL_DetectionService.GetNDTRateByNDTRID(isoInfo.NDTR_ID);
                                //if (rate != null)
                                //{
                                //    newWeldReportItem.NDTR_Name = rate.NDTR_Name;
                                //}
                            }
                            newWeldReportItem.WED_Code1 = CellWelderCode;
                            newWeldReportItem.JOT_CellWelder = welderIds[0];
                            newWeldReportItem.WED_Code2 = FloorWelderCode;
                            newWeldReportItem.JOT_FloorWelder = welderIds[1];
                            // 如存在日报,默认还是原日报焊工
                            if (!string.IsNullOrEmpty(jotInfo.DReportID))
                            {
                                newWeldReportItem.JOT_CellWelder = jotInfo.JOT_CellWelder;
                                newWeldReportItem.JOT_FloorWelder = jotInfo.JOT_FloorWelder;
                                var welderCell = BLL.WelderService.GetWelderById(jotInfo.JOT_CellWelder);
                                if (welderCell != null)
                                {
                                    newWeldReportItem.WED_Code1 = welderCell.WED_Code;
                                }
                                var floorCell = BLL.WelderService.GetWelderById(jotInfo.JOT_FloorWelder);
                                if (floorCell != null)
                                {
                                    newWeldReportItem.WED_Code2 = floorCell.WED_Code;
                                }
                            }
                            if (jotInfo.WLO_Code == "F")
                            {
                                newWeldReportItem.WLO_Code = "安装";
                            }
                            else
                            {
                                newWeldReportItem.WLO_Code = "预制";
                            }
                            newWeldReportItem.JOT_JointAttribute = jotInfo.JOT_JointAttribute;
                            newWeldReportItem.JOT_Location = jotInfo.JOT_Location;
                            //var joty = BLL.Base_WeldTypeService.GetWeldTypeByWeldTypeId(jotInfo.JOTY_ID);
                            //if (jotInfo.JOT_JointAttribute == "固定")
                            //{
                            //    if (joty != null && (joty.JOTY_Group == "2" || joty.JOTY_Group == "3"))
                            //    {
                            //        newWeldReportItem.JOT_Location = "5F";
                            //    }
                            //    else
                            //    {
                            //        newWeldReportItem.JOT_Location = "5G";
                            //    }
                            //}
                            //else
                            //{
                            //    if (joty != null && (joty.JOTY_Group == "2" || joty.JOTY_Group == "3"))
                            //    {
                            //        newWeldReportItem.JOT_Location = "1F";
                            //    }
                            //    else
                            //    {
                            //        newWeldReportItem.JOT_Location = "1G";
                            //    }
                            //}
                            newWeldReportItem.JOT_Size = jotInfo.JOT_Size;
                            newWeldReportItem.JOT_DoneDin = jotInfo.JOT_Size;
                            newWeldReportItem.JOT_Electricity = jotInfo.JOT_Electricity;
                            newWeldReportItem.JOT_Voltage = jotInfo.JOT_Voltage;
                            newWeldReportItem.WeldingSpeed = jotInfo.WeldingSpeed;
                            newWeldReportItem.JOT_PrepareTemp = jotInfo.JOT_PrepareTemp;
                            newWeldReportItem.ActualPrepareTemp = jotInfo.ActualPrepareTemp;
                            newWeldReportItem.JOT_CellTemp = jotInfo.JOT_CellTemp;
                            newWeldReportItem.JOT_LastTemp = jotInfo.JOT_LastTemp;
                            returnViewMatch.Add(newWeldReportItem);
                            //}
                        }
                    }
                }
            }
            return returnViewMatch;
        }
    }
}