using FCOMMON;
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;

namespace FEQ0000
{
    public partial class fPurchase_Data : fBase
    {
        public fPurchase_Data()
        {
            InitializeComponent();
            Properties.Settings.Default["gwcs"] = FCOMMON.info.CS;
            Properties.Settings.Default["EEEntities"] = FCOMMON.info.CS;
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            EnsureVisibleAndUsableSize();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dataList = FCOMMON.DBM.getGroupList("supply", "Purchase", "supply <> ''");
            var cn = FCOMMON.DBM.getCn();
            cn.Open();
            var cmd = new System.Data.SqlClient.SqlCommand("", cn);

            int dataCnt = 0;
            foreach(var item in dataList)
            {
                //이 코드가 서플라이목록에 없다면 추가한다.
                string sql = "select count(*) from Customs where name='" + item.Trim() + "'";
                cmd.CommandText = sql;
                var cnt = (int)(cmd.ExecuteScalar());
                if(cnt == 0)
                {
                    sql = "insert into Customs(grp,name,wuid,wdate,memo) values('{0}','{1}','{2}',getdate(),'{3}')";
                    sql = string.Format(sql, "Supply",item.Replace("'","''"),FCOMMON.info.Login.no,"IMP");
                    cmd.CommandText = sql;
                    dataCnt += cmd.ExecuteNonQuery();
                }
            }

            //모든 서플라이이름을 가지고 index를 찾아서 업데이트 한다.
            dataCnt = 0;
            foreach (var item in dataList)
            {
                //이서플라이이의 코드를 찾는다.
                cmd.CommandText = "select idx from Customs where name='" + item + "'";
                var Rawidx = cmd.ExecuteScalar();
                if (Rawidx == DBNull.Value) continue;

                cmd.CommandText = "update Purchase set " +
                    " supplyidx=" + Rawidx.ToString() +
                    " where supply = '" + item + "'";
                cmd.ExecuteNonQuery();
            }
            cmd.CommandText = "";
            

            cn.Close();
            FCOMMON.Util.MsgI("inserted supply customs");
        }
    }
}
