edb_info.go 59 KB

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