edb_info.go 60 KB

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