edb_info.go 69 KB

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