TBaseStationController.cs 8.8 KB
using Infrastructure;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using WebApp;
using WebRepository;

namespace WebMvc
{
    /// <summary>
	/// TBaseStation
	/// </summary>
    [Area("acs")]
    public class TBaseStationController : BaseController
    {
        private readonly TBaseStationApp _app;

        public TBaseStationController(IAuth authUtil, TBaseStationApp app) : base(authUtil)
        {
            _app = app.SetLoginInfo(_loginInfo);
        }

        #region 视图功能
        /// <summary>
        /// 默认视图Action
        /// </summary>
        /// <returns></returns>
        [Authenticate]
        [ServiceFilter(typeof(OperLogFilter))]
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 加载及分页查询
        /// </summary>
        /// <param name="pageRequest">表单请求信息</param>
        /// <param name="entity">请求条件实例</param>
        /// <returns></returns>
        [HttpPost]
        public string Load(PageReq pageRequest, TBaseStation entity)
        {
            entity.StationType = 4;
            return JsonHelper.Instance.Serialize(_app.Query(entity));
            return JsonHelper.Instance.Serialize(_app.Load(pageRequest, entity));
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="Table_entity">新增实例</param>
        /// <returns></returns>
        [HttpPost]
        [ServiceFilter(typeof(OperLogFilter))]
        public string Ins(TBaseStation Table_entity)
        {
            try
            {
                ApiRequest apiRequest = new ApiRequest("RCS");
                var stationState = Table_entity.StationState;
                TBaseStation baseStation = _app.FindSingle(u => u.Id == Table_entity.Id);
                if (baseStation != null && baseStation.StationState != 0)
                {
                    stationState = baseStation.StationState;
                }
                string parameter = JsonHelper.Instance.Serialize(new ChangeStationModel
                {
                    stationType = Table_entity.StationType.Value,
                    Group = Table_entity.StationGroup,
                    Area = Table_entity.StationArea,
                    isEnable = Table_entity.IsEnable ?? false,
                    isLocked = Table_entity.IsLocked ?? false,
                    Barcode = Table_entity.StrBarcode,
                    Name = Table_entity.StrStationNo,
                    stationState = stationState.Value,
                    type = "insert",
                }); 
                Response response = apiRequest.Post<Response>(parameter, "ChangeStation", "RCS");
                if (response.Code != 0)
                {
                    throw new Exception(response.Message);
                }
            }
            catch (Exception ex)
            {
                Result.Status = false;
                Result.Message = ex.Message;
            }
            return JsonHelper.Instance.Serialize(Result);
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="Table_entity">修改实例</param>
        /// <returns></returns>
        [HttpPost]
        [ServiceFilter(typeof(OperLogFilter))]
        public string Upd(TBaseStation Table_entity)
        {
            try
            {
                //strBarcode也就是RFID必须是唯一,且一旦insert成功不允许更改
                TBaseStation baseStation = _app.FindSingle(u => u.Id == Table_entity.Id);
                Table_entity.StrBarcode = baseStation.StrBarcode;
                var stationState = Table_entity.StationState;
                if (baseStation != null && baseStation.StationState != 0)
                {
                    stationState = baseStation.StationState;
                }
                ApiRequest apiRequest = new ApiRequest("RCS");
                string parameter = JsonHelper.Instance.Serialize(new ChangeStationModel
                {
                    stationType = Table_entity.StationType.Value,
                    Group = Table_entity.StationGroup,
                    Area = Table_entity.StationArea,
                    isEnable = Table_entity.IsEnable ?? false,
                    isLocked = Table_entity.IsLocked ?? false,
                    Barcode = baseStation.StrBarcode,
                    Name = Table_entity.StrStationNo,
                    stationState = Table_entity.StationState.Value,
                    type = "update",
                }); 
                Response response = apiRequest.Post<Response>(parameter, "ChangeStation", "RCS");
                if (response.Code != 0)
                {
                    throw new Exception(response.Message);
                }
            }
            catch (Exception ex)
            {
                Result.Status = false;
                Result.Message = ex.Message;
            }
            return JsonHelper.Instance.Serialize(Result);
        }

        [HttpPost]
        [ServiceFilter(typeof(OperLogFilter))]
        public string DelByIds(int[] ids)
        {
            try
            {
                foreach (var item in ids)
                {
                    TBaseStation baseStation = _app.FindSingle(u => u.Id == item);
                    ApiRequest apiRequest = new ApiRequest("RCS");

                    string parameter = JsonHelper.Instance.Serialize(new ChangeStationModel
                    {
                        stationType = baseStation.StationType.Value,
                        Group = baseStation.StationGroup,
                        Area = baseStation.StationArea,
                        isEnable = baseStation.IsEnable ?? false,
                        isLocked = baseStation.IsLocked ?? false,
                        Barcode = baseStation.StrBarcode,
                        Name = baseStation.StrStationNo,
                        stationState = baseStation.StationState.Value,
                        type = "delete",
                    }); 
                    Response response = apiRequest.Post<Response>(parameter, "ChangeStation", "RCS");
                    if (response.Code != 0)
                    {
                        throw new Exception(response.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Result.Status = false;
                Result.Message = ex.Message;
            }
            return JsonHelper.Instance.Serialize(Result);
        }
        #endregion

        #region 导出数据
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="entity">请求条件实例</param>
        /// <returns></returns>
        [HttpPost]
        public string Export(TBaseStation entity)
        {
            return JsonHelper.Instance.Serialize(_app.ExportData(entity));
        }
        #endregion

        #region 导出模板
        /// <summary>
        /// 导出模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string GetTemplate()
        {
            var result = new TableData();
            List<TBaseStation> listTBaseStation = new List<TBaseStation>();
            TBaseStation entity = _app.FindSingle(u => u.Id > 0);
            if (entity != null)
            {
                listTBaseStation.Add(entity);
            }
            else
            {
                listTBaseStation.Add(new TBaseStation());
            }

            result.data = listTBaseStation;
            result.count = listTBaseStation.Count;

            return JsonHelper.Instance.Serialize(result);
        }
        #endregion

        #region 导入数据
        /// <summary>
        /// 导入数据
        /// </summary>
        /// <param name="excelfile">表单提交的文件信息</param>
        /// <returns></returns>
        [HttpPost]
        public string Import(IFormFile excelfile)
        {
            try
            {
                Response result = _app.ImportIn(excelfile);
                if (!result.Status)
                {
                    Result.Status = false;
                    Result.Message = result.Message;
                }
            }
            catch (Exception ex)
            {
                Result.Status = false;
                Result.Message = ex.Message;
            }
            return JsonHelper.Instance.Serialize(Result);
        }
        #endregion

        #region 自定义方法
        /// <summary>
        /// 获取站台信息
        /// </summary>
        /// <returns></returns>
        public string GetStationInfo()
        {
            return JsonHelper.Instance.Serialize(_app.GetStationInfo());
        }
        #endregion
    }
}