using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CNAS_DBSync
{
    public class InstrumentCodeOperation
    {
        //仪器编码合法性检测

        public int CheckInstrumentCode(string strCode, List<SyncInstrumentItemInfo> lstSyncInstrument)
        {
            //为空检测
            if (strCode == null || strCode.Trim() == "") return 0;

            //命名不允许有特殊字符
            //if (!isSpecialChar(strCode)) return -1;

            //是否在库中已存在
            if (!CheckCodeRepeat(strCode, lstSyncInstrument)) return -2;

            return 1;
        }

        /// <summary>
        /// 检查是否重复
        /// </summary>
        /// <param name="strCode"></param>
        /// <param name="lstSyncInstrument"></param>
        /// <returns></returns>
        private bool CheckCodeRepeat(string strCode,List<SyncInstrumentItemInfo> lstSyncInstrument)
        {
            bool bSuccess = true;

            var item = lstSyncInstrument.Where(s => s.Code == strCode).ToList<SyncInstrumentItemInfo>();
            if (item != null && item.Count > 0)
            {
                bSuccess = false;
            }

            return bSuccess;
        }

        /// <summary>
        /// 是否含有特殊字符
        /// </summary>
        /// <param name="strCheckString"></param>
        /// <returns></returns>
        public bool isSpecialChar(String strCheckString)
        {
            Regex regex = new Regex(@"[_.`~!@#$%^&*()+=|{}':;',\\[\\]<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]|\n|\r|\t");
            return regex.IsMatch(strCheckString); ;
        }
    }
}
