using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLL
{
    /// 
    /// 热处理设备
    /// 
    public static class HotProess_TrustService
    {
        /// 
        /// 根据主键获取热处理
        /// 
        /// 
        /// 
        public static Model.HJGL_HotProess_Trust GetHotProessTrustById(string hotProessTrustId)
        {
            return Funs.DB.HJGL_HotProess_Trust.FirstOrDefault(e => e.HotProessTrustId == hotProessTrustId);
        }
        /// 
        /// 添加热处理
        /// 
        /// 
        public static void AddHotProessTrust(Model.HJGL_HotProess_Trust hotProessTrust)
        {
            Model.SGGLDB db = Funs.DB;
            Model.HJGL_HotProess_Trust newHotProessTrust = new Model.HJGL_HotProess_Trust();
            newHotProessTrust.HotProessTrustId = hotProessTrust.HotProessTrustId;
            newHotProessTrust.HotProessTrustNo = hotProessTrust.HotProessTrustNo;
            newHotProessTrust.ProessDate = hotProessTrust.ProessDate;
            newHotProessTrust.ProjectId = hotProessTrust.ProjectId;
            newHotProessTrust.UnitWorkId = hotProessTrust.UnitWorkId;
            newHotProessTrust.UnitId = hotProessTrust.UnitId;
            newHotProessTrust.Tabler = hotProessTrust.Tabler;
            newHotProessTrust.Remark = hotProessTrust.Remark;
            newHotProessTrust.ProessMethod = hotProessTrust.ProessMethod;
            newHotProessTrust.ProessEquipment = hotProessTrust.ProessEquipment;
            db.HJGL_HotProess_Trust.InsertOnSubmit(newHotProessTrust);
            db.SubmitChanges();
        }
        /// 
        /// 修改热处理
        /// 
        /// 
        public static void UpdateHotProessTrust(Model.HJGL_HotProess_Trust hotProessTrust)
        {
            Model.SGGLDB db = Funs.DB;
            Model.HJGL_HotProess_Trust newHotProessTrust = db.HJGL_HotProess_Trust.FirstOrDefault(e => e.HotProessTrustId == hotProessTrust.HotProessTrustId);
            if (newHotProessTrust != null)
            {
                newHotProessTrust.HotProessTrustNo = hotProessTrust.HotProessTrustNo;
                newHotProessTrust.ProessDate = hotProessTrust.ProessDate;
                newHotProessTrust.UnitWorkId = hotProessTrust.UnitWorkId;
                newHotProessTrust.ProjectId = hotProessTrust.ProjectId;
                newHotProessTrust.UnitId = hotProessTrust.UnitId;
                newHotProessTrust.Tabler = hotProessTrust.Tabler;
                newHotProessTrust.Remark = hotProessTrust.Remark;
                newHotProessTrust.ProessMethod = hotProessTrust.ProessMethod;
                newHotProessTrust.ProessEquipment = hotProessTrust.ProessEquipment;
                newHotProessTrust.ReportNo = hotProessTrust.ReportNo;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除热处理
        /// 
        /// 
        public static void DeleteHotProessTrustById(string hotProessTrustId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.HJGL_HotProess_Trust hotProessTrust = db.HJGL_HotProess_Trust.FirstOrDefault(e => e.HotProessTrustId == hotProessTrustId);
            if (hotProessTrust != null)
            {
                db.HJGL_HotProess_Trust.DeleteOnSubmit(hotProessTrust);
                db.SubmitChanges();
            }
        }
        /// 
        /// 热处理委托单编号是否存在
        /// 
        /// 
        /// 
        /// 
        public static bool IsExistTrustCode(string hotProessTrustNo, string hotProessTrustId, string projectId)
        {
            var q = Funs.DB.HJGL_HotProess_Trust.FirstOrDefault(x => x.HotProessTrustNo == hotProessTrustNo && x.ProjectId == projectId && x.HotProessTrustId != hotProessTrustId);
            if (q != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 查找后返回集合增加到列表集团中
        /// 
        /// 
        /// 
        public static List GetHotProessTrustAddItem(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_HotProess_TrustItem newItem = new Model.View_HJGL_HotProess_TrustItem();
                    newItem.HotProessTrustItemId = SQLHelper.GetNewID(typeof(Model.View_HJGL_HotProess_TrustItem));
                    newItem.WeldJointId = jotInfo.WeldJointId;
                    newItem.WeldJointCode = jotInfo.WeldJointCode;
                    newItem.PipelineCode = jotInfo.PipelineCode;
                    newItem.Specification = jotInfo.Specification;
                    newItem.MaterialCode = jotInfo.Material1Code;
                    returnViewMatch.Add(newItem);
                }
            }
            return returnViewMatch;
        }
        /// 
        /// 根据项目状态获取热处理委托明细信息
        /// 
        /// 
        /// 
        public static List GetHotProessTrustItem(string projectId, string hotProessTrustId)
        {
            List returnViewMatch = (from x in Funs.DB.View_HJGL_HotProess_TrustItem
                                                                         where x.ProjectId == projectId && x.HotProessTrustId == hotProessTrustId
                                                                    select x).ToList();
            return returnViewMatch;
        }
        /// 
        /// 查找需要热处理的焊口信息
        /// 
        /// 
        /// 
        /// 
        /// 
        public static List GetHotProessTrustFind(string projectId, string hotProessTrustId, string pipelineId)
        {
            var weldJoints = (from x in Funs.DB.View_HJGL_WeldJoint select x).ToList();
            List returnViewMatch = new List();
            var hotProessTrustItems = from x in Funs.DB.HJGL_HotProess_TrustItem
                                      join z in Funs.DB.HJGL_WeldJoint
                                      on x.WeldJointId equals z.WeldJointId
                                      where z.ProjectId == projectId && z.PipelineId == pipelineId
                                      select x;
            if (weldJoints.Count() > 0)
            {
                foreach (var item in weldJoints)
                {
                    var jothotProessTrustItems = from x in hotProessTrustItems where x.WeldJointId == item.WeldJointId select x;
                    bool isShow = false;
                    if (item.IsHotProess == true)//需要热处理
                    {
                        if (jothotProessTrustItems.Count() == 0) //未进行过热处理
                        {
                            isShow = true;
                        }
                        if (isShow)
                        {
                            Model.View_HJGL_HotProessTrustItemSearch newItem = new Model.View_HJGL_HotProessTrustItemSearch();
                            newItem.WeldJointId = item.WeldJointId;
                            newItem.PipelineId = item.PipelineId;
                            newItem.PipelineCode = item.PipelineCode;
                            newItem.WeldJointCode = item.WeldJointCode;
                            newItem.Specification = item.Specification;
                            newItem.MaterialCode = item.Material1Code;
                            newItem.WeldingDailyId = item.WeldingDailyId;
                            var weldingDaily = BLL.WeldingDailyService.GetPipeline_WeldingDailyByWeldingDailyId(item.WeldingDailyId);
                            if (weldingDaily != null)
                            {
                                newItem.WeldingDate = weldingDaily.WeldingDate;
                            }
                            returnViewMatch.Add(newItem);
                        }
                    }
                }
            }
            return returnViewMatch;
        }
    }
}