277 lines
10 KiB
Go
277 lines
10 KiB
Go
package service
|
|
|
|
import (
|
|
"energy-management-system/form"
|
|
"energy-management-system/global"
|
|
peak_valley_model "energy-management-system/model/peak-valley"
|
|
"energy-management-system/repository"
|
|
"energy-management-system/utils"
|
|
"energy-management-system/utils/exception"
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
"strconv"
|
|
)
|
|
|
|
type PeakValley struct{}
|
|
|
|
// GetPeakValleyType 获取峰谷类型
|
|
func (r *PeakValley) GetPeakValleyType() (typeList []*peak_valley_model.PeakValleyType) {
|
|
types := repository.GroupRepositorys.PeakValley.GetPeakValleyTypes()
|
|
for key, val := range types {
|
|
typeList = append(typeList, &peak_valley_model.PeakValleyType{
|
|
Value: key,
|
|
Title: val,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
// CreatePeakValleyRule 创建峰谷规则
|
|
func (r *PeakValley) CreatePeakValleyRule(req *form.CreatePeakValleyRuleReq) {
|
|
var err error
|
|
tx := global.Db.Begin()
|
|
// 确保在结束时提交或回滚事务
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
tx.Rollback() // 如果发生错误,回滚事务
|
|
fmt.Println("事务回滚")
|
|
exception.PanicMsg(rec)
|
|
} else if err != nil {
|
|
tx.Rollback() // 错误时回滚事务
|
|
fmt.Println("事务回滚")
|
|
exception.PanicMsgErr(err, err.Error())
|
|
} else {
|
|
err = tx.Commit().Error // 提交事务
|
|
if err != nil {
|
|
exception.PanicMsgErr(err, "事务提交失败")
|
|
}
|
|
}
|
|
}()
|
|
pvr := &peak_valley_model.PeakValleyRule{}
|
|
pvr.RuleName = req.RuleName
|
|
pvr.Description = req.Description
|
|
err = repository.GroupRepositorys.PeakValley.CreatePeakValleyRule(tx, pvr)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var total, start, end, blockTotal = 0, 0, 0, 0
|
|
|
|
// 每个时间段的时间区块ID
|
|
var timeBlockIds []uint
|
|
var timeBlockIdsMap = make(map[string][]uint)
|
|
|
|
for _, item := range req.RuleItem {
|
|
start, end, blockTotal, err = utils.GetMinutesFromTimeRange(item.StartTime, item.EndTime)
|
|
if err != nil {
|
|
return
|
|
}
|
|
total += blockTotal
|
|
if total > peak_valley_model.MinutesInADay {
|
|
err = errors.New("选择的时间段超过一天")
|
|
return
|
|
}
|
|
timeBlockIds, err = repository.GroupRepositorys.PeakValley.GetTimeBlockIdsByTimeBlock(tx, uint(start), uint(end))
|
|
if err != nil {
|
|
return
|
|
}
|
|
timeBlockIdsMap[item.StartTime+item.EndTime] = timeBlockIds
|
|
}
|
|
if total < peak_valley_model.MinutesInADay {
|
|
err = errors.New("各个时间段累加不足一天")
|
|
return
|
|
}
|
|
var timeBlockPrices []*peak_valley_model.PeakValleyTimeBlockPrice
|
|
for index, item := range req.RuleItem {
|
|
// 获取某个区块的具体时间区块编号
|
|
timeBlockIds = timeBlockIdsMap[item.StartTime+item.EndTime]
|
|
for _, timeBlockId := range timeBlockIds {
|
|
timeBlockPrices = append(timeBlockPrices, &peak_valley_model.PeakValleyTimeBlockPrice{
|
|
CustomName: strconv.Itoa(index+1) + "_TIME_" + item.CustomName,
|
|
BlockId: timeBlockId,
|
|
Price: item.Price,
|
|
PeakValleyRuleId: pvr.RuleId,
|
|
PeakValleyType: item.PeakValleyType,
|
|
})
|
|
}
|
|
}
|
|
err = repository.GroupRepositorys.PeakValley.CreatePeakValleyTimeBlockPrices(tx, timeBlockPrices)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// UpdatePeakValleyRule 修改峰谷规则
|
|
func (r *PeakValley) UpdatePeakValleyRule(req *form.UpdatePeakValleyRuleReq) {
|
|
var err error
|
|
tx := global.Db.Begin()
|
|
// 确保在结束时提交或回滚事务
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
tx.Rollback() // 如果发生错误,回滚事务
|
|
fmt.Println("事务回滚1", rec)
|
|
exception.PanicMsg(rec)
|
|
} else if err != nil {
|
|
tx.Rollback() // 错误时回滚事务
|
|
fmt.Println("事务回滚2", err)
|
|
exception.PanicMsgErr(err, err.Error())
|
|
} else {
|
|
err = tx.Commit().Error // 提交事务
|
|
if err != nil {
|
|
exception.PanicMsgErr(err, "事务提交失败")
|
|
}
|
|
}
|
|
}()
|
|
|
|
pvr, err := repository.GroupRepositorys.PeakValley.GetOnePeakValleyRule(map[string]interface{}{"rule_id": req.RuleId})
|
|
if err != nil {
|
|
return
|
|
}
|
|
pvr.RuleName = req.RuleName
|
|
pvr.Description = req.Description
|
|
err = repository.GroupRepositorys.PeakValley.UpdatePeakValleyRule(tx, pvr)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var total, start, end, blockTotal = 0, 0, 0, 0
|
|
|
|
// 每个时间段的时间区块ID
|
|
var timeBlockIds []uint
|
|
var timeBlockIdsMap = make(map[string][]uint)
|
|
|
|
for _, item := range req.RuleItem {
|
|
start, end, blockTotal, err = utils.GetMinutesFromTimeRange(item.StartTime, item.EndTime)
|
|
if err != nil {
|
|
return
|
|
}
|
|
total += blockTotal
|
|
if total > peak_valley_model.MinutesInADay {
|
|
err = errors.New("选择的时间段超过一天")
|
|
return
|
|
}
|
|
timeBlockIds, err = repository.GroupRepositorys.PeakValley.GetTimeBlockIdsByTimeBlock(tx, uint(start), uint(end))
|
|
exception.PanicMsgBool(err != nil, "获取时间区块ID列表失败")
|
|
timeBlockIdsMap[item.StartTime+item.EndTime] = timeBlockIds
|
|
}
|
|
if total < peak_valley_model.MinutesInADay {
|
|
err = errors.New("各个时间段累加不足一天")
|
|
return
|
|
}
|
|
// 删除旧的
|
|
err = repository.GroupRepositorys.PeakValley.DeletePeakValleyTimeBlockPricesByRuleId(tx, uint(req.RuleId), true)
|
|
if err != nil {
|
|
return
|
|
}
|
|
//
|
|
var timeBlockPrices []*peak_valley_model.PeakValleyTimeBlockPrice
|
|
for index, item := range req.RuleItem {
|
|
// 获取某个区块的具体时间区块编号
|
|
timeBlockIds = timeBlockIdsMap[item.StartTime+item.EndTime]
|
|
for _, timeBlockId := range timeBlockIds {
|
|
timeBlockPrices = append(timeBlockPrices, &peak_valley_model.PeakValleyTimeBlockPrice{
|
|
CustomName: strconv.Itoa(index+1) + "_TIME_" + item.CustomName,
|
|
BlockId: timeBlockId,
|
|
Price: item.Price,
|
|
PeakValleyRuleId: pvr.RuleId,
|
|
PeakValleyType: item.PeakValleyType,
|
|
})
|
|
}
|
|
}
|
|
err = repository.GroupRepositorys.PeakValley.CreatePeakValleyTimeBlockPrices(tx, timeBlockPrices)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// PeakValleyRuleList 峰谷规则列表
|
|
func (r *PeakValley) PeakValleyRuleList(req *form.PeakValleyRuleListReq) map[string]interface{} {
|
|
count, list, err := repository.GroupRepositorys.PeakValley.GetPeakValleyRulePage(req)
|
|
if err != nil {
|
|
exception.PanicMsgErr(err, "获取列表失败")
|
|
//return
|
|
}
|
|
ListRsp := make(map[string]interface{})
|
|
ListRsp["total"] = count
|
|
ListRsp["list"] = list
|
|
return ListRsp
|
|
}
|
|
|
|
// PeakValleyRuleDetail 获取规则详情吧
|
|
func (r *PeakValley) PeakValleyRuleDetail(req *form.PeakValleyRuleDetailReq) map[string]interface{} {
|
|
// 获取分了多少个时间段 以及每个时间段的自定义名称 峰谷类型
|
|
ruleTypes, err := repository.GroupRepositorys.PeakValley.GetPeakValleyRuleTypes(req.RuleId)
|
|
fmt.Println(ruleTypes)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败1")
|
|
// 查询指定规则 指定峰谷类型的区块
|
|
for idx, ruleType := range ruleTypes {
|
|
// 查询当前类型的时间区块
|
|
typeBlockList, err := repository.GroupRepositorys.PeakValley.GetPeakValleyRulePriceByTypeAndRuleID(uint(req.RuleId), ruleType.PeakValleyType)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败2")
|
|
// 根据连贯性 小区块 组合成大区块 (一个区块 或者多个区块)
|
|
blocks := utils.SplitIntoGroups(typeBlockList)
|
|
for _, block := range blocks {
|
|
maxBlockId, minBlockId, err := utils.GetSplitIntMaxMin(block)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败3")
|
|
customName, err := repository.GroupRepositorys.PeakValley.GetPeakValleyRulePriceCustomName(minBlockId, uint(req.RuleId))
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败4")
|
|
startTime, err := repository.GroupRepositorys.PeakValley.GetBlockStartTime(minBlockId)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败5")
|
|
endTime, err := repository.GroupRepositorys.PeakValley.GetBlockEndTime(maxBlockId)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败6")
|
|
startTimeStr := fmt.Sprintf("%02d:%02d", startTime/60, startTime%60)
|
|
endTimeStr := fmt.Sprintf("%02d:%02d", endTime/60, endTime%60)
|
|
ruleTimeBlock := peak_valley_model.RuleTimeBlock{
|
|
TimeBlockIds: block,
|
|
StartTime: startTime,
|
|
EndTime: endTime,
|
|
StartTimeStr: startTimeStr,
|
|
EndTimeStr: endTimeStr,
|
|
CustomName: customName,
|
|
}
|
|
ruleTypes[idx].RuleTimeBlocks = append(ruleTypes[idx].RuleTimeBlocks, ruleTimeBlock)
|
|
}
|
|
}
|
|
DetailRsp := make(map[string]interface{})
|
|
DetailRsp["rule_id"] = req.RuleId
|
|
DetailRsp["detail"] = ruleTypes
|
|
return DetailRsp
|
|
}
|
|
|
|
// PeakValleyRuleEditDetail 获取规则详情-编辑使用
|
|
func (r *PeakValley) PeakValleyRuleEditDetail(req *form.PeakValleyRuleEditDetailReq) map[string]interface{} {
|
|
// 获取分了多少个时间段 以及每个时间段的自定义名称 峰谷类型
|
|
customNames, err := repository.GroupRepositorys.PeakValley.GetPeakValleyRuleCustomName(req.RuleId)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败1")
|
|
// 查询指定规则 指定峰谷类型的区块
|
|
for idx, customName := range customNames {
|
|
// 查询当前类型的时间区块
|
|
customNameBlockList, err := repository.GroupRepositorys.PeakValley.GetPeakValleyRulePriceByCustomNameAndRuleID(uint(req.RuleId), customName.CustomName)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败2")
|
|
maxBlockId, minBlockId, err := utils.GetSplitIntMaxMin(customNameBlockList)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败3")
|
|
startTime, err := repository.GroupRepositorys.PeakValley.GetBlockStartTime(minBlockId)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败4")
|
|
endTime, err := repository.GroupRepositorys.PeakValley.GetBlockEndTime(maxBlockId)
|
|
exception.PanicMsgBool(err != nil, "获取规则区块类型失败5")
|
|
startTimeStr := fmt.Sprintf("%02d:%02d", startTime/60, startTime%60)
|
|
endTimeStr := fmt.Sprintf("%02d:%02d", endTime/60, endTime%60)
|
|
//dump.P(customNameBlockList)
|
|
customNames[idx].StartTimeStr = startTimeStr
|
|
customNames[idx].StartTime = startTime
|
|
customNames[idx].EndTimeStr = endTimeStr
|
|
customNames[idx].EndTime = endTime
|
|
customNames[idx].EndTimeStr = endTimeStr
|
|
}
|
|
|
|
// 根据 自定义名称 中的数字部分进行升序排序
|
|
sort.Sort(peak_valley_model.ByCustomNameNumber(customNames))
|
|
|
|
DetailRsp := make(map[string]interface{})
|
|
DetailRsp["rule_id"] = req.RuleId
|
|
DetailRsp["detail"] = customNames
|
|
return DetailRsp
|
|
}
|