edb_info.go 54 KB

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