Zoe
         /// <summary>
        /// 展示终端列表(后台)(Zoe)
        /// </summary>
        /// <returns></returns>
        public KeyValuePair<int, IEnumerable<TerminalAll>> GetTerminalList(string SerialNum, int rows, int page)
        {
            if (string.IsNullOrWhiteSpace(SerialNum)) SerialNum = "";
            IExtensionRepository<NJTerminal> TerminalService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<NJTerminal>>();
            IExtensionRepository<TerminalType> TerminalTypeService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<TerminalType>>();
            IExtensionRepository<TerminalUpgradePackage> terminalUpgradePackageRepo = DIFactory.ObjectContainer.Resolve<IExtensionRepository<TerminalUpgradePackage>>();
            var TerminalQuery = TerminalService.GetModel(p => p.SerialNum.Contains(SerialNum) ).AsEnumerable();
            var TerminalTypeQuery = TerminalTypeService.GetModel().AsEnumerable();
            var terminalUpgradePackageQuery = terminalUpgradePackageRepo.GetModel().AsEnumerable();
            var query = from u in TerminalQuery
                        from o in TerminalTypeQuery
                        join p in terminalUpgradePackageQuery on o.Id equals p.TerminalTypeId into jop
                        from p in jop.DefaultIfEmpty()
                        where u.TypeId== o.Id
                        orderby u.CreateTime descending
                        select new TerminalAll
                        {
                            Id = u.Id,
                            SerialNum = u.SerialNum,
                            TypeId=o.Id,
                            TypeName=o.TypeName,
                            FirmwareVersion = p!=null? p.Versions:"",
                            SIMNumber = u.SIMNumber!=null?u.SIMNumber.ToString():"",
                            IsDisabled = u.IsDisabled,
                            StartTime = u.StartTime.ToString("yyyy-MM-dd"),
                            AreaId = u.AreaId,
                            OrganizationId = u.OrganizationId,
                        };
            IEnumerable<TerminalAll> TerminalList = query.ToList();
         
             int startIndex = (page - 1) * rows;
             int count = TerminalList.Count();
             if (count > rows && rows != -1)
            {
                if ((startIndex + rows) > count)
                    TerminalList = TerminalList.Skip(startIndex).Take(count - startIndex);
                else
                    TerminalList = TerminalList.Skip(startIndex).Take(rows);
            }
           KeyValuePair<int, IEnumerable<TerminalAll>> kv = new KeyValuePair<int, IEnumerable<TerminalAll>>(count, TerminalList);
            return kv;
        }
 public DtoResult<string> InsertTerminal(TerminalInsert model)
        {
            DtoResult<string> result = new DtoResult<string>() { Result = false };
            IExtensionRepository<AIMS.Model.NJTerminal> TerminalService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<AIMS.Model.NJTerminal>>();
            AIMS.Model.NJTerminal Terminal = TerminalService.GetModel().Where(u => u.SerialNum== model.SerialNum).FirstOrDefault();
            if (Terminal != null)
            {
                result.Message = "该终端序列号已存在!";
                return result;
            }
           Terminal = TerminalService.GetModel().Where(u => u.SIMNumber== model.SIMNumber).FirstOrDefault();
            if (Terminal != null)
            {
                result.Message = "该SIM卡号已存在!";
                return result;
            }
            else
            { 
            Terminal = new AIMS.Model.NJTerminal()
            {
                Id= Guid.NewGuid(),
                 SerialNum =model.SerialNum,
                   TypeId=model.TypeId,
                   SIMNumber=model.SIMNumber,
                   //增加时间项,防止自动生成无效的时间 by:xyong
                    CreateTime=DateTime.Now,
                    StartTime=DateTime.Now,
                    TokenTimesout=DateTime.Now,
                   //end xyong
            };
            }
            TerminalService.Insert(Terminal);
            result.Result = true;
            result.Message = "操作成功!";
            return result;
        }
  public TerminalInfo GetTerminalInfo(Guid terId)
        {
            TerminalInfo model = null;
            IExtensionRepository<AIMS.Model.NJTerminal> TerminalService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<AIMS.Model.NJTerminal>>();
            var Terminal = TerminalService.Find(terId);
            if (Terminal!=null)
            {
                model = new TerminalInfo();
                model.Id = Terminal.Id;
                model.SerialNum = Terminal.SerialNum;
                model.TypeId = Terminal.TypeId;
                model.SIMNumber = Terminal.SIMNumber;
            }
            return model;
        }
   /// <summary>
        ///修改终端(后台)(Zoe)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DtoResult<string> UpdateTerminal(TerminalUpdate model)
        {
            DtoResult<string> result = new DtoResult<string>() { Result = false };
            IExtensionRepository<AIMS.Model.NJTerminal> TerminalService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<AIMS.Model.NJTerminal>>();
            AIMS.Model.NJTerminal Terminal = TerminalService.Find(model.Id);
                if (Terminal != null)
            {
                if (Terminal.SerialNum != model.SerialNum&&Terminal.Id!=model.Id)
                {
                    AIMS.Model.NJTerminal item = TerminalService.GetModel().Where(u => u.SerialNum == model.SerialNum).FirstOrDefault();
                    if (item != null)
                    {
                        result.Message = "该终端序列号已存在!";
                        return result;
                    }
                }

                if (Terminal.SIMNumber != model.SIMNumber && Terminal.Id != model.Id)
                {
                    AIMS.Model.NJTerminal item = TerminalService.GetModel().Where(u => u.SIMNumber == model.SIMNumber).FirstOrDefault();
                    if (item != null)
                    {
                        result.Message = "该SIM卡号已存在!";
                        return result;
                    }
                }

                Terminal.SerialNum = model.SerialNum;
                Terminal.TypeId = model.TypeId;
                Terminal.SIMNumber = model.SIMNumber;
                TerminalService.Update(Terminal);
                result.Result = true;
                result.Message = "操作成功!";
                return result;
            }
            result.Message = "该终端不存在!";
            return result;
        }
  /// <summary>
        /// 删除终端(后台)(Zoe)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DtoResult<string> DelTerminal(int id)
        {
            DtoResult<string> result = new DtoResult<string>() { Result = false };
            IExtensionRepository<AIMS.Model.NJTerminal> TerminalService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<AIMS.Model.NJTerminal>>();
            AIMS.Model.NJTerminal item = TerminalService.Find(id);
            if (item != null)
            {
                item.IsDeleted = true;
                TerminalService.Update(item);
                result.Result = true;
                result.Message = "操作成功!";
                return result;
            }
            result.Message = "终端不存在,传入Id错误!";
            return result;
        }
