edb_inspection.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage/edb_inspection"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 所有巡检配置key
  15. var allDefaultEdbInspectionConfigKey = `edb_inspection_config:default:all:`
  16. // GetAllDefaultEdbInspectionConfigListBySource
  17. // @Description: 获取默认的所有巡检配置列表
  18. // @author: Roc
  19. // @datetime 2024-01-10 15:03:36
  20. // @param source int
  21. // @return list []*edb_inspection.EdbInspectionConfig
  22. // @return err error
  23. func GetAllDefaultEdbInspectionConfigListBySource(source int) (list []*edb_inspection.EdbInspectionConfig, err error) {
  24. key := getAllDefaultEdbInspectionConfigKey(source)
  25. if utils.Re == nil {
  26. if utils.Re == nil && utils.Rc.IsExist(key) {
  27. if data, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  28. err = json.Unmarshal(data, &list)
  29. return
  30. }
  31. }
  32. }
  33. list, err = edb_inspection.GetListBySource(source)
  34. if err != nil {
  35. return
  36. }
  37. // 将数据加入缓存
  38. if utils.Re == nil {
  39. data, _ := json.Marshal(list)
  40. utils.Rc.Put(key, data, 2*time.Hour)
  41. }
  42. return
  43. }
  44. // SaveEdbInspectionConfig
  45. // @Description: 设置巡检配置接口
  46. // @author: Roc
  47. // @datetime 2024-01-10 15:11:19
  48. // @param req *edb_inspection.EdbInspectionConfigAddReq
  49. // @return err error
  50. // @return errMsg string
  51. // @return isSendEmail bool
  52. func SaveEdbInspectionConfig(req *edb_inspection.EdbInspectionConfigAddReq) (err error, errMsg string, isSendEmail bool) {
  53. isSendEmail = true
  54. errMsg = `保存失败`
  55. if req.Source <= 0 {
  56. errMsg = "来源不能为空"
  57. err = errors.New(errMsg)
  58. isSendEmail = false
  59. return
  60. }
  61. if req.TerminalCode == "" {
  62. errMsg = "终端编码不能为空"
  63. err = errors.New(errMsg)
  64. isSendEmail = false
  65. return
  66. }
  67. // 判断终端是否存在
  68. terminal, e := data_manage.GetEdbTerminalByCode(req.TerminalCode)
  69. if e != nil {
  70. errMsg = "终端不存在"
  71. err = errors.New(errMsg)
  72. isSendEmail = false
  73. return
  74. }
  75. if terminal.TerminalCode != "" && terminal.Source != req.Source {
  76. errMsg = "数据源不匹配"
  77. err = errors.New(errMsg)
  78. isSendEmail = false
  79. return
  80. }
  81. //判断该终端配置是否已存在
  82. if req.ConfigId <= 0 {
  83. inspectionConfig, e := edb_inspection.GetListByTerminalCode(req.TerminalCode)
  84. if e != nil && !utils.IsErrNoRow(e) {
  85. errMsg = "查询终端配置失败"
  86. err = errors.New(errMsg)
  87. isSendEmail = false
  88. return
  89. }
  90. if e == nil && len(inspectionConfig) > 0 {
  91. errMsg = "终端配置已存在"
  92. err = errors.New(errMsg)
  93. isSendEmail = false
  94. return
  95. }
  96. }
  97. lenConf := len(req.List)
  98. if req.DateType == 1 && lenConf == 0 {
  99. errMsg = "至少需要一个巡检配置"
  100. err = errors.New(errMsg)
  101. isSendEmail = false
  102. return
  103. }
  104. // if req.DateType == 1 && lenConf > 5 {
  105. // errMsg = "巡检时间设置最多不超过5个"
  106. // err = errors.New(errMsg)
  107. // isSendEmail = false
  108. // return
  109. // }
  110. tmpArr := []string{"每自然日", "每交易日", "每周"}
  111. // 配置的map,避免同一种类型配置同一个时间
  112. configMap := make(map[string]string)
  113. for _, v := range req.List {
  114. if !utils.InArrayByStr(tmpArr, v.InspectionFrequency) {
  115. errMsg = "巡检频率不合法"
  116. err = errors.New(errMsg)
  117. isSendEmail = false
  118. return
  119. }
  120. if v.InspectionTime == "" {
  121. errMsg = "请选择具体时间"
  122. err = errors.New(errMsg)
  123. isSendEmail = false
  124. return
  125. }
  126. // 配置的map,避免同一种类型配置同一个时间
  127. key := fmt.Sprint(v.InspectionFrequency, "_", v.InspectionDate, "_", v.InspectionTime)
  128. if _, ok := configMap[key]; ok {
  129. errMsg = "巡检频率和日期不能重复"
  130. err = errors.New(errMsg)
  131. isSendEmail = false
  132. return
  133. }
  134. configMap[key] = key
  135. }
  136. configId := req.ConfigId
  137. if configId > 0 {
  138. // 查询配置
  139. inspectionConfig, e := edb_inspection.GetById(configId)
  140. if e != nil {
  141. errMsg = "配置不存在"
  142. err = errors.New(errMsg)
  143. isSendEmail = false
  144. return
  145. }
  146. inspectionConfig.ConfigId = req.ConfigId
  147. updateCols := []string{"source", "terminal_code", "date_type", "start_time", "interval_time", "notify_users", "modify_time"}
  148. inspectionConfig.Source = req.Source
  149. inspectionConfig.TerminalCode = req.TerminalCode
  150. inspectionConfig.DateType = req.DateType
  151. inspectionConfig.StartTime = req.StartTime
  152. inspectionConfig.IntervalTime = req.IntervalTime
  153. inspectionConfig.NotifyUsers = req.NotifyUsers
  154. inspectionConfig.ModifyTime = time.Now()
  155. err = inspectionConfig.Update(updateCols)
  156. } else {
  157. // 创建巡检配置
  158. inspectionConfig := &edb_inspection.EdbInspectionConfig{
  159. Source: req.Source,
  160. TerminalCode: req.TerminalCode,
  161. DateType: req.DateType,
  162. StartTime: req.StartTime,
  163. Status: 1,
  164. IntervalTime: req.IntervalTime,
  165. NotifyUsers: req.NotifyUsers,
  166. CreateTime: time.Now(),
  167. ModifyTime: time.Now(),
  168. }
  169. err = inspectionConfig.Add()
  170. if err != nil {
  171. return
  172. }
  173. configId = inspectionConfig.ConfigId
  174. }
  175. if err != nil {
  176. return
  177. }
  178. // 删除原先的配置
  179. err = edb_inspection.DeleteEdbInspectionDateConfigByConfigId(configId)
  180. if err != nil {
  181. return
  182. }
  183. // 创建巡检日期配置
  184. dateConfigList := make([]*edb_inspection.EdbInspectionDateConfig, 0)
  185. if req.DateType == 1 {
  186. for _, v := range req.List {
  187. dateConfig := &edb_inspection.EdbInspectionDateConfig{
  188. InspectionFrequency: v.InspectionFrequency,
  189. InspectionFrequencyDay: v.InspectionFrequencyDay,
  190. InspectionDate: v.InspectionDate,
  191. InspectionTime: v.InspectionTime,
  192. ConfigId: configId,
  193. CreateTime: time.Now(),
  194. ModifyTime: time.Now(),
  195. }
  196. dateConfigList = append(dateConfigList, dateConfig)
  197. }
  198. if len(dateConfigList) > 0 {
  199. err = edb_inspection.AddEdbInspectionDateConfigList(dateConfigList, configId)
  200. if err != nil {
  201. return
  202. }
  203. }
  204. }
  205. // 清除缓存
  206. {
  207. key := getAllDefaultEdbInspectionConfigKey(req.Source)
  208. if utils.Re == nil {
  209. _ = utils.Rc.Delete(key)
  210. }
  211. }
  212. return
  213. }
  214. // HandleInspectionTime
  215. // @Description: 处理巡检时间的显示
  216. // @author: Roc
  217. // @datetime 2024-01-10 17:00:03
  218. // @param source int
  219. // @param terminalCode string
  220. // @return list []*edb_inspection.EdbInspectionConfig
  221. // @return err error
  222. // @return errMsg string
  223. // @return isSendEmail bool
  224. func GetConfigList(source int, terminalCode string) (list []*edb_inspection.EdbInspectionConfigItem, err error, errMsg string, isSendEmail bool) {
  225. isSendEmail = true
  226. errMsg = "获取失败"
  227. list, err = edb_inspection.GetConfigListBySourceAndTerminalCode(source, terminalCode)
  228. if err != nil {
  229. return
  230. }
  231. if len(list) <= 0 {
  232. return
  233. }
  234. configIdList := make([]int64, 0)
  235. adminIds := make([]int, 0)
  236. for _, config := range list {
  237. configIdList = append(configIdList, config.ConfigId)
  238. adminIdSlice := strings.Split(config.NotifyUsers, ",")
  239. for _, adminId := range adminIdSlice {
  240. if adminId != "" {
  241. id, _ := strconv.Atoi(adminId)
  242. adminIds = append(adminIds, id)
  243. }
  244. }
  245. }
  246. adminNameMap := make(map[int]string)
  247. if len(adminIds) > 0 {
  248. adminList, e := system.GetAdminItemByIdList(adminIds)
  249. if e != nil {
  250. errMsg = "获取通知用户失败"
  251. err = errors.New(errMsg+e.Error())
  252. isSendEmail = false
  253. return
  254. }
  255. for _, admin := range adminList {
  256. adminNameMap[admin.AdminId] = admin.RealName
  257. }
  258. }
  259. // 获取每个配置的日期配置
  260. dateConfigs, tmpErr := edb_inspection.GetEdbInspectionDateConfigListByConfigIdList(configIdList)
  261. if tmpErr != nil {
  262. err = tmpErr
  263. return
  264. }
  265. // 处理巡检时间显示
  266. inspectionTimeList := make(map[int64][]string, 0)
  267. for _, dateConfig := range dateConfigs {
  268. inspectionTimeList[dateConfig.ConfigId] = append(inspectionTimeList[dateConfig.ConfigId], GetInspectionStr(dateConfig.InspectionFrequency, dateConfig.InspectionFrequencyDay, dateConfig.InspectionTime))
  269. }
  270. for _, config := range list {
  271. if config.NotifyUsers != "" {
  272. adminIdSlice := strings.Split(config.NotifyUsers, ",")
  273. nameList := make([]string, 0)
  274. for _, adminId := range adminIdSlice {
  275. if adminId != "" {
  276. id, _ := strconv.Atoi(adminId)
  277. nameList = append(nameList, adminNameMap[id])
  278. }
  279. }
  280. config.NotifyUsersName = strings.Join(nameList, ",")
  281. }
  282. if config.DateType == 1 {
  283. tmpList, ok := inspectionTimeList[config.ConfigId]
  284. if ok {
  285. config.InspectionTime = strings.Join(tmpList, ",")
  286. }
  287. } else {
  288. config.InspectionTime = fmt.Sprintf("%s开始每间隔%d小时", config.StartTime, config.IntervalTime)
  289. }
  290. }
  291. return
  292. }
  293. func GetConfigDetail(configId int64) (detail *edb_inspection.EdbInspectionConfigDetailResp, err error) {
  294. item, err := edb_inspection.GetById(configId)
  295. if err != nil {
  296. return
  297. }
  298. dateConfigs, err := edb_inspection.GetEdbInspectionDateConfigListByConfigId(configId)
  299. if err != nil {
  300. return
  301. }
  302. list := make([]edb_inspection.InspectionConfigReq, 0)
  303. for _, dateConfig := range dateConfigs {
  304. list = append(list, edb_inspection.InspectionConfigReq{
  305. InspectionFrequency: dateConfig.InspectionFrequency,
  306. InspectionFrequencyDay: dateConfig.InspectionFrequencyDay,
  307. InspectionDate: dateConfig.InspectionDate,
  308. InspectionTime: dateConfig.InspectionTime,
  309. })
  310. }
  311. detail = &edb_inspection.EdbInspectionConfigDetailResp{
  312. EdbInspectionConfig: item,
  313. List: list,
  314. }
  315. return
  316. }
  317. // getAllDefaultEdbInspectionConfigKey
  318. // @Description: 获取默认的所有巡检配置key
  319. // @author: Roc
  320. // @datetime 2024-01-10 15:02:49
  321. // @param source int
  322. // @return string
  323. func getAllDefaultEdbInspectionConfigKey(source int) string {
  324. return allDefaultEdbInspectionConfigKey + fmt.Sprintf("%d", source)
  325. }
  326. // GetInspectionStr
  327. // @Description: 获取巡检配置的中文字符串
  328. // @author: Roc
  329. // @datetime 2024-01-10 16:05:10
  330. // @param inspectionFrequency string
  331. // @param inspectionFrequencyDay int
  332. // @param inspectionTime string
  333. // @return string
  334. func GetInspectionStr(inspectionFrequency string, inspectionFrequencyDay int, inspectionTime string) string {
  335. inspectionDayStr := ``
  336. switch inspectionFrequency {
  337. case "每自然日", "每交易日":
  338. case "每周":
  339. switch inspectionFrequencyDay {
  340. case 0:
  341. inspectionDayStr = "日"
  342. case 1:
  343. inspectionDayStr = "一"
  344. case 2:
  345. inspectionDayStr = "二"
  346. case 3:
  347. inspectionDayStr = "三"
  348. case 4:
  349. inspectionDayStr = "四"
  350. case 5:
  351. inspectionDayStr = "五"
  352. case 6:
  353. inspectionDayStr = "六"
  354. case 7:
  355. inspectionDayStr = "日"
  356. }
  357. default:
  358. if inspectionFrequencyDay > 0 {
  359. inspectionDayStr = fmt.Sprintf("第%d天", inspectionFrequencyDay)
  360. } else {
  361. inspectionDayStr = `最后一天`
  362. }
  363. }
  364. return inspectionFrequency + inspectionDayStr + " " + inspectionTime
  365. }