コード例 #1
0
        public Dictionary <string, int> dicsOut; //实际出药数据表
        //开始出药
        public void Out()
        {
            //锁定出药
            IsOut = true;

            //添加计数列,初始化为0
            dtToOut.Columns.Add("Record");
            dtToOut.Columns.Add("Error");
            foreach (DataRow row in dtToOut.Rows)
            {
                row["Record"] = 0;
                row["Error"]  = 0;
            }

            bool haveOut = false;

            //检查每个储位库存是否足够出药,库存不足error=100,忽略出药
            foreach (DataRow row in dtToOut.Rows)
            {
                string poscode = row["poscode"].ToString().Trim();
                int    num     = int.Parse(row["num"].ToString().Trim());
                //查询是否缺药
                string s;
                string sql = "select isnull(drugnum,0) from drug_pos where maccode='{0}' and poscode='{1}' and errorNum<3";
                sql = string.Format(sql, Config.Soft.MacCode, poscode);
                csSql.ExecuteScalar(sql, Config.Soft.ConnString, out s);
                int kc = 0;
                if (s != null)
                {
                    kc = int.Parse(s);
                }
                if (num > kc)
                {
                    row["error"] = 100;
                }
            }

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

            if (NeedOut(dtToOut, out poss))
            {
                PLC_Tcp_CP.ChangeOut(1);

                if (!PLC_Tcp_CP.LiftOnMeet())
                {
                    PLC_Tcp_CP.LiftAutoMoveToPos(PLC_Tcp_AP.MovePos.Meet);
                }
                if (!PLC_Com_CP.BaffleOnClose())
                {
                    PLC_Com_CP.BaffleClose();
                }

                //ThrowMsg("出药准备");
                PLC_Tcp_CP.TransferBeltMove(PLC_Tcp_AP.TransferBeltMoveType.Right);
                Thread.Sleep(Config.Mac_C.WaitTime_Start);

                //ThrowMsg("等待提升机到位");
                DateTime timeBegin = DateTime.Now;
                Thread.Sleep(200);
                while (!PLC_Tcp_CP.LiftOnMeet())
                {
                    if (DateTime.Now > timeBegin.AddSeconds(Config.Mac_C.WaitTime_Auto_Lift))
                    {
                        ThrowMsg("提升机未到位");
                        IsOut = false;
                        return;
                    }
                    Thread.Sleep(200);
                }
                //ThrowMsg("等待挡板关闭");
                timeBegin = DateTime.Now;
                Thread.Sleep(200);
                while (!PLC_Com_CP.BaffleOnClose())
                {
                    if (DateTime.Now > timeBegin.AddSeconds(Config.Mac_C.WaitTime_Auto_Baffle))
                    {
                        ThrowMsg("挡板未关闭");
                        IsOut = false;
                        return;
                    }
                    Thread.Sleep(200);
                }
            }

            //出药开始标签
outStart:
            NeedOut(dtToOut, out poss);
            //计数清零
            PLC_Tcp_CP.ClearRecordMutil(poss);
            Dictionary <string, int> dicsOldRecord = PLC_Tcp_CP.ReadRecordMutil(poss);
            Dictionary <string, int> dicsNewRecord;

            //ThrowMsg("开始出药");
            while (NeedOut(dtToOut, out poss))
            {
                //ThrowMsg("出药动作");
                PLC_Tcp_CP.DCTMoveDownMutil(poss);
                //ThrowMsg("等待计数");
                Thread.Sleep(1000);
                dicsNewRecord = PLC_Tcp_CP.ReadRecordMutil(poss);
                //ThrowMsg("更新出药数据表");
                foreach (string p in dicsNewRecord.Keys)
                {
                    int oldRecord = 0;
                    if (dicsOldRecord.Keys.Contains(p))
                    {
                        oldRecord = dicsOldRecord[p];
                    }
                    else
                    {
                        dicsOldRecord.Add(p, 0);
                    }
                    int       newRecord = dicsNewRecord[p];
                    DataRow[] rows      = dtToOut.Select("poscode ='" + p + "'");
                    rows[0]["record"] = newRecord;
                    if (newRecord - oldRecord != 1)
                    {
                        rows[0]["error"] = int.Parse(rows[0]["error"].ToString()) + 1;

                        ThrowMsg("记录出药异常");
                        string sql = "insert into sys_error select '{0}','{1}',drugonlycode,'OE',1,'{2}',getdate() from drug_pos where poscode='{1}'";
                        sql  = string.Format(sql, Config.Soft.MacCode, p, newRecord - oldRecord);
                        sql += ";update drug_pos set errornum=(errornum+1) where maccode='{0}' and poscode='{1}'";
                        sql  = string.Format(sql, Config.Soft.MacCode, p);
                        csSql.ExecuteSql(sql, Config.Soft.ConnString);
                    }
                }
                dicsOldRecord = dicsNewRecord;
            }
            //ThrowMsg("出药数据表");
            foreach (DataRow row in dtToOut.Rows)
            {
                string poscode = row["poscode"].ToString().Trim();
                int    record  = int.Parse(row["record"].ToString().Trim());
                if (record > 0)
                {
                    haveOut = true;
                    if (dicsOut.Keys.Contains(poscode))
                    {
                        dicsOut[poscode] += record;
                    }
                    else
                    {
                        dicsOut.Add(poscode, record);
                    }
                }
            }

            //ThrowMsg("补偿出药");
            if (BC)
            {
                DataTable dtBC = dtToOut.DefaultView.ToTable();
                dtBC.Clear();
                foreach (DataRow row in dtToOut.Rows)
                {
                    string poscode = row["poscode"].ToString().Trim();
                    int    num     = int.Parse(row["num"].ToString().Trim());
                    int    record  = int.Parse(row["record"].ToString().Trim());
                    if (num > record)
                    {
                        string drugOnlyCode;
                        string sql = "select drugonlycode from drug_pos where maccode='{0}' and poscode='{1}'";
                        sql = string.Format(sql, Config.Soft.MacCode, poscode);
                        csSql.ExecuteScalar(sql, Config.Soft.ConnString, out drugOnlyCode);
                        if (!string.IsNullOrEmpty(drugOnlyCode))
                        {
                            BCOut(drugOnlyCode, num - record, ref dtBC);
                        }
                    }
                }
                dtToOut = dtBC;
                BC      = false;
                goto outStart;
            }

            //ThrowMsg("更新库存");
            UpdateStock(dicsOut);
            //ThrowMsg("更新出药结果(多出,少出)");
            if (!string.IsNullOrEmpty(PrescNo))
            {
                UpdateOutResult(dicsOut, dtDrug);
            }

            if (haveOut)
            {
                Thread.Sleep(Config.Mac_C.WaitTime_Stop);
            }

            PLC_Tcp_CP.TransferBeltMove(PLC_Tcp_AP.TransferBeltMoveType.Stop);
            if (haveOut)
            {
                if (OutPosition == PLC_Tcp_AP.MovePos.Top)
                {
                    PLC_Tcp_CP.TopBeltMove(PLC_Tcp_AP.TopBeltMoveType.Turn);
                }

                //ThrowMsg("送至出药口");
                PLC_Tcp_CP.LiftAutoMoveToPos(OutPosition);
                DateTime timeBegin = DateTime.Now;
                Thread.Sleep(500);
                while (!PLC_Tcp_CP.LiftAutoMoveIsOK())
                {
                    if (DateTime.Now > timeBegin.AddSeconds(Config.Mac_C.WaitTime_Auto_Lift))
                    {
                        ThrowMsg("提升机未到位");
                        IsOut = false;
                        return;
                    }
                    Thread.Sleep(200);
                }

                //ThrowMsg("出药");
                PLC_Com_CP.BaffleOpen();
                while (!PLC_Com_CP.BaffleOnOpen())
                {
                    Thread.Sleep(200);
                }
                Thread.Sleep(Config.Mac_C.OpenTime_Baffle);
                PLC_Com_CP.BaffleClose();

                if (OutPosition == PLC_Tcp_AP.MovePos.Top)
                {
                    Thread.Sleep(2000);
                    PLC_Tcp_CP.TopBeltMove(PLC_Tcp_AP.TopBeltMoveType.Stop);
                }
                //ThrowMsg("出药返回");
                PLC_Tcp_CP.LiftAutoMoveToPos(PLC_Tcp_AP.MovePos.Meet);

                IsOut = false;
            }
            else
            {
                IsOut = false;
            }
        }