using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using BLL;
namespace FineUIPro.Web.WeldingProcess.TrustManage
{
    public partial class OpenResetPoint : PageBase
    {
        #region 定义项       
        /// 
        /// 批主键
        /// 
        public string PointBatchId
        {
            get
            {
                return (string)ViewState["PointBatchId"];
            }
            set
            {
                ViewState["PointBatchId"] = value;
            }
        }
        #endregion
        #region 加载页面
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.PointBatchId = Request.Params["PointBatchId"];
                this.BindGrid();          
            }
        }
        #endregion        
        #region 数据绑定
        /// 
        /// 数据绑定
        /// 
        private void BindGrid()
        {
            string strSql = @"SELECT PointBatchItem.PointBatchItemId,PointBatch.PointBatchCode, jot.WeldJointCode,
                                     ins.InstallationCode, WorkArea.WorkAreaCode,unit.UnitCode,
                                    (CASE PointBatchItem.IsAudit WHEN 1 THEN '是' ELSE '否' END) AS PointIsAudit,
                                     jot.JointArea,WeldingDaily.WeldingDate,PipingClass.PipingClassName,
                                     (CASE WHEN jot.BackingWelderId!=jot.CoverWelderId THEN backingWelder.WelderCode+'/'+coverWelder.WelderCode ELSE backingWelder.WelderCode END) AS WelderCode--焊工号
                               FROM Batch_PointBatchItem AS PointBatchItem
                               LEFT JOIN Batch_PointBatch AS PointBatch ON PointBatch.PointBatchId=PointBatchItem.PointBatchId
                               LEFT JOIN Pipeline_WeldJoint AS jot ON jot.WeldJointId=PointBatchItem.WeldJointId
                               LEFT JOIN Pipeline_Pipeline AS Pipeline ON Pipeline.PipelineId=jot.PipelineId
							   LEFT JOIN dbo.Welder_Welder backingWelder ON backingWelder.WelderId = jot.BackingWelderId
                               LEFT JOIN dbo.Welder_Welder coverWelder ON coverWelder.WelderId = jot.CoverWelderId
                               LEFT JOIN Project_WorkArea AS WorkArea ON WorkArea.WorkAreaId=Pipeline.WorkAreaId
                               LEFT JOIN Pipeline_WeldingDaily AS WeldingDaily ON WeldingDaily.WeldingDailyId=jot.WeldingDailyId
                               LEFT JOIN Base_PipingClass AS PipingClass ON PipingClass.PipingClassId=Pipeline.PipingClassId
                               LEFT JOIN dbo.Project_Installation ins ON ins.InstallationId = PointBatch.InstallationId
                               LEFT JOIN dbo.Base_Unit unit ON unit.UnitId = PointBatch.UnitId
                               WHERE PointBatchItem.PointState IS NULL 
                                     AND PointBatch.ProjectId=@ProjectId AND PointBatchItem.PointBatchId=@PointBatchId";
            List listStr = new List();
            listStr.Add(new SqlParameter("@ProjectId", this.CurrUser.LoginProjectId));
            listStr.Add(new SqlParameter("@PointBatchId", this.PointBatchId));
            SqlParameter[] parameter = listStr.ToArray();
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
            Grid1.RecordCount = tb.Rows.Count;
            // tb = GetFilteredTable(Grid1.FilteredData, tb);
            var table = this.GetPagedDataTable(Grid1, tb);
            Grid1.DataSource = table;
            Grid1.DataBind();
            //string ids = string.Empty;
            //for (int i = 0; i < this.Grid1.Rows.Count; i++)
            //{
            //    var pointItem = BLL.Batch_PointBatchItemService.GetPointBatchItemByPointBatchItemId(this.Grid1.Rows[i].DataKeys[0].ToString());
            //    if (pointItem != null && pointItem.PointState == "1")
            //    {
            //        ids += pointItem.PointBatchItemId + ",";
            //    }
            //}
            //if (!string.IsNullOrEmpty(ids))
            //{
            //    ids = ids.Substring(0, ids.Length - 1);
            //    this.Grid1.SelectedRowIDArray = ids.Split(',');
            //}
        }
        #endregion
        #region 排序
        /// 
        /// 排序
        /// 
        /// 
        /// 
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            this.BindGrid();
        }
        #endregion
        #region 确定按钮
        /// 
        /// 确定按钮
        /// 
        /// 
        /// 
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            Model.HJGLDB db = Funs.DB;
            string[] selectRowId = Grid1.SelectedRowIDArray;
            if (selectRowId.Count() > 0)
            {
                foreach (var item in selectRowId)
                {
                    BLL.Batch_PointBatchItemService.UpdatePointBatchItem(item, "1", DateTime.Now, null);
                    var pWelder = from x in db.Batch_PointBatchItem
                                  join y in db.Pipeline_WeldJoint on x.WeldJointId equals y.WeldJointId
                                  where x.PointBatchItemId == item
                                  select new { x.PointBatchId, y.BackingWelderId };
                    if (pWelder.Count() > 0)
                    {
                        // 查找该焊工所在批所有未关闭的焊口
                        var pItem = (from x in db.Batch_PointBatchItem
                                    join y in db.Pipeline_WeldJoint on x.WeldJointId equals y.WeldJointId
                                    where x.IsCompletedPoint == false && x.PointBatchId == pWelder.First().PointBatchId
                                          && y.BackingWelderId == pWelder.First().BackingWelderId
                                    select x).ToList();
                        if (pItem.Count() > 0)
                        {
                            foreach (var q in pItem)
                            {
                                q.IsCompletedPoint = true;
                                db.SubmitChanges();
                            }
                        }
                    }
                }
            }
           
            BLL.Sys_LogService.AddLog(BLL.Const.System_3, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_PointManageMenuId, Const.BtnOpenResetPoint, this.PointBatchId);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        protected void btnCancelAccept_Click(object sender, EventArgs e)
        {
            Model.HJGLDB db = Funs.DB;
            string[] selectRowId = Grid1.SelectedRowIDArray;
            if (selectRowId.Count() > 0)
            {
                foreach (var item in selectRowId)
                {
                    BLL.Batch_PointBatchItemService.UpdatePointBatchItem(item, null, null, null);
                }
            }
            BLL.Sys_LogService.AddLog(BLL.Const.System_3, this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HJGL_PointManageMenuId, Const.BtnOpenResetPoint, this.PointBatchId);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
    }
}