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


namespace DeviceRepair.Api.Controllers
{
    [RoutePrefix("Api/Field")]
    public class FieldController : CFController
    {
        /// <summary>
        /// 根据编码查询字段信息
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        [HttpGet, HttpAuthorize, Route("GetQuery")]
        public APIResponseData Query(string Code)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                return FieldAccess.Instance.GetFieldsByCodes(Code);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 检验数据是否存在
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Text"></param>
        /// <returns></returns>
        [HttpGet, HttpAuthorize, Route("IsExists")]
        public APIResponseData Exists(string Code, string Text)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "未能获取到数据" };
            try
            {
                apiResponseData = FieldAccess.Instance.TextIsExists(Code, Text);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 数据新增或者编辑
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("InsertOrEdit")]
        public APIResponseData InsertOrEdit(FieldsInfo entity)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                base.GetParams();
                apiResponseData = FieldAccess.Instance.InsertOrEdit(entity, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="AutoID"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        [HttpPost, HttpAuthorize, Route("ChangeStatus")]
        public APIResponseData ChangeStatus(int AutoID, bool Status, string Description)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1 };
            try
            {
                base.GetParams();
                apiResponseData = FieldAccess.Instance.ChangeStatus(AutoID, Status, Description, OperationInfo);
            }
            catch (Exception ex)
            {
                apiResponseData.Code = -1;
                apiResponseData.Message = ex.Message;
            }
            return apiResponseData;
        }
    }
}
