IPListController

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PT.BLL;
using PT.Common;
using PT.Model;
using PT.Web.Mvc.App_Start;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PT.Web.Mvc.Controllers
{
    [LoginAuthorizationFilter]
    public class IPListController : BaseController<IPList, IPList>
    {
        IPListBll bll = new IPListBll();
        private SessionHelper sessionHelper;
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Edit()
        {
            return View();
        }


        public IActionResult IsExistMacAddress(IPList t)
        {
            ResponseResult result = new ResponseResult()
            {
                code = 0,
                msg = "成功",
                data = 0

            };
            try
            {
                bool b = bll.IsExistMacAddress(t);
                result.data = b;

            }
            catch (Exception ex)
            {

                result.code = -1;
                result.msg = ex.Message;
            }

            var res = new JsonResult(result);
            return res;

        }


        /// <summary>
        /// 视图 多表联查 查询表未删除数据 按条件查询 (根据业务重写)
        /// </summary>
        /// <returns></returns>
        public IActionResult QueryableNotDeletedVWhere(IPListV v)
        {
            LayuiDataResult<IPListV> dataResult = new LayuiDataResult<IPListV>()
            {
                code = 0,
                msg = "成功",
                count = 0,
                data = new List<IPListV>()

            };
            try
            {
                List<IPListV> list = baseBll.QueryableNotDeletedV(v);
                dataResult.count = list.Count;
                dataResult.data = list;

            }
            catch (Exception ex)
            {

                dataResult.code = -1;
                dataResult.msg = ex.Message;
            }

            var res = new JsonResult(dataResult);
            return res;

        }


        /// <summary>
        /// 视图 多表联查 分页查询 查询表未删除数据 按条件查询 (根据业务重写)
        /// </summary>
        /// <param name="where"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public IActionResult QueryablePageList(IPListV where, int page, int limit)
        {
            LayuiDataResult<IPListV> dataResult = new LayuiDataResult<IPListV>()
            {
                code = 0,
                msg = "成功",
                count = 0,
                data = new List<IPListV>()

            };
            try
            {
                int totalCount = 0;
                List<IPListV> list = baseBll.QueryablePageList(where, page, limit, out totalCount);
                dataResult.count = totalCount;
                dataResult.data = list;

                //string s = "{\"Floor\":\"" + where.Floor + "\",\"IP\":\"" + where.IP + "\",\"UserName\":\"" + where.UserName + "\",\"AssgnName\":\"" + where.AssgnName + "\"" +
                //    ",\"IPAddress\":\"" + where.IPAddress + "\",\"MacAddress\":\"" + where.MacAddress + "\",\"NodeName\":\"" + where.NodeName + "\"}";

                string s = "{\"Floor\":\"" + where.Floor + "\",\"IP\":\"" + where.IP + "\",\"SearchType\":\"" + where.SearchType + "\",\"SearchVal\":\"" + where.SearchVal + "\"}";
                AddSystemLog(new SystemLog() { OperateType = "query", OperateModule = "SearchIP", OperateFunction = "查询", OperatePage = "查询分配IP信息:IPList/Index", OperateBefore = "查询条件:" + s, OperateAfter = "" });

            }
            catch (Exception ex)
            {

                dataResult.code = -1;
                dataResult.msg = ex.Message;
            }

            var res = new JsonResult(dataResult);
            return res;

        }

        public override IActionResult InsertableIgnoreColumns(IPList t)
        {
            ResponseResult result = new ResponseResult()
            {
                code = 0,
                msg = "成功",
                data = 0

            };
            try
            {

                if (bll.IsExist(t))
                {

                    result.code = -2;
                    result.msg = "IP已存在!";
                }
                else
                {
                    sessionHelper = new SessionHelper(HttpContext.Session);
                    UserList user = sessionHelper.GetUserInfoString();
                    t.CreatBy = user.Id + "|" + user.UserID + "|" + user.UserName;
                    t.CreatDate = DateTime.Now;
                    result.data = bll.InsertableIgnoreColumns(t);

                    AddSystemLog(new SystemLog() { OperateType = "add", OperateModule = "SearchIP", OperateFunction = "新增IP信息", OperatePage = "新增IP信息:IPList/Edit", OperateBefore = "", OperateAfter = t.ToJson() });
                }

            }
            catch (Exception ex)
            {

                result.code = -1;
                result.msg = ex.Message;
            }

            var res = new JsonResult(result);
            return res;
        }

        public override IActionResult UpdateableIgnoreColumns(IPList t)
        {

            ResponseResult result = new ResponseResult()
            {
                code = 0,
                msg = "成功",
                data = 0

            };
            try
            {
                sessionHelper = new SessionHelper(HttpContext.Session);
                UserList user = sessionHelper.GetUserInfoString();
                t.Assgn = user.Id + "|" + user.UserID + "|" + user.UserName;
                t.ModifyBy = user.Id + "|" + user.UserID + "|" + user.UserName;
                t.ModifyDate = DateTime.Now;

                result.data = baseBll.UpdateableIgnoreColumns(t);

            }
            catch (Exception ex)
            {

                result.code = -1;
                result.msg = ex.Message;
            }

            var res = new JsonResult(result);
            return res;




        }



        public override IActionResult UpdateableIgnoreColumnsWhere(IPList t)
        {
            ResponseResult result = new ResponseResult()
            {
                code = 0,
                msg = "成功",
                data = 0

            };
            try
            {
                if (bll.IsExistMacAddress(t))
                {
                    result.code = -2;
                    result.msg = "同Floor之内MacAddress不能重复!";
                    return new JsonResult(result);
                }

                sessionHelper = new SessionHelper(HttpContext.Session);
                UserList user = sessionHelper.GetUserInfoString();
                t.Assgn = user.Id + "|" + user.UserID + "|" + user.UserName;
                t.ModifyBy = user.Id + "|" + user.UserID + "|" + user.UserName;
                t.ModifyDate = DateTime.Now;
                t.Date = DateTime.Now;
                IPList tt;
                string _OperateBefore = "", _OperateAfter = "";
                if (t.Id > 0)
                {
                    tt = baseBll.QueryableInSingle(t.Id.ToString());
                    _OperateBefore = tt.ToJson();
                }
                else
                {
                    tt = bll.QueryableSingle(new IPList() { IP = t.IP });
                    _OperateBefore = tt.ToJson();
                }

                result.data = bll.UpdateableIgnoreColumnsWhere(t);

                if (t.Id > 0)
                {
                    tt = baseBll.QueryableInSingle(t.Id.ToString());
                    _OperateAfter = tt.ToJson();
                }
                else
                {
                    tt = bll.QueryableSingle(new IPList() { IP = t.IP });
                    _OperateAfter = tt.ToJson();
                }

                AddSystemLog(new SystemLog() { OperateType = "modify", OperateModule = "SearchIP", OperateFunction = "分配IP信息", OperatePage = "分配IP信息:IPList/Edit", OperateBefore = _OperateBefore, OperateAfter = _OperateAfter });

            }
            catch (Exception ex)
            {

                result.code = -1;
                result.msg = ex.Message;
            }

            var res = new JsonResult(result);
            return res;
        }

        /// <summary>
        /// 获取未删除的可用IP
        /// </summary>
        /// <returns></returns>
        public IActionResult QueryableNotDeletedAvailableIP(string IPSegment)
        {
            LayuiDataResult<IPList> dataResult = new LayuiDataResult<IPList>()
            {
                code = 0,
                msg = "成功",
                count = 0,
                data = new List<IPList>()

            };
            try
            {
                List<IPList> list = bll.QueryableNotDeletedAvailableIP(IPSegment);
                //foreach (var item in list)
                //{
                //    item.IP = item.IP.Substring(item.IP.LastIndexOf("."));
                //}
                dataResult.count = list.Count;
                dataResult.data = list;

            }
            catch (Exception ex)
            {

                dataResult.code = -1;
                dataResult.msg = ex.Message;
            }

            var res = new JsonResult(dataResult);
            return res;

        }


        /// <summary>
        /// 清除IP绑定数据只保留IP
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public IActionResult UpdateClearIP(IPList t)
        {
            ResponseResult result = new ResponseResult()
            {
                code = 0,
                msg = "成功",
                data = 0

            };
            try
            {
                sessionHelper = new SessionHelper(HttpContext.Session);
                UserList user = sessionHelper.GetUserInfoString();
                t.Assgn = "";
                t.ModifyBy = user.Id + "|" + user.UserID + "|" + user.UserName;
                t.ModifyDate = DateTime.Now;
                t.Date = null;

                IPList tt = baseBll.QueryableInSingle(t.Id.ToString());

                result.data = bll.UpdateClearIP(t);

                AddSystemLog(new SystemLog() { OperateType = "delete", OperateModule = "SearchIP", OperateFunction = "删除已分配IP信息", OperatePage = "删除已分配IP信息:IPList/Index", OperateBefore = tt.ToJson(), OperateAfter = "" });

            }
            catch (Exception ex)
            {

                result.code = -1;
                result.msg = ex.Message;
            }

            var res = new JsonResult(result);
            return res;
        }


        /// <summary>
        /// 导入ip数据
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public IActionResult ExcelToTable(string FilePath)
        {
            //FilePath = @"D:\Work\SourceCode\ADCIPMANNEW\ProjectTemplate\PT.Web.Mvc\FileManage\IPList\20230606\ff7e2767-08ad-4c7d-a544-7c78a9836a90.xlsx";
            ResponseResult result = new ResponseResult()
            {
                code = 0,
                msg = "成功",
                data = 0

            };
            try
            {
                sessionHelper = new SessionHelper(HttpContext.Session);
                UserList user = sessionHelper.GetUserInfoString();

                DataTable dataTable = ExcelHelper.ExcelToTable(@FilePath);
                string s = dataTable.ToJson();
                List<IPList> ts = s.ToList<IPList>();
                //查询已存在数据的mac
                List<IPList> ts2 = bll.QueryableByIPMac(ts.Select(x => x.IP).ToArray(), ts.Select(x => x.MacAddress).ToArray());
                string repeatMacAddress = "", repeatMacAddress2 = "";
                List<string> MacAddressArr = new List<string>();
                string errorIP = "";

                foreach (IPList item in ts)
                {

                    string ip = item.IP;
                    string mac = item.MacAddress;
                    //当前IP数据
                    IPList t = ts2.Find(x => x.IP == ip);
                    //当前mac数据  会有多个
                    List<IPList> ts3 = ts2.Where(x => x.MacAddress == mac).ToList();
                    if (t != null)
                    {

                        //导入表格中与数据库中发生重复
                        if (ts3.Where(x => x.Floor == t.Floor).Any())
                        {
                            if (repeatMacAddress2.IndexOf(mac) < 0)
                            {
                                repeatMacAddress2 += mac + ",";
                            }

                        }
                    }

                    if (MacAddressArr.Exists(x => x == mac))
                    {
                        //导入表格内的重复
                        if (repeatMacAddress.IndexOf(mac) < 0)
                        {
                            repeatMacAddress += mac + ",";
                        }

                    }

                    MacAddressArr.Add(mac);
                    if (string.IsNullOrWhiteSpace(mac) || mac.Length != 12)
                    {
                        errorIP += ip + "|";
                        //校验成功

                    }
                    else if (!Regex.IsMatch(mac, "^[0-9A-Fa-f]+$"))
                    {
                        errorIP += ip + "|";
                        //校验成功

                    }


                }

                if (!string.IsNullOrWhiteSpace(errorIP))
                {
                    result.code = -2;
                    result.msg = "mac地址不符合规范";
                    result.data = errorIP;
                    return new JsonResult(result);
                }

                if (!string.IsNullOrWhiteSpace(repeatMacAddress))
                {
                    result.code = -3;
                    result.msg = "表格中mac地址重复";
                    result.data = repeatMacAddress;
                    return new JsonResult(result);
                }

                if (!string.IsNullOrWhiteSpace(repeatMacAddress2))
                {
                    result.code = -4;
                    result.msg = "数据库中已存在mac地址重复";
                    result.data = repeatMacAddress2;
                    return new JsonResult(result);
                }

                ts[0].Assgn = user.Id + "|" + user.UserID + "|" + user.UserName;
                ts[0].ModifyBy = user.Id + "|" + user.UserID + "|" + user.UserName;
                result.data = bll.ImportUpdate(ts);

                AddSystemLog(new SystemLog() { OperateType = "import", OperateModule = "SearchIP", OperateFunction = "导入", OperatePage = "导入分配IP信息:IPList/Index", OperateBefore = "", OperateAfter = "" });
            }
            catch (Exception ex)
            {

                result.code = -1;
                result.msg = ex.Message;
            }

            var res = new JsonResult(result);
            return res;
        }



    }
}

 

posted @ 2023-06-26 11:37  妖狐鬼魅  阅读(7)  评论(0编辑  收藏  举报