BaseGatewayService.cs 11.7 KB
using Hh.Mes.Common.Infrastructure;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.WebEntity;
using NPOI.POIFS.FileSystem;

namespace Hh.Mes.Service.Base
{
    /// <summary>
    /// 网关信息
    /// </summary>
    public class BaseGatewayService : RepositorySqlSugar<base_gateway>
    {
        public dynamic Load(PageReq pageReq, base_gateway entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var expression = LinqWhere(entity);
                //先组合查询表达式(多表查询查看IOT 设备列表案例)
                var query = Context.Queryable<base_gateway, base_gateway_vpnsim_rel, base_sim, base_vpn>(
                                    (g, r, s, v) => new JoinQueryInfos
                                    (
                                        JoinType.Left, g.gatewayKey ==  r.gatewayKey,
                                        JoinType.Left, r.simKey==s.simKey,
                                        JoinType.Left, r.vpnKey==v.vpnKey
                                    ))
                                   .Where(expression)
                                   .Select((g, r, s, v) => new
                                   {
                                       g.id,
                                       g.gatewayKey,
                                       g.gatewaySerialNumber,
                                       g.gateway,
                                       g.password,

                                       g.remark,
                                       g.createTime,
                                       g.createBy,
                                       g.updateTime,
                                       g.updateBy,
                                       g.isUse,

                                       s.simKey,
                                       s.simId,
                                       s.simNumber,
                                       s.simIP,
                                       s.suppliersInfo,

                                       s.gatewayClient,
                                       s.getCardDate,
                                       s.getCardUser,
                                       s.debugDate,

                                       s.factoryDate,
                                       s.isDelete,
                                       s.isVirtual,
                                       s.simRemark,

                                       v.vpnKey,
                                       v.vpnNumber,
                                       v.vpnIP,
                                       v.virtualIP,
                                       v.vpnRemark
                                   });
                //Exel false 分页
                if (!entity.Exel)
                {
                    int total = 0;
                    result.Result = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Count = total;
                }
                else
                {
                    result.Result = query.ToList();
                    result.Count = result.Result.Count;
                }
                return result;
            }, catchRetrunValue: "list");
        }

        public dynamic Ins(base_gateway entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                //网关信息录入
                var gatewayKey = Guid.NewGuid();
                entity.gatewayKey = gatewayKey;
                entity.createBy = sysWebUser.Account;
                entity.createTime = DateTime.Now;
                Context.Insertable<base_gateway>(entity).AddQueue();

                base_sim sim = new base_sim();
                var simKey = Guid.NewGuid();
                sim.simKey = simKey;
                sim.simId = entity.simNumber;
                sim.simNumber = entity.simNumber;
                sim.simIP = entity.simIP;
                sim.suppliersInfo = entity.suppliersInfo;
                sim.isVirtual = entity.isVirtual;
                sim.isDelete = entity.isDelete;
                sim.gatewayClient = entity.gatewayClient;
                sim.factoryDate = entity.factoryDate;
                sim.simRemark = entity.simRemark;
                sim.createTime = DateTime.Now;
                sim.createBy = sysWebUser.Account;
                Context.Insertable<base_sim>(sim).AddQueue();

                base_vpn vpn = new base_vpn();
                vpn.vpnKey = Guid.NewGuid();
                vpn.vpnNumber = entity.vpnNumber;
                vpn.vpnIP = entity.vpnIP;
                vpn.virtualIP = entity.virtualIP;
                vpn.vpnRemark = entity.vpnRemark;
                vpn.createTime = DateTime.Now;
                vpn.createBy = sysWebUser.Account;
                Context.Insertable<base_vpn>(vpn).AddQueue();

                base_gateway_vpnsim_rel rel = new base_gateway_vpnsim_rel();
                rel.gatewayKey = gatewayKey;
                rel.vpnKey = vpn.vpnKey;
                rel.simKey = sim.simKey;
                Context.Insertable<base_gateway_vpnsim_rel>(rel).AddQueue();

                bool result = Context.SaveQueuesAsync().Result > 0;
                response.Status = result;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic Upd(base_gateway entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                entity.updateBy = sysWebUser.Account;
                entity.updateTime = DateTime.Now;
                Context.Updateable(entity).AddQueue();

                var gatewayRel = Context.Queryable<base_gateway_vpnsim_rel>().Where(x => x.gatewayKey == entity.gatewayKey).First();
                var simKey = gatewayRel.simKey;
                var vpnKey = gatewayRel.vpnKey;

                base_sim sim = new base_sim();
                sim.simKey = simKey;
                sim.simId = entity.simNumber;
                sim.simNumber = entity.simNumber;
                sim.simIP = entity.simIP;
                sim.suppliersInfo = entity.suppliersInfo;
                sim.isVirtual = entity.isVirtual;
                sim.isDelete = entity.isDelete;
                sim.gatewayClient = entity.gatewayClient;
                sim.factoryDate = entity.factoryDate;
                sim.simRemark = entity.simRemark;
                sim.updateTime = DateTime.Now;
                sim.updateBy = sysWebUser.Account;

                sim.debugDate = entity.debugDate;
                sim.getCardDate=entity.getCardDate; 
                sim.getCardUser=entity.getCardUser; 
                sim.gatewayClient=entity.gatewayClient;
                Context.Updateable(sim).Where(i=>i.simKey==simKey).AddQueue();

                base_vpn vpn = new base_vpn();
                vpn.vpnKey = vpnKey;
                vpn.vpnNumber = entity.vpnNumber;
                vpn.vpnIP = entity.vpnIP;
                vpn.virtualIP = entity.virtualIP;
                vpn.vpnRemark = entity.vpnRemark;
                vpn.updateTime = DateTime.Now;
                vpn.updateBy = sysWebUser.Account;
                Context.Updateable<base_vpn>(vpn).Where(i=>i.vpnKey==vpnKey).AddQueue();

                response.Status = Context.SaveQueuesAsync().Result > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic DelByIds(int[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                foreach (int id in ids)
                {
                    var gatewayKey = Context.Queryable<base_gateway>().Where(x => x.id == id).First().gatewayKey;
                    var gatewayRel = Context.Queryable<base_gateway_vpnsim_rel>().Where(x => x.gatewayKey == gatewayKey).First();
                    var simKey = gatewayRel.simKey;
                    var vpnKey = gatewayRel.vpnKey;
                    Context.Deleteable<base_gateway>().Where(i=>i.id==id).AddQueue();
                    Context.Deleteable<base_vpn>().Where(i=>i.vpnKey==vpnKey).AddQueue();
                    Context.Deleteable<base_sim>().Where(i=>i.simKey==simKey).AddQueue();
                    Context.Deleteable<base_gateway_vpnsim_rel>(gatewayRel).AddQueue();
                }
                response.Status = Context.SaveQueuesAsync().Result > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public Response ExportData(base_gateway entity)
        {
            return Load(null, entity);
        }

        public Expression<Func<base_gateway, base_gateway_vpnsim_rel, base_sim, base_vpn, bool>> LinqWhere(base_gateway model)
        {
            try
            {
                var exp = Expressionable.Create<base_gateway, base_gateway_vpnsim_rel, base_sim, base_vpn>();
                //数据过滤条件
                if (!string.IsNullOrWhiteSpace(model.gatewaySerialNumber))
                {
                    exp.And((g, r, s, v) => g.gatewaySerialNumber.Contains(model.gatewaySerialNumber));
                }
                if (!string.IsNullOrWhiteSpace(model.virtualIP))
                {
                    exp.And((g, r, s, v) => v.virtualIP.Contains(model.virtualIP));
                }
                if (!string.IsNullOrWhiteSpace(model.gatewayClient))
                {
                    exp.And((g, r, s, v) => s.gatewayClient.Contains(model.gatewayClient));
                }
                return exp.ToExpression();//拼接表达式
            }
            catch (Exception ex)
            {
                throw new Exception($"{ex.Message}");
            }
        }


        /// <summary>
        /// 领卡操作 更新字段 领卡日期、调试日期、网关客户信息
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="getCardDate"></param>
        /// <param name="debugDate"></param>
        /// <param name="client"></param>
        /// <returns></returns>
        public dynamic Use(base_gateway entity)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var gatewayKey = Context.Queryable<base_gateway>().Where(i => i.id == entity.id).First().gatewayKey;
                var simKey=Context.Queryable<base_gateway_vpnsim_rel>().Where(i=>i.gatewayKey==gatewayKey).First().simKey;

                base_sim sim = new base_sim();
                sim.getCardDate = DateTime.Now;
                sim.getCardUser=entity.getCardUser;
                sim.debugDate=entity.debugDate;
                sim.gatewayClient=entity.gatewayClient;
                Context.Updateable(sim).UpdateColumns("getCardDate", "getCardUser", "debugDate", "gatewayClient").Where(i => i.simKey == simKey).AddQueue();

                base_gateway g=new base_gateway();
                g.isUse= true;
                Context.Updateable(g).UpdateColumns("isUse").Where(i=>i.id==entity.id).AddQueue();

                var status = Context.SaveQueuesAsync().Result > 0;
                response.Status = status;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }
    }
}