BiDecision_SN_SIMService.cs 4.87 KB
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, DateTime end)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                CalculateDateRanges(begin, 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 count(equipmentCode) as count  from base_equipment t1 where t1.equipmentCode in ({regSnList})
                                 select count(equipmentCode) as count  from base_equipment t1  where t1.equipmentCode in ({regSnList90})
                                 select count(distinct EquipmentCode)  from daq_equipment_alarm_record where EquipmentCode in ({regSnList})
                                 select count(distinct EquipmentCode)  from daq_equipment_alarm_record where EquipmentCode in ({regSnList90})";

                var iotResults = Context.Ado.GetDataSetAll(iotSql);
                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[0]["count"],
                    AccessIotMonth90 = iotResults.Tables[1].Rows[0]["count"],

                    alarmMonth = iotResults.Tables[3].Rows.Count,
                    alarmMonth90 = iotResults.Tables[4].Rows.Count,
                };
                response.Count = licenseDs.Tables[0].Rows.Count;
                return response;
            }, catchRetrunValue: "list");
        }

        public void CalculateDateRanges(DateTime inputDate, out DateTime monthStart, out DateTime monthEnd,out DateTime monthStart90, out DateTime monthEnd90)
        {
            DateTime now = DateTime.Now;
            // 计算本月起始时间(当月第一天00:00:00)
            monthStart = new DateTime(inputDate.Year, inputDate.Month, 1, 0, 0, 0);
            // 计算本月结束时间
            if (inputDate.Year == now.Year && inputDate.Month == now.Month)
            {
                // 当前月份:结束时间为输入日期的23:59:59
                monthEnd = new DateTime(inputDate.Year, inputDate.Month, inputDate.Day, 23, 59, 59);
            }
            else
            {
                // 非当前月份:结束时间为当月最后一天23:59:59
                DateTime nextMonth = monthStart.AddMonths(1);
                monthEnd = nextMonth.AddDays(-1).AddHours(23).AddMinutes(59).AddSeconds(59);
            }
            // 计算90天前的日期
            monthStart90 = inputDate.AddDays(-90).AddHours(23).AddMinutes(59).AddSeconds(59);
            monthEnd90 = monthEnd90 = inputDate.AddHours(23).AddMinutes(59).AddSeconds(59);
        }
    }
}