BiDecision_SN_SIMService.cs 4.52 KB
using Hh.Mes.Common;
using Hh.Mes.Common.log;
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.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hh.Mes.Service
{
    public partial class BiDecisionService : RepositorySqlSugar<sys_user>
    {
        /// <summary>
        /// 决策支持 SN统计
        /// </summary>
        public dynamic GetSNInfo(DateTime begin)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                CalculateDateRanges(begin, out DateTime monthStart7, out DateTime monthEnd7, out DateTime monthStart, out DateTime monthEnd, out DateTime monthStart90, out DateTime monthEnd90);

                // SN申请、授权统计 mysql数据库 
                var lincenseSql = $@"select sn from  sninfo  where Created >= @monthStart&&Created<=@monthEnd;
                                     select sn from  sninfo  where Created >= @monthStart90&&Created<=@monthEnd90;
                                     select sn from  licenseinfo  where Created >= @monthStart&&Created<=@monthEnd;
                                     select sn from  licenseinfo  where Created >= @monthStart90&&Created<=@monthEnd90";
                var licenseDs = ContextLicense.Ado.GetDataSetAll(lincenseSql, new List<SugarParameter>(){
                                new("@monthStart", monthStart),
                                new("@monthEnd", monthEnd),
                                new("@monthStart90", monthStart90),
                                new("@monthEnd90", monthEnd90),
                            });
                // 注册sn
                var regSn = licenseDs.Tables[0].AsEnumerable()
                                          .Select(r => $"'{r["sn"].ToString()}'")
                                          .Distinct();
                var regSnList = string.Join(",", regSn);

                var regSn90 = licenseDs.Tables[1].AsEnumerable()
                                            .Select(r => $"'{r["sn"].ToString()}'")
                                            .Distinct();
                var regSnList90 = string.Join(",", regSn90);

                // IOT设备信息批量查询 微软数据库 daq_equipment_status_record_history 表数据库大需要注意
                var iotSql = $@" select equipmentCode as sn  from base_equipment t1 where createTime >= @monthStart and createTime<=@monthEnd
                                 select equipmentCode as sn from base_equipment t1 where createTime >= @monthStart90 and createTime<=@monthEnd90
                                 select distinct EquipmentCode  as sn from daq_equipment_alarm_record where CreateTime >= @monthStart and CreateTime<=@monthEnd
                                 select distinct EquipmentCode  as sn from daq_equipment_alarm_record where CreateTime >= @monthStart90 and CreateTime<=@monthEnd90";

                var iotResults = Context.Ado.GetDataSetAll(iotSql, new List<SugarParameter>(){
                    new("@monthStart", monthStart),
                    new("@monthEnd", monthEnd),
                    new("@monthStart90", monthStart90),
                    new("@monthEnd90", monthEnd90)
                });

                // 处理数据
                var processor = new SnDataProcessor();
                var snDetail = processor.ProcessAndMergeData(licenseDs.Tables[0], licenseDs.Tables[2], iotResults.Tables[0], iotResults.Tables[2]);

                var snDetail90 = processor.ProcessAndMergeData(licenseDs.Tables[1], licenseDs.Tables[3], iotResults.Tables[1], iotResults.Tables[3]);

                response.Result = new
                {
                    regSnMonth = licenseDs.Tables[0].Rows.Count,
                    regSnMonth90 = licenseDs.Tables[1].Rows.Count,
                    authMonth = licenseDs.Tables[2].Rows.Count,
                    authMonth90 = licenseDs.Tables[3].Rows.Count,

                    AccessIotMonth = iotResults.Tables[0].Rows.Count,
                    AccessIotMonth90 = iotResults.Tables[1].Rows.Count,

                    alarmMonth = iotResults.Tables[2].Rows.Count,
                    alarmMonth90 = iotResults.Tables[3].Rows.Count,

                    snDetail = snDetail,
                    snDetail90 = snDetail90,
                };
                response.Count = licenseDs.Tables[0].Rows.Count;
                return response;
            }, catchRetrunValue: "list");
        }
    }
}