edb_info.go 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  1. package data_manage
  2. import (
  3. "eta/eta_api/services/alarm_msg"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbInfo struct {
  13. EdbInfoId int `orm:"column(edb_info_id);pk"`
  14. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  15. SourceName string `description:"来源名称"`
  16. Source int `description:"来源id"`
  17. EdbCode string `description:"指标编码"`
  18. EdbName string `description:"指标名称"`
  19. EdbNameEn string `description:"英文指标名称"`
  20. EdbNameSource string `description:"指标名称来源"`
  21. Frequency string `description:"频率"`
  22. Unit string `description:"单位"`
  23. UnitEn string `description:"英文单位"`
  24. StartDate string `description:"起始日期"`
  25. EndDate string `description:"终止日期"`
  26. ClassifyId int `description:"分类id"`
  27. SysUserId int
  28. SysUserRealName string
  29. UniqueCode string `description:"指标唯一编码"`
  30. CreateTime time.Time
  31. ModifyTime time.Time
  32. MinValue float64 `description:"指标最小值"`
  33. MaxValue float64 `description:"指标最大值"`
  34. CalculateFormula string `description:"计算公式"`
  35. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  36. Sort int `description:"排序字段"`
  37. LatestDate string `description:"数据最新日期"`
  38. LatestValue float64 `description:"数据最新值"`
  39. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  40. MoveFrequency string `description:"移动频度"`
  41. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  42. ServerUrl string `description:"服务器地址"`
  43. ChartImage string `description:"图表图片"`
  44. Calendar string `description:"公历/农历" orm:"default(公历);"`
  45. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  46. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  47. }
  48. type EdbInfoFullClassify struct {
  49. *EdbInfo
  50. CorrelationStr string `description:"相关性系数字符串"`
  51. ClassifyList []*EdbClassifyIdItems
  52. }
  53. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  54. o := orm.NewOrmUsingDB("data")
  55. lastId, err = o.Insert(item)
  56. return
  57. }
  58. type EdbInfoItem struct {
  59. EdbInfoId int `description:"指标id"`
  60. EdbName string `description:"指标名称"`
  61. ClassifyId int `description:"指标分类"`
  62. }
  63. // GetEdbInfoAll 用于分类展示
  64. func GetEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
  67. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  68. return
  69. }
  70. // GetPredictEdbInfoAll 用于分类展示(预测指标)
  71. func GetPredictEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. sql := ` SELECT edb_info_id,classify_id,edb_name AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type FROM edb_info WHERE edb_info_type = ? order by sort asc,edb_info_id asc`
  74. _, err = o.Raw(sql, edbInfoType).QueryRows(&items)
  75. return
  76. }
  77. // 用于分类展示
  78. func GetEdbInfoAllList() (items []*EdbInfo, err error) {
  79. o := orm.NewOrmUsingDB("data")
  80. sql := ` SELECT * FROM edb_info `
  81. _, err = o.Raw(sql).QueryRows(&items)
  82. return
  83. }
  84. // 指标检索数据
  85. type EdbInfoSearch struct {
  86. EdbCode string `description:"指标编码"`
  87. StartDate string `description:"起始日期"`
  88. EndDate string `description:"终止日期"`
  89. EdbName string `description:"指标名称"`
  90. Unit string `description:"单位"`
  91. Frequency string `description:"频率"`
  92. DataList []*EdbInfoSearchData
  93. }
  94. type EdbInfoSearchData struct {
  95. DataTime string `description:"数据日期"`
  96. Value float64 `description:"数据"`
  97. }
  98. type EdbInfoSearchResp struct {
  99. SearchItem *EdbInfoSearch `description:"指标分类"`
  100. Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
  101. ClassifyList []*EdbClassifySimplify
  102. }
  103. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  104. o := orm.NewOrmUsingDB("data")
  105. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  106. err = o.Raw(sql, source, edbCode).QueryRow(&item)
  107. return
  108. }
  109. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  110. o := orm.NewOrmUsingDB("data")
  111. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  112. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  113. return
  114. }
  115. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  116. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  117. num := len(edbInfoIdList)
  118. if num <= 0 {
  119. return
  120. }
  121. o := orm.NewOrmUsingDB("data")
  122. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  123. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&items)
  124. return
  125. }
  126. type AddEdbInfoReq struct {
  127. Source int `description:"来源id"`
  128. EdbCode string `description:"指标编码"`
  129. EdbName string `description:"指标名称"`
  130. Frequency string `description:"频率"`
  131. Unit string `description:"单位"`
  132. ClassifyId int `description:"分类id"`
  133. StartDate string `description:"起始日期"`
  134. EndDate string `description:"终止日期"`
  135. }
  136. func DeleteEdbInfoAndData(edbInfoId, source int) (err error) {
  137. o := orm.NewOrmUsingDB("data")
  138. to, err := o.Begin()
  139. if err != nil {
  140. return
  141. }
  142. defer func() {
  143. if err != nil {
  144. _ = to.Rollback()
  145. } else {
  146. _ = to.Commit()
  147. }
  148. }()
  149. // 删除指标信息
  150. sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
  151. _, err = to.Raw(sql, edbInfoId).Exec()
  152. if err != nil {
  153. return
  154. }
  155. // 删除指标数据
  156. tableName := GetEdbDataTableName(source)
  157. if tableName != "" {
  158. sql = ` DELETE FROM %s WHERE edb_info_id=? `
  159. sql = fmt.Sprintf(sql, tableName)
  160. _, err = to.Raw(sql, edbInfoId).Exec()
  161. if err != nil {
  162. return
  163. }
  164. }
  165. //calculateTableName := GetEdbInfoCalculateTableName(source)
  166. //if calculateTableName != "" {
  167. // sql = ` DELETE FROM %s WHERE edb_info_id=? `
  168. // sql = fmt.Sprintf(sql, calculateTableName)
  169. // _, err = to.Raw(sql, edbInfoId).Exec()
  170. // return
  171. //}
  172. //return
  173. // 删除计算指标的关系
  174. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  175. _, err = to.Raw(sql, edbInfoId).Exec()
  176. // 删除预测指标的配置
  177. sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id=? `
  178. _, err = to.Raw(sql, edbInfoId).Exec()
  179. return
  180. }
  181. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  182. o := orm.NewOrmUsingDB("data")
  183. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  184. if condition != "" {
  185. sql += condition
  186. }
  187. err = o.Raw(sql, pars).QueryRow(&count)
  188. return
  189. }
  190. type EditEdbInfoReq struct {
  191. EdbInfoId int `description:"指标ID"`
  192. EdbName string `description:"指标名称"`
  193. Frequency string `description:"频率"`
  194. Unit string `description:"单位"`
  195. ClassifyId int `description:"分类id"`
  196. CalculateFormula string `description:"计算公式"`
  197. }
  198. type EditEdbEnInfoReq struct {
  199. EdbInfoId int `description:"指标ID"`
  200. EdbNameEn string `description:"英文指标名称"`
  201. UnitEn string `description:"英文单位"`
  202. }
  203. func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
  204. o := orm.NewOrmUsingDB("data")
  205. sql := ` UPDATE edb_info
  206. SET
  207. edb_name =?,
  208. edb_name_source =?,
  209. frequency = ?,
  210. unit = ?,
  211. classify_id = ?,
  212. modify_time = NOW()
  213. WHERE edb_info_id = ?`
  214. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.EdbInfoId).Exec()
  215. return
  216. }
  217. // ModifyEdbEnInfo 修改指标英文信息
  218. func ModifyEdbEnInfo(item *EditEdbEnInfoReq) (err error) {
  219. o := orm.NewOrmUsingDB("data")
  220. sql := ` UPDATE edb_info
  221. SET
  222. edb_name_en =?,
  223. unit_en = ?,
  224. modify_time = NOW()
  225. WHERE edb_info_id = ?`
  226. _, err = o.Raw(sql, item.EdbNameEn, item.UnitEn, item.EdbInfoId).Exec()
  227. return
  228. }
  229. type EdbInfoList struct {
  230. EdbInfoId int `orm:"column(edb_info_id);pk"`
  231. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  232. SourceName string `description:"来源名称"`
  233. Source int `description:"来源id"`
  234. EdbCode string `description:"指标编码"`
  235. EdbNameEn string `description:"英文指标名称"`
  236. EdbName string `description:"指标名称"`
  237. Frequency string `description:"频率"`
  238. FrequencyEn string `description:"英文频率"`
  239. Unit string `description:"单位"`
  240. UnitEn string `description:"英文单位"`
  241. StartDate string `description:"起始日期"`
  242. EndDate string `description:"终止日期"`
  243. LatestDate string `description:"数据最新日期"`
  244. LatestValue float64 `description:"数据最新值"`
  245. ClassifyId int `description:"分类id"`
  246. UniqueCode string `description:"指标唯一编码"`
  247. SysUserId int `description:"创建人id"`
  248. SysUserRealName string `description:"创建人姓名"`
  249. ModifyTime string `description:"最新修改时间"`
  250. CreateTime string `description:"创建时间"`
  251. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  252. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  253. ChartImage string `description:"图表图片"`
  254. RuleType int `description:"预测规则,1:最新,2:固定值"`
  255. FixedValue float64 `description:"固定值"`
  256. DataList []*EdbData `description:"实际指标数据"`
  257. PredictDataList []*EdbData `description:"预测指标数据"`
  258. Button EdbClassifyItemsButton `description:"操作权限"`
  259. IsEnEdb bool `description:"是否展示英文标识"`
  260. DataInsertConfig EdbDataInsertConfigItem `description:"指标数据插入配置"`
  261. DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
  262. }
  263. type EdbDataInsertConfigItem struct {
  264. Date string `description:"插入的日期"`
  265. RealDate string `description:"实际最晚的日期"`
  266. Value string `description:"插入的值"`
  267. }
  268. type EdbData struct {
  269. EdbDataId int `orm:"column(edb_data_id);pk"`
  270. EdbInfoId int
  271. DataTime string
  272. Value float64
  273. }
  274. type EdbInfoListResp struct {
  275. Paging *paging.PagingItem
  276. Item *EdbInfoList
  277. ClassifyList []*EdbClassifyIdItems
  278. }
  279. func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  280. o := orm.NewOrmUsingDB("data")
  281. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  282. if condition != "" {
  283. sql += condition
  284. }
  285. err = o.Raw(sql, pars).QueryRow(&item)
  286. return
  287. }
  288. func GetEdbDataCountByCondition(condition string, pars []interface{}, source int) (count int, err error) {
  289. o := orm.NewOrmUsingDB("data")
  290. tableName := GetEdbDataTableName(source)
  291. sql := ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  292. sql = fmt.Sprintf(sql, tableName)
  293. if condition != "" {
  294. sql += condition
  295. }
  296. err = o.Raw(sql, pars).QueryRow(&count)
  297. return
  298. }
  299. func GetEdbDataListByCondition(condition string, pars []interface{}, source, pageSize, startSize int) (item []*EdbData, err error) {
  300. o := orm.NewOrmUsingDB("data")
  301. tableName := GetEdbDataTableName(source)
  302. sql := ` SELECT * FROM %s WHERE 1=1 `
  303. sql = fmt.Sprintf(sql, tableName)
  304. if condition != "" {
  305. sql += condition
  306. }
  307. sql += ` ORDER BY data_time DESC `
  308. sql += ` LIMIT ?,? `
  309. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
  310. return
  311. }
  312. // GetAllEdbDataListByCondition 根据条件获取所有的数据
  313. func GetAllEdbDataListByCondition(condition string, pars []interface{}, source int) (item []*EdbData, err error) {
  314. o := orm.NewOrmUsingDB("data")
  315. tableName := GetEdbDataTableName(source)
  316. sql := ` SELECT * FROM %s WHERE 1=1 `
  317. sql = fmt.Sprintf(sql, tableName)
  318. if condition != "" {
  319. sql += condition
  320. }
  321. sql += ` ORDER BY data_time DESC `
  322. _, err = o.Raw(sql, pars).QueryRows(&item)
  323. return
  324. }
  325. func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
  326. o := orm.NewOrmUsingDB("data")
  327. sql := ` SELECT * FROM edb_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
  328. err = o.Raw(sql).QueryRow(&item)
  329. return
  330. }
  331. func ModifyEdbInfoModifyTime(edbInfoId int64) (err error) {
  332. o := orm.NewOrmUsingDB("data")
  333. sql := ` UPDATE edb_info SET modify_time = NOW() WHERE edb_info_id = ? `
  334. _, err = o.Raw(sql, edbInfoId).Exec()
  335. return
  336. }
  337. type MoveEdbInfoReq struct {
  338. EdbInfoId int `description:"指标ID"`
  339. PrevEdbInfoId int `description:"上一个指标ID"`
  340. NextEdbInfoId int `description:"下一个指标ID"`
  341. ClassifyId int `description:"分类id"`
  342. }
  343. func MoveEdbInfo(edbInfoId, classifyId int) (err error) {
  344. o := orm.NewOrmUsingDB("data")
  345. sql := ` UPDATE edb_info
  346. SET
  347. classify_id = ?
  348. WHERE edb_info_id = ?`
  349. _, err = o.Raw(sql, classifyId, edbInfoId).Exec()
  350. return
  351. }
  352. // GetEdbInfoByName 根据名称获取普通的指标
  353. func GetEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  354. o := orm.NewOrmUsingDB("data")
  355. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 0 `
  356. _, err = o.Raw(sql, edbName).QueryRows(&items)
  357. return
  358. }
  359. // GetPredictEdbInfoByName 根据指标名称获取预测指标
  360. func GetPredictEdbInfoByName(edbName string) (items []*EdbInfoList, err error) {
  361. o := orm.NewOrmUsingDB("data")
  362. sql := ` SELECT * FROM edb_info WHERE edb_name=? AND edb_info_type = 1 `
  363. _, err = o.Raw(sql, edbName).QueryRows(&items)
  364. return
  365. }
  366. func GetEdbInfoByEnName(edbNameEn string) (items []*EdbInfoList, err error) {
  367. o := orm.NewOrmUsingDB("data")
  368. sql := ` SELECT * FROM edb_info WHERE edb_name_en=? AND edb_info_type = 0 `
  369. _, err = o.Raw(sql, edbNameEn).QueryRows(&items)
  370. return
  371. }
  372. func ModifyEdbInfoNameSource(edbNameSource string, edbInfoId int) (err error) {
  373. o := orm.NewOrmUsingDB("data")
  374. sql := ` UPDATE edb_info SET edb_name_source=? WHERE edb_info_id = ? `
  375. _, err = o.Raw(sql, edbNameSource, edbInfoId).Exec()
  376. return
  377. }
  378. func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
  379. o := orm.NewOrmUsingDB("data")
  380. sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
  381. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  382. return
  383. }
  384. type ChartEdbInfo struct {
  385. EdbInfoId int `description:"指标id"`
  386. EdbName string `description:"指标名称"`
  387. SourceName string `json:"-"`
  388. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  389. }
  390. func EdbInfoSearchByKeyWord(keyword string) (searchList []*ChartEdbInfo, err error) {
  391. pars := make([]interface{}, 0)
  392. o := orm.NewOrmUsingDB("data")
  393. sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  394. if keyword != "" {
  395. sql += ` AND (edb_name LIKE ? OR edb_code LIKE ? ) `
  396. pars = append(pars, utils.GetLikeKeyword(keyword), utils.GetLikeKeyword(keyword))
  397. }
  398. sql += ` ORDER BY create_time DESC `
  399. _, err = o.Raw(sql, pars...).QueryRows(&searchList)
  400. return
  401. }
  402. type EdbInfoMaxAndMinInfo struct {
  403. MinDate string `description:"最小日期"`
  404. MaxDate string `description:"最大日期"`
  405. MinValue float64 `description:"最小值"`
  406. MaxValue float64 `description:"最大值"`
  407. LatestValue float64 `description:"最新值"`
  408. }
  409. func GetEdbInfoMaxAndMinInfo(source int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  410. o := orm.NewOrmUsingDB("data")
  411. sql := ``
  412. tableName := GetEdbDataTableName(source)
  413. sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM %s WHERE edb_code=? `
  414. sql = fmt.Sprintf(sql, tableName)
  415. err = o.Raw(sql, edbCode).QueryRow(&item)
  416. if err != nil {
  417. return
  418. }
  419. var latest_value float64
  420. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  421. sql = fmt.Sprintf(sql, tableName)
  422. err = o.Raw(sql, edbCode).QueryRow(&latest_value)
  423. item.LatestValue = latest_value
  424. return
  425. }
  426. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  427. o := orm.NewOrmUsingDB("data")
  428. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? `
  429. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Exec()
  430. return
  431. }
  432. func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  433. o := orm.NewOrmUsingDB("data")
  434. sql := ` SELECT * FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  435. if condition != "" {
  436. sql += condition
  437. }
  438. sql += ` ORDER BY create_time DESC `
  439. _, err = o.Raw(sql, pars).QueryRows(&list)
  440. return
  441. }
  442. //order:1升序,其余值为降序
  443. func GetEdbDataListAll(condition string, pars []interface{}, source, order int) (item []*EdbInfoSearchData, err error) {
  444. o := orm.NewOrmUsingDB("data")
  445. sql := ``
  446. tableName := GetEdbDataTableName(source)
  447. sql = ` SELECT * FROM %s WHERE 1=1 `
  448. sql = fmt.Sprintf(sql, tableName)
  449. if condition != "" {
  450. sql += condition
  451. }
  452. if order == 1 {
  453. sql += ` ORDER BY data_time ASC `
  454. } else {
  455. sql += ` ORDER BY data_time DESC `
  456. }
  457. _, err = o.Raw(sql, pars).QueryRows(&item)
  458. return
  459. }
  460. // GetLastEdbData 获取最近的一条指标数据
  461. func GetLastEdbData(condition string, pars []interface{}, source int) (item *EdbInfoSearchData, err error) {
  462. o := orm.NewOrmUsingDB("data")
  463. sql := ``
  464. tableName := GetEdbDataTableName(source)
  465. sql = ` SELECT * FROM %s WHERE 1=1 `
  466. sql = fmt.Sprintf(sql, tableName)
  467. if condition != "" {
  468. sql += condition
  469. }
  470. sql += ` ORDER BY data_time DESC `
  471. err = o.Raw(sql, pars).QueryRow(&item)
  472. return
  473. }
  474. //order:1升序,其余值为降序
  475. func GetEdbDataCount(condition string, pars []interface{}, source int) (count int, err error) {
  476. o := orm.NewOrmUsingDB("data")
  477. sql := ``
  478. tableName := GetEdbDataTableName(source)
  479. sql = ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  480. sql = fmt.Sprintf(sql, tableName)
  481. if condition != "" {
  482. sql += condition
  483. }
  484. err = o.Raw(sql, pars).QueryRow(&count)
  485. return
  486. }
  487. func ModifyCalculateEdbInfo(item *EditEdbInfoReq) (err error) {
  488. o := orm.NewOrmUsingDB("data")
  489. sql := ` UPDATE edb_info
  490. SET
  491. edb_name =?,
  492. edb_name_source =?,
  493. frequency = ?,
  494. unit = ?,
  495. classify_id = ?,
  496. calculate_formula=?,
  497. modify_time = NOW()
  498. WHERE edb_info_id = ?`
  499. _, err = o.Raw(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.CalculateFormula, item.EdbInfoId).Exec()
  500. return
  501. }
  502. type AddEdbInfoResp struct {
  503. EdbInfoId int `description:"指标ID"`
  504. UniqueCode string `description:"指标唯一编码"`
  505. ClassifyId int `description:"分类id"`
  506. }
  507. // 根据基础指标获取所有关联指标
  508. func GetNeedRefreshCalculateEdbInfoFromBase(fromEdbInfo int) (list []*EdbInfo, err error) {
  509. o := orm.NewOrmUsingDB("data")
  510. sql := ` SELECT b.*
  511. FROM edb_info_calculate AS a
  512. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  513. WHERE from_edb_info_id=? `
  514. _, err = o.Raw(sql, fromEdbInfo).QueryRows(&list)
  515. return
  516. }
  517. func GetEdbInfoCalculateCount(edbInfoId int) (count int, err error) {
  518. o := orm.NewOrmUsingDB("data")
  519. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate WHERE from_edb_info_id=? `
  520. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  521. return
  522. }
  523. func GetEdbInfoCalculateLjzzyCount(edbInfoId int) (count int, err error) {
  524. o := orm.NewOrmUsingDB("data")
  525. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_ljzzy WHERE from_edb_info_id=? `
  526. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  527. return
  528. }
  529. func GetEdbInfoCalculateTbzCount(edbInfoId int) (count int, err error) {
  530. o := orm.NewOrmUsingDB("data")
  531. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tbz WHERE from_edb_info_id=? `
  532. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  533. return
  534. }
  535. func GetEdbInfoCalculateTczCount(edbInfoId int) (count int, err error) {
  536. o := orm.NewOrmUsingDB("data")
  537. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_tcz WHERE from_edb_info_id=? `
  538. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  539. return
  540. }
  541. func GetEdbInfoCalculateNszydpjjsCount(edbInfoId int) (count int, err error) {
  542. o := orm.NewOrmUsingDB("data")
  543. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_nszydpjjs WHERE from_edb_info_id=? `
  544. err = o.Raw(sql, edbInfoId).QueryRow(&count)
  545. return
  546. }
  547. func GetEdbInfoCalculateCountByCondition(source int, condition string, pars []interface{}) (count int, err error) {
  548. o := orm.NewOrmUsingDB("data")
  549. //calculateTableName := GetEdbInfoCalculateTableName(source)
  550. //if calculateTableName == "" {
  551. // err = errors.New("无效的表名")
  552. // return
  553. //}
  554. sql := ` SELECT COUNT(1) AS count FROM edb_info_calculate_mapping WHERE 1=1 `
  555. //sql = fmt.Sprintf(sql, calculateTableName)
  556. if condition != "" {
  557. sql += condition
  558. }
  559. err = o.Raw(sql, pars).QueryRow(&count)
  560. return
  561. }
  562. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  563. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  564. o := orm.NewOrmUsingDB("data")
  565. //calculateTableName := GetEdbInfoCalculateTableName(source)
  566. //if calculateTableName == "" {
  567. // err = errors.New("无效的表名")
  568. // return
  569. //}
  570. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  571. //sql = fmt.Sprintf(sql, calculateTableName)
  572. if condition != "" {
  573. sql += condition
  574. }
  575. _, err = o.Raw(sql, pars).QueryRows(&items)
  576. return
  577. }
  578. func GetRefreshEdbInfoFromCalculate(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  579. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  580. if err != nil && err.Error() != utils.ErrNoRow() {
  581. return
  582. }
  583. for _, item := range calculateList {
  584. if item.EdbType == 1 {
  585. baseEdbInfoArr = append(baseEdbInfoArr, item)
  586. } else {
  587. calculateInfoArr = append(calculateInfoArr, item)
  588. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromCalculate(item.EdbInfoId, item.Source)
  589. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  590. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  591. }
  592. }
  593. return
  594. }
  595. // 获取基础指标对应的所有计算指标
  596. func GetEdbInfoAllCalculate(edbInfoId int) (list []*EdbInfo, err error) {
  597. o := orm.NewOrmUsingDB("data")
  598. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  599. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  600. WHERE a.from_edb_info_id=?
  601. GROUP BY a.edb_info_id
  602. ORDER BY a.edb_info_id ASC `
  603. _, err = o.Raw(sql, edbInfoId).QueryRows(&list)
  604. return
  605. }
  606. // GetEdbInfoAllCalculateByEdbInfoIdList 根据指标id集合 获取基础指标对应的所有计算指标
  607. func GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList []int) (list []*EdbInfo, err error) {
  608. num := len(edbInfoIdList)
  609. if num <= 0 {
  610. return
  611. }
  612. o := orm.NewOrmUsingDB("data")
  613. sql := ` SELECT b.* FROM edb_info_calculate_mapping AS a
  614. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  615. WHERE a.from_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  616. GROUP BY a.edb_info_id
  617. ORDER BY a.edb_info_id ASC `
  618. _, err = o.Raw(sql, edbInfoIdList).QueryRows(&list)
  619. return
  620. }
  621. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  622. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  623. if err != nil && err.Error() != utils.ErrNoRow() {
  624. return
  625. }
  626. for _, item := range calculateList {
  627. if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
  628. continue
  629. }
  630. if item.EdbType == 1 {
  631. baseEdbInfoArr = append(baseEdbInfoArr, item)
  632. } else {
  633. calculateInfoArr = append(calculateInfoArr, item)
  634. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  635. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  636. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  637. }
  638. }
  639. return
  640. }
  641. func ModifyEdbInfoDataStatus(edbInfoId int64, source int, edbCode string) (err error) {
  642. o := orm.NewOrmUsingDB("data")
  643. sql := ``
  644. tableName := GetEdbDataTableName(source)
  645. sql = ` UPDATE %s SET edb_info_id=?,modify_time=NOW() WHERE edb_code=? `
  646. sql = fmt.Sprintf(sql, tableName)
  647. _, err = o.Raw(sql, edbInfoId, edbCode).Exec()
  648. return
  649. }
  650. // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  651. func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
  652. o := orm.NewOrmUsingDB("data")
  653. sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
  654. err = o.Raw(sql, classifyId).QueryRow(&item)
  655. return
  656. }
  657. // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
  658. func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  659. o := orm.NewOrmUsingDB("data")
  660. sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=?`
  661. if prevEdbInfoId > 0 {
  662. sql += ` AND ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  663. } else {
  664. sql += ` AND ( sort > ? )`
  665. }
  666. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  667. return
  668. }
  669. // Update 更新指标基础信息
  670. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  671. o := orm.NewOrmUsingDB("data")
  672. _, err = o.Update(edbInfo, cols...)
  673. return
  674. }
  675. type EdbInfoDataResp struct {
  676. EdbInfo *EdbInfo
  677. DataList []*EdbDataList
  678. }
  679. type EdbInfoDataFullClassifyResp struct {
  680. EdbInfo *EdbInfoFullClassify
  681. DataList []*EdbDataList
  682. }
  683. type EdbInfoReplaceReq struct {
  684. OldEdbInfoId int `description:"原指标ID"`
  685. NewEdbInfoId int `description:"替换为指标ID"`
  686. }
  687. /*
  688. EdbInfoReplace 替换指标初始版本(已废弃,废弃时间:2022年01月05日15:33:42)
  689. 1、替换图库中的指标
  690. 2、替换计算指标中的指标,完了计算指标中的指标,全部重新计算
  691. */
  692. func EdbInfoReplace(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (replaceChartTotal, replaceCalculateTotal int, err error) {
  693. var errmsg string
  694. o := orm.NewOrmUsingDB("data")
  695. to, err := o.Begin()
  696. if err != nil {
  697. return
  698. }
  699. defer func() {
  700. if errmsg != "" {
  701. fmt.Println("errmsg:" + errmsg)
  702. }
  703. }()
  704. //替换图表
  705. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  706. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  707. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  708. if err != nil {
  709. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  710. return
  711. }
  712. replaceChartTotal = len(chartEdbMappingList)
  713. for _, mv := range chartEdbMappingList {
  714. //获取图表所有指标信息
  715. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  716. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  717. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  718. if err != nil {
  719. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  720. return
  721. }
  722. var minData, maxData float64
  723. minData = newEdbInfo.MinValue
  724. maxData = newEdbInfo.MaxValue
  725. for _, cv := range chartEdbList {
  726. if mv.IsAxis == cv.IsAxis {
  727. if minData > cv.MinData {
  728. minData = cv.MinData
  729. }
  730. if maxData < cv.MaxData {
  731. maxData = cv.MaxData
  732. }
  733. }
  734. }
  735. //修改图表关联指标
  736. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  737. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  738. if err != nil {
  739. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  740. return
  741. }
  742. }
  743. //替换计算指标
  744. //获取所有包含的计算指标
  745. mappingList := make([]*EdbInfoCalculateMapping, 0)
  746. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  747. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  748. if err != nil {
  749. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  750. return
  751. }
  752. replaceCalculateTotal = len(mappingList)
  753. //计算指标
  754. for _, mv := range mappingList {
  755. //替换原指标
  756. msql := `UPDATE edb_info_calculate_mapping SET from_edb_info_id=?,from_edb_code=?,from_edb_name=?,from_source=?,from_source_name=? WHERE edb_info_calculate_mapping_id=? `
  757. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  758. if err != nil {
  759. return
  760. }
  761. }
  762. if err != nil && errmsg != "" {
  763. go alarm_msg.SendAlarmMsg("替换指标失败提醒,errmsg:"+errmsg, 3)
  764. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  765. _ = to.Rollback()
  766. } else {
  767. _ = to.Commit()
  768. }
  769. //获取计算指标关联指标
  770. allMappingList := make([]*EdbInfoCalculateMapping, 0)
  771. allMapping := make(map[int]int)
  772. tmpMapping := make(map[int]int)
  773. for _, v := range mappingList {
  774. if _, ok := allMapping[v.EdbInfoId]; !ok {
  775. allMappingList = append(allMappingList, v)
  776. }
  777. newList, _ := GetAllCalculate(v.EdbInfoId, tmpMapping)
  778. for _, nv := range newList {
  779. if _, ok := allMapping[v.EdbInfoId]; !ok {
  780. allMappingList = append(allMappingList, nv)
  781. }
  782. allMapping[v.EdbInfoId] = v.EdbInfoId
  783. }
  784. allMapping[v.EdbInfoId] = v.EdbInfoId
  785. }
  786. for mk, mv := range allMappingList { //原指标关联的所有计算指标
  787. fmt.Println("mk/mv", mk, mv)
  788. //重新计算计算指标
  789. {
  790. edbInfo, err := GetEdbInfoById(mv.EdbInfoId) //计算指标
  791. if err != nil {
  792. if err.Error() == utils.ErrNoRow() {
  793. errmsg = "计算指标已被删除"
  794. return replaceChartTotal, replaceCalculateTotal, err
  795. }
  796. errmsg = "获取计算指标失败:err:" + err.Error()
  797. return replaceChartTotal, replaceCalculateTotal, err
  798. }
  799. fromEdbInfoList, err := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
  800. if err != nil {
  801. errmsg = "获取计算指标失败:err:" + err.Error()
  802. return replaceChartTotal, replaceCalculateTotal, err
  803. }
  804. if len(fromEdbInfoList) <= 0 {
  805. errmsg = "计算指标所依赖指标不存在"
  806. return replaceChartTotal, replaceCalculateTotal, err
  807. }
  808. fromEdbInfoItem := fromEdbInfoList[0]
  809. if fromEdbInfoItem == nil {
  810. return replaceChartTotal, replaceCalculateTotal, err
  811. }
  812. fromEdbInfo, err := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
  813. if err != nil {
  814. errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
  815. return replaceChartTotal, replaceCalculateTotal, err
  816. }
  817. edbCode := edbInfo.EdbCode
  818. uniqueCode := edbInfo.UniqueCode
  819. sourName := edbInfo.SourceName
  820. source := edbInfo.Source
  821. edbInfoId := edbInfo.EdbInfoId
  822. req := new(EdbInfoCalculateBatchSaveReq)
  823. req.EdbInfoId = edbInfoId
  824. req.EdbName = edbInfo.EdbName
  825. req.Frequency = edbInfo.Frequency
  826. req.Unit = edbInfo.Unit
  827. req.ClassifyId = edbInfo.ClassifyId
  828. req.FromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  829. if source == utils.DATA_SOURCE_CALCULATE {
  830. //检验公式
  831. var formulaStr string
  832. var edbInfoIdBytes []string
  833. for _, v := range fromEdbInfoList {
  834. formulaStr += v.FromTag + ","
  835. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  836. }
  837. formulaStr = strings.Trim(formulaStr, ",")
  838. formulaMap := CheckFormula(edbInfo.CalculateFormula)
  839. for _, v := range formulaMap {
  840. if !strings.Contains(formulaStr, v) {
  841. errmsg = "公式错误,请重新填写"
  842. return replaceChartTotal, replaceCalculateTotal, err
  843. }
  844. }
  845. edbInfoList := make([]*EdbInfo, 0)
  846. for _, v := range fromEdbInfoList {
  847. edbInfo, err := GetEdbInfoById(v.FromEdbInfoId)
  848. if err != nil {
  849. if err.Error() == utils.ErrNoRow() {
  850. errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  851. return replaceChartTotal, replaceCalculateTotal, err
  852. }
  853. errmsg = "获取指标失败:Err:" + err.Error()
  854. return replaceChartTotal, replaceCalculateTotal, err
  855. }
  856. edbInfoList = append(edbInfoList, edbInfo)
  857. }
  858. //清除历史数据
  859. err = DeleteCalculateData(edbInfoId)
  860. if err != nil {
  861. errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
  862. return replaceChartTotal, replaceCalculateTotal, err
  863. }
  864. err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  865. if err != nil {
  866. errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
  867. return replaceChartTotal, replaceCalculateTotal, err
  868. }
  869. } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  870. _, err = AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  871. } else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  872. sourName = "同比值"
  873. edbInfoId, err = AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  874. } else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  875. sourName = "同差值"
  876. edbInfoId, err = AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  877. } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  878. sourName = "N数值移动平均计算"
  879. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  880. edbInfoId, err = AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  881. } else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  882. sourName = "环比值"
  883. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  884. edbInfoId, err = AddCalculateHbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  885. } else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  886. sourName = "环差值"
  887. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  888. edbInfoId, err = AddCalculateHcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  889. } else if source == utils.DATA_SOURCE_CALCULATE_BP {
  890. sourName = "变频"
  891. fmt.Println("变频", req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  892. edbInfoId, err = AddCalculateBp(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  893. } else {
  894. errmsg = "无效计算方式,source:" + strconv.Itoa(source)
  895. return replaceChartTotal, replaceCalculateTotal, err
  896. }
  897. maxAndMinItem, err := GetEdbInfoMaxAndMinInfo(source, edbCode)
  898. if err != nil && err.Error() != utils.ErrNoRow() {
  899. errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
  900. return replaceChartTotal, replaceCalculateTotal, err
  901. }
  902. if maxAndMinItem != nil {
  903. err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  904. }
  905. }
  906. }
  907. return
  908. }
  909. // EdbInfoReplaceV2 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  910. //func EdbInfoReplaceV2(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (mappingList,replaceChartTotal, replaceCalculateTotal int, err error) {
  911. // defer func() {
  912. // if err != nil {
  913. // fmt.Println("ERR:", err.Error())
  914. // }
  915. // }()
  916. //
  917. // // 替换关联表中的source_edb_info_id
  918. // mappingList, replaceChartTotal, replaceCalculateTotal, err := replaceChartEdb(oldEdbInfo, newEdbInfo)
  919. // if err != nil {
  920. // return
  921. // }
  922. // err = replaceRelationEdbInfo(mappingList, oldEdbInfo, newEdbInfo, sysAdminId, sysAdminRealName)
  923. // return
  924. //}
  925. // ReplaceChartEdb 替换图表中的指标
  926. func ReplaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (relationEdbInfoIdList []int, replaceChartTotal, replaceCalculateTotal int, err error) {
  927. var errmsg string
  928. relationEdbInfoIdList = make([]int, 0) // 关联的指标id
  929. logMsg := `` // 记录替换的日志
  930. o := orm.NewOrmUsingDB("data")
  931. to, err := o.Begin()
  932. if err != nil {
  933. return
  934. }
  935. defer func() {
  936. if err != nil {
  937. _ = to.Rollback()
  938. } else {
  939. _ = to.Commit()
  940. if logMsg != `` {
  941. utils.FileLog.Info(fmt.Sprintf("替换指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  942. }
  943. }
  944. if errmsg != "" {
  945. fmt.Println("errmsg:" + errmsg)
  946. }
  947. if err != nil && errmsg != "" {
  948. go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
  949. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  950. }
  951. }()
  952. //替换图表
  953. {
  954. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  955. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  956. _, err = to.Raw(csql, oldEdbInfo.EdbInfoId).QueryRows(&chartEdbMappingList)
  957. if err != nil {
  958. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  959. return
  960. }
  961. replaceChartTotal = len(chartEdbMappingList)
  962. if len(chartEdbMappingList) > 0 {
  963. chartInfoIdList := make([]string, 0)
  964. for _, mv := range chartEdbMappingList {
  965. //获取图表所有指标信息
  966. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  967. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  968. _, err = to.Raw(csql, mv.ChartInfoId).QueryRows(&chartEdbList)
  969. if err != nil {
  970. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  971. return
  972. }
  973. var minData, maxData float64
  974. minData = newEdbInfo.MinValue
  975. maxData = newEdbInfo.MaxValue
  976. for _, cv := range chartEdbList {
  977. if mv.IsAxis == cv.IsAxis {
  978. if minData > cv.MinData {
  979. minData = cv.MinData
  980. }
  981. if maxData < cv.MaxData {
  982. maxData = cv.MaxData
  983. }
  984. }
  985. }
  986. //修改图表关联指标
  987. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  988. _, err = to.Raw(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Exec()
  989. if err != nil {
  990. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  991. return
  992. }
  993. chartInfoIdList = append(chartInfoIdList, fmt.Sprint(mv.ChartInfoId))
  994. }
  995. logMsg += `涉及到的图表id:` + strings.Join(chartInfoIdList, ",") + ";"
  996. }
  997. }
  998. //替换计算指标
  999. {
  1000. //获取所有包含的计算指标
  1001. mappingList := make([]*EdbInfoCalculateMapping, 0)
  1002. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  1003. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&mappingList)
  1004. if err != nil {
  1005. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  1006. return
  1007. }
  1008. replaceCalculateTotal = len(mappingList)
  1009. //计算指标
  1010. if len(mappingList) > 0 {
  1011. edbInfoIdList := make([]string, 0)
  1012. for _, mv := range mappingList {
  1013. //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
  1014. if mv.EdbInfoId == newEdbInfo.EdbInfoId {
  1015. continue
  1016. }
  1017. //替换原指标
  1018. msql := `UPDATE edb_info_calculate_mapping SET from_edb_info_id=?,from_edb_code=?,from_edb_name=?,from_source=?,from_source_name=? WHERE edb_info_calculate_mapping_id=? `
  1019. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Exec()
  1020. if err != nil {
  1021. return
  1022. }
  1023. relationEdbInfoIdList = append(relationEdbInfoIdList, mv.EdbInfoId)
  1024. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(mv.EdbInfoId))
  1025. }
  1026. logMsg += `涉及到的指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1027. }
  1028. }
  1029. // 替换预测指标中的来源指标
  1030. {
  1031. //获取所有包含的计算指标
  1032. predictEdbConfList := make([]*PredictEdbConf, 0)
  1033. msql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? GROUP BY predict_edb_info_id `
  1034. _, err = to.Raw(msql, oldEdbInfo.EdbInfoId).QueryRows(&predictEdbConfList)
  1035. if err != nil {
  1036. errmsg = "获取关联的预测指标信息失败:Err:" + err.Error()
  1037. return
  1038. }
  1039. replaceCalculateTotal += len(predictEdbConfList)
  1040. //替换预测指标的来源指标
  1041. msql = `UPDATE predict_edb_conf SET source_edb_info_id=? WHERE source_edb_info_id=? `
  1042. _, err = to.Raw(msql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).Exec()
  1043. if err != nil {
  1044. return
  1045. }
  1046. if len(predictEdbConfList) > 0 {
  1047. edbInfoIdList := make([]string, 0)
  1048. for _, v := range predictEdbConfList {
  1049. relationEdbInfoIdList = append(relationEdbInfoIdList, v.PredictEdbInfoId)
  1050. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(v.PredictEdbInfoId))
  1051. }
  1052. logMsg += `涉及到的预测指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1053. }
  1054. }
  1055. return
  1056. }
  1057. // replaceRelationEdbInfo 替换关联指标
  1058. func replaceRelationEdbInfo(mappingList []*EdbInfoCalculateMapping, oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (err error) {
  1059. errmsg := ``
  1060. defer func() {
  1061. if errmsg != "" {
  1062. fmt.Println("errmsg:" + errmsg)
  1063. }
  1064. if err != nil && errmsg != "" {
  1065. go alarm_msg.SendAlarmMsg("替换关联指标失败提醒,errmsg:"+errmsg, 3)
  1066. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  1067. }
  1068. }()
  1069. //获取计算指标关联指标
  1070. allMappingList := make([]*EdbInfoCalculateMapping, 0)
  1071. allMapping := make(map[int]int)
  1072. tmpMapping := make(map[int]int)
  1073. for _, v := range mappingList {
  1074. if _, ok := allMapping[v.EdbInfoId]; !ok {
  1075. allMappingList = append(allMappingList, v)
  1076. }
  1077. newList, _ := GetAllCalculateV2(v.EdbInfoId, newEdbInfo.EdbInfoId, tmpMapping)
  1078. for _, nv := range newList {
  1079. if _, ok := allMapping[v.EdbInfoId]; !ok {
  1080. allMappingList = append(allMappingList, nv)
  1081. }
  1082. allMapping[v.EdbInfoId] = v.EdbInfoId
  1083. }
  1084. allMapping[v.EdbInfoId] = v.EdbInfoId
  1085. }
  1086. for mk, mv := range allMappingList { //原指标关联的所有计算指标
  1087. fmt.Println("mk/mv", mk, mv)
  1088. //重新计算计算指标
  1089. {
  1090. edbInfo, tmpErr := GetEdbInfoById(mv.EdbInfoId) //计算指标
  1091. err = tmpErr
  1092. if err != nil {
  1093. if err.Error() == utils.ErrNoRow() {
  1094. errmsg = "计算指标已被删除"
  1095. } else {
  1096. errmsg = "获取计算指标失败:err:" + err.Error()
  1097. }
  1098. return
  1099. }
  1100. fromEdbInfoList, tmpErr := GetEdbInfoCalculateDetail(mv.EdbInfoId, edbInfo.Source)
  1101. err = tmpErr
  1102. if err != nil {
  1103. errmsg = "获取计算指标失败:err:" + err.Error()
  1104. return
  1105. }
  1106. if len(fromEdbInfoList) <= 0 {
  1107. errmsg = "计算指标所依赖指标不存在"
  1108. err = fmt.Errorf("计算指标所依赖指标不存在")
  1109. return
  1110. }
  1111. fromEdbInfoItem := fromEdbInfoList[0]
  1112. if fromEdbInfoItem == nil {
  1113. return
  1114. }
  1115. fromEdbInfo, tmpErr := GetEdbInfoById(fromEdbInfoItem.FromEdbInfoId)
  1116. err = tmpErr
  1117. if err != nil {
  1118. errmsg = "获取计算指标 来源指标失败:err:" + err.Error()
  1119. return
  1120. }
  1121. edbCode := edbInfo.EdbCode
  1122. uniqueCode := edbInfo.UniqueCode
  1123. sourName := edbInfo.SourceName
  1124. source := edbInfo.Source
  1125. edbInfoId := edbInfo.EdbInfoId
  1126. req := new(EdbInfoCalculateBatchSaveReq)
  1127. req.EdbInfoId = edbInfoId
  1128. req.EdbName = edbInfo.EdbName
  1129. req.Frequency = edbInfo.Frequency
  1130. req.Unit = edbInfo.Unit
  1131. req.ClassifyId = edbInfo.ClassifyId
  1132. req.FromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1133. if source == utils.DATA_SOURCE_CALCULATE {
  1134. //检验公式
  1135. var formulaStr string
  1136. var edbInfoIdBytes []string
  1137. for _, v := range fromEdbInfoList {
  1138. formulaStr += v.FromTag + ","
  1139. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  1140. }
  1141. formulaStr = strings.Trim(formulaStr, ",")
  1142. formulaMap := CheckFormula(edbInfo.CalculateFormula)
  1143. for _, v := range formulaMap {
  1144. if !strings.Contains(formulaStr, v) {
  1145. errmsg = "公式错误,请重新填写"
  1146. err = fmt.Errorf("公式错误,请重新填写")
  1147. return
  1148. }
  1149. }
  1150. edbInfoList := make([]*EdbInfo, 0)
  1151. for _, v := range fromEdbInfoList {
  1152. edbInfo, tmpErr := GetEdbInfoById(v.FromEdbInfoId)
  1153. err = tmpErr
  1154. if err != nil {
  1155. if err.Error() == utils.ErrNoRow() {
  1156. errmsg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  1157. } else {
  1158. errmsg = "获取指标失败:Err:" + err.Error()
  1159. }
  1160. return
  1161. }
  1162. edbInfoList = append(edbInfoList, edbInfo)
  1163. }
  1164. //清除历史数据
  1165. err = DeleteCalculateData(edbInfoId)
  1166. if err != nil {
  1167. errmsg = "清空运算指标失败:Err:" + err.Error() + " edb_info_id:" + strconv.Itoa(edbInfoId)
  1168. return
  1169. }
  1170. err = Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  1171. if err != nil {
  1172. errmsg = "生成计算指标失败,Calculate Err:" + err.Error()
  1173. return
  1174. }
  1175. } else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  1176. _, err = AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1177. } else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  1178. sourName = "同比值"
  1179. edbInfoId, err = AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1180. } else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  1181. sourName = "同差值"
  1182. edbInfoId, err = AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1183. } else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  1184. sourName = "N数值移动平均计算"
  1185. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1186. edbInfoId, err = AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1187. } else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  1188. sourName = "环比值"
  1189. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1190. edbInfoId, err = AddCalculateHbz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1191. } else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  1192. sourName = "环差值"
  1193. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1194. edbInfoId, err = AddCalculateHcz(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName, formulaInt)
  1195. } else if source == utils.DATA_SOURCE_CALCULATE_BP {
  1196. sourName = "变频"
  1197. fmt.Println("变频", req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1198. edbInfoId, err = AddCalculateBp(req, fromEdbInfo, edbCode, uniqueCode, sysAdminId, sysAdminRealName)
  1199. } else if source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT {
  1200. req2 := &EdbInfoCalculateBatchEditReq{
  1201. EdbName: edbInfo.EdbName,
  1202. Frequency: edbInfo.Frequency,
  1203. Unit: edbInfo.Unit,
  1204. ClassifyId: edbInfo.ClassifyId,
  1205. Formula: edbInfo.CalculateFormula,
  1206. EdbInfoId: edbInfo.EdbInfoId,
  1207. FromEdbInfoId: fromEdbInfoList[0].FromEdbInfoId,
  1208. Source: edbInfo.Source,
  1209. MoveType: edbInfo.MoveType,
  1210. MoveFrequency: edbInfo.MoveFrequency,
  1211. }
  1212. sourName = "时间移位"
  1213. edbInfoId, err = EditCalculateTimeShift(req2, fromEdbInfo, edbCode, edbInfo)
  1214. } else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ {
  1215. edbInfoIdArr := make([]EdbInfoFromTag, 0)
  1216. //A指标
  1217. aFromEdbInfoId := fromEdbInfoList[0].FromEdbInfoId
  1218. for _, v := range fromEdbInfoList {
  1219. if v.FromTag == "A" {
  1220. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1221. aFromEdbInfoId = newEdbInfo.EdbInfoId
  1222. } else {
  1223. aFromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1224. }
  1225. }
  1226. if v.FromTag == "B" {
  1227. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1228. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1229. EdbInfoId: newEdbInfo.EdbInfoId,
  1230. FromTag: v.FromTag,
  1231. MoveValue: v.MoveValue,
  1232. })
  1233. } else {
  1234. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1235. EdbInfoId: v.FromEdbInfoId,
  1236. FromTag: v.FromTag,
  1237. MoveValue: v.MoveValue,
  1238. })
  1239. }
  1240. }
  1241. }
  1242. req2 := &EdbInfoCalculateBatchEditReq{
  1243. EdbName: edbInfo.EdbName,
  1244. Frequency: edbInfo.Frequency,
  1245. Unit: edbInfo.Unit,
  1246. ClassifyId: edbInfo.ClassifyId,
  1247. Formula: edbInfo.CalculateFormula,
  1248. EdbInfoId: edbInfo.EdbInfoId,
  1249. FromEdbInfoId: aFromEdbInfoId,
  1250. Source: edbInfo.Source,
  1251. MoveType: edbInfo.MoveType,
  1252. MoveFrequency: edbInfo.MoveFrequency,
  1253. EdbInfoIdArr: edbInfoIdArr,
  1254. }
  1255. sourName = "直接拼接"
  1256. if len(req2.EdbInfoIdArr) != 1 {
  1257. errmsg = "请传入拼接日期之后的指标"
  1258. err = fmt.Errorf("请传入拼接日期之后的指标")
  1259. return
  1260. }
  1261. secondEdbInfoReq := req2.EdbInfoIdArr[0]
  1262. secondEdbInfo, tmpErr := GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1263. err = tmpErr
  1264. if err != nil {
  1265. errmsg = "获取拼接日期之后的指标信息失败:Err:" + err.Error()
  1266. err = fmt.Errorf("获取拼接日期之后的指标信息失败:Err:" + err.Error())
  1267. return
  1268. }
  1269. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1270. errmsg = "两个指标不允许为同一个"
  1271. err = fmt.Errorf("两个指标不允许为同一个")
  1272. return
  1273. }
  1274. tmpEdbInfoId, tmpErr := EditCalculateZjpj(req2, edbInfo, fromEdbInfo, secondEdbInfo)
  1275. err = tmpErr
  1276. edbInfoId = tmpEdbInfoId
  1277. } else if source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ { //累计值同比拼接
  1278. edbInfoIdArr := make([]EdbInfoFromTag, 0)
  1279. //A指标
  1280. aFromEdbInfoId := fromEdbInfoList[0].FromEdbInfoId
  1281. for _, v := range fromEdbInfoList {
  1282. if v.FromTag == "A" {
  1283. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1284. aFromEdbInfoId = newEdbInfo.EdbInfoId
  1285. } else {
  1286. aFromEdbInfoId = fromEdbInfoList[0].FromEdbInfoId
  1287. }
  1288. }
  1289. if v.FromTag == "B" {
  1290. if v.FromEdbInfoId == oldEdbInfo.EdbInfoId {
  1291. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1292. EdbInfoId: newEdbInfo.EdbInfoId,
  1293. FromTag: v.FromTag,
  1294. MoveValue: v.MoveValue,
  1295. })
  1296. } else {
  1297. edbInfoIdArr = append(edbInfoIdArr, EdbInfoFromTag{
  1298. EdbInfoId: v.FromEdbInfoId,
  1299. FromTag: v.FromTag,
  1300. MoveValue: v.MoveValue,
  1301. })
  1302. }
  1303. }
  1304. }
  1305. req2 := &EdbInfoCalculateBatchEditReq{
  1306. EdbName: edbInfo.EdbName,
  1307. Frequency: edbInfo.Frequency,
  1308. Unit: edbInfo.Unit,
  1309. ClassifyId: edbInfo.ClassifyId,
  1310. Formula: edbInfo.CalculateFormula,
  1311. EdbInfoId: edbInfo.EdbInfoId,
  1312. FromEdbInfoId: aFromEdbInfoId,
  1313. Source: edbInfo.Source,
  1314. MoveType: edbInfo.MoveType,
  1315. MoveFrequency: edbInfo.MoveFrequency,
  1316. EdbInfoIdArr: edbInfoIdArr,
  1317. }
  1318. sourName = "累计值同比拼接"
  1319. if fromEdbInfo.Frequency != "月度" {
  1320. errmsg = "累计值同比拼接,待拼接指标只能筛选月度指标"
  1321. err = fmt.Errorf("累计值同比拼接,待拼接指标只能筛选月度指标")
  1322. return
  1323. }
  1324. if len(req2.EdbInfoIdArr) != 1 {
  1325. errmsg = "请传入同比值指标"
  1326. err = fmt.Errorf("请传入同比值指标")
  1327. return
  1328. }
  1329. secondEdbInfoReq := req2.EdbInfoIdArr[0]
  1330. tbzEdbInfo, tmpErr := GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1331. err = tmpErr
  1332. if err != nil {
  1333. errmsg = "获取同比值指标信息失败:Err:" + err.Error()
  1334. err = fmt.Errorf("获取同比值指标信息失败:Err:" + err.Error())
  1335. return
  1336. }
  1337. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  1338. errmsg = "指标必须是传入同比值指标类型"
  1339. err = fmt.Errorf("指标必须是传入同比值指标类型")
  1340. return
  1341. }
  1342. if tbzEdbInfo.Frequency != "月度" {
  1343. errmsg = "同比值指标只能筛选月度指标"
  1344. err = fmt.Errorf("同比值指标只能筛选月度指标")
  1345. return
  1346. }
  1347. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  1348. errmsg = "两个指标不允许为同一个"
  1349. err = fmt.Errorf("两个指标不允许为同一个")
  1350. return
  1351. }
  1352. tmpEdbInfoId, tmpErr := EditCalculateLjztbpj(req2, edbInfo, fromEdbInfo, tbzEdbInfo)
  1353. err = tmpErr
  1354. edbInfoId = tmpEdbInfoId
  1355. } else if source == utils.DATA_SOURCE_PYTHON {
  1356. //python代码运算的过滤
  1357. continue
  1358. } else {
  1359. errmsg = "无效计算方式,source:" + strconv.Itoa(source)
  1360. err = fmt.Errorf("无效计算方式,source:" + strconv.Itoa(source))
  1361. return
  1362. }
  1363. maxAndMinItem, tmpErr := GetEdbInfoMaxAndMinInfo(source, edbCode)
  1364. err = tmpErr
  1365. if err != nil && err.Error() != utils.ErrNoRow() {
  1366. errmsg = "生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error()
  1367. err = fmt.Errorf("生成" + sourName + "失败,GetEdbInfoMaxAndMinInfo Err:" + err.Error())
  1368. return
  1369. }
  1370. if maxAndMinItem != nil {
  1371. err = ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  1372. }
  1373. }
  1374. }
  1375. return
  1376. }
  1377. type EdbInfoView struct {
  1378. EdbInfoId int `orm:"column(edb_info_id);pk"`
  1379. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  1380. SourceName string `description:"来源名称"`
  1381. Source int `description:"来源id"`
  1382. EdbCode string `description:"指标编码"`
  1383. EdbName string `description:"指标名称"`
  1384. EdbNameSource string `description:"指标名称来源"`
  1385. Frequency string `description:"频率"`
  1386. Unit string `description:"单位"`
  1387. StartDate string `description:"起始日期"`
  1388. EndDate string `description:"终止日期"`
  1389. ClassifyId int `description:"分类id"`
  1390. SysUserId int
  1391. SysUserRealName string
  1392. UniqueCode string `description:"指标唯一编码"`
  1393. CreateTime string
  1394. ModifyTime string
  1395. MinValue float64 `description:"指标最小值"`
  1396. MaxValue float64 `description:"指标最大值"`
  1397. CalculateFormula string `description:"计算公式"`
  1398. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  1399. Sort int `description:"排序字段"`
  1400. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  1401. LatestDate string `description:"数据最新日期"`
  1402. LatestValue float64 `description:"数据最新值"`
  1403. }
  1404. // 获取指标的所有计算指标,以及计算指标所依赖计算指标
  1405. func GetAllCalculateByEdbInfoId(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1406. o := orm.NewOrmUsingDB("data")
  1407. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  1408. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  1409. if err != nil {
  1410. return
  1411. }
  1412. return
  1413. }
  1414. func GetAllCalculate(edbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1415. calculateList, err := GetAllCalculateByEdbInfoId(edbInfoId)
  1416. if err != nil && err.Error() != utils.ErrNoRow() {
  1417. return
  1418. }
  1419. for _, item := range calculateList {
  1420. _, ok := edbInfoMap[item.EdbInfoId]
  1421. if !ok {
  1422. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  1423. mappingList = append(mappingList, item)
  1424. newCalculateInfoArr, _ := GetAllCalculate(item.EdbInfoId, edbInfoMap)
  1425. mappingList = append(mappingList, newCalculateInfoArr...)
  1426. }
  1427. }
  1428. return
  1429. }
  1430. // GetAllCalculateByEdbInfoIdV2 获取计算指标的所有所依赖的指标(基础指标+计算指标)
  1431. func GetAllCalculateByEdbInfoIdV2(edbInfoId int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1432. o := orm.NewOrmUsingDB("data")
  1433. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE edb_info_id=? GROUP BY from_edb_info_id `
  1434. _, err = o.Raw(msql, edbInfoId).QueryRows(&mappingList)
  1435. if err != nil {
  1436. return
  1437. }
  1438. return
  1439. }
  1440. // GetAllCalculateV2 获取所有的指标(如果下层指标包含了待替换指标,那么就不加入)
  1441. func GetAllCalculateV2(oldEdbInfoId, replaceEdbInfoId int, edbInfoMap map[int]int) (mappingList []*EdbInfoCalculateMapping, err error) {
  1442. calculateList, err := GetAllCalculateByEdbInfoId(oldEdbInfoId)
  1443. if err != nil && err.Error() != utils.ErrNoRow() {
  1444. return
  1445. }
  1446. tmpMappingList := make([]*EdbInfoCalculateMapping, 0)
  1447. for _, item := range calculateList {
  1448. //如果计算指标里面存在即将替换的指标id,那么退出当前循环,进入下一循环
  1449. if item.EdbInfoId == replaceEdbInfoId {
  1450. err = fmt.Errorf("指标重复1")
  1451. return
  1452. }
  1453. //获取该指标的依赖指标列表,如果依赖指标列表中存在即将替换的指标,那么过滤该指标
  1454. childCalculateList, tmpErr := GetAllCalculateByEdbInfoIdV2(item.EdbInfoId)
  1455. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  1456. err = tmpErr
  1457. return
  1458. }
  1459. for _, childCalculate := range childCalculateList {
  1460. if childCalculate.FromEdbInfoId == replaceEdbInfoId {
  1461. err = fmt.Errorf("指标重复2")
  1462. return
  1463. }
  1464. }
  1465. _, ok := edbInfoMap[item.EdbInfoId]
  1466. if !ok {
  1467. edbInfoMap[item.EdbInfoId] = item.EdbInfoId
  1468. newCalculateInfoArr, tmpErr := GetAllCalculateV2(item.EdbInfoId, replaceEdbInfoId, edbInfoMap)
  1469. if tmpErr != nil {
  1470. err = tmpErr
  1471. continue
  1472. } else {
  1473. mappingList = append(mappingList, item)
  1474. tmpMappingList = append(tmpMappingList, newCalculateInfoArr...)
  1475. }
  1476. }
  1477. }
  1478. mappingList = append(mappingList, tmpMappingList...)
  1479. return
  1480. }
  1481. // EdbInfoFilterDataResp 搜索指标列表数据返回
  1482. type EdbInfoFilterDataResp struct {
  1483. Paging *paging.PagingItem
  1484. List []*EdbInfoList
  1485. }
  1486. // GetEdbInfoFilterList 获取指指标列表数据接口
  1487. func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
  1488. o := orm.NewOrmUsingDB("data")
  1489. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  1490. err = o.Raw(sql, pars).QueryRow(&total)
  1491. if err != nil {
  1492. return
  1493. }
  1494. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  1495. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  1496. return
  1497. }
  1498. // SetEdbInfoImageReq 设置指标图片
  1499. type SetEdbInfoImageReq struct {
  1500. EdbInfoId int `description:"指标ID"`
  1501. ImageUrl string `description:"指标图片地址"`
  1502. }
  1503. // GetNextEdbInfoByCondition 根据条件获取下一个指标
  1504. func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
  1505. o := orm.NewOrmUsingDB("data")
  1506. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1507. if condition != "" {
  1508. sql += condition
  1509. }
  1510. sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
  1511. err = o.Raw(sql, pars).QueryRow(&item)
  1512. return
  1513. }
  1514. // GetNextEdbInfo 根据分类id获取下一个 指标
  1515. func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
  1516. o := orm.NewOrmUsingDB("data")
  1517. sql := ` SELECT b.* FROM edb_classify AS a
  1518. INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
  1519. WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
  1520. AND a.classify_type=? AND b.edb_info_type=?
  1521. ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
  1522. LIMIT 1 `
  1523. err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).QueryRow(&item)
  1524. return
  1525. }
  1526. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  1527. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  1528. num := len(edbCodeList)
  1529. if num <= 0 {
  1530. return
  1531. }
  1532. o := orm.NewOrmUsingDB("data")
  1533. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  1534. _, err = o.Raw(sql, source, edbCodeList).QueryRows(&items)
  1535. return
  1536. }
  1537. // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
  1538. func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
  1539. o := orm.NewOrmUsingDB("data")
  1540. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  1541. if condition != "" {
  1542. sql += condition
  1543. }
  1544. err = o.Raw(sql, pars).QueryRow(&count)
  1545. return
  1546. }
  1547. // GetEdbInfoListByCondition 根据条件获取指标列表数据
  1548. func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfoList, err error) {
  1549. o := orm.NewOrmUsingDB("data")
  1550. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1551. if condition != "" {
  1552. sql += condition
  1553. }
  1554. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  1555. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1556. return
  1557. }
  1558. // GetEdbInfoListV2ByCondition 根据条件获取指标列表数据(EdbInfo原始数据结构)
  1559. func GetEdbInfoListV2ByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfo, err error) {
  1560. o := orm.NewOrmUsingDB("data")
  1561. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1562. if condition != "" {
  1563. sql += condition
  1564. }
  1565. sql += ` ORDER BY edb_info_id ASC LIMIT ?,? `
  1566. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1567. return
  1568. }
  1569. // GetEdbInfoListGroupByUserId 根据指标id列表、用户分组获取指标信息
  1570. func GetEdbInfoListGroupByUserId(edbIdList []string) (items []*EdbInfo, err error) {
  1571. num := len(edbIdList)
  1572. if num <= 0 {
  1573. return
  1574. }
  1575. o := orm.NewOrmUsingDB("data")
  1576. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
  1577. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1578. return
  1579. }
  1580. // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
  1581. func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1582. num := len(edbIdList)
  1583. if num <= 0 {
  1584. return
  1585. }
  1586. o := orm.NewOrmUsingDB("data")
  1587. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1588. _, err = o.Raw(sql, userId, userName, edbIdList).Exec()
  1589. return
  1590. }
  1591. // MoveEdbChartList 图表/指标信息
  1592. type MoveEdbChartList struct {
  1593. DataId string `description:"指标/图表唯一id"`
  1594. Code string `description:"指标code"`
  1595. Name string `description:"指标/图表名称"`
  1596. ClassifyName string `description:"分类名称"`
  1597. CreateUserId int `description:"创建人id"`
  1598. CreateUserName string `description:"创建人名称"`
  1599. }
  1600. // MoveEdbChartListResp 更改创建人返回数据
  1601. type MoveEdbChartListResp struct {
  1602. Paging *paging.PagingItem
  1603. List []MoveEdbChartList
  1604. }
  1605. // MoveEdbChartReq 转移指标/图表的请求
  1606. type MoveEdbChartReq struct {
  1607. Source int `description:"来源id"`
  1608. DataIdList []string `description:"指标/图表唯一id列表"`
  1609. NewUserId int `description:"新的创建人id"`
  1610. }
  1611. type EdbChartClassifyResp struct {
  1612. List []*EdbChartClassify
  1613. }
  1614. type EdbChartClassify struct {
  1615. ClassifyId int
  1616. ClassifyName string
  1617. ParentId int
  1618. Child []*EdbChartClassify
  1619. }
  1620. // EdbInfoDataSeasonalResp 指标季节性数据返回
  1621. type EdbInfoDataSeasonalResp struct {
  1622. EdbInfo *EdbInfo
  1623. DataList interface{}
  1624. }
  1625. // SetEdbDataInsertConfigReq 设置插入规则的请求
  1626. type SetEdbDataInsertConfigReq struct {
  1627. EdbInfoId int `description:"指标ID"`
  1628. Date string `description:"日期"`
  1629. Value float64 `description:"值"`
  1630. }
  1631. // GetEdbInfoByClassifyId 用于分类展示
  1632. func GetEdbInfoByClassifyId(classifyId, edbInfoType, adminId int) (items []*EdbClassifyItems, err error) {
  1633. o := orm.NewOrmUsingDB("data")
  1634. sql := ` SELECT edb_info_id,classify_id,edb_name_source AS classify_name,edb_name_en AS classify_name_en,unique_code,source_name,source,sys_user_id,sys_user_real_name,start_date,edb_code,edb_type, sort FROM edb_info WHERE classify_id = ? AND edb_info_type = ?`
  1635. pars := []interface{}{classifyId, edbInfoType}
  1636. // 如果筛选了用户id
  1637. if adminId > 0 {
  1638. sql += ` AND sys_user_id = ? `
  1639. pars = append(pars, adminId)
  1640. }
  1641. sql += ` order by sort asc,edb_info_id asc `
  1642. _, err = o.Raw(sql, pars).QueryRows(&items)
  1643. return
  1644. }
  1645. type EdbAndClassify struct {
  1646. EdbInfoId int `description:"指标id"`
  1647. EdbCode string `description:"指标code"`
  1648. UniqueCode string `description:"唯一code"`
  1649. EdbName string `description:"指标名称"`
  1650. FirstClassifyId int `description:"第1级的分类id"`
  1651. FirstClassifyName string `description:"第1级的分类名称"`
  1652. SecondClassifyId int `description:"第2级的分类id"`
  1653. SecondClassifyName string `description:"第2级的分类名称"`
  1654. }
  1655. // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
  1656. func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
  1657. num := len(edbIdList)
  1658. if num <= 0 {
  1659. return
  1660. }
  1661. o := orm.NewOrmUsingDB("data")
  1662. sql := ` SELECT a.edb_info_id,a.edb_code,a.unique_code,a.edb_name,b.classify_id as second_classify_id,b.classify_name as second_classify_name,c.classify_id as first_classify_id,c.classify_name as first_classify_name FROM edb_info AS a
  1663. JOIN edb_classify b on a.classify_id=b.classify_id
  1664. JOIN edb_classify c on b.parent_id=c.classify_id
  1665. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1666. _, err = o.Raw(sql, edbIdList).QueryRows(&items)
  1667. return
  1668. }
  1669. // TraceEdbInfoResp 指标追溯数据返回
  1670. type TraceEdbInfoResp struct {
  1671. EdbInfoId int `description:"指标id"`
  1672. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  1673. EdbName string `description:"指标名称"`
  1674. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  1675. RuleTitle string `description:"指标规则"`
  1676. UniqueCode string `description:"唯一编码"`
  1677. ClassifyId int `description:"分类ID"`
  1678. Child []TraceEdbInfoResp `description:"下级来源"`
  1679. EdbInfo *EdbInfo `description:"指标信息" json:"-"`
  1680. }
  1681. // BeforeAndAfterDateDataResp 前后几期数据
  1682. type BeforeAndAfterDateDataResp struct {
  1683. List []*EdbDataList `description:"list"`
  1684. Date string `description:"实际日期"`
  1685. }
  1686. // GetEdbInfoAdminList
  1687. func GetEdbInfoAdminList(edbType int) (list []int, err error) {
  1688. o := orm.NewOrmUsingDB("data")
  1689. sql := ` SELECT sys_user_id FROM edb_info WHERE edb_info_type = ? GROUP BY sys_user_id `
  1690. _, err = o.Raw(sql, edbType).QueryRows(&list)
  1691. return
  1692. }