using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYY.Core.Standard.Data.Infrastructure;
using Dapper;
namespace XYY.Service.Standard.Order
{
    public interface IOrderNumberService
    {
        List<string> GetNumber(int qty);
    }

    public class GetOrderNumbserService : IOrderNumberService
    {
        private IUnitOfWork _unitOfWork;

        public GetOrderNumbserService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public List<string> GetNumber(int qty)
        {
            List<string> numbers = new List<string>();
            string connText = _unitOfWork.ConnectionAddress;
            using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connText))
            {
                string sql = @"INSERT INTO  SysNewID (TmpInt) OUTPUT  inserted.ID Values(1); ";
                using (System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(sql, conn))
                {
                    try
                    {
                        conn.Open();
                        for (int i = 0; i < qty; i++)
                        {
                            var val = command.ExecuteScalar();
                            try
                            {
                                int id = (int)val;
                                numbers.Add("XYYEX" + id.ToString("000000000") + "YQ");
                            }
                            catch
                            {
                                throw new Exception("标识生成失败");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("标识列生成失败:" + ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
            return numbers;
        }
    }
}
