using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RDH.Data.Models;

namespace RDH.Data.BLL
{
    public partial class DispensingDeviceBLL : IDispensingDeviceBLL
    {

        public DispensingDevice DispensingDevice()
        {
            var sql = BuildBaseSql();
            return SqlMapper.Query<DispensingDevice>(ConnectionFactory.Current.GetSessionConnection(), sql).FirstOrDefault();
        }

        public IEnumerable<DispensingDevice> ListDispensingDevice()
        {
            var ListDispensingDevice = SqlMapper.Query<DispensingDevice>(ConnectionFactory.Current.GetSessionConnection(), BuildBaseSql());

            return ListDispensingDevice;
        }

        public IEnumerable<DispensingDevice> ListDispensingDeviceWithSection()
        {
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.Append("SELECT ");
            foreach (KeyValuePair<String, String> pair in this.ColumnPropMaps) 
            {
                sqlBuilder.AppendFormat("d.{0} {1},", pair.Key, pair.Value);
            }
            sqlBuilder.Append("'' spiltor1,");
            SectionBLL bllSection = new SectionBLL();
            foreach (KeyValuePair<String, String> pair in bllSection.ColumnPropMaps)
            {
                sqlBuilder.AppendFormat("s.{0} {1},", pair.Key, pair.Value);
            }
            sqlBuilder.Remove(sqlBuilder.Length - 1, 1);
            sqlBuilder.AppendFormat(" FROM {0} d"
                + " LEFT JOIN {1} s ON s.KEY=d.OPERATION_ROOM_KEY"
                + " WHERE LOWER(d.ENABEL_FLAG)='true'",
                this.TABLE_NAME, bllSection.TABLE_NAME);
            return SqlMapper.Query<DispensingDevice, Section, DispensingDevice>(ConnectionFactory.Current.GetSessionConnection(),
                sqlBuilder.ToString(),
                (d, s) =>
                {
                    d.SectionInfo = s;
                    return d;
                },
                splitOn: "spiltor1");
        }

        public bool IsCodeRegister(DispensingDevice deviceByCode)
        {
            var param = GetParam(deviceByCode);

            //string sql = "";
            //if (deviceByCode.LastKey != null)
            //{
            //    sql = string.Format("{0} WHERE UPPER(\"DEVICE_CODE\")=UPPER(:DeviceCode_) AND \"KEY\"<>:LastKey_", this.BuildBaseSql());
            //}
            //else
            //{
            string sql = string.Format("{0} WHERE UPPER(DEVICE_CODE)=UPPER(:DeviceCode_) AND ENABEL_FLAG='True'", this.BuildBaseSql());
            //}

            return SqlMapper.Query<DispensingDevice>(ConnectionFactory.Current.GetSessionConnection(), sql, param).Count() > 0;
        }

        /// <summary>
        /// 是不是加上LastKey
        /// </summary>
        public bool IsNodeExit(DispensingDevice deviceByNode)
        {
            var param = GetParam(deviceByNode);

            //string sql = "";
            //if (deviceByNode.LastKey != null)
            //{
            //    sql = string.Format("{0} WHERE UPPER(\"NODE_ID\")=UPPER(:NodeId_) AND \"KEY\"<>:LastKey_", this.BuildBaseSql());
            //}
            //else
            //{
            string sql = string.Format("{0} WHERE UPPER(\"NODE_ID\")=UPPER(:NodeId_) AND ENABEL_FLAG='True'", this.BuildBaseSql());
            //}

            return SqlMapper.Query<DispensingDevice>(ConnectionFactory.Current.GetSessionConnection(), sql, param).Count() > 0;
        }

        /// <summary>
        /// 1.复制 STORAGE_SPACE  DISPENSING_DEVICE数据
        /// 2.删除所有数据
        /// 3.写数据到DISPENSING_DEVICE STORAGE_SPACE
        /// 4.写配置详情到SYS_DICTIONARY
        /// 5.重新启动程序
        /// </summary>
        public void ResetDevice()
        {
            
        }
    }

}