edb_info.go 58 KB

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