EMS單號規律與順豐單號規律(C#)

weixin_34402090發表於2012-10-08
 /// <summary>
        /// 獲取EMS物流單號
        /// </summary>
        /// <param name="strNo">當前物流單號</param>
        /// <returns></returns>
        public static string GetNewEmsNo(string strNo)
        {
            try
            {

                string fri = "";
                int res;
                int num3, num4, num5, num6, num7, num8, num9, num0;
                int mid;
                string strZero = "00000000";
                fri = (Convert.ToUInt32(strNo.Substring(2, 8)) + 1).ToString();
                //不足零自動補零
                //if (fri.Length < 8)
                //{
                //    string strTmp = strZero + fri;
                //    fri = strTmp.Substring(strTmp.Length - 8);
                //}
                fri=fri.PadLeft(8, '0');
                num3 = Convert.ToInt32(fri.Substring(0, 1));
                num4 = Convert.ToInt32(fri.Substring(1, 1));
                num5 = Convert.ToInt32(fri.Substring(2, 1));
                num6 = Convert.ToInt32(fri.Substring(3, 1));
                num7 = Convert.ToInt32(fri.Substring(4, 1));
                num8 = Convert.ToInt32(fri.Substring(5, 1));
                num9 = Convert.ToInt32(fri.Substring(6, 1));
                num0 = Convert.ToInt32(fri.Substring(7, 1));
                mid = 8 * num3 + 6 * num4 + 4 * num5 + 2 * num6 + 3 * num7 + 5 * num8 + 9 * num9 + 7 * num0;
                res = 11 - (mid) % (11);
                if (res == 10)
                    res = 0;
                if (res == 11)
                    res = 5;

                string strHead = strNo.Substring(0, 2);
                if (!IsLetter(strHead))
                {
                    throw new Exception("EMS單號前2位不為字母");
                }
                string strEnd = strNo.Substring(strNo.Length - 2);
                if (!IsLetter(strEnd))
                {
                    throw new Exception("EMS單號末2位不為字母");
                }

                string strEMSres = (strHead + fri + res.ToString() + strEnd);
                return strEMSres;

            }
            catch (Exception ex)
            {
                MessageForm.Error(ex.Message);
                return string.Empty;
            }
        }
    /// <summary>
        /// 獲取順豐物流單號
        /// </summary>
        /// <param name="strNo"></param>
        /// <returns></returns>
        public static string GetNewShunfengNo(string strNo)
        {
            try
            {
                if (strNo.Length != 12)
                {
                    throw new Exception("順豐單號為12位,請檢測單號是否正確");
                }
                string fri, Nfri, Yuandanhao, strNewNo;
                int num9, num10, num11, num12;
                int Nnum9, Nnum12;
                int iIndex = 8;

                fri = strNo.Substring(0, 11);
                Yuandanhao = strNo;

                Nfri = (Convert.ToInt64(fri) + 1).ToString().PadLeft(11,'0');
                num9 = Convert.ToInt32(Yuandanhao.Substring(iIndex++, 1));
                num10 = Convert.ToInt32(Yuandanhao.Substring(iIndex++, 1));
                num11 = Convert.ToInt32(Yuandanhao.Substring(iIndex++, 1));
                num12 = Convert.ToInt32(Yuandanhao.Substring(iIndex++, 1));

                iIndex = 8;
                Nnum9 = Convert.ToInt32(Nfri.Substring(iIndex++, 1));

                if ((Nnum9 - num9 == 1) && ((num9) % (2) == 1))
                {
                    if (num12 - 8 >= 0)
                        Nnum12 = num12 - 8;          // -8
                    else
                        Nnum12 = num12 - 8 + 10;
                }
                else if ((Nnum9 - num9 == 1) && ((num9) % (2) == 0))
                {
                    if (num12 - 7 >= 0)
                        Nnum12 = num12 - 7;         // -7
                    else
                        Nnum12 = num12 - 7 + 10;
                }
                else
                {
                    if (((num10 == 3) || (num10 == 6)) && (num11 == 9))
                    {
                        if (num12 - 5 >= 0)
                            Nnum12 = num12 - 5;            // -5
                        else
                            Nnum12 = num12 - 5 + 10;
                    }
                    else if (num11 == 9)
                    {
                        if (num12 - 4 >= 0)
                            Nnum12 = num12 - 4;          // -4
                        else
                            Nnum12 = num12 - 4 + 10;
                    }
                    else
                    {
                        if (num12 - 1 >= 0)
                            Nnum12 = num12 - 1;           // -1
                        else
                            Nnum12 = num12 - 1 + 10;
                    }
                }
                return strNewNo = Nfri + Nnum12.ToString();
            }
            catch (Exception ex)
            {
                throw ex;                
            }            
        }

  public static bool IsLetter(string str)
        {
            foreach (char c in str)
            {
                if (!char.IsLetter(c))
                {
                    return false;
                }
            }
            return true;
        }

 

相關文章