edb_info.go 57 KB

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