edb_info.go 65 KB

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