edb_info.go 60 KB

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