edb_info.go 66 KB

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