using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MediII.Adapter.Scan.Interface;
using MediII.Adapter.Builder.Interface;

namespace MediII.Adapter.Scanner.@@SYSNAME
{
    public class @@SCANNAME_Scanner : DBScaner, IScan
    {
        public override long Do(DateTime dtEventTime)
        {
            IEnumerable<DataRow> listRow;
            string sql = "@@SQL";
            listRow = ctx.ExecuteDataSet(CommandType.Text, sql).Tables[0].AsEnumerable();

            if (listRow.Count() > 0)
            {
                List<MessageItem> listMessage = BuildMSG<DataRow>(listRow, entity =>
                {
                    //OML_O21_Builder builder = HIS_MessageBuilder.GetBuilder<OML_O21_Builder>();
                    //builder.BuildCheXiao(entity);
                    //return builder.MessageHolder;
					var o = new NHapi.Model.V24.Message.OML_O21();
                    o.MSH.MessageControlID.Value = Guid.NewGuid().ToString("N");
                    return o;
					//return null;
                });

                BeforeSend(listMessage.Select(s => s.MessageStruct).ToList());
                SendMessages<DataRow>(listMessage, list =>
                {
                    string[] idList = list.Select(s => s["XUHAO"].ToString()).ToArray();
                    string sqlUpdate = string.Format("update hl7_l_laborder set zhuangtai = 1 where xuhao in ('{0}')", string.Join(",", idList));
                    SqlDataAccess.ExecuteNonQuery(ctx,System.Data.CommandType.Text, sqlUpdate);
                    return 1;
                });
            }
            return 0;
        }
    }
}
