lixiaojun
2022-08-12 424d2d20a24b76850b73fae8ecc9bc98505be4f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Net.Http.Headers;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.AspNetCore.Http.Extensions;
using IStation.Untity;
using Furion.DynamicApiController;
using System.ComponentModel.DataAnnotations;
using Mapster;
using IStation.Calculation;
using IStation.Model;
using Microsoft.AspNetCore.Authorization;
using Furion.FriendlyException;
 
namespace IStation.Application
{
    /// <summary>
    /// MonitorGeneralMonthRecord
    /// </summary>
    [Route("Run/MonitorGeneralMonthRecord/Mgr")]
    [ApiDescriptionSettings("Run", Name = "监测常规月记录(管理)", Order = 930)]
    public class MonitorGeneralMonthRecord_MgrController : IDynamicApiController
    {
 
        /// <summary>
        /// 重新分析某个月份区间(仅针对一个常规月分析测点的分析,会删除已有记录)
        /// </summary>
        [Route("AnalyAgainOfMonthRange@V1.0")]
        [HttpPost]
        public AnalyAgainMonitorGeneralMonthRecordOfMonthRangeResultDto AnalyAgainOfMonthRange
            ([Required] AnalyAgainMonitorGeneralMonthRecordOfMonthRangeInput input)
        {
            var corpId = input.CorpID;
            var monitorId = input.MonitorPointID;
            var startYear=input.StartYear;
            var endYear=input.EndYear;
            var startMonth=input.StartMonth;
            var endMonth=input.EndMonth;
            var monitor = new Service.MonitorPoint().GetExSignalWithSignalTypeByID(corpId, monitorId);
            if (monitor == null)
            {
                throw Oops.Oh(ErrorCodes.D001, $"测点:{monitorId}");
            }
            if (monitor.CronType != Model.Monitor.eCronType.EachMonth)
            {
                throw Oops.Oh(ErrorCodes.D002, $"测点:{monitorId}", "不是月测点");
            }
            if (monitor.SourceType != Model.Monitor.eSourceType.Analyse)
            {
                throw Oops.Oh(ErrorCodes.D002, $"测点:{monitorId}", "不是分析测点");
            }
            if (string.IsNullOrEmpty(monitor.SourceParas))
            {
                throw Oops.Oh(ErrorCodes.D002, $"测点:{monitorId}", "未配置分析参数");
            }
            var analyModel = Model.Monitor.AnalyseParameters.ToModel(monitor.SourceParas);
            if (analyModel == null)
            {
                throw Oops.Oh(ErrorCodes.D002, $"测点:{monitorId}", "分析参数配置错误");
            }
            if (string.IsNullOrEmpty(analyModel.FormulaType))
            {
                throw Oops.Oh(ErrorCodes.D002, $"测点:{monitorId}", "未配置公式类型");
            }
            if (string.IsNullOrEmpty(analyModel.FormulaParas))
            {
                throw Oops.Oh(ErrorCodes.D002, $"测点:{monitorId}", "未配置公式参数");
            }
 
            var getMonitorPoint = new Func<long, Model.MonitorPoint_Signal_SignalType>(x => {
                return new Service.MonitorPoint().GetExSignalWithSignalTypeByID(corpId, x);
            });
            var getLastRecord = new Func<long, long, Model.MonitorBasicRecord>((x, y) => {
                var dtLast = new DateTime(startYear, startMonth, 1);
                dtLast = dtLast.AddMonths(-1);
                return new Service.MonitorMonthRecord().GetBySignalIDOfMonth(corpId, x, y, dtLast.Year,dtLast.Month);
            });
            var getRecordList = new Func<long, long,Model.Monitor.eCronType, DateTime,DateTime, List<Model.MonitorBasicRecord>>((x, y,cy, st,et) => {
                switch (cy)
                {
                    case Model.Monitor.eCronType.Real:
                        {
                            return new Service.MonitorRealRecord()
                                            .GetFixedBySignalIDOfTimeRange(corpId,x,y,10000,st,et)?.Select(x=>x as Model.MonitorBasicRecord).ToList();
                        }
                    case Model.Monitor.eCronType.EachDay:
                        {
                            return new Service.MonitorDayRecord().GetBySignalIDOfDayRange
                                            (monitor.CorpID, x, y,st.Date,et.Date.AddDays(-1))?.Select(x=>x as Model.MonitorBasicRecord).ToList();
                        }
                    default:return default;
                }
            });
 
            List<Model.MonitorMonthRecordPure> recordList = null; 
            string msg = string.Empty;
            switch (analyModel.FormulaType)
            {
                case Model.Monitor.FormulaType.Statistics_Max://最大值统计
                    {
                        recordList = MonitorFormulaHistoryCalcuHelper.StatisticalMonthMaxValue
                            (startYear,startMonth,endYear,endMonth, monitor, analyModel.FormulaParas, getMonitorPoint, getRecordList, getLastRecord, out msg);
                    }
                    break;
                case Model.Monitor.FormulaType.Statistics_Min://最小值统计
                    {
                        recordList = MonitorFormulaHistoryCalcuHelper.StatisticalMonthMinValue
                            (startYear, startMonth, endYear, endMonth, monitor, analyModel.FormulaParas, getMonitorPoint, getRecordList, getLastRecord, out msg);
                    }
                    break;
                case Model.Monitor.FormulaType.Statistics_Avg://平均值统计
                    {
                        recordList = MonitorFormulaHistoryCalcuHelper.StatisticalMonthAvgValue
                            (startYear, startMonth, endYear, endMonth, monitor, analyModel.FormulaParas, getMonitorPoint, getRecordList, getLastRecord, out msg);
                    }
                    break;
                case Model.Monitor.FormulaType.Statistics_Diff://差值统计
                    {
                        recordList = MonitorFormulaHistoryCalcuHelper.StatisticalMonthDiffValue
                              (startYear, startMonth, endYear, endMonth, monitor, analyModel.FormulaParas, getMonitorPoint, getRecordList, getLastRecord, out msg);
                    }
                    break;
                case Model.Monitor.FormulaType.Statistics_Sum://和值统计
                    {
                        recordList = MonitorFormulaHistoryCalcuHelper.StatisticalMonthSumValue
                               (startYear, startMonth, endYear, endMonth, monitor, analyModel.FormulaParas, getMonitorPoint, getRecordList, getLastRecord, out msg);
                    }
                    break;
                default: msg = $"不支持的公式类型:{analyModel.FormulaType}"; break;
            }
 
            if (recordList == null || recordList.Count < 1)
            {
                return new AnalyAgainMonitorGeneralMonthRecordOfMonthRangeResultDto(false, msg);
            }
            var bol = new Service.MonitorMonthRecord().InsertsAgain(recordList);
            return new AnalyAgainMonitorGeneralMonthRecordOfMonthRangeResultDto(bol, msg);
        }
 
 
 
    }
}