using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UAS_MES_NEW.DataOperate;
using UAS_MES_NEW.Entity;
using UAS_MES_NEW.PublicMethod;

namespace UAS_MES_NEW.OQC
{
    public partial class OQC_CheckNoSplit : Form
    {

        AutoSizeFormClass asc = new AutoSizeFormClass();
        DataHelper dh;
        LogStringBuilder sql = new LogStringBuilder();
        DataTable dt;
        string ErrorMessage = "";

        List<string> DeleteSn = new List<string>();

        public OQC_CheckNoSplit()
        {
            InitializeComponent();
        }

        private void OQC_CheckNoSplit_Load(object sender, EventArgs e)
        {
            asc.controllInitializeSize(this);
            dh = SystemInf.dh;
        }

        private void OQC_CheckNoSplit_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        private void KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                GetBatch.PerformClick();
            }
        }

        private void GetBatch_Click(object sender, EventArgs e)
        {
            string ErrorMessage = "";
            DataTable[] dt = LogicHandler.GetOQCBatch(ms_sncode.Text, obd_outboxcode.Text, ob_checkno.Text, "OQCCHECKNOSPLIT", out ErrorMessage);
            if (ErrorMessage == "")
            {
                BaseUtil.SetFormValue(this.Controls, dt[0]);
                if (dt[1] != null)
                {
                    BaseUtil.FillDgvWithDataTable(OutBoxDGV, dt[1]);
                }
                int oldchecknocount = dh.getRowCount("oqcbatch", "ob_oldcheckno='" + ob_checkno.Text + "'");
                new_checkno.Text = ob_checkno.Text + "-0" + (oldchecknocount + 1);
                choosedsn.Text = "0";
                totalsn.Text = OutBoxDGV.Rows.Count.ToString();
                ALL = true;
                DeleteSn.Clear();
            }
            else OperateResult.AppendText(">>" + ErrorMessage + "\n", Color.Red);
        }

        private void SplitCheckNo_Click(object sender, EventArgs e)
        {
            if (choosedsn.Text != "0")
            {
                dt = dh.getFieldsDataByCondition("oqcbatch", new string[] { "ob_status", "ob_makecode" }, "ob_checkno='" + ob_checkno.Text + "'");
                if (dt.Rows.Count > 0)
                {
                    if (dt.Rows[0]["ob_status"].ToString() == "UNCHECK")
                    {
                        List<string> sqls = new List<string>();
                        //插入新批次的主表
                        dt = (DataTable)dh.ExecuteSql("select ob_id,ob_makecode from oqcbatch where ob_checkno='" + new_checkno.Text + "'", "select");
                        string ob_id = "";
                        if (dt.Rows.Count > 0)
                        {
                            ob_id = dt.Rows[0]["ob_id"].ToString();
                            if (ob_makecode.Text != dt.Rows[0]["ob_makecode"].ToString())
                            {
                                OperateResult.AppendText(">>原批次工单" + ob_makecode.Text + ",新批次工单" + dt.Rows[0]["ob_makecode"].ToString() + ",不允许拆批\n", Color.Red);
                                return;
                            }
                        }
                        if (dt.Rows.Count == 0)
                        {
                            ob_id = dh.GetSEQ("oqcbatch_seq");
                            sql.Clear();
                            sql.Append("insert into oqcbatch(ob_id,ob_checkno,ob_indate,ob_oldcheckno,ob_source,");
                            sql.Append("ob_batchqty,ob_status,ob_prodcode,ob_sourcecode,ob_reworkcode,ob_orgcheckno,");
                            sql.Append("OB_CRAFTCODE,OB_STEPCODE,OB_MAKECODE,oB_WCCODE,OB_FACTORY,OB_PROJECTCODE,OB_DESCRIPTION,");
                            sql.Append("OB_AQLCODE,OB_REMARK,OB_INMAN) select '" + ob_id + "','" + new_checkno.Text + "',sysdate,");
                            sql.Append("'" + ob_checkno.Text + "',ob_source,ob_batchqty,ob_status,ob_prodcode, ob_sourcecode,");
                            sql.Append("ob_reworkcode,ob_orgcheckno,OB_CRAFTCODE, OB_STEPCODE,OB_MAKECODE,oB_WCCODE,OB_FACTORY,");
                            sql.Append("OB_PROJECTCODE,OB_DESCRIPTION,OB_AQLCODE,OB_REMARK,'" + User.UserCode + "' from oqcbatch where ob_checkno='" + ob_checkno.Text + "'");
                            sqls.Add(sql.GetString());
                        }
                        //插入新明细
                        sql.Clear();
                        sql.Append("insert into oqcbatchdetail(OBD_ID,OBD_OBID,OBD_CHECKNO,OBD_SNCODE,obd_outboxcode,OBD_MAKECODE");
                        sql.Append(",OBD_BUILDDATE,obd_prodcode) SELECT oqcbatchdetail_seq.nextval,'" + ob_id + "','" + new_checkno.Text + "',OBD_SNCODE,obd_outboxcode");
                        sql.Append(",OBD_MAKECODE,sysdate,obd_prodcode from oqcbatchdetail where obd_checkno='" + ob_checkno.Text + "' and obd_sncode=:obd_sncode ");
                        dh.BatchInsert(sql.GetString(), new string[] { "obd_sncode" }, DeleteSn.ToArray());
                        dh.BatchInsert("delete from oqcbatchdetail where obd_checkno='" + ob_checkno.Text + "' and obd_sncode =:obd_sncode ", new string[] { "obd_sncode" }, DeleteSn.ToArray());
                        dh.BatchInsert("update package set pa_checkno='" + new_checkno.Text + "' where pa_outboxcode in (select distinct obd_outboxcode from oqcbatchdetail where obd_sncode=:obd_sncode)", new string[] { "obd_sncode" }, DeleteSn.ToArray());
                        //删除之前的明细
                        dh.BatchInsert("update makeserial set ms_checkno='" + new_checkno.Text + "' where ms_makecode='" + ob_makecode.Text + "' and ms_sncode=:obd_sncode ", new string[] { "obd_sncode" }, DeleteSn.ToArray());
                        dh.BatchInsert("delete from oqcbatchdetail where obd_checkno='" + ob_checkno.Text + "' and obd_sncode =:obd_sncode ", new string[] { "obd_sncode" }, DeleteSn.ToArray());
                        //插入新批次的从表,更新两个批次
                        sqls.Add("update OQCBatch set ob_nowcheckqty=(select count(1) from oqcbatchdetail where obd_checkno='" + new_checkno.Text + "') where ob_checkno='" + new_checkno.Text + "'");
                        sqls.Add("update OQCBatch set ob_nowcheckqty=(select count(1) from oqcbatchdetail where obd_checkno='" + ob_checkno.Text + "') where ob_checkno='" + ob_checkno.Text + "'");
                        dh.ExecuteSQLTran(sqls.ToArray());
                        LogicHandler.DoCommandLog(Tag.ToString(), User.UserCode, "", User.UserLineCode, User.UserSourceCode, "抽检批拆解", "拆解成功,原批次" + ob_checkno.Text + "新批次:" + new_checkno.Text, "", ob_checkno.Text);
                        DeleteSn.Clear();
                        OperateResult.AppendText(">>批次" + ob_checkno.Text + ",成功拆解至批次" + new_checkno.Text + "\n ", Color.Green);
                        GetBatch.PerformClick();
                    }
                    else OperateResult.AppendText(">>批次状态必须是待检验\n", Color.Red);
                }
                else OperateResult.AppendText(">>需要拆解的批次" + ob_checkno.Text + "不存在\n", Color.Red);
            }
            else OperateResult.AppendText(">>请勾选需要拆解的序列号\n", Color.Red);
        }

        private void OutBoxDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (OutBoxDGV.Columns[e.ColumnIndex].Name == "choose")
                {
                    if (OutBoxDGV.Rows[e.RowIndex].Cells["obd_outboxcode1"].Value.ToString() != "")
                    {
                        string Checked = OutBoxDGV.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString();
                        for (int i = 0; i < OutBoxDGV.Rows.Count; i++)
                        {
                            if (OutBoxDGV.Rows[i].Cells["obd_outboxcode1"].Value.ToString() == OutBoxDGV.Rows[e.RowIndex].Cells["obd_outboxcode1"].Value.ToString())
                            {
                                if (Checked == "True")
                                    OutBoxDGV.Rows[i].Cells[0].Value = true;
                                else
                                    OutBoxDGV.Rows[i].Cells[0].Value = false;
                            }
                        }
                    }
                    int ChoosedNum = 0;
                    for (int i = 0; i < OutBoxDGV.Rows.Count; i++)
                    {
                        if (OutBoxDGV.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True")
                        {
                            ChoosedNum = ChoosedNum + 1;
                            if (!DeleteSn.Contains(OutBoxDGV.Rows[i].Cells["obd_sncode"].Value.ToString()))
                                DeleteSn.Add(OutBoxDGV.Rows[i].Cells["obd_sncode"].Value.ToString());
                        }
                    }
                    choosedsn.Text = ChoosedNum.ToString();
                }
            }
        }

        private void sncode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                bool FindSN = false;
                for (int i = 0; i < OutBoxDGV.Rows.Count; i++)
                {
                    if (sncode.Text == OutBoxDGV.Rows[i].Cells["obd_sncode"].Value.ToString())
                    {
                        if (OutBoxDGV.Rows[i].Cells[0].EditedFormattedValue.ToString() != "True")
                            OutBoxDGV.Rows[i].Cells[0].Value = true;
                        else
                            OutBoxDGV.Rows[i].Cells[0].Value = false;
                        if (OutBoxDGV.Rows[i].Cells["obd_outboxcode1"].Value.ToString() != "")
                        {
                            string Checked = OutBoxDGV.Rows[i].Cells[0].EditedFormattedValue.ToString();
                            for (int j = 0; j < OutBoxDGV.Rows.Count; j++)
                            {
                                if (OutBoxDGV.Rows[j].Cells["obd_outboxcode1"].Value.ToString() == OutBoxDGV.Rows[i].Cells["obd_outboxcode1"].Value.ToString())
                                {
                                    if (Checked == "True")
                                        OutBoxDGV.Rows[j].Cells[0].Value = true;
                                    else
                                        OutBoxDGV.Rows[j].Cells[0].Value = false;
                                }
                            }
                            FindSN = true;
                            break;
                        }
                    }
                }
                if (!FindSN)
                    OperateResult.AppendText(">>批次" + ob_checkno.Text + "不存在序列号" + sncode.Text + "\n", Color.Red);
                sncode.Text = "";
            }
        }
        bool ALL = true;
        private void ChooseAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < OutBoxDGV.Rows.Count; i++)
            {
                OutBoxDGV.Rows[i].Cells["choose"].Value = ALL;
            }
            if (!ALL)
                ALL = true;
            else
                ALL = false;
        }

        private void OutBoxDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int ChoosedNum = 0;
            for (int i = 0; i < OutBoxDGV.Rows.Count; i++)
            {
                if (OutBoxDGV.Rows[i].Cells[0].EditedFormattedValue.ToString() == "True")
                {
                    ChoosedNum = ChoosedNum + 1;
                    if (!DeleteSn.Contains(OutBoxDGV.Rows[i].Cells["obd_sncode"].Value.ToString()))
                        DeleteSn.Add(OutBoxDGV.Rows[i].Cells["obd_sncode"].Value.ToString());
                }
            }
            choosedsn.Text = ChoosedNum.ToString();
        }
    }
}