using DeviceRepair.Api.Common;
using DeviceRepair.Api.CustomAttribute;
using DeviceRepair.DataAccess;
using DeviceRepair.Models;
using System;
using System.Collections.Generic;
using System.Web.Http;

namespace DeviceRepair.Api.Controllers
{
    [RoutePrefix("Api/Device")]
    public class DeviceController : CFController
    {
        [HttpGet]
        [Route("GetModelByEquipmentID")]
        [HttpAuthorize]
        public APIResponseData GetModelByEquipmentID(string EquipmentID)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                if (string.IsNullOrWhiteSpace(EquipmentID))
                {
                    throw new Exception("设备编号是必须的参数！");
                }

                return DeviceAccess.Instance.GetModelByEquipmentID(EquipmentID);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message.ToString();
            }
            return apiResponseData;
        }

        /// <summary>
        /// 设备台账数据获取
        /// </summary>
        /// <param name="FilterString"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetQuery")]
        [HttpAuthorize]
        public APIResponseData GetQuery(string FilterString)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据！" };
            try
            {
                return DeviceAccess.Instance.GetQuery(FilterString);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 获取树形结构
        /// </summary>
        /// <param name="FilterString"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetDeviceTreeDatas")]
        [HttpAuthorize]
        public APIResponseData GetDeviceTreeDatas(string FilterString)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据！" };
            try
            {
                return DeviceAccess.Instance.GetDeviceTreeDatas(FilterString);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="objjson"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetDataByAutoID")]
        [HttpAuthorize]
        public APIResponseData GetDataByAutoID(int pk)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                return DeviceAccess.Instance.GetDataByAutoID(pk);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 修改/新增 设备信息
        /// </summary>
        /// <param name="dictionary"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateDriveInformation")]
        [HttpAuthorize]
        public APIResponseData UpdateDriveInformation(Dictionary<string, object> dictionary)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            if (dictionary == null || dictionary.Count == 0)
                return apiResponseData;
            try
            {
                base.GetParams();
                return DeviceAccess.Instance.UpdateDriveInformation(dictionary, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }

            return apiResponseData;
        }
    }
}