using DeviceRepair.Models;
using DeviceRepair.Utils.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Http;

namespace DeviceRepair.Api.Common
{
    public class CFController : ApiController
    {
        public HeaderModel OperationInfo = null;

        /// <summary>
        /// 获取Header中的操作人信息
        /// </summary>
        [NonAction]
        public void GetParams()
        {
            OperationInfo = new HeaderModel();
            Guid guid = Guid.Empty;
            try
            {
                var props = typeof(HeaderModel).GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
                foreach (PropertyInfo prop in props)
                {
                    IEnumerable<string> values;
                    if (Request.Headers.TryGetValues(prop.Name, out values))
                    {
                        if (values != null && values.Count() > 0)
                        {
                            string value = values.FirstOrDefault();
                            if (!string.IsNullOrWhiteSpace(value))
                            {
                                value = EncryptionHelper.UrlDecode(value);
                                if (prop.PropertyType == typeof(Guid?) && Guid.TryParse(value, out guid))
                                {
                                    prop.SetValue(OperationInfo, guid, null);
                                }
                                else
                                {
                                    prop.SetValue(OperationInfo, Convert.ChangeType(value, prop.PropertyType), null);
                                }
                            }
                        }
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
        }
    }
}