using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLL
{
    /// 
    /// 硬度委托
    /// 
    public static class Hard_TrustService
    {
        /// 
        /// 根据主键获取硬度委托
        /// 
        /// 
        /// 
        public static Model.HJGL_Hard_Trust GetHardTrustById(string hardTrustID)
        {
            return Funs.DB.HJGL_Hard_Trust.FirstOrDefault(e => e.HardTrustID == hardTrustID);
        }
        /// 
        /// 添加硬度委托
        /// 
        /// 
        public static void AddHardTrust(Model.HJGL_Hard_Trust hardTrust)
        {
            Model.SGGLDB db = Funs.DB;
            Model.HJGL_Hard_Trust newHardTrust = new Model.HJGL_Hard_Trust();
            newHardTrust.HardTrustID = hardTrust.HardTrustID;
            newHardTrust.HardTrustNo = hardTrust.HardTrustNo;
            newHardTrust.HardTrustUnit = hardTrust.HardTrustUnit;
            newHardTrust.HardTrustDate = hardTrust.HardTrustDate;
            newHardTrust.AuditMan = hardTrust.AuditMan;
            newHardTrust.AuditDate = hardTrust.AuditDate;
            newHardTrust.HardnessRate = hardTrust.HardnessRate;
            newHardTrust.HardnessMethod = hardTrust.HardnessMethod;
            newHardTrust.CheckUnit = hardTrust.CheckUnit;
            newHardTrust.ProjectId = hardTrust.ProjectId;
            newHardTrust.UnitWorkId = hardTrust.UnitWorkId;
            newHardTrust.DetectionTime = hardTrust.DetectionTime;
            newHardTrust.Sendee = hardTrust.Sendee;
            newHardTrust.Standards = hardTrust.Standards;
            newHardTrust.InspectionNum = hardTrust.InspectionNum;
            newHardTrust.CheckNum = hardTrust.CheckNum;
            newHardTrust.TestWeldNum = hardTrust.TestWeldNum;
            newHardTrust.HardTrustMan = hardTrust.HardTrustMan;
            newHardTrust.CheckName = hardTrust.CheckName;
            newHardTrust.AcceptStandard = hardTrust.AcceptStandard;
            newHardTrust.EquipmentModel = hardTrust.EquipmentModel;
            db.HJGL_Hard_Trust.InsertOnSubmit(newHardTrust);
            db.SubmitChanges();
        }
        /// 
        /// 修改硬度委托
        /// 
        /// 
        public static void UpdateHardTrust(Model.HJGL_Hard_Trust hardTrust)
        {
            Model.SGGLDB db = Funs.DB;
            Model.HJGL_Hard_Trust newHardTrust = db.HJGL_Hard_Trust.FirstOrDefault(e => e.HardTrustID == hardTrust.HardTrustID);
            if (newHardTrust != null)
            {
                newHardTrust.HardTrustNo = hardTrust.HardTrustNo;
                newHardTrust.HardTrustUnit = hardTrust.HardTrustUnit;
                newHardTrust.HardTrustDate = hardTrust.HardTrustDate;
                newHardTrust.AuditMan = hardTrust.AuditMan;
                newHardTrust.AuditDate = hardTrust.AuditDate;
                newHardTrust.HardnessRate = hardTrust.HardnessRate;
                newHardTrust.HardnessMethod = hardTrust.HardnessMethod;
                newHardTrust.CheckUnit = hardTrust.CheckUnit;
                newHardTrust.ProjectId = hardTrust.ProjectId;
                newHardTrust.UnitWorkId = hardTrust.UnitWorkId;
                newHardTrust.DetectionTime = hardTrust.DetectionTime;
                newHardTrust.Sendee = hardTrust.Sendee;
                newHardTrust.Standards = hardTrust.Standards;
                newHardTrust.InspectionNum = hardTrust.InspectionNum;
                newHardTrust.CheckNum = hardTrust.CheckNum;
                newHardTrust.TestWeldNum = hardTrust.TestWeldNum;
                newHardTrust.HardTrustMan = hardTrust.HardTrustMan;
                newHardTrust.CheckName = hardTrust.CheckName;
                newHardTrust.AcceptStandard = hardTrust.AcceptStandard;
                newHardTrust.EquipmentModel = hardTrust.EquipmentModel;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除硬度委托
        /// 
        /// 
        public static void DeleteHardTrustById(string hardTrustID)
        {
            Model.SGGLDB db = Funs.DB;
            Model.HJGL_Hard_Trust hardTrust = db.HJGL_Hard_Trust.FirstOrDefault(e => e.HardTrustID == hardTrustID);
            if (hardTrust != null)
            {
                db.HJGL_Hard_Trust.DeleteOnSubmit(hardTrust);
                db.SubmitChanges();
            }
        }
        /// 
        /// 硬度委托委托单编号是否存在
        /// 
        /// 
        /// 
        /// 
        public static bool IsExistTrustCode(string hardTrustNo, string hardTrustID, string projectId)
        {
            var q = Funs.DB.HJGL_Hard_Trust.FirstOrDefault(x => x.HardTrustNo == hardTrustNo && x.ProjectId == projectId && x.HardTrustID != hardTrustID);
            if (q != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 查找后返回集合增加到列表集团中
        /// 
        /// 
        /// 
        public static List GetHardTrustAddItem(string hdItemsString)
        {
            var jointInfos = from x in Funs.DB.View_HJGL_WeldJoint select x;
            List returnViewMatch = new List();
            if (!string.IsNullOrEmpty(hdItemsString))
            {
                List jotIds = Funs.GetStrListByStr(hdItemsString, '|');
                foreach (var jotItem in jotIds)
                {
                    string[] strs = jotItem.Split(',');
                    var jotInfo = jointInfos.FirstOrDefault(x => x.WeldJointId == strs[0]);
                    Model.View_HJGL_Hard_TrustItem newItem = new Model.View_HJGL_Hard_TrustItem();
                    newItem.HardTrustItemID = SQLHelper.GetNewID(typeof(Model.View_HJGL_Hard_TrustItem));
                    newItem.WeldJointId = jotInfo.WeldJointId;
                    newItem.HotProessTrustItemId = strs[1];
                    newItem.PipelineCode = jotInfo.PipelineCode;
                    newItem.WeldJointCode = jotInfo.WeldJointCode;
                    newItem.WelderCode = jotInfo.WelderCode;
                    newItem.Specification = jotInfo.Specification;
                    newItem.MaterialCode = jotInfo.MaterialCode;
                    newItem.SingleNumber = jotInfo.SingleNumber;
                    newItem.Remark = jotInfo.Remark;
                    returnViewMatch.Add(newItem);
                }
            }
            return returnViewMatch;
        }
        /// 
        /// 根据项目状态获取硬度委托委托明细信息
        /// 
        /// 
        /// 
        public static List GetHardTrustItem(string hardTrustID)
        {
            List returnViewMatch = (from x in Funs.DB.View_HJGL_Hard_TrustItem
                                                                    where x.HardTrustID == hardTrustID
                                                               select x).ToList();
            return returnViewMatch;
        }
        /// 
        /// 查找需要硬度委托的焊口信息
        /// 
        /// 
        /// 
        /// 
        /// 
        public static List GetHardTrustFind(string projectId, string hardTrustID, string pipelineId)
        {
            ///根据已经热处理且需要硬度检测且未进行硬度检测的焊口获取焊口视图集合
            var weldJoints = (from x in Funs.DB.HJGL_HotProess_TrustItem
                              join y in Funs.DB.View_HJGL_WeldJoint
                              on x.WeldJointId equals y.WeldJointId
                              where y.PipelineId == pipelineId && x.IsHardness == true && x.IsTrust == null
                              select new {
                                  WeldJointId = y.WeldJointId,
                                  PipelineCode = y.PipelineCode,
                                  WeldJointCode = y.WeldJointCode,
                                  WelderCode = y.WelderCode,
                                  Specification = y.Specification,
                                  MaterialCode = y.MaterialCode,
                                  SingleNumber = y.SingleNumber,
                                  Remark=y.Remark,
                                  HotProessTrustItemId = x.HotProessTrustItemId,
                              }).Distinct().ToList();
            List returnViewMatch = new List();
            foreach (var item in weldJoints)
            {
                Model.View_HJGL_Hard_TrustItem newItem = new Model.View_HJGL_Hard_TrustItem();
                newItem.WeldJointId = item.WeldJointId;
                newItem.PipelineCode = item.PipelineCode;
                newItem.WeldJointCode = item.WeldJointCode;
                newItem.WelderCode = item.WelderCode;
                newItem.Specification = item.Specification;
                newItem.MaterialCode = item.MaterialCode;
                newItem.SingleNumber = item.SingleNumber;
                newItem.Remark = item.Remark;
                newItem.HotProessTrustItemId = item.HotProessTrustItemId;
                returnViewMatch.Add(newItem);
            }
            return returnViewMatch;
        }
    }
}