custom_analysis.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package fix
  2. import (
  3. "encoding/json"
  4. excelModel "eta/eta_index_lib/models/excel"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "time"
  8. )
  9. // FixTableData ETA1.0.2 自定义分析(生成指标数据修复)
  10. func FixTableData() {
  11. // 获取一级分类
  12. classifyList, err := excelModel.GetExcelClassifyByParentId(0, utils.EXCEL_DEFAULT)
  13. if err != nil && err.Error() != utils.ErrNoRow() {
  14. fmt.Println("数据修复失败,Err:" + err.Error())
  15. return
  16. }
  17. timeTableMap := make(map[int]int)
  18. mixTableMap := make(map[int]int)
  19. for _, v := range classifyList {
  20. // 时间序列表格
  21. classify := &excelModel.ExcelClassify{
  22. //ExcelClassifyId: 0,
  23. ExcelClassifyName: v.ExcelClassifyName,
  24. ParentId: v.ParentId,
  25. Source: utils.TIME_TABLE,
  26. SysUserId: v.SysUserId,
  27. SysUserRealName: v.SysUserRealName,
  28. Level: v.Level,
  29. UniqueCode: utils.MD5(fmt.Sprint(v.UniqueCode, "_", utils.TIME_TABLE)),
  30. Sort: v.Sort,
  31. CreateTime: time.Now(),
  32. ModifyTime: time.Now(),
  33. }
  34. _, err = excelModel.AddExcelClassify(classify)
  35. timeTableMap[v.ExcelClassifyId] = classify.ExcelClassifyId
  36. // 混合表格
  37. classify2 := &excelModel.ExcelClassify{
  38. //ExcelClassifyId: 0,
  39. ExcelClassifyName: v.ExcelClassifyName,
  40. ParentId: v.ParentId,
  41. Source: utils.MIXED_TABLE,
  42. SysUserId: v.SysUserId,
  43. SysUserRealName: v.SysUserRealName,
  44. Level: v.Level,
  45. UniqueCode: utils.MD5(fmt.Sprint(v.UniqueCode, "_", utils.MIXED_TABLE)),
  46. Sort: v.Sort,
  47. CreateTime: time.Now(),
  48. ModifyTime: time.Now(),
  49. }
  50. _, err = excelModel.AddExcelClassify(classify2)
  51. mixTableMap[v.ExcelClassifyId] = classify2.ExcelClassifyId
  52. }
  53. // 修改时间序列表
  54. {
  55. // 获取时间序列表
  56. timeTableExcelList, err := excelModel.GetNoContentExcelInfoAll(utils.TIME_TABLE, 0)
  57. if err != nil && err.Error() != utils.ErrNoRow() {
  58. fmt.Println("获取时间序列表列表失败,Err:" + err.Error())
  59. return
  60. }
  61. for _, v := range timeTableExcelList {
  62. classifyId, ok := timeTableMap[v.ExcelClassifyId]
  63. if !ok {
  64. continue
  65. }
  66. excelModel.UpdateExcelInfoClassifyId(classifyId, v.ExcelInfoId)
  67. }
  68. }
  69. // 修改混合序列表
  70. {
  71. // 获取时间序列表
  72. mixTableExcelList, err := excelModel.GetNoContentExcelInfoAll(utils.MIXED_TABLE, 0)
  73. if err != nil && err.Error() != utils.ErrNoRow() {
  74. fmt.Println("获取时间序列表列表失败,Err:" + err.Error())
  75. return
  76. }
  77. for _, v := range mixTableExcelList {
  78. classifyId, ok := mixTableMap[v.ExcelClassifyId]
  79. if !ok {
  80. continue
  81. }
  82. excelModel.UpdateExcelInfoClassifyId(classifyId, v.ExcelInfoId)
  83. }
  84. }
  85. fmt.Println("完成生成指标数据修复")
  86. }
  87. // FixTableDataMapping ETA1.0.2 自定义分析(修复excel与指标的关系)
  88. func FixTableDataMapping() {
  89. // 修改时间序列表
  90. {
  91. // 获取时间序列表
  92. timeTableExcelList, err := excelModel.GetAllExcelInfoBySource(utils.TIME_TABLE)
  93. if err != nil && err.Error() != utils.ErrNoRow() {
  94. fmt.Println("获取时间序列表列表失败,Err:" + err.Error())
  95. return
  96. }
  97. for _, v := range timeTableExcelList {
  98. var tableData excelModel.TableDataReq
  99. err = json.Unmarshal([]byte(v.Content), &tableData)
  100. if err != nil {
  101. fmt.Println(v.ExcelInfoId, "json转结构体失败,Err:"+err.Error())
  102. continue
  103. }
  104. if len(tableData.EdbInfoIdList) > 0 {
  105. excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
  106. for _, edbInfoId := range tableData.EdbInfoIdList {
  107. excelEdbMappingList = append(excelEdbMappingList, &excelModel.ExcelEdbMapping{
  108. //ExcelEdbMappingId: 0,
  109. ExcelInfoId: v.ExcelInfoId,
  110. Source: v.Source,
  111. EdbInfoId: edbInfoId,
  112. CreateTime: time.Now(),
  113. ModifyTime: time.Now(),
  114. })
  115. }
  116. err = excelModel.AddExcelEdbMappingMulti(excelEdbMappingList)
  117. if err != nil {
  118. fmt.Println(v.ExcelInfoId, "自定义表格关系保存失败,Err:"+err.Error())
  119. continue
  120. }
  121. }
  122. }
  123. }
  124. // 修改混合序列表
  125. {
  126. // 获取时间序列表
  127. mixTableExcelList, err := excelModel.GetAllExcelInfoBySource(utils.MIXED_TABLE)
  128. if err != nil && err.Error() != utils.ErrNoRow() {
  129. fmt.Println("获取时间序列表列表失败,Err:" + err.Error())
  130. return
  131. }
  132. for _, excelInfo := range mixTableExcelList {
  133. var result excelModel.MixedTableReq
  134. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  135. if err != nil {
  136. fmt.Println(excelInfo.ExcelInfoId, "修改混合序列表,json转结构体失败,Err:"+err.Error())
  137. continue
  138. }
  139. newResult, tmpErr, _ := GetMixedTableCellData(result.CellRelation, result.Data)
  140. if tmpErr != nil {
  141. fmt.Println(excelInfo.ExcelInfoId, "获取最新的数据失败,Err:"+err.Error())
  142. continue
  143. }
  144. edbInfoIdList := make([]int, 0)
  145. edbInfoIdMap := make(map[int]int)
  146. for _, tmpV := range newResult {
  147. for _, v := range tmpV {
  148. if v.EdbInfoId > 0 {
  149. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  150. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  151. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  152. }
  153. }
  154. }
  155. }
  156. if len(edbInfoIdList) > 0 {
  157. excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
  158. for _, edbInfoId := range edbInfoIdList {
  159. excelEdbMappingList = append(excelEdbMappingList, &excelModel.ExcelEdbMapping{
  160. //ExcelEdbMappingId: 0,
  161. ExcelInfoId: excelInfo.ExcelInfoId,
  162. Source: excelInfo.Source,
  163. EdbInfoId: edbInfoId,
  164. CreateTime: time.Now(),
  165. ModifyTime: time.Now(),
  166. })
  167. }
  168. err = excelModel.AddExcelEdbMappingMulti(excelEdbMappingList)
  169. if err != nil {
  170. fmt.Println(excelInfo.ExcelInfoId, "混合表格关系保存失败,Err:"+err.Error())
  171. continue
  172. }
  173. }
  174. }
  175. }
  176. fmt.Println("完成excel与指标的关系修复")
  177. }
  178. // CellRelationConf
  179. // @Description: 单元格的关系配置结构体
  180. type CellRelationConf struct {
  181. CellRelation
  182. //Type int `json:"type" description:"数据类型,跟MixedTableCellDataReq的DataType保持一致"`
  183. //Key string `json:"key" description:"单元格的唯一标识"`
  184. RelationDate CellRelation `json:"relation_date"`
  185. RelationEdb CellRelation `json:"relation_edb"`
  186. }
  187. // CellRelation
  188. // @Description: 单元格的关系结构体
  189. type CellRelation struct {
  190. Type int `json:"type" description:"数据类型,跟MixedTableCellDataReq的DataType保持一致"`
  191. Key string `json:"key" description:"单元格的唯一标识"`
  192. }
  193. // GetMixedTableCellData 获取混合表格数据
  194. func GetMixedTableCellData(cellRelationConf string, config [][]excelModel.MixedTableCellDataReq) (newMixedTableCellDataList [][]excelModel.MixedTableCellDataReq, err error, errMsg string) {
  195. // 单元格关系配置x信息
  196. cellRelationConfMap := make(map[string]CellRelationConf)
  197. cellRelationConfList := make([]CellRelationConf, 0)
  198. if cellRelationConf != `` {
  199. err = json.Unmarshal([]byte(cellRelationConf), &cellRelationConfList)
  200. if err != nil {
  201. return
  202. }
  203. for _, v := range cellRelationConfList {
  204. cellRelationConfMap[v.Key] = v
  205. }
  206. }
  207. // 找出所有的关联指标id
  208. config, _, _, err, errMsg = handleConfig(config)
  209. if err != nil {
  210. return
  211. }
  212. newMixedTableCellDataList = config
  213. return
  214. }
  215. // 单元格的数据类型
  216. const (
  217. DateDT = iota + 1 //日期
  218. EdbDT // 指标类型
  219. CustomTextDT // 自定义文本
  220. InsertDataDT // 插值
  221. PopInsertDataDT // 弹框插值
  222. )
  223. func handleConfig(configList [][]excelModel.MixedTableCellDataReq) (newConfig [][]excelModel.MixedTableCellDataReq, edbInfoIdList []int, dataEdbInfoIdList []int, err error, errMsg string) {
  224. edbInfoIdList = make([]int, 0)
  225. dataEdbInfoIdList = make([]int, 0)
  226. for ck, rowList := range configList {
  227. for _, cell := range rowList {
  228. switch cell.DataType {
  229. case EdbDT: // 指标信息
  230. edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
  231. case InsertDataDT, PopInsertDataDT: // 插值、弹框插值
  232. dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
  233. }
  234. }
  235. configList[ck] = rowList
  236. }
  237. newConfig = configList
  238. return
  239. }