using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using BLL;
using System.Web;
using System.Collections;
namespace FineUIPro.Web.CheckManage
{
    public partial class CheckManage : PageBase
    {
        #region 定义项
        /// 
        /// 检测单主键
        /// 
        public string NDEID
        {
            get
            {
                return (string)ViewState["NDEID"];
            }
            set
            {
                ViewState["NDEID"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtNDEDateMonth.Text = string.Format("{0:yyyy-MM}", DateTime.Now);
                this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
                this.NDEID = string.Empty;
                this.InitTreeMenu();//加载树
            }
        }
        #endregion
        #region 加载树
        /// 
        /// 加载树
        /// 
        private void InitTreeMenu()
        {
            if (!string.IsNullOrEmpty(this.txtNDEDateMonth.Text.Trim()))
            {
                DateTime startTime = Convert.ToDateTime(this.txtNDEDateMonth.Text.Trim() + "-01");
                DateTime endTime = startTime.AddMonths(1);
                this.tvControlItem.Nodes.Clear();
                List units = new List();
                Model.Project_Unit pUnit = BLL.Project_UnitService.GetProject_UnitByProjectIdUnitId(this.CurrUser.LoginProjectId, this.CurrUser.UnitId);
                if (pUnit == null || pUnit.UnitType == BLL.Const.UnitType_1 || pUnit.UnitType == BLL.Const.UnitType_2
                   || pUnit.UnitType == BLL.Const.UnitType_3 || pUnit.UnitType == BLL.Const.UnitType_4
                   || pUnit.UnitType == BLL.Const.UnitType_6)
                {
                    units = (from x in Funs.DB.Base_Unit
                             join y in Funs.DB.Project_Unit on x.UnitId equals y.UnitId
                             where y.ProjectId == this.CurrUser.LoginProjectId && y.UnitType.Contains(BLL.Const.UnitType_5)
                             select x).ToList();
                }
                else
                {
                    units.Add(BLL.Base_UnitService.GetUnit(this.CurrUser.UnitId));
                }
                if (units != null)
                {
                    foreach (var unit in units)
                    {
                        TreeNode newNode = new TreeNode();//定义根节点
                        newNode.Text = unit.UnitName;
                        newNode.NodeID = unit.UnitId;
                        newNode.Expanded = true;
                        newNode.ToolTip = "Unit";
                        this.tvControlItem.Nodes.Add(newNode);
                        this.BindNodes(newNode);
                    }
                }
                else
                {
                    Alert.ShowInTop(Resources.Lan.PleaseAddUnitFirst, MessageBoxIcon.Warning);
                }
            }
            else
            {
                Alert.ShowInTop(Resources.Lan.PleaseSelectTrustMonth, MessageBoxIcon.Warning);
            }
        }
        /// 
        ///  绑定树节点
        /// 
        /// 
        private void BindNodes(TreeNode node)
        {
            if (node.ToolTip == "Unit")
            {
                ///装置
                var install = (from x in Funs.DB.Project_Installation
                               join y in Funs.DB.Batch_NDE on x.InstallationId equals y.InstallationId
                               where y.UnitId == node.NodeID && x.ProjectId==this.CurrUser.LoginProjectId
                               orderby x.InstallationCode
                               select x).Distinct();
                foreach (var q in install)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = q.InstallationName;
                    newNode.NodeID = q.InstallationId + "|" + node.NodeID;
                    newNode.ToolTip = "Installation";
                    node.Nodes.Add(newNode);
                    BindNodes(newNode);
                }
            }
            else if (node.ToolTip == "Installation")
            {
                var ndt = from x in Funs.DB.Base_DetectionType select x;
                foreach (var q in ndt)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = q.DetectionTypeCode;
                    newNode.NodeID = q.DetectionTypeId + "|" + node.NodeID.Split('|')[0];
                    newNode.ToolTip = "DetectionType";
                    newNode.EnableExpandEvent = true;
                    node.Nodes.Add(newNode);
                    BindNodes(newNode);
                }
                //var types = (from x in Funs.DB.View_Batch_NDE
                //             join y in Funs.DB.Base_DetectionType
                //             on x.DetectionTypeId equals y.DetectionTypeId
                //             where x.NDEDate < Convert.ToDateTime(this.txtNDEDateMonth.Text.Trim() + "-01").AddMonths(1)
                //                  && x.NDEDate >= Convert.ToDateTime(this.txtNDEDateMonth.Text.Trim() + "-01").AddMonths(-1)
                //                  && x.UnitId == node.NodeID.Split('|')[1] && x.InstallationId == node.NodeID.Split('|')[0]
                //                  && x.ProjectId==this.CurrUser.LoginProjectId
                //             orderby x.DetectionTypeCode
                //             select y).Distinct();
                ////var u = BLL.Base_UnitService.GetUnit(node.ParentNode.NodeID);
                //foreach (var q in types)
                //{
                //    TreeNode newNode = new TreeNode();
                //    newNode.Text = q.DetectionTypeCode; // + "(" + u.UnitCode + ")";
                //    newNode.NodeID = q.DetectionTypeId + "|" + node.NodeID.Split('|')[0];
                //    newNode.ToolTip = "DetectionType";
                //    node.Nodes.Add(newNode);
                //    BindNodes(newNode, startTime, endTime);
                //}
            }
            else if (node.ToolTip == "DetectionType")
            {
                TreeNode newNode = new TreeNode();
                newNode.Text = "加载...";
                newNode.NodeID = "加载...";
                node.Nodes.Add(newNode);
                ////单号
                //string ndtTypeId = node.NodeID.Split('|')[0];
                //var checks = from x in Funs.DB.View_Batch_NDE
                //             where x.NDEDate < Convert.ToDateTime(this.txtNDEDateMonth.Text.Trim() + "-01").AddMonths(1)
                //             && x.NDEDate >= Convert.ToDateTime(this.txtNDEDateMonth.Text.Trim() + "-01").AddMonths(-1)
                //             && x.ProjectId == this.CurrUser.LoginProjectId && x.NDECode.Contains(this.txtSearchCode.Text.Trim())
                //             && x.InstallationId.ToString() == node.NodeID.Split('|')[1]
                //             && x.UnitId == node.ParentNode.ParentNode.NodeID
                //             && x.DetectionTypeId == ndtTypeId
                //             orderby x.NDECode descending
                //             select x;
                //foreach (var check in checks)
                //{
                //    TreeNode newNode = new TreeNode();
                //    if (BLL.Batch_NDEService.IsShowNDECode(check.NDEID))
                //    {
                //        newNode.Text = "" + check.NDECode + "";
                //    }
                //    //if (!check.AuditDate.HasValue)
                //    //{
                //    //    newNode.Text = "" + check.NDECode + "";
                //    //}
                //    else
                //    {
                //        newNode.Text = check.NDECode;
                //    }
                //    newNode.NodeID = check.NDEID;
                //    newNode.ToolTip = "check";
                //    newNode.EnableClickEvent = true;
                //    node.Nodes.Add(newNode);
                //}
            }
        }
        #endregion
        protected void tvControlItem_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.ToolTip == "DetectionType")
            {
                e.Node.Nodes.Clear();
                //单号
                string ndtTypeId = e.Node.NodeID.Split('|')[0];
                var checks = from x in Funs.DB.View_Batch_NDE
                             where x.NDEDate < Convert.ToDateTime(this.txtNDEDateMonth.Text.Trim() + "-01").AddMonths(1)
                             && x.NDEDate >= Convert.ToDateTime(this.txtNDEDateMonth.Text.Trim() + "-01").AddMonths(-1)
                             && x.ProjectId == this.CurrUser.LoginProjectId && x.NDECode.Contains(this.txtSearchCode.Text.Trim())
                             && x.InstallationId.ToString() == e.Node.NodeID.Split('|')[1]
                             && x.UnitId == e.Node.ParentNode.ParentNode.NodeID
                             && x.DetectionTypeId == ndtTypeId
                             orderby x.NDECode descending
                             select x;
                foreach (var check in checks)
                {
                    TreeNode newNode = new TreeNode();
                    if (BLL.Batch_NDEService.IsShowNDECode(check.NDEID))
                    {
                        newNode.Text = "" + check.NDECode + "";
                    }
                    //if (!check.AuditDate.HasValue)
                    //{
                    //    newNode.Text = "" + check.NDECode + "";
                    //}
                    else
                    {
                        newNode.Text = check.NDECode;
                    }
                    newNode.NodeID = check.NDEID;
                    newNode.ToolTip = "check";
                    newNode.EnableClickEvent = true;
                    e.Node.Nodes.Add(newNode);
                }
            }
        }
        #region 点击TreeView
        /// 
        /// 点击TreeView
        /// 
        /// 
        /// 
        protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
        {
            this.NDEID = tvControlItem.SelectedNodeID;
            this.BindGrid();
        }
        #endregion
        #region 数据绑定
        protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            this.BindGrid();
        }
        /// 
        /// 数据绑定
        /// 
        private void BindGrid()
        {
            if (this.tvControlItem.SelectedNode != null && this.tvControlItem.SelectedNode.ToolTip == "check")
            {
                this.SetTextTemp();
                this.PageInfoLoad(); ///页面输入提交信息
                string strSql = @"SELECT * FROM dbo.View_Batch_NDEItem d WHERE NDEID=@NDEID ";
                List listStr = new List
                {
                };
                listStr.Add(new SqlParameter("@NDEID", this.tvControlItem.SelectedNodeID));
                SqlParameter[] parameter = listStr.ToArray();
                DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
                // 2.获取当前分页数据
                //var table = this.GetPagedDataTable(Grid1, tb1);
                Grid1.RecordCount = tb.Rows.Count;
                tb = GetFilteredTable(Grid1.FilteredData, tb);
                var table = this.GetPagedDataTable(Grid1, tb);
                Grid1.DataSource = table;
                Grid1.DataBind();
                for (int i = 0; i < this.Grid1.Rows.Count; i++)
                {
                    string id = Grid1.DataKeys[i][0].ToString();
                    Model.Batch_NDEItem item = BLL.Batch_NDEItemService.GetNDEItemById(id);
                    if (item != null)
                    {
                        if (item.SubmitDate == null)    //未审核
                        {
                            this.Grid1.Rows[i].CellCssClasses[15] = "f-grid-cell-uneditable";
                        }
                    }
                }
            }
        }
        #region 加载页面输入提交信息
        /// 
        /// 加载页面输入提交信息
        /// 
        private void PageInfoLoad()
        {
            this.SimpleForm1.Reset(); ///重置所有字段
            var check = Funs.DB.View_Batch_NDE.FirstOrDefault(x => x.NDEID == this.NDEID);
            if (check != null)
            {
                this.txtNDECode.Text = check.NDECode;
                this.txtUnitName.Text = check.UnitName;
                this.txtInstallation.Text = check.InstallationName;
                this.txtCheckUnit.Text = check.NDEUnitName;
                this.txtDetectionTypeCode.Text = check.DetectionTypeCode;
                if (check.NDEDate != null)
                {
                    this.txtNDEDate.Text = string.Format("{0:yyyy-MM-dd}", check.NDEDate);
                }
                this.txtTrustBatchCode.Text = check.TrustBatchCode;
            }
        }
        #endregion
        /// 
        /// 情况
        /// 
        private void SetTextTemp()
        {
            this.txtNDECode.Text = string.Empty;
            this.txtUnitName.Text = string.Empty;
            this.txtInstallation.Text = string.Empty;
            this.txtCheckUnit.Text = string.Empty;
            this.txtDetectionTypeCode.Text = string.Empty;
            this.txtNDEDate.Text = string.Empty;
            this.txtTrustBatchCode.Text = string.Empty;
        }
        #endregion
        #region 分页排序
        #region 页索引改变事件
        /// 
        /// 页索引改变事件
        /// 
        /// 
        /// 
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            BindGrid();
        }
        #endregion
        #region 分页选择下拉改变事件
        /// 
        /// 分页选择下拉改变事件
        /// 
        /// 
        /// 
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
            BindGrid();
        }
        #endregion
        #endregion
        #region 检测单 维护事件
        /// 
        /// 增加检测单
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_CheckManageMenuId, Const.BtnAdd))
            {
                this.SetTextTemp();
                string window = String.Format("CheckManageEdit.aspx?NDEID={0}", string.Empty, "新增 - ");
                PageContext.RegisterStartupScript(Window1.GetSaveStateReference(this.hdNDEID.ClientID)
                  + Window1.GetShowReference(window));
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #region 编辑检测单
        /// 
        /// 编辑检测单
        /// 
        /// 
        /// 
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_CheckManageMenuId, Const.BtnSave))
            {
                if (this.tvControlItem.SelectedNode != null)
                {
                    Model.Batch_NDE check = BLL.Batch_NDEService.GetNDEById(this.tvControlItem.SelectedNodeID);
                    if (check != null)
                    {
                        string window = String.Format("CheckManageEdit.aspx?NDEID={0}", this.NDEID, "编辑 - ");
                        PageContext.RegisterStartupScript(Window1.GetSaveStateReference(this.hdNDEID.ClientID)
                          + Window1.GetShowReference(window));
                    }
                    else
                    {
                        ShowNotify(Resources.Lan.SelectEditRecord, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify(Resources.Lan.SelectEditRecord, MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 删除检测单
        /// 
        /// 删除检测单
        /// 
        /// 
        /// 
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_CheckManageMenuId, Const.BtnDelete))
            {
                if (this.tvControlItem.SelectedNode != null)
                {
                    Model.Batch_NDE check = BLL.Batch_NDEService.GetNDEById(this.tvControlItem.SelectedNodeID);
                    if (check != null)
                    {
                        if (judgementDelete(this.tvControlItem.SelectedNodeID))
                        {
                            BLL.Batch_NDEItemService.DeleteNDEItemById(this.NDEID);
                            BLL.Batch_NDEService.DeleteNDEById(this.NDEID);
                            BLL.Sys_LogService.AddLog(BLL.Const.System_3, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_CheckManageMenuId, Const.BtnDelete, this.NDEID);
                            ShowNotify(Resources.Lan.DeletedSuccessfully, MessageBoxIcon.Success);
                            this.InitTreeMenu();
                            this.Grid1.DataSource = null;
                            this.Grid1.DataBind();
                            this.SetTextTemp();
                        }
                    }
                    else
                    {
                        ShowNotify(Resources.Lan.SelectDeleteRecord, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify(Resources.Lan.SelectDeleteRecord, MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
                return;
            }
        }
        #endregion
        #endregion
        #region 生成返修通知单
        protected void BtnRepairRecord_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_CheckManageMenuId, Const.BtnRepairNotice))
            {
                string ndtItem = Grid1.SelectedRowID;
                if (ndtItem != string.Empty)
                {
                    var q = BLL.Batch_NDEItemService.GetNDEItemById(ndtItem);
                    if (q.PassFilm != q.TotalFilm && q.SubmitDate.HasValue)
                    {
                        string window = String.Format("RepairNotice.aspx?NDEItemID={0}", ndtItem, "返修通知单");
                        PageContext.RegisterStartupScript(WindowRepair.GetShowReference(window));
                    }
                    else
                    {
                        ShowNotify("请选择不合格并且已审核的检测项!", MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    ShowNotify("请选择不合格检测项!", MessageBoxIcon.Warning);
                }
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        #endregion
        #region 判断是否可删除
        /// 
        /// 判断是否可以删除
        /// 
        /// 
        private bool judgementDelete(string id)
        {
            string content = string.Empty;
            var ndeItems = BLL.Batch_NDEItemService.GetNDEItemByNDEID(id);
            foreach (var ndeItem in ndeItems)
            {
                if (ndeItem.SubmitDate != null)
                {
                    content = "检测单明细已审核,不能删除!";
                    break;
                }
            }
            if (string.IsNullOrEmpty(content))
            {
                return true;
            }
            else
            {
                Alert.ShowInTop(content, MessageBoxIcon.Error);
                return false;
            }
        }
        #endregion
        #region 关闭弹出窗口及刷新页面
        /// 
        /// 关闭弹出窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            this.NDEID = this.hdNDEID.Text;
            this.BindGrid();
            //this.InitTreeMenu();
            this.hdNDEID.Text = string.Empty;
        }
        /// 
        /// 查询
        /// 
        /// 
        /// 
        protected void Tree_TextChanged(object sender, EventArgs e)
        {
            this.InitTreeMenu();
            //this.BindGrid();
        }
        #endregion
        #region 格式化字符串
        /// 
        /// 获取缺陷
        /// 
        /// 
        /// 
        protected string ConvertCheckDefects(object CheckDefects)
        {
            string str = string.Empty;
            if (CheckDefects != null)
            {
                HttpCookie lanCookie = Request.Cookies["SelectLan"];
                if (lanCookie["lan"] == "zh-CN")   //中文
                {
                    str = BLL.Base_DefectService.GetDefectNameStrByDefectIdStr(CheckDefects.ToString());
                }
                else
                {
                    str = BLL.Base_DefectService.GetDefectEngNameStrByDefectIdStr(CheckDefects.ToString());
                }
            }
            return str;
        }
        #endregion
        #region 检测单 审核事件
        /// 
        /// 编辑检测单
        /// 
        /// 
        /// 
        protected void btnAudit_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_CheckManageMenuId, Const.BtnAuditing))
            {
                List GetNDEItem = BLL.Batch_NDEItemService.GetNDEItemByNDEID(this.NDEID);
                Model.HJGLDB db = Funs.DB;
                //全部记录都已录入探伤报告编号
                var isNull = GetNDEItem.FirstOrDefault(x => x.NDEReportNo == null);
                if (isNull == null)
                {
                    foreach (var item in GetNDEItem)
                    {
                        if (!item.SubmitDate.HasValue)
                        {
                            if (!string.IsNullOrEmpty(item.CheckResult) && !String.IsNullOrEmpty(item.NDEReportNo))
                            {
                                var ndt = BLL.Base_DetectionTypeService.GetDetectionTypeByDetectionTypeId(item.DetectionTypeId);
                                if (ndt.SysType == Resources.Lan.RTest && (!item.PassFilm.HasValue || !item.TotalFilm.HasValue))
                                {
                                    ShowNotify(Resources.Lan.PleaseInputTFAndPF, MessageBoxIcon.Warning);
                                    return;
                                }
                                if (item.TotalFilm < item.PassFilm)
                                {
                                    ShowNotify(Resources.Lan.PFNotBigTF, MessageBoxIcon.Warning);
                                    return;
                                }
                                //var trustBatchItem = db.Batch_BatchTrustItem.FirstOrDefault(x => x.TrustBatchItemId == item.TrustBatchItemId);
                                //if (trustBatchItem != null)
                                //{
                                //    var pointBatchItem = db.Batch_PointBatchItem.FirstOrDefault(x => x.PointBatchItemId == trustBatchItem.PointBatchItemId);
                                //    if (pointBatchItem != null && item.CheckResult == "2")
                                //    {
                                //        BLL.Batch_PointBatchService.UpdateNewRepairJointNo(pointBatchItem.WeldJointId);
                                //        // 提交时更新状态
                                //        //var newPointBatchItem = db.Batch_PointBatchItem.FirstOrDefault(x => x.PointBatchItemId == pointBatchItem.PointBatchItemId);
                                //        pointBatchItem.IsCheckRepair = true;
                                //        pointBatchItem.IsBuildTrust = false;
                                //        pointBatchItem.RepairDate = Convert.ToDateTime(DateTime.Now.Date);
                                //        db.SubmitChanges();
                                //        var pointBatch = BLL.Batch_PointBatchService.GetPointBatchById(pointBatchItem.PointBatchId);
                                //        if (pointBatch != null)
                                //        {
                                //            pointBatch.IsTrust = false;
                                //            pointBatch.IsCheck = false;
                                //            db.SubmitChanges();
                                //        }
                                        // 自动扩口 获取要选取的扩透口
                                        //var pointBatchItemExpand = (from x in Funs.DB.View_Batch_PointBatchItem
                                        //                            where x.PointBatchId == pointBatchItem.PointBatchId && !x.CutDate.HasValue
                                        //                                && x.PointState == null && !x.RepairDate.HasValue
                                        //                            select x).ToList();
                                        //if (pointBatchItemExpand.Count() > 0)
                                        //{
                                        //    if (pointBatchItemExpand.Count() <= 2)
                                        //    {
                                        //        foreach (var p in pointBatchItemExpand)
                                        //        {
                                        //            AutoExpandUpdate(p.PointBatchItemId, pointBatchItem.PointBatchItemId);
                                        //        }
                                        //    }
                                        //    else
                                        //    {
                                        //        Hashtable hashtable = new Hashtable();
                                        //        Random rm = new Random();
                                        //        int RmNum = 2;
                                        //        for (int i = 0; hashtable.Count < RmNum; i++)
                                        //        {
                                        //            int nValue = rm.Next(pointBatchItemExpand.Count());
                                        //            if (!hashtable.ContainsValue(nValue) && nValue != 0)
                                        //            {
                                        //                hashtable.Add(i, nValue);
                                        //            }
                                        //        }
                                        //        int n1 = Convert.ToInt32(hashtable[0]);
                                        //        int n2 = Convert.ToInt32(hashtable[1]);
                                        //        string pointBatchItemId1 = (pointBatchItemExpand).ToList()[n1].PointBatchItemId;
                                        //        string pointBatchItemId2 = (pointBatchItemExpand).ToList()[n2].PointBatchItemId;
                                        //        AutoExpandUpdate(pointBatchItemId1, pointBatchItem.PointBatchItemId);
                                        //        AutoExpandUpdate(pointBatchItemId2, pointBatchItem.PointBatchItemId);
                                        //    }
                                        //}
                                //    }
                                //}
                            }
                            item.SubmitDate = DateTime.Now;
                            BLL.Batch_NDEItemService.UpdateNDEItem(item);
                        }
                    }
                }
                else
                {
                    ShowNotify("所有记录需填写探伤报告编号后才可审核!", MessageBoxIcon.Warning);
                    return;
                }
                Model.Batch_NDE nde = BLL.Batch_NDEService.GetNDEById(this.NDEID);
                if (nde != null)
                {
                    int trustItemCount = BLL.Batch_BatchTrustItemService.GetBatchTrustItemByTrustBatchId(nde.TrustBatchId).Count;
                    int checkItemCount = BLL.Batch_NDEItemService.GetNDEItemByNDEID(this.NDEID).Count;
                    int noResultCheckItemCount = (from x in Funs.DB.Batch_NDEItem where x.NDEID == this.NDEID && x.CheckResult == null select x).Count();
                    if (trustItemCount == checkItemCount && noResultCheckItemCount == 0)  //全部检测结果录入完毕,更新字段
                    {
                        BLL.Batch_BatchTrustService.UpdatTrustBatchtState(nde.TrustBatchId, true);
                    }
                }
                ShowNotify("审核成功!", MessageBoxIcon.Success);
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
                return;
            }
        }
        /// 
        /// 更新自动扩透焊口
        /// 
        /// 批明细Id
        private void AutoExpandUpdate(string pointBatchItemId, string toPointBatchItemId)
        {
            Model.HJGLDB db = Funs.DB;
            var pointBatchItem = db.Batch_PointBatchItem.FirstOrDefault(e => e.PointBatchItemId == pointBatchItemId);
            pointBatchItem.PointDate = Convert.ToDateTime(DateTime.Now.Date);
            pointBatchItem.PointState = "2";
            pointBatchItem.IsBuildTrust = false;
            pointBatchItem.IsCheckRepair = false;
            pointBatchItem.ToPointBatchItemId = toPointBatchItemId; 
            db.SubmitChanges(); // 扩透口
            var jointInfo = db.Pipeline_WeldJoint.FirstOrDefault(x => x.WeldJointId == pointBatchItem.WeldJointId);
            int k_num = 0;
            string jot = "K1";
            int indexK = jointInfo.WeldJointCode.LastIndexOf("K");
            if (indexK > 0)
            {
                try
                {
                    k_num = Convert.ToInt32(jointInfo.WeldJointCode.Substring(indexK + 1, 1)) + 1;
                    jot = "K" + k_num.ToString();
                }
                catch { }
            }
            BLL.Batch_PointBatchService.UpdateNewKuoOrCutJointNo(pointBatchItem.PointBatchItemId, jot);
        }
        #endregion
        #region Grid行点击事件
        /// 
        /// Grid行点击事件
        /// 
        /// 
        /// 
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string id = Grid1.DataKeys[e.RowIndex][0].ToString();
            if (e.CommandName == "CancelAudit")
            {
                if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_CheckManageMenuId, Const.BtnCancelAuditing))
                {
                    Model.Batch_NDEItem item = BLL.Batch_NDEItemService.GetNDEItemById(id);
                    if (item.SubmitDate == null)
                    {
                    }
                    else
                    {
                        var trustBatchItem = Funs.DB.Batch_BatchTrustItem.FirstOrDefault(x => x.TrustBatchItemId == item.TrustBatchItemId);
                        if (trustBatchItem != null)
                        {
                            //var pointBatchItem = Funs.DB.Batch_PointBatchItem.FirstOrDefault(x => x.PointBatchItemId == trustBatchItem.PointBatchItemId);
                            //if (pointBatchItem != null)
                            //{
                            //    if ((item.CheckResult == "2"))
                            //    {
                            //        if (pointBatchItem.IsBuildTrust == false || pointBatchItem.IsBuildTrust == null)
                            //        {
                            //            BLL.Batch_PointBatchService.CancelRepairJointNo(pointBatchItem.WeldJointId);
                            //            // 提交时更新状态
                            //            var newPointBatchItem = Funs.DB.Batch_PointBatchItem.FirstOrDefault(y => y.PointBatchItemId == pointBatchItem.PointBatchItemId);
                            //            newPointBatchItem.IsCheckRepair = false;
                            //            newPointBatchItem.IsBuildTrust = false;
                            //            newPointBatchItem.RepairDate = null;
                            //            Funs.DB.SubmitChanges();
                            //            // 取消扩透口
                            //            var Kpoint = Funs.DB.Batch_PointBatchItem.Where(x => x.ToPointBatchItemId == pointBatchItem.PointBatchItemId && x.IsBuildTrust == false);
                            //            if (Kpoint.Count() > 0)
                            //            {
                            //                foreach (var k in Kpoint)
                            //                {
                            //                    k.ToPointBatchItemId = null;
                            //                    k.PointState = null;
                            //                    k.PointDate = null;
                            //                    Funs.DB.SubmitChanges();
                            //                    BLL.Batch_PointBatchService.CancellationJointNo(k.WeldJointId);
                            //                }
                            //            }
                            //        }
                            //        else
                            //        {
                            //            Alert.ShowInTop(Resources.Lan.NotCancelReview, MessageBoxIcon.Warning);
                            //            return;
                            //        }
                            //    }
                            //}
                            item.SubmitDate = null;
                            BLL.Batch_NDEItemService.UpdateNDEItem(item);
                            Model.Batch_NDE nde = BLL.Batch_NDEService.GetNDEById(item.NDEID);
                            BLL.Batch_BatchTrustService.UpdatTrustBatchtState(nde.TrustBatchId, false);
                            ShowNotify(Resources.Lan.SubmitSuccessful, MessageBoxIcon.Success);
                        }
                    }
                }
                else
                {
                    Alert.ShowInTop(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
                    return;
                }
            }
        }
        #endregion
        #region 导入
        /// 
        /// 导入按钮
        /// 
        /// 
        /// 
        protected void btnImport_Click(object sender, EventArgs e)
        {
            if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_CheckManageMenuId, Const.BtnAdd))
            {
                PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("CheckManageIn.aspx", "导入 - ")));
            }
            else
            {
                ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning);
            }
        }
        /// 
        /// 关闭导入弹出窗口
        /// 
        /// 
        /// 
        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            InitTreeMenu();
        }
        #endregion
    }
}