123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models/data_manage/edb_inspection"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- // 所有巡检配置key
- var allDefaultEdbInspectionConfigKey = `edb_inspection_config:default:all:`
- // GetAllDefaultEdbInspectionConfigListBySource
- // @Description: 获取默认的所有巡检配置列表
- // @author: Roc
- // @datetime 2024-01-10 15:03:36
- // @param source int
- // @return list []*edb_inspection.EdbInspectionConfig
- // @return err error
- func GetAllDefaultEdbInspectionConfigListBySource(source int) (list []*edb_inspection.EdbInspectionConfig, err error) {
- key := getAllDefaultEdbInspectionConfigKey(source)
- if utils.Re == nil {
- if utils.Re == nil && utils.Rc.IsExist(key) {
- if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
- err = json.Unmarshal(data, &list)
- return
- }
- }
- }
- list, err = edb_inspection.GetListBySource(source)
- if err != nil {
- return
- }
- // 将数据加入缓存
- if utils.Re == nil {
- data, _ := json.Marshal(list)
- utils.Rc.Put(key, data, 2*time.Hour)
- }
- return
- }
- // SaveEdbInspectionConfig
- // @Description: 设置巡检配置接口
- // @author: Roc
- // @datetime 2024-01-10 15:11:19
- // @param req *edb_inspection.EdbInspectionConfigAddReq
- // @return err error
- // @return errMsg string
- // @return isSendEmail bool
- func SaveEdbInspectionConfig(req *edb_inspection.EdbInspectionConfigAddReq) (err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- errMsg = `保存失败`
- if req.Source <= 0 {
- errMsg = "来源不能为空"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if req.TerminalCode == "" {
- errMsg = "终端编码不能为空"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 判断终端是否存在
- terminal, e := data_manage.GetEdbTerminalByCode(req.TerminalCode)
- if e != nil {
- errMsg = "终端不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if terminal.TerminalCode != "" && terminal.Source != req.Source {
- errMsg = "数据源不匹配"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- //判断该终端配置是否已存在
- if req.ConfigId <= 0 {
- inspectionConfig, e := edb_inspection.GetListByTerminalCode(req.TerminalCode)
- if e != nil && !utils.IsErrNoRow(e) {
- errMsg = "查询终端配置失败"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if e == nil && len(inspectionConfig) > 0 {
- errMsg = "终端配置已存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- lenConf := len(req.List)
- if req.DateType == 1 && lenConf == 0 {
- errMsg = "至少需要一个巡检配置"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // if req.DateType == 1 && lenConf > 5 {
- // errMsg = "巡检时间设置最多不超过5个"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- // }
- tmpArr := []string{"每自然日", "每交易日", "每周"}
- // 配置的map,避免同一种类型配置同一个时间
- configMap := make(map[string]string)
- for _, v := range req.List {
- if !utils.InArrayByStr(tmpArr, v.InspectionFrequency) {
- errMsg = "巡检频率不合法"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if v.InspectionTime == "" {
- errMsg = "请选择具体时间"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 配置的map,避免同一种类型配置同一个时间
- key := fmt.Sprint(v.InspectionFrequency, "_", v.InspectionDate, "_", v.InspectionTime)
- if _, ok := configMap[key]; ok {
- errMsg = "巡检频率和日期不能重复"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- configMap[key] = key
- }
- configId := req.ConfigId
- if configId > 0 {
- // 查询配置
- inspectionConfig, e := edb_inspection.GetById(configId)
- if e != nil {
- errMsg = "配置不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- inspectionConfig.ConfigId = req.ConfigId
- updateCols := []string{"source", "terminal_code", "date_type", "start_time", "interval_time", "notify_users", "modify_time"}
- inspectionConfig.Source = req.Source
- inspectionConfig.TerminalCode = req.TerminalCode
- inspectionConfig.DateType = req.DateType
- inspectionConfig.StartTime = req.StartTime
- inspectionConfig.IntervalTime = req.IntervalTime
- inspectionConfig.NotifyUsers = req.NotifyUsers
- inspectionConfig.ModifyTime = time.Now()
- err = inspectionConfig.Update(updateCols)
- } else {
- // 创建巡检配置
- inspectionConfig := &edb_inspection.EdbInspectionConfig{
- Source: req.Source,
- TerminalCode: req.TerminalCode,
- DateType: req.DateType,
- StartTime: req.StartTime,
- Status: 1,
- IntervalTime: req.IntervalTime,
- NotifyUsers: req.NotifyUsers,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- err = inspectionConfig.Add()
- if err != nil {
- return
- }
- configId = inspectionConfig.ConfigId
- }
- if err != nil {
- return
- }
- // 删除原先的配置
- err = edb_inspection.DeleteEdbInspectionDateConfigByConfigId(configId)
- if err != nil {
- return
- }
- // 创建巡检日期配置
- dateConfigList := make([]*edb_inspection.EdbInspectionDateConfig, 0)
- if req.DateType == 1 {
- for _, v := range req.List {
- dateConfig := &edb_inspection.EdbInspectionDateConfig{
- InspectionFrequency: v.InspectionFrequency,
- InspectionFrequencyDay: v.InspectionFrequencyDay,
- InspectionDate: v.InspectionDate,
- InspectionTime: v.InspectionTime,
- ConfigId: configId,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- dateConfigList = append(dateConfigList, dateConfig)
- }
- if len(dateConfigList) > 0 {
- err = edb_inspection.AddEdbInspectionDateConfigList(dateConfigList, configId)
- if err != nil {
- return
- }
- }
- }
- // 清除缓存
- {
- key := getAllDefaultEdbInspectionConfigKey(req.Source)
- if utils.Re == nil {
- _ = utils.Rc.Delete(key)
- }
- }
- return
- }
- // HandleInspectionTime
- // @Description: 处理巡检时间的显示
- // @author: Roc
- // @datetime 2024-01-10 17:00:03
- // @param source int
- // @param terminalCode string
- // @return list []*edb_inspection.EdbInspectionConfig
- // @return err error
- // @return errMsg string
- // @return isSendEmail bool
- func GetConfigList(source int, terminalCode string) (list []*edb_inspection.EdbInspectionConfigItem, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- errMsg = "获取失败"
- list, err = edb_inspection.GetConfigListBySourceAndTerminalCode(source, terminalCode)
- if err != nil {
- return
- }
- if len(list) <= 0 {
- return
- }
- configIdList := make([]int64, 0)
- adminIds := make([]int, 0)
- for _, config := range list {
- configIdList = append(configIdList, config.ConfigId)
- adminIdSlice := strings.Split(config.NotifyUsers, ",")
- for _, adminId := range adminIdSlice {
- if adminId != "" {
- id, _ := strconv.Atoi(adminId)
- adminIds = append(adminIds, id)
- }
- }
- }
- adminNameMap := make(map[int]string)
- if len(adminIds) > 0 {
- adminList, e := system.GetAdminItemByIdList(adminIds)
- if e != nil {
- errMsg = "获取通知用户失败"
- err = errors.New(errMsg+e.Error())
- isSendEmail = false
- return
- }
- for _, admin := range adminList {
- adminNameMap[admin.AdminId] = admin.RealName
- }
- }
- // 获取每个配置的日期配置
- dateConfigs, tmpErr := edb_inspection.GetEdbInspectionDateConfigListByConfigIdList(configIdList)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 处理巡检时间显示
- inspectionTimeList := make(map[int64][]string, 0)
- for _, dateConfig := range dateConfigs {
- inspectionTimeList[dateConfig.ConfigId] = append(inspectionTimeList[dateConfig.ConfigId], GetInspectionStr(dateConfig.InspectionFrequency, dateConfig.InspectionFrequencyDay, dateConfig.InspectionTime))
- }
- for _, config := range list {
- if config.NotifyUsers != "" {
- adminIdSlice := strings.Split(config.NotifyUsers, ",")
- nameList := make([]string, 0)
- for _, adminId := range adminIdSlice {
- if adminId != "" {
- id, _ := strconv.Atoi(adminId)
- nameList = append(nameList, adminNameMap[id])
- }
- }
- config.NotifyUsersName = strings.Join(nameList, ",")
- }
- if config.DateType == 1 {
- tmpList, ok := inspectionTimeList[config.ConfigId]
- if ok {
- config.InspectionTime = strings.Join(tmpList, ",")
- }
- } else {
- config.InspectionTime = fmt.Sprintf("%s开始每间隔%d小时", config.StartTime, config.IntervalTime)
- }
- }
- return
- }
- func GetConfigDetail(configId int64) (detail *edb_inspection.EdbInspectionConfigDetailResp, err error) {
- item, err := edb_inspection.GetById(configId)
- if err != nil {
- return
- }
- dateConfigs, err := edb_inspection.GetEdbInspectionDateConfigListByConfigId(configId)
- if err != nil {
- return
- }
- list := make([]edb_inspection.InspectionConfigReq, 0)
- for _, dateConfig := range dateConfigs {
- list = append(list, edb_inspection.InspectionConfigReq{
- InspectionFrequency: dateConfig.InspectionFrequency,
- InspectionFrequencyDay: dateConfig.InspectionFrequencyDay,
- InspectionDate: dateConfig.InspectionDate,
- InspectionTime: dateConfig.InspectionTime,
- })
- }
- detail = &edb_inspection.EdbInspectionConfigDetailResp{
- EdbInspectionConfig: item,
- List: list,
- }
- return
- }
- // getAllDefaultEdbInspectionConfigKey
- // @Description: 获取默认的所有巡检配置key
- // @author: Roc
- // @datetime 2024-01-10 15:02:49
- // @param source int
- // @return string
- func getAllDefaultEdbInspectionConfigKey(source int) string {
- return allDefaultEdbInspectionConfigKey + fmt.Sprintf("%d", source)
- }
- // GetInspectionStr
- // @Description: 获取巡检配置的中文字符串
- // @author: Roc
- // @datetime 2024-01-10 16:05:10
- // @param inspectionFrequency string
- // @param inspectionFrequencyDay int
- // @param inspectionTime string
- // @return string
- func GetInspectionStr(inspectionFrequency string, inspectionFrequencyDay int, inspectionTime string) string {
- inspectionDayStr := ``
- switch inspectionFrequency {
- case "每自然日", "每交易日":
- case "每周":
- switch inspectionFrequencyDay {
- case 0:
- inspectionDayStr = "日"
- case 1:
- inspectionDayStr = "一"
- case 2:
- inspectionDayStr = "二"
- case 3:
- inspectionDayStr = "三"
- case 4:
- inspectionDayStr = "四"
- case 5:
- inspectionDayStr = "五"
- case 6:
- inspectionDayStr = "六"
- case 7:
- inspectionDayStr = "日"
- }
- default:
- if inspectionFrequencyDay > 0 {
- inspectionDayStr = fmt.Sprintf("第%d天", inspectionFrequencyDay)
- } else {
- inspectionDayStr = `最后一天`
- }
- }
- return inspectionFrequency + inspectionDayStr + " " + inspectionTime
- }
|