using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BLL
{
    /// 
    /// 点口主表
    /// 
    public static class Batch_PointBatchService
    {       
        /// 
        /// 根据主键获取点口主表
        /// 
        /// 
        /// 
        public static Model.Batch_PointBatch GetPointBatchById(string pointBatchId)
        {
            return Funs.DB.Batch_PointBatch.FirstOrDefault(e => e.PointBatchId == pointBatchId);
        }
        #region 点口单维护
        /// 
        /// 添加点口
        /// 
        /// 
        public static void AddPointBatch(Model.Batch_PointBatch pointBatch)
        {
            Model.HJGLDB db = Funs.DB;
            Model.Batch_PointBatch newPointBatch = new Model.Batch_PointBatch
            {
                PointBatchId = pointBatch.PointBatchId,
                PointBatchCode = pointBatch.PointBatchCode,
                ProjectId = pointBatch.ProjectId,
                UnitId = pointBatch.UnitId,
                InstallationId = pointBatch.InstallationId,
                PipelineId=pointBatch.PipelineId,
                DetectionTypeId = pointBatch.DetectionTypeId,
                DetectionRateId = pointBatch.DetectionRateId,
                WelderId = pointBatch.WelderId,
                MediumId = pointBatch.MediumId,
                MaterialId = pointBatch.MaterialId,
                WeldTypeId = pointBatch.WeldTypeId,
                StartDate = pointBatch.StartDate,
                EndDate = pointBatch.EndDate,
                ClearDate = pointBatch.ClearDate,
                IsTrust = pointBatch.IsTrust,
                IsCheck = pointBatch.IsCheck
            };
            db.Batch_PointBatch.InsertOnSubmit(newPointBatch);
            db.SubmitChanges();
        }
        /// 
        /// 修改点口主表
        /// 
        /// 
        public static void UpdatePointBatch(Model.Batch_PointBatch pointBatch)
        {
            Model.HJGLDB db = Funs.DB;
            Model.Batch_PointBatch newPointBatch = db.Batch_PointBatch.FirstOrDefault(e => e.PointBatchId == pointBatch.PointBatchId);
            if (newPointBatch != null)
            {
                newPointBatch.PointBatchCode = pointBatch.PointBatchCode;
                newPointBatch.ProjectId = pointBatch.ProjectId;
                newPointBatch.UnitId = pointBatch.UnitId;
                newPointBatch.InstallationId = pointBatch.InstallationId;
                newPointBatch.PipelineId = pointBatch.PipelineId;
                newPointBatch.DetectionTypeId = pointBatch.DetectionTypeId;
                newPointBatch.DetectionRateId = pointBatch.DetectionRateId;
                newPointBatch.WelderId = pointBatch.WelderId;
                newPointBatch.MediumId = pointBatch.MediumId;
                newPointBatch.MaterialId = pointBatch.MaterialId;
                newPointBatch.WeldTypeId = pointBatch.WeldTypeId;
                newPointBatch.StartDate = pointBatch.StartDate;
                newPointBatch.EndDate = pointBatch.EndDate;
                newPointBatch.ClearDate = pointBatch.ClearDate;
                newPointBatch.IsTrust = pointBatch.IsTrust;
                newPointBatch.IsCheck = pointBatch.IsCheck;
                db.SubmitChanges();
            }
        }
        /// 
        /// 根据主键删除点口主表
        /// 
        /// 
        public static void DeletePointBatchById(string pointBatchId)
        {
            Model.HJGLDB db = Funs.DB;
            Model.Batch_PointBatch pointBatch = db.Batch_PointBatch.FirstOrDefault(e => e.PointBatchId == pointBatchId);
            if (pointBatch!=null)
            {
                db.Batch_PointBatch.DeleteOnSubmit(pointBatch);
                db.SubmitChanges();
            }
        }
        #endregion
        #region 更新批主表信息-委托状态
        /// 
        /// 更新批主表委托状态
        /// 
        /// 
        /// 
        public static void UpdatePointTrustState(string pointBatchId, bool? isTrust)
        {
            Model.HJGLDB db = Funs.DB;
            Model.Batch_PointBatch update = db.Batch_PointBatch.FirstOrDefault(e => e.PointBatchId == pointBatchId);
            if (update != null)
            {
                update.IsTrust = isTrust;
                db.SubmitChanges();
            }
        }
        #endregion
        #region 生成批号
        /// 
        /// 生成批号
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static string GetNewPointBatchCode(string projectId, string pipeCode, string detectionTypeCode, string detectionRateCode)
        {
            string prefix = null;
            if (!string.IsNullOrEmpty(pipeCode))
            {
                prefix = pipeCode + "-";
            }
            if (!string.IsNullOrEmpty(detectionTypeCode))
            {
                prefix = prefix + detectionTypeCode + "-";
            }
            if (!string.IsNullOrEmpty(detectionRateCode))
            {
                prefix = prefix + detectionRateCode + "-";
            }
            return BLL.SQLHelper.RunProcNewId("SpGetThreeNumber", "Batch_PointBatch", "PointBatchCode", projectId, prefix);
        }
        #endregion
        #region 更新批主表信息-开\关闭
        /// 
        /// 更新批主表信息
        /// 
        /// 批主表实体
        public static void UpdatePointBatch(string pointBatchId, DateTime? endDate)
        {
            Model.HJGLDB db = Funs.DB;
            Model.Batch_PointBatch newPointBatch = db.Batch_PointBatch.FirstOrDefault(e => e.PointBatchId == pointBatchId);
            if (newPointBatch != null)
            {
                newPointBatch.EndDate = endDate;
                db.SubmitChanges();
            }
        }
        #endregion
        #region 更新批主表信息-批结束日期
        /// 
        /// 更新批主表信息-批结束日期
        /// 
        /// 批主表实体
        public static void UpdatePointBatchClearDate(string pointBatchId, DateTime? clearDate)
        {
            Model.HJGLDB db = Funs.DB;
            Model.Batch_PointBatch newPointBatch = db.Batch_PointBatch.FirstOrDefault(e => e.PointBatchId == pointBatchId);
            if (newPointBatch != null)
            {
                newPointBatch.ClearDate = clearDate;
                db.SubmitChanges();
            }
        }
        #endregion
        #region 是否显示返修\切除页面通过批id
        /// 
        /// 是否显示返修\切除页面 
        /// 
        /// 
        public static bool GetIsShowPointRepairByPointBatchId(string pointBatchId)
        {
            ////判断该焊口是否点口\扩透 是显示链接
            var pointBatchItem = Funs.DB.Batch_PointBatchItem.FirstOrDefault(x => x.PointBatchId == pointBatchId
                                     && !x.CutDate.HasValue && x.PointDate.HasValue && x.IsCheckRepair.HasValue && x.IsCheckRepair.Value);
            if (pointBatchItem != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
        #region 是否 取消扩透
        /// 
        /// 是否显示取消扩透
        /// 
        /// 
        public static bool GetIsShowCancellation(string pointBatchItemId, string pointState)
        {
            Model.HJGLDB db = Funs.DB;
            bool isShow = false;
            ////判断该焊口是否点口\扩透 是显示链接
            var pointBatchItem = Funs.DB.Batch_PointBatchItem.FirstOrDefault(x => x.PointBatchItemId == pointBatchItemId
                && (x.IsCheckRepair == null || x.IsCheckRepair == false) && (x.PointState == pointState || x.CutDate.HasValue));
            if (pointBatchItem != null)
            {
                var trust = db.Batch_BatchTrustItem.Where(p => p.PointBatchItemId == pointBatchItemId);
                if (trust.Count() == 1)
                {
                   var checkItem = db.Batch_NDEItem.FirstOrDefault(x => x.TrustBatchItemId == trust.First().TrustBatchItemId);
                    if (checkItem == null)
                    {
                        isShow = true;
                    }
                   
                }
                if (trust.Count() == 0)
                {
                    isShow = true;
                }
            }
            return isShow;
        }
        #endregion
        #region 是否满足生成委托条件
        /// 
        /// 是否满足生成委托条件
        /// 
        /// 
        public static bool GetIsGenerateTrust(string pointBatchItemId)
        {
            bool isShow = true;
            var trustBatchItem = Funs.DB.Batch_BatchTrustItem.FirstOrDefault(x => x.PointBatchItemId == pointBatchItemId);
            if (trustBatchItem != null)
            {
                var checkItem = Funs.DB.Batch_NDEItem.FirstOrDefault(x => x.TrustBatchItemId == trustBatchItem.TrustBatchItemId && x.CheckResult == "1");
                if (checkItem != null)
                {
                    isShow = false;
                }
            }
            return isShow;
        }
        #endregion
        #region 判断批次主键判断批是否关闭
        /// 
        /// 判断批次主键判断批是否关闭
        /// 
        /// 批次主键
        /// 
        public static bool IsPointBatchClose(string pointBatchId)
        {
            bool isColse = false;
            var pointBatch = Funs.DB.Batch_PointBatch.FirstOrDefault(x => x.PointBatchId == pointBatchId);
            if (pointBatch.EndDate.HasValue)
            {
                isColse = true;
            }
            else
            {
                isColse = false;
            }
            return isColse;
        }
        #endregion
        #region 更新返修焊口号
        /// 
        /// 返修\切除更新焊口号
        /// 
        /// 
        /// 
        public static void UpdateNewRepairJointNo(string WeldJointId)
        {
            Model.HJGLDB db = Funs.DB;
            var jointInfo = db.Pipeline_WeldJoint.FirstOrDefault(x => x.WeldJointId == WeldJointId);
            if (jointInfo != null)
            {
                string jointNo = jointInfo.WeldJointCode;
                string lastNo = jointNo;
                string startNo = jointNo;
                string newjointNo = string.Empty;
                ////焊口字符串长度
                int noLength = jointNo.Length;
                if (noLength > 2)
                {
                    lastNo = jointNo.Substring(noLength - 2); ////截取最后两位
                    startNo = jointNo.Substring(0, noLength - 2);
                    ////焊口包含R的索引                
                    int indexR = lastNo.LastIndexOf("R");
                    if (indexR >= 0)
                    {
                        string rString = string.Empty;
                        ////截取R 前的字符串
                        rString = lastNo.Substring(0, indexR + 1);
                        ////截取R 后的字符
                        string subNo = lastNo.Substring(indexR + 1);
                        if (!string.IsNullOrEmpty(subNo))
                        {
                            subNo = (Convert.ToInt32(subNo) + 1).ToString();
                        }
                        else
                        {
                            subNo = "1";
                        }
                        newjointNo = startNo + rString + subNo;
                    }
                    else
                    {
                        newjointNo = jointNo + "R1";
                    }
                }
                else
                {
                    newjointNo = jointNo + "R1";
                }
                jointInfo.WeldJointCode = newjointNo;
                db.SubmitChanges();
            }
        }
        #endregion
        #region 撤消返修焊口号
        /// 
        /// 撤消返修焊口号
        /// 
        /// 
        /// 
        public static void CancelRepairJointNo(string WeldJointId)
        {
            Model.HJGLDB db = Funs.DB;
            var jointInfo = db.Pipeline_WeldJoint.FirstOrDefault(x => x.WeldJointId == WeldJointId);
            if (jointInfo != null)
            {
                string jointNo = jointInfo.WeldJointCode;
                string lastNo = jointNo;
                string startNo = jointNo;
                string newjointNo = string.Empty;
                //焊口字符串长度
                int noLength = jointNo.Length;
                if (noLength > 2)
                {
                    lastNo = jointNo.Substring(noLength - 2); ////截取最后两位
                    startNo = jointNo.Substring(0, noLength - 2);
                    // 焊口包含R的索引                
                    int indexR = lastNo.LastIndexOf("R");
                    if (indexR >= 0)
                    {
                        string rString = string.Empty;
                        // 截取R 前的字符串
                        rString = lastNo.Substring(0, indexR + 1);
                        // 截取R 后的字符
                        string subNo = lastNo.Substring(indexR + 1);
                        if (!string.IsNullOrEmpty(subNo) && Convert.ToInt32(subNo) > 1)
                        {
                            subNo = (Convert.ToInt32(subNo) - 1).ToString();
                            newjointNo = startNo + rString + subNo;
                        }
                        else
                        {
                            newjointNo = startNo;
                        }
                    }
                }
                jointInfo.WeldJointCode = newjointNo;
                db.SubmitChanges();
            }
        }
        #endregion
        #region 更新扩透或切除焊口号
        /// 
        /// 更新扩透或切除焊口号
        /// 
        /// 
        /// 
        public static void UpdateNewKuoOrCutJointNo(string pointBatchItemId, string value)
        {
            Model.HJGLDB db = Funs.DB;
            var pointBatchItem = db.Batch_PointBatchItem.FirstOrDefault(x => x.PointBatchItemId == pointBatchItemId);
            var jointInfo = db.Pipeline_WeldJoint.FirstOrDefault(x => x.WeldJointId == pointBatchItem.WeldJointId);
            if (jointInfo != null)
            {
                jointInfo.OldWeldJointCode = jointInfo.WeldJointCode;
                string jointNo = jointInfo.WeldJointCode;
                //// 焊口字符串长度
                //int noLength = jointNo.Length;
                //if (noLength > 2 && value == "C")
                //{
                //    // 焊口包含K的索引                
                //    int indexK = jointNo.LastIndexOf("K");
                //    if (indexK >= 0)
                //    {
                //        // 截取K 前的字符串
                //        jointNo = jointNo.Substring(0, indexK);
                //    }
                //    else
                //    {
                //        // 焊口包含R的索引                
                //        int indexR = jointNo.LastIndexOf("R");
                //        if (indexR >= 0)
                //        {
                //            jointNo = jointNo.Substring(0, indexR);
                //        }
                //    }
                //}
                jointInfo.WeldJointCode = jointNo + value;
                db.SubmitChanges();
            }
        }
        #endregion
        #region 撤销扩透或切除焊口号
        /// 
        /// 撤销扩透或切除焊口号
        /// 
        /// 
        /// 
        public static void CancellationJointNo(string WeldJointId)
        {
            Model.HJGLDB db = Funs.DB;
            var jointInfo = db.Pipeline_WeldJoint.FirstOrDefault(x => x.WeldJointId == WeldJointId);
            if (jointInfo != null && !string.IsNullOrEmpty(jointInfo.OldWeldJointCode))
            {
                jointInfo.WeldJointCode = jointInfo.OldWeldJointCode;  
                jointInfo.OldWeldJointCode = null;
                db.SubmitChanges();
            }
        }
        #endregion
    }
}