/// <summary>
        /// 启用或禁用终端(后台)(Zoe)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DtoResult<string> SetTerminalEnabled(Guid id, bool enabled)
        {
            DtoResult<string> result = new DtoResult<string>() { Result = false };
            IExtensionRepository<AIMS.Model.NJTerminal> TerminalService = DIFactory.ObjectContainer.Resolve<IExtensionRepository<AIMS.Model.NJTerminal>>();
            AIMS.Model.NJTerminal item = TerminalService.Find(id);
            if (item != null)
            {
                item.IsDisabled = enabled;
                TerminalService.Update(item);
                result.Result = true;
                result.Message = "操作成功!";
                return result;
            }
            result.Message = "终端不存在,传入Id错误!";
            return result;
        }

 

using AIMS.Bussiness.Application.Application.NJTerminalApp;
using AIMS.Bussiness.ApplicationDto.Dto;
using AIMS.Bussiness.ApplicationDto.NJWorkTasksDto;
using AIMS.Bussiness.ApplicationDto.TerminalDto;
using AIMS.Bussiness.Interface;
using AIMS.Model;
using AIMS.Web.Helper;
using Common.BaseLibrary.Extension;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http;


namespace AIMS.Web.Api
{
    /// <summary>
    /// 终端控制器(Zoe)
    /// </summary>
    public class NJTerminalController : ApiBaseController
    {
        private INJTerminalAppService iNJTerminalAppService = null;

        public NJTerminalController(INJTerminalAppService njerminalAppService, IAIMSOrganizationService organizationService)
            : base(organizationService)
        {
            this.iNJTerminalAppService = njerminalAppService;
        }
        //===================================展示终端列表============================================================
        /// <summary>
        /// 展示终端列表(后台)
        /// </summary>
        /// <param name="SerialNum">终端序列号</param>
        /// <param name="rows">行数</param>
        /// <param name="page">页码</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/GetTerminalList")]
        public DtoResult<GridDto<List<TerminalAll>>> GetTerminalList(string SerialNum, int rows, int page)
        {
            KeyValuePair<int, IEnumerable<TerminalAll>> modelList = iNJTerminalAppService.GetTerminalList(SerialNum, rows, page);
            GridDto<List<TerminalAll>> dto = new GridDto<List<TerminalAll>>();
            dto.Total = modelList.Key;
            dto.Rows = modelList.Value.ToList();
            return DtoResultHelper<GridDto<List<TerminalAll>>>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, dto);
        }

        /// <summary>
        /// 展示终端列表(前台)管理员版本
        /// </summary>
        /// <param name="areaId"></param>
        /// <param name="serialNum"></param>
        /// <param name="nJNum"></param>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/ReceptionGetTerminalListM")]
       
        public DtoResult<GridDto<List<ReceptionTerminalAll>>> ReceptionGetTerminalListM(string areaId, string serialNum, string nJNum, int rows, int page)
        {
            KeyValuePair<int, IEnumerable<ReceptionTerminalAll>> modelList = iNJTerminalAppService.ReceptionGetTerminalListM( areaId, serialNum, nJNum, rows, page);
            GridDto<List<ReceptionTerminalAll>> dto = new GridDto<List<ReceptionTerminalAll>>();
            dto.Total = modelList.Key;
            dto.Rows = modelList.Value.ToList();
            return DtoResultHelper<GridDto<List<ReceptionTerminalAll>>>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, dto);
        }
//------------------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// 展示终端列表(前台)合作社版本
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="serialNum"></param>
        /// <param name="nJNum"></param>
        /// <param name="rows"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/ReceptionGetTerminalListH")]

        public DtoResult<GridDto<List<ReceptionTerminalAll>>> ReceptionGetTerminalListH(Guid orgId, string serialNum, string nJNum, int rows, int page)
        {
            KeyValuePair<int, IEnumerable<ReceptionTerminalAll>> modelList = iNJTerminalAppService.ReceptionGetTerminalListH(orgId, serialNum, nJNum, rows, page);
            GridDto<List<ReceptionTerminalAll>> dto = new GridDto<List<ReceptionTerminalAll>>();
            dto.Total = modelList.Key;
            dto.Rows = modelList.Value.ToList();
            return DtoResultHelper<GridDto<List<ReceptionTerminalAll>>>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, dto);
        }
        //=======================================================================================================================
        /// <summary>
        /// 增加前展示通过终端序列号找农机车牌号(前台)
        /// </summary>
        /// <param name="serialNum"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/ReceptionInsertTerminalFromSerialNum")]
        public DtoResult<ReceptionBeforeInsertTerminalFromSerialNum> ReceptionInsertTerminalFromSerialNum(string serialNum)
        {
           
            var model = iNJTerminalAppService.ReceptionInsertTerminalFromSerialNum(serialNum);
            if (model != null)
            {
                return DtoResultHelper<ReceptionBeforeInsertTerminalFromSerialNum>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, model);
            }
            return DtoResultHelper<ReceptionBeforeInsertTerminalFromSerialNum>.RetDtoResult((int)HttpStatusCode.NotFound, "没有找到对应车牌号", false, null);
        }

        /// <summary>
        /// 增加前展示通过农机车牌号找终端序列号(前台)
        /// </summary>
        /// <param name="nJNum"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/ReceptionInsertTerminalFromNJNum")]
        public DtoResult<ReceptionBeforeInsertTerminalFromNJNum> ReceptionInsertTerminalFromNJNum(string nJNum)
        {

            var model = iNJTerminalAppService.ReceptionInsertTerminalFromNJNum(nJNum);
            if (model != null)
            {
                return DtoResultHelper<ReceptionBeforeInsertTerminalFromNJNum>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, model);
            }
            return DtoResultHelper<ReceptionBeforeInsertTerminalFromNJNum>.RetDtoResult((int)HttpStatusCode.NotFound, "没有找到对应终端序列号", false, null);
        }

        //=====================================新增终端======================================================
        /// <summary>
        /// 新增终端(后台)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/api/NJTerminal/InsertTerminal")]
        public DtoResult<string> InsertTerminal([FromBody]TerminalInsert model)
        {
            DtoResult<string> dtoResult = iNJTerminalAppService.InsertTerminal(model);
            dtoResult.Code = ((int)(HttpStatusCode.OK)).ToString();
            return dtoResult;
        }
        /// <summary>
        /// 新增终端(前台)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/api/NJTerminal/ReceptionInsertTerminal")]
        public DtoResult<string> ReceptionInsertTerminal([FromBody]ReceptionTerminalInsert model)
        {
            DtoResult<string> dtoResult = iNJTerminalAppService.ReceptionInsertTerminal(model);
            dtoResult.Code = ((int)(HttpStatusCode.OK)).ToString();
            return dtoResult;
        }
        //=================================修改前展示终端列表==========================================================
        /// <summary>
        /// 修改前展示终端(后台)
        /// </summary>
        /// <param name="terId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/GetTerminalInfo")]
        public DtoResult<TerminalInfo> GetTerminalInfo(string terId)
        {
            var id = Guid.Parse(terId);
            var model = iNJTerminalAppService.GetTerminalInfo(id);
            if (model != null)
            {
                return DtoResultHelper<TerminalInfo>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, model);
            }
            return DtoResultHelper<TerminalInfo>.RetDtoResult((int)HttpStatusCode.NotFound, "没有找到对应终端", false, null);
        }

        /// <summary>
        /// 修改前展示终端列表(前台)
        /// </summary>
        /// <param name="terId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/GetReceptionTerminalInfo")]
        public DtoResult<ReceptionTerminalInfo> GetReceptionTerminalInfo(string terId)
        {
            var id = Guid.Parse(terId);
            var model = iNJTerminalAppService.GetReceptionTerminalInfo(id);
            if (model != null)
            {
                return DtoResultHelper<ReceptionTerminalInfo>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, model);
            }
            return DtoResultHelper<ReceptionTerminalInfo>.RetDtoResult((int)HttpStatusCode.NotFound, "没有找到对应终端", false, null);
        }
        //======================================更改终端===============================================================
        /// <summary>
        /// 更改终端(后台)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/api/NJTerminal/UpdateTerminal")]
        public DtoResult<string> UpdateTerminal([FromBody]TerminalUpdate model)
        {
            DtoResult<string> dtoResult = iNJTerminalAppService.UpdateTerminal(model);
            dtoResult.Code = ((int)(HttpStatusCode.OK)).ToString();
            return dtoResult;
        }


        /// <summary>
        /// 更改终端(绑定农机)(前台)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/api/NJTerminal/ReceptionUpdateTerminal")]
        public DtoResult<string> ReceptionUpdateTerminal([FromBody]ReceptionTerminalUpdate model)
        {
            DtoResult<string> dtoResult = iNJTerminalAppService.ReceptionUpdateTerminal(model);
            dtoResult.Code = ((int)(HttpStatusCode.OK)).ToString();
            return dtoResult;
        }
        //=======================================删除终端========================================================
        /// <summary>
        /// 删除终端(后台)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/api/NJTerminal/DelTerminal")]
        public DtoResult<string> DelTerminal([FromBody]int id)
        {
            DtoResult<string> dtoResult = iNJTerminalAppService.DelTerminal(id);
            dtoResult.Code = ((int)(HttpStatusCode.OK)).ToString();
            return dtoResult;
        }

        /// <summary>
        /// 删除终端(前台)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/api/NJTerminal/ReceptionDelTerminal")]
        public DtoResult<string> ReceptionDelTerminal([FromBody]int id)
        {
            DtoResult<string> dtoResult = iNJTerminalAppService.ReceptionDelTerminal(id);
            dtoResult.Code = ((int)(HttpStatusCode.OK)).ToString();
            return dtoResult;
        }
        //===========================================启用或禁用终端==========================================================
        /// <summary>
        /// 启用或禁用终端(后台)
        /// </summary>
        /// <param name="model">参数{id:终端Id,enabled:是否启用}</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/api/NJTerminal/SetTerminalEnabled")]
        public DtoResult<string> SetTerminalEnabled([FromBody]TerminalEnableModel model)
        {
            DtoResult<string> dtoResult = iNJTerminalAppService.SetTerminalEnabled(Guid.Parse(model.id), model.enabled);
            dtoResult.Code = ((int)(HttpStatusCode.OK)).ToString();
            return dtoResult;
        }

        //=================================================解绑农机(前台)============================================================================

        /// <summary>
        /// 解绑农机(前台)
        /// </summary>
        /// <param name="serialNum"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/api/NJTerminal/ReceptionUnbindTerminal")]
        public DtoResult<string> ReceptionUnbindTerminal([FromBody]string serialNum)
        {
            DtoResult<string> dtoResult = iNJTerminalAppService.ReceptionUnbindTerminal(serialNum);
            dtoResult.Code = ((int)(HttpStatusCode.OK)).ToString();
            return dtoResult;
        }

        /// <summary>
        /// 通过用户Id,初次加载树,展开第一层节点
        /// </summary>
        /// <param name="userId">用户Id guid</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/LoadAreaTreeNodeByUserId")]
        public DtoResult<List<TreeNode>> LoadAreaTreeNodeByUserId(Guid userId)
        {
            List<TreeNode> treeNodelList = iNJTerminalAppService.LoadAreaTreeNodeByUserId(userId);
            return DtoResultHelper<List<TreeNode>>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, treeNodelList);
        }

        /// <summary>
        ///通过区域Id展开树下层节点 
        /// </summary>
        /// <param name="code">区域Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/api/NJTerminal/GetAreaTreeNode")]
        public DtoResult<List<TreeNode>> GetAreaTreeNode(string code)
        {
            List<TreeNode> treeNodelList = iNJTerminalAppService.GetAreaTreeNode(code);
            return DtoResultHelper<List<TreeNode>>.RetDtoResult((int)HttpStatusCode.OK, "成功", true, treeNodelList);
        }

    }
    /// <summary>
    /// IsTerminalDisabled的参数
    /// </summary>
    public class TerminalEnableModel
    {
        /// <summary>
        /// 终端Id
        /// </summary>
        public string id { get; set; }
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool enabled { get; set; }
    }


  



}

 

posted on 2017-11-22 17:20  口袋里的SKY  阅读(628)  评论(0编辑  收藏  举报