edb_info.go 69 KB

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