BaseSimService.cs 12.6 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 NPOI.POIFS.FileSystem;
using NPOI.SS.Formula.Functions;

namespace Hh.Mes.Service.Base
{
    public class BaseSimService : RepositorySqlSugar<base_sim>
    {
        public dynamic Load(PageReq pageReq, base_sim entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var result = new Response();
                var expression = LinqWhere(entity);
                //先组合查询表达式
                var query = Context.Queryable<base_sim>().Where(expression);

                //Exel为ture就不分页,因为导出的话是全部导出
                if (pageReq != null)
                {
                    int total = 0;
                    var list = query.ToOffsetPage(pageReq.page, pageReq.limit, ref total);
                    result.Result = HandleSimInfo(list);
                    result.Count = total;
                }
                else
                {
                    result.Result = HandleSimInfo(query.ToList());
                    result.Count = result.Result.Count;
                }
                return result;
            }, catchRetrunValue: "list");
        }

        public dynamic Ins(base_sim entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                entity.createBy = sysWebUser.Account;
                entity.createTime = DateTime.Now;
                entity.getCardDate = null;
                entity.debugDate = null;
                //新增sim卡信息
                Context.Insertable(entity).AddQueue();
                //新增sim卡与vpn配置关联关系
                if (!string.IsNullOrEmpty(entity.accountNumber))
                {
                    var vpnList = entity.accountNumber.Split(',', StringSplitOptions.RemoveEmptyEntries);
                    foreach (var vpn in vpnList)
                    {
                        base_sim_vpn_rel sv = new base_sim_vpn_rel()
                        {
                            simNumber = entity.simNumber,
                            accountNumber = vpn,
                        };
                        Context.Insertable(sv).AddQueue();
                    }
                }
                var res = Context.SaveQueues();
                if (res < 1) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic Upd(base_sim entity)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                //判断如果关联了设备,禁止修改卡号
                var historySimInfo = Context.Queryable<base_sim, base_sim_equipment_rel>((x, y) => x.simNumber == y.simNumber)
                                    .Where((x, y) => x.id == entity.id)
                                    .Select((x, y) => new { x.id, x.simNumber })
                                    .First();
                if (historySimInfo != null && entity.simNumber != historySimInfo.simNumber)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = $"当前sim卡已经关联了设备,禁止修改卡号【{historySimInfo.simNumber}】,如果要修改请先解绑sim卡关联的设备!";
                    return response;
                }
                //判断如果关联了VPN,禁止修改卡号;
                var vpnSimRel = Context.Queryable<base_sim, base_sim_vpn_rel>((x, y) => x.simNumber == y.simNumber)
                                       .Where((x, y) => x.id == entity.id)
                                       .Select((x, y) => new { x.id, x.simNumber })
                                       .First();
                if (vpnSimRel != null && entity.simNumber != vpnSimRel.simNumber)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = $"当前sim卡已经关联了VPN,禁止修改卡号【{vpnSimRel.simNumber}】,如果要修改请先解绑sim卡配置的vpn账号!";
                    return response;
                }
                entity.updateBy = sysWebUser.Account;
                entity.updateTime = DateTime.Now;
                //如果accountNumber为空,删除关联
                //删除旧关联
                if (vpnSimRel != null)
                {
                    Context.Deleteable<base_sim_vpn_rel>().Where(x => x.simNumber == entity.simNumber).AddQueue();
                }
                //添加vpn关联
                if (!string.IsNullOrEmpty(entity.accountNumber))
                {
                    var vpnList = entity.accountNumber.Split(',', StringSplitOptions.RemoveEmptyEntries);
                    foreach (var vpn in vpnList)
                    {
                        base_sim_vpn_rel sv = new base_sim_vpn_rel()
                        {
                            simNumber = entity.simNumber,
                            accountNumber = vpn,
                        };
                        Context.Insertable(sv).AddQueue();
                    }
                }

                Context.Updateable(entity).AddQueue();//修改sim卡信息
                var res = Context.SaveQueues();
                if (res < 0) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        public dynamic DelByIds(string[] ids)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                //判断如果关联了设备,禁止删除
                var simNumber = ids[0];
                var isSimInfo = Context.Queryable<base_sim_equipment_rel>().Any(x => x.simNumber == simNumber);
                if (isSimInfo)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = "当前sim卡已经关联了设备,禁止删除SIM卡,如果要删除请先解绑与sim卡关联的设备!";
                    return response;
                }
                //如果关联了VPN,禁止删除
                var isVpnRel = Context.Queryable<base_sim_vpn_rel>().Any(x => x.simNumber == simNumber);
                if (isVpnRel)
                {
                    response.Code = 500;
                    response.Status = false;
                    response.Message = "当前sim卡已经关联了VPN,禁止删除SIM卡,如果要删除请先解绑sim卡关联的VPN账号!";
                    return response;
                }
                Context.Deleteable<base_sim>(t => ids.Contains(t.simNumber)).ExecuteCommand();
                return response;
            });
        }

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

        public Expression<Func<base_sim, bool>> LinqWhere(base_sim model)
        {
            try
            {
                var exp = Expressionable.Create<base_sim>();
                //数据过滤条件
                //if (!string.IsNullOrWhiteSpace(model.XXX)) exp.And(x => x.XXX.Contains(model.XXX));
                if (!string.IsNullOrWhiteSpace(model.simNumber))
                {
                    exp.And(x => x.simNumber.Contains(model.simNumber));
                }
                if (!string.IsNullOrWhiteSpace(model.ip))
                {
                    exp.And(x => x.ip.Contains(model.ip));
                }
                if (!string.IsNullOrWhiteSpace(model.suppliersInfo))
                {
                    exp.And(x => x.suppliersInfo.Contains(model.suppliersInfo));
                }
                return exp.ToExpression();//拼接表达式
            }
            catch (Exception ex)
            {
                throw new Exception($"{ex.Message}");
            }
        }

        public dynamic BindEquipment(string simNumber, bool checkeds, string equipmentCode, string projectKeys)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                string[] strArray = equipmentCode.Split(new char[] { ',' });
                var simBindCount = Context.Queryable<base_sim_equipment_rel>().Where(x => x.simNumber == simNumber).ToList().Count;
                if (strArray != null && strArray.Length > 0)
                {
                    foreach (var item in strArray)
                    {
                        if (checkeds)
                        {
                            var isHave = Context.Queryable<base_sim_equipment_rel>()
                                                .Any(x => x.simNumber == simNumber && x.projectKey == new Guid(projectKeys) && x.equipmentCode == item);
                            if (isHave) continue;
                            //添加
                            if (simBindCount < 250 && !isHave)
                            {
                                base_sim_equipment_rel bser = new base_sim_equipment_rel();
                                bser.simNumber = simNumber;
                                bser.equipmentCode = item;
                                bser.projectKey = new Guid(projectKeys);
                                Context.Insertable(bser).AddQueue();
                                response.Status = Context.SaveQueues() > 0;
                                if (!response.Status)
                                {
                                    response.Message = "添加SIM卡关联设备数据失败!";
                                    return response;
                                }
                            }
                            else
                            {
                                response.Message = "不可重复绑定!一张SIM卡只允许绑定250个设备,请选择其它SIM卡绑定";
                                return response;
                            }
                        }
                        else
                        {
                            Context.Deleteable<base_sim_equipment_rel>(x => x.simNumber == simNumber && x.projectKey == new Guid(projectKeys) && x.equipmentCode == item).AddQueue();
                            response.Status = Context.SaveQueues() > 0;
                            if (!response.Status)
                            {
                                response.Message = "删除sim卡与设备绑定关系数据失败!";
                                return response;
                            }
                        }
                    }
                }
                response.Message = "业务数据操作成功,稍后刷新列表数据!";
                return response;
            });
        }

        public dynamic UseSim(int[] ids,DateTime getCardDate,DateTime debugDate)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var response = new Response();
                if (ids != null)
                {
                    foreach(var i in ids)
                    {
                        var simInfo = Context.Queryable<base_sim>().First(sim => sim.id == i);
                        simInfo.getCardDate=getCardDate;
                        simInfo.debugDate=debugDate;
                        simInfo.updateBy = sysWebUser.Account;
                        simInfo.updateTime = DateTime.Now;
                        Context.Updateable<base_sim>(simInfo).AddQueue();
                    }
                    var res=Context.SaveQueues();
                    if (res < 0) response.Message = SystemVariable.dataActionError;
                }
                return response;
            });
        }

        public List<base_sim> HandleSimInfo(List<base_sim> simList)
        {
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                if (simList != null)
                {
                    foreach (var sim in simList)
                    {
                        var sim_Vpn_Rels = Context.Queryable<base_sim_vpn_rel>().Where(x => x.simNumber == sim.simNumber).Select(x => x.accountNumber).ToList();
                        sim.accountNumber = string.Join(",", sim_Vpn_Rels.ToArray());
                    }
                }
                return simList;
            });
        }
    }
}