edb_info.go 58 KB

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