edb_info.go 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/global"
  5. "eta_gn/eta_api/models/data_manage/line_equation/request"
  6. "eta_gn/eta_api/models/mgo"
  7. "eta_gn/eta_api/services/alarm_msg"
  8. "eta_gn/eta_api/utils"
  9. "fmt"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "gorm.io/gorm"
  14. "go.mongodb.org/mongo-driver/bson"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. )
  17. type EdbInfo struct {
  18. EdbInfoId int `gorm:"primaryKey" `
  19. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  20. SourceName string `description:"来源名称"`
  21. Source int `description:"来源id"`
  22. EdbCode string `description:"指标编码"`
  23. EdbName string `description:"指标名称"`
  24. EdbNameEn string `description:"英文指标名称"`
  25. EdbNameSource string `description:"指标名称来源"`
  26. Frequency string `description:"频率"`
  27. Unit string `description:"单位"`
  28. UnitEn string `description:"英文单位"`
  29. StartDate time.Time `description:"起始日期"`
  30. EndDate time.Time `description:"终止日期"`
  31. ClassifyId int `description:"分类id"`
  32. SysUserId int
  33. SysUserRealName string
  34. UniqueCode string `description:"指标唯一编码"`
  35. CreateTime time.Time
  36. ModifyTime time.Time
  37. BaseModifyTime time.Time
  38. MinValue float64 `description:"指标最小值"`
  39. MaxValue float64 `description:"指标最大值"`
  40. CalculateFormula string `description:"计算公式"`
  41. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  42. Sort int `description:"排序字段"`
  43. LatestDate string `description:"数据最新日期(实际日期)"`
  44. LatestValue float64 `description:"数据最新值(实际值)"`
  45. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  46. MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
  47. MoveFrequency string `description:"移动频度"`
  48. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  49. ServerUrl string `description:"服务器地址"`
  50. ChartImage string `description:"图表图片"`
  51. Calendar string `description:"公历/农历" orm:"default(公历);"`
  52. DataDateType string `orm:"column(data_date_type);size(255);null;default(交易日)"`
  53. ManualSave int `description:"是否有手动保存过上下限: 0-否; 1-是"`
  54. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  55. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  56. TerminalCode string `description:"终端编码,用于配置在机器上"`
  57. DataUpdateTime string `description:"最近一次数据发生变化的时间"`
  58. ErDataUpdateDate string `description:"本次更新,数据发生变化的最早日期"`
  59. SourceIndexName string `description:"数据源中的指标名称"`
  60. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  61. SubSourceName string `description:"子数据来源名称"`
  62. IndicatorCode string `description:"指标代码"`
  63. StockCode string `description:"证券代码"`
  64. Extra string `description:"指标额外配置"`
  65. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  66. }
  67. type EdbInfoFullClassify struct {
  68. *EdbInfo
  69. CorrelationStr string `description:"相关性系数字符串"`
  70. ClassifyList []*EdbClassifyIdItems
  71. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  72. IsSupplierStop int `description:"是否供应商停更:1:停更,0:未停更"`
  73. }
  74. func AddEdbInfo(item *EdbInfo) (lastId int64, err error) {
  75. err = global.DmSQL["data"].Create(item).Error
  76. if err != nil {
  77. return
  78. }
  79. lastId = int64(item.EdbInfoId)
  80. return
  81. }
  82. type BaseEdbNameItem struct {
  83. EdbInfoId int `description:"指标id"`
  84. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  85. SourceName string `description:"来源名称"`
  86. Source int `description:"来源id"`
  87. EdbCode string `description:"指标编码"`
  88. EdbName string `description:"指标名称"`
  89. EdbNameEn string `description:"指标英文名称"`
  90. Frequency string `description:"频率"`
  91. Unit string `description:"单位"`
  92. UnitEn string `description:"英文单位"`
  93. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  94. }
  95. type BaseEdbInfoResp struct {
  96. List []*BaseEdbNameItem
  97. }
  98. // GetEdbInfoAll 用于分类展示
  99. func GetEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  100. o := global.DmSQL["data"]
  101. 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`
  102. err = o.Raw(sql, edbInfoType).Scan(&items).Error
  103. return
  104. }
  105. // GetPredictEdbInfoAll 用于分类展示(预测指标)
  106. func GetPredictEdbInfoAll(edbInfoType uint8) (items []*EdbClassifyItems, err error) {
  107. 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`
  108. err = global.DmSQL["data"].Raw(sql, edbInfoType).Scan(&items).Error
  109. return
  110. }
  111. // 用于分类展示
  112. func GetEdbInfoAllList() (items []*EdbInfo, err error) {
  113. sql := ` SELECT * FROM edb_info `
  114. err = global.DmSQL["data"].Raw(sql).Scan(&items).Error
  115. return
  116. }
  117. // 指标检索数据
  118. type EdbInfoSearch struct {
  119. EdbCode string `description:"指标编码"`
  120. StartDate string `description:"起始日期"`
  121. EndDate string `description:"终止日期"`
  122. EdbName string `description:"指标名称"`
  123. Unit string `description:"单位"`
  124. Frequency string `description:"频率"`
  125. DataList []*EdbInfoSearchData
  126. StockList []*StockInfo `description:"时序数据"`
  127. }
  128. type StockInfo struct {
  129. StockCode string
  130. EdbCode string
  131. DataList []*EdbInfoSearchData
  132. }
  133. type EdbInfoSearchData struct {
  134. DataTime string `description:"数据日期"`
  135. Value float64 `description:"数据"`
  136. }
  137. type BaseIndexInfo struct {
  138. IndexName string `description:"数据日期"`
  139. Unit string `description:"单位"`
  140. Frequency string `description:"频率"`
  141. }
  142. type EdbInfoSearchResp struct {
  143. SearchItem *EdbInfoSearch `description:"指标分类"`
  144. Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
  145. ClassifyList []*EdbClassifySimplify
  146. StockSearchList []*EdbInfoSearch
  147. }
  148. type EdbInfoMySteelChemicalCheckResp struct {
  149. Status int `description:"1:数据已存在于弘则数据库,2:新数据,3:数据已存在于弘则数据库,但是当前账号无权限"`
  150. }
  151. func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error) {
  152. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code=? `
  153. err = global.DmSQL["data"].Raw(sql, source, edbCode).First(&item).Error
  154. return
  155. }
  156. func GetEdbInfoListByEdbCodes(source int, edbCodes []string) (items []*EdbInfo, err error) {
  157. if len(edbCodes) == 0 {
  158. return
  159. }
  160. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `) `
  161. err = global.DmSQL["data"].Raw(sql, source, edbCodes).Scan(&items).Error
  162. return
  163. }
  164. func (e *EdbInfo) AfterFind(db *gorm.DB) error {
  165. tmpTime, err := time.Parse(utils.FormatDateWallWithLoc, e.LatestDate)
  166. if err != nil {
  167. return nil
  168. }
  169. e.LatestDate = tmpTime.Format(utils.FormatDate)
  170. //tmpTime, err = time.Parse(utils.FormatDateWallWithLoc, e.StartDate)
  171. //if err != nil {
  172. // return nil
  173. //}
  174. //e.StartDate = tmpTime.Format(utils.FormatDate)
  175. //tmpTime, err = time.Parse(utils.FormatDateWallWithLoc, e.EndDate)
  176. //if err != nil {
  177. // return nil
  178. //}
  179. //e.EndDate = tmpTime.Format(utils.FormatDate)
  180. return nil
  181. }
  182. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  183. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  184. err = global.DmSQL["data"].Raw(sql, edbInfoId).First(&item).Error
  185. return
  186. }
  187. // GetEdbInfoByUniqueCode
  188. // @Description: 根据uniqueCode获取指标详情
  189. // @author: Roc
  190. // @datetime 2024-01-18 13:40:01
  191. // @param uniqueCode string
  192. // @return item *EdbInfo
  193. // @return err error
  194. func GetEdbInfoByUniqueCode(uniqueCode string) (item *EdbInfo, err error) {
  195. sql := ` SELECT * FROM edb_info WHERE unique_code=? `
  196. err = global.DmSQL["data"].Raw(sql, uniqueCode).First(&item).Error
  197. return
  198. }
  199. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  200. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  201. num := len(edbInfoIdList)
  202. if num <= 0 {
  203. return
  204. }
  205. o := global.DmSQL["data"]
  206. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  207. err = o.Raw(sql, edbInfoIdList).Scan(&items).Error
  208. return
  209. }
  210. // type MysteelChemicalDataListReq struct {
  211. // Frequency string `form:"Frequency" description:"频度"`
  212. // Source int `form:"Source" description:"来源id"`
  213. // Keywords string `form:"Keywords" description:"指标ID/指标名称"`
  214. // SortField int `form:"SortField" description:"排序字段: 0-默认; 1-开始时间; 2-最新时间; 3-更新时间"`
  215. // SortRule int `form:"SortRule" description:"排序方式: 0-默认; 1-正序; 2-倒序"`
  216. // }
  217. // MysteelChemicalDataBatchAddCheckReq 钢联化工指标批量添加校验
  218. type MysteelChemicalDataBatchAddCheckReq struct {
  219. // MysteelChemicalDataListReq
  220. IndexCodes []string `form:"IndexCodes" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  221. }
  222. type AddEdbInfoReq struct {
  223. Source int `description:"来源id"`
  224. EdbCode string `description:"指标编码"`
  225. EdbName string `description:"指标名称"`
  226. Frequency string `description:"频率"`
  227. Unit string `description:"单位"`
  228. ClassifyId int `description:"分类id"`
  229. StartDate string `description:"起始日期"`
  230. EndDate string `description:"终止日期"`
  231. }
  232. type NameCheckEdbInfoReq struct {
  233. EdbCode string `description:"指标编码"`
  234. EdbName string `description:"指标名称"`
  235. Frequency string `description:"频率"`
  236. }
  237. func DeleteEdbInfoAndData(edbInfoId, source, subSource int) (err error) {
  238. to := global.DmSQL["data"].Begin()
  239. defer func() {
  240. if err != nil {
  241. _ = to.Rollback()
  242. } else {
  243. _ = to.Commit()
  244. }
  245. }()
  246. // 删除指标信息
  247. sql := ` DELETE FROM edb_info WHERE edb_info_id=? `
  248. err = to.Exec(sql, edbInfoId).Error
  249. if err != nil {
  250. return
  251. }
  252. // 删除指标的明细数据
  253. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  254. err = deleteAllEdbDataByMongo(to, edbInfoId, source, subSource)
  255. } else if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  256. mogDataObj := mgo.EdbDataThsHf{}
  257. err = mogDataObj.RemoveMany(bson.M{"edb_info_id": edbInfoId})
  258. } else {
  259. err = deleteAllEdbDataByMysql(to, edbInfoId, source, subSource)
  260. }
  261. if err != nil {
  262. return
  263. }
  264. //calculateTableName := GetEdbInfoCalculateTableName(source)
  265. //if calculateTableName != "" {
  266. // sql = ` DELETE FROM %s WHERE edb_info_id=? `
  267. // sql = fmt.Sprintf(sql, calculateTableName)
  268. // _, err = to.Raw(sql, edbInfoId).Exec()
  269. // return
  270. //}
  271. //return
  272. // 删除计算指标的关系
  273. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=? `
  274. err = to.Exec(sql, edbInfoId).Error
  275. if err != nil {
  276. return
  277. }
  278. // 删除预测指标的配置
  279. sql = ` DELETE FROM predict_edb_conf WHERE predict_edb_info_id=? `
  280. err = to.Exec(sql, edbInfoId).Error
  281. if err != nil {
  282. return
  283. }
  284. // 删除跨品种分析标签绑定的指标
  285. sql = ` DELETE FROM chart_tag_variety WHERE edb_info_id=?`
  286. err = to.Exec(sql, edbInfoId).Error
  287. if err != nil {
  288. return
  289. }
  290. return
  291. }
  292. // deleteAllEdbDataByMysql
  293. // @Description: 删除指标的明细数据(mysql)
  294. // @author: Roc
  295. // @datetime 2024-05-09 13:31:54
  296. // @param to orm.TxOrmer
  297. // @param edbInfoId int
  298. // @param source int
  299. // @param subSource int
  300. // @return err error
  301. func deleteAllEdbDataByMysql(to *gorm.DB, edbInfoId, source, subSource int) (err error) {
  302. tableName := GetEdbDataTableName(source, subSource)
  303. if tableName == `` {
  304. return
  305. }
  306. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id=? `, tableName)
  307. err = to.Exec(sql, edbInfoId).Error
  308. return
  309. }
  310. // deleteAllEdbDataByMongo
  311. // @Description: 删除指标的明细数据(mongo)
  312. // @author: Roc
  313. // @datetime 2024-05-09 13:31:37
  314. // @param to orm.TxOrmer
  315. // @param edbInfoId int
  316. // @param source int
  317. // @param subSource int
  318. // @return err error
  319. func deleteAllEdbDataByMongo(to *gorm.DB, edbInfoId, source, subSource int) (err error) {
  320. mogDataObj := mgo.EdbDataBusiness{}
  321. err = mogDataObj.RemoveMany(bson.M{"edb_info_id": edbInfoId})
  322. return
  323. }
  324. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (count int, err error) {
  325. o := global.DmSQL["data"]
  326. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  327. if condition != "" {
  328. sql += condition
  329. }
  330. err = o.Raw(sql, pars...).Scan(&count).Error
  331. return
  332. }
  333. type EditEdbInfoReq struct {
  334. EdbInfoId int `description:"指标ID"`
  335. EdbName string `description:"指标名称"`
  336. Frequency string `description:"频率"`
  337. Unit string `description:"单位"`
  338. ClassifyId int `description:"分类id"`
  339. CalculateFormula string `description:"计算公式"`
  340. }
  341. type EditEdbEnInfoReq struct {
  342. EdbInfoId int `description:"指标ID"`
  343. EdbNameEn string `description:"英文指标名称"`
  344. UnitEn string `description:"英文单位"`
  345. }
  346. // EditEdbBaseInfoReq
  347. // @Description: 修改指标基础信息请求参数
  348. type EditEdbBaseInfoReq struct {
  349. EdbInfoId int `description:"指标ID"`
  350. EdbName string `description:"指标名称"`
  351. Unit string `description:"指标单位"`
  352. }
  353. func ModifyEdbInfo(item *EditEdbInfoReq) (err error) {
  354. o := global.DmSQL["data"]
  355. sql := ` UPDATE edb_info
  356. SET
  357. edb_name =?,
  358. edb_name_source =?,
  359. frequency = ?,
  360. unit = ?,
  361. classify_id = ?,
  362. modify_time = NOW()
  363. WHERE edb_info_id = ?`
  364. err = o.Exec(sql, item.EdbName, item.EdbName, item.Frequency, item.Unit, item.ClassifyId, item.EdbInfoId).Error
  365. return
  366. }
  367. // ModifyEdbEnInfo 修改指标英文信息
  368. func ModifyEdbEnInfo(item *EditEdbEnInfoReq) (err error) {
  369. o := global.DmSQL["data"]
  370. sql := ` UPDATE edb_info
  371. SET
  372. edb_name_en =?,
  373. unit_en = ?,
  374. modify_time = NOW()
  375. WHERE edb_info_id = ?`
  376. err = o.Exec(sql, item.EdbNameEn, item.UnitEn, item.EdbInfoId).Error
  377. return
  378. }
  379. type EdbInfoList struct {
  380. EdbInfoId int `orm:"column(edb_info_id);pk" gorm:"primaryKey" `
  381. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  382. SourceName string `description:"来源名称"`
  383. Source int `description:"来源id"`
  384. EdbCode string `description:"指标编码"`
  385. EdbNameEn string `description:"英文指标名称"`
  386. EdbName string `description:"指标名称"`
  387. Frequency string `description:"频率"`
  388. FrequencyEn string `description:"英文频率"`
  389. Unit string `description:"单位"`
  390. UnitEn string `description:"英文单位"`
  391. StartDate string `description:"起始日期"`
  392. EndDate string `description:"终止日期"`
  393. LatestDate string `description:"数据最新日期(实际日期)"`
  394. LatestValue float64 `description:"数据最新值(实际值)"`
  395. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  396. ClassifyId int `description:"分类id"`
  397. UniqueCode string `description:"指标唯一编码"`
  398. SysUserId int `description:"创建人id"`
  399. SysUserRealName string `description:"创建人姓名"`
  400. ModifyTime string `description:"最新修改时间"`
  401. CreateTime string `description:"创建时间"`
  402. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  403. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  404. ChartImage string `description:"图表图片"`
  405. RuleType int `description:"预测规则,1:最新,2:固定值"`
  406. FixedValue float64 `description:"固定值"`
  407. DataList []*EdbData `gorm:"-" description:"实际指标数据"`
  408. PredictDataList []*EdbData `gorm:"-" description:"预测指标数据"`
  409. Button EdbClassifyItemsButton `gorm:"-" description:"操作权限"`
  410. IsEnEdb bool `description:"是否展示英文标识"`
  411. DataInsertConfig EdbDataInsertConfigItem `gorm:"-" description:"指标数据插入配置"`
  412. DataDateType string `description:"数据日期类型,枚举值:交易日、自然日"`
  413. EmptyType int `description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
  414. MaxEmptyType int `description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
  415. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  416. SubSourceName string `description:"子数据来源名称"`
  417. IndicatorCode string `description:"指标代码"`
  418. StockCode string `description:"证券代码"`
  419. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  420. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  421. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  422. IsSupplierStop int `description:"是否供应商停更:1:停更,0:未停更"`
  423. }
  424. type EdbDataInsertConfigItem struct {
  425. Date string `description:"插入的日期"`
  426. RealDate string `description:"实际最晚的日期"`
  427. Value string `description:"插入的值"`
  428. }
  429. type EdbData struct {
  430. EdbDataId int `orm:"column(edb_data_id);pk" gorm:"primaryKey" `
  431. EdbInfoId int
  432. DataTime string
  433. Value float64
  434. }
  435. func (e *EdbData) AfterFind(db *gorm.DB) (err error) {
  436. tmpDataTime, err := time.Parse(utils.FormatDateWallWithLoc, e.DataTime)
  437. if err != nil {
  438. return nil
  439. }
  440. e.DataTime = utils.TimeTransferString(utils.FormatDate, tmpDataTime)
  441. return nil
  442. }
  443. type EdbInfoListResp struct {
  444. Paging *paging.PagingItem
  445. Item *EdbInfoList
  446. ClassifyList []*EdbClassifyIdItems
  447. }
  448. type WindEdbInfoList struct {
  449. *EdbInfoList
  450. ClassifyList []*EdbClassifyIdItems
  451. }
  452. func (e *EdbInfoList) AfterFind(db *gorm.DB) (err error) {
  453. tmpLatestDate, err := time.Parse(utils.FormatDateWallWithLoc, e.LatestDate)
  454. if err != nil {
  455. return nil
  456. }
  457. tmpStartDate, err := time.Parse(utils.FormatDateWallWithLoc, e.StartDate)
  458. if err != nil {
  459. return nil
  460. }
  461. tmpEndDate, err := time.Parse(utils.FormatDateWallWithLoc, e.EndDate)
  462. if err != nil {
  463. return nil
  464. }
  465. tmpCreateTime, err := time.Parse(utils.FormatDateWallWithLoc, e.CreateTime)
  466. if err != nil {
  467. return nil
  468. }
  469. tmpModifyTime, err := time.Parse(utils.FormatDateWallWithLoc, e.ModifyTime)
  470. if err != nil {
  471. return nil
  472. }
  473. e.LatestDate = utils.TimeTransferString(utils.FormatDate, tmpLatestDate)
  474. e.StartDate = utils.TimeTransferString(utils.FormatDate, tmpStartDate)
  475. e.EndDate = utils.TimeTransferString(utils.FormatDate, tmpEndDate)
  476. e.CreateTime = utils.TimeTransferString(utils.FormatDateTime, tmpCreateTime)
  477. e.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, tmpModifyTime)
  478. return nil
  479. }
  480. func GetEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfoList, err error) {
  481. o := global.DmSQL["data"]
  482. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  483. if condition != "" {
  484. sql += condition
  485. }
  486. err = o.Raw(sql, pars...).First(&item).Error
  487. return
  488. }
  489. func GetEdbDataCountByCondition(condition string, pars []interface{}, source, subSource int) (count int, err error) {
  490. o := global.DmSQL["data"]
  491. tableName := GetEdbDataTableName(source, subSource)
  492. sql := ` SELECT COUNT(1) AS count FROM %s WHERE 1=1 `
  493. sql = fmt.Sprintf(sql, tableName)
  494. if condition != "" {
  495. sql += condition
  496. }
  497. err = o.Raw(sql, pars...).Scan(&count).Error
  498. return
  499. }
  500. func GetEdbDataListByCondition(condition string, pars []interface{}, source, subSource, pageSize, startSize int) (item []*EdbData, err error) {
  501. o := global.DmSQL["data"]
  502. tableName := GetEdbDataTableName(source, subSource)
  503. sql := ` SELECT * FROM %s WHERE 1=1 `
  504. sql = fmt.Sprintf(sql, tableName)
  505. if condition != "" {
  506. sql += condition
  507. }
  508. sql += ` ORDER BY data_time DESC `
  509. sql += ` LIMIT ?,? `
  510. pars = append(pars, startSize)
  511. pars = append(pars, pageSize)
  512. err = o.Raw(sql, pars...).Find(&item).Error
  513. return
  514. }
  515. // GetAllEdbDataListByCondition 根据条件获取所有的数据
  516. func GetAllEdbDataListByCondition(condition string, pars []interface{}, source, subSource int) (item []*EdbData, err error) {
  517. tableName := GetEdbDataTableName(source, subSource)
  518. sql := ` SELECT * FROM %s WHERE 1=1 `
  519. sql = fmt.Sprintf(sql, tableName)
  520. if condition != "" {
  521. sql += condition
  522. }
  523. sql += ` ORDER BY data_time DESC `
  524. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&item).Error
  525. return
  526. }
  527. func GetEdbInfoByNewest() (item *EdbInfoList, err error) {
  528. sql := ` SELECT * FROM edb_info WHERE 1=1 ORDER BY modify_time DESC LIMIT 1 `
  529. err = global.DmSQL["data"].Raw(sql).Scan(&item).Error
  530. return
  531. }
  532. func ModifyEdbInfoModifyTime(edbInfoId int64) (err error) {
  533. sql := ` UPDATE edb_info SET modify_time = NOW() WHERE edb_info_id = ? `
  534. err = global.DmSQL["data"].Exec(sql, edbInfoId).Error
  535. return
  536. }
  537. type MoveEdbInfoReq struct {
  538. EdbInfoId int `description:"指标ID"`
  539. PrevEdbInfoId int `description:"上一个指标ID"`
  540. NextEdbInfoId int `description:"下一个指标ID"`
  541. ClassifyId int `description:"分类id"`
  542. }
  543. func MoveEdbInfo(edbInfoId, classifyId int) (err error) {
  544. sql := ` UPDATE edb_info
  545. SET
  546. classify_id = ?
  547. WHERE edb_info_id = ?`
  548. err = global.DmSQL["data"].Exec(sql, classifyId, edbInfoId).Error
  549. return
  550. }
  551. func GetChartEdbMappingCount(edbInfoId int) (count int, err error) {
  552. sql := ` SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE edb_info_id=? `
  553. err = global.DmSQL["data"].Raw(sql, edbInfoId).Scan(&count).Error
  554. return
  555. }
  556. type ChartEdbInfo struct {
  557. EdbInfoId int `description:"指标id"`
  558. EdbName string `description:"指标名称"`
  559. SourceName string `json:"-"`
  560. EdbNameAlias string `json:"-" description:"指标名称,别名"`
  561. }
  562. func EdbInfoSearchByKeyWord(keyword string) (searchList []*ChartEdbInfo, err error) {
  563. pars := make([]interface{}, 0)
  564. sql := ` SELECT edb_info_id,edb_name,source_name FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  565. if keyword != "" {
  566. sql += ` AND (edb_name LIKE ? OR edb_code LIKE ? ) `
  567. pars = append(pars, utils.GetLikeKeyword(keyword), utils.GetLikeKeyword(keyword))
  568. }
  569. sql += ` ORDER BY create_time DESC `
  570. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&searchList).Error
  571. return
  572. }
  573. type EdbInfoMaxAndMinInfo struct {
  574. MinDate string `description:"最小日期"`
  575. MaxDate string `description:"最大日期"`
  576. MinValue float64 `description:"最小值"`
  577. MaxValue float64 `description:"最大值"`
  578. LatestValue float64 `description:"最新值"`
  579. }
  580. // GetEdbInfoMaxAndMinInfo
  581. // @Description: 获取指标最大最小值
  582. // @author: Roc
  583. // @datetime 2024-05-07 15:34:00
  584. // @param source int
  585. // @param subSource int
  586. // @param edbCode string
  587. // @return item *EdbInfoMaxAndMinInfo
  588. // @return err error
  589. func GetEdbInfoMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  590. // 自有数据需要额外处理(从mongo获取)
  591. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  592. return GetEdbInfoMaxAndMinInfoByMongo(source, subSource, edbCode)
  593. }
  594. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  595. return GetThsHfEdbInfoMaxAndMinInfoByMongo(source, subSource, edbCode)
  596. }
  597. // 默认走mysql
  598. return GetEdbInfoMaxAndMinInfoByMysql(source, subSource, edbCode)
  599. }
  600. // GetEdbInfoMaxAndMinInfoByMysql
  601. // @Description: 从mysql中获取指标最大最小值
  602. // @author: Roc
  603. // @datetime 2024-05-07 15:33:43
  604. // @param source int
  605. // @param subSource int
  606. // @param edbCode string
  607. // @return item *EdbInfoMaxAndMinInfo
  608. // @return err error
  609. func GetEdbInfoMaxAndMinInfoByMysql(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  610. o := global.DmSQL["data"]
  611. sql := ``
  612. tableName := GetEdbDataTableName(source, subSource)
  613. 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=? `
  614. sql = fmt.Sprintf(sql, tableName)
  615. err = global.DmSQL["data"].Raw(sql, edbCode).Scan(&item).Error
  616. if err != nil {
  617. return
  618. }
  619. var latest_value float64
  620. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  621. sql = fmt.Sprintf(sql, tableName)
  622. err = o.Raw(sql, edbCode).Scan(&latest_value).Error
  623. item.LatestValue = latest_value
  624. return
  625. }
  626. // GetEdbInfoMaxAndMinInfoByMongo
  627. // @Description: 从mongo中获取指标最大最小值
  628. // @author: Roc
  629. // @datetime 2024-05-07 15:33:53
  630. // @param source int
  631. // @param subSource int
  632. // @param edbCode string
  633. // @return item *EdbInfoMaxAndMinInfo
  634. // @return err error
  635. func GetEdbInfoMaxAndMinInfoByMongo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  636. mogDataObj := new(mgo.EdbDataBusiness)
  637. pipeline := []bson.M{
  638. {"$match": bson.M{"edb_code": edbCode}},
  639. {"$group": bson.M{
  640. "_id": nil,
  641. "min_date": bson.M{"$min": "$data_time"},
  642. "max_date": bson.M{"$max": "$data_time"},
  643. "min_value": bson.M{"$min": "$value"},
  644. "max_value": bson.M{"$max": "$value"},
  645. }},
  646. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  647. }
  648. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  649. if err != nil {
  650. fmt.Println("EdbDataBusiness getEdbDataBusinessList Err:" + err.Error())
  651. return
  652. }
  653. if !result.MaxDate.IsZero() {
  654. whereQuery := bson.M{"edb_code": edbCode, "data_time": result.MaxDate}
  655. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  656. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  657. if tmpErr != nil {
  658. err = tmpErr
  659. return
  660. }
  661. result.LatestValue = latestValue.Value
  662. result.EndValue = latestValue.Value
  663. }
  664. item = &EdbInfoMaxAndMinInfo{
  665. MinDate: result.MinDate.Format(utils.FormatDate),
  666. MaxDate: result.MaxDate.Format(utils.FormatDate),
  667. MinValue: result.MinValue,
  668. MaxValue: result.MaxValue,
  669. LatestValue: result.LatestValue,
  670. }
  671. return
  672. }
  673. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  674. 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=? `
  675. err = global.DmSQL["data"].Exec(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Error
  676. return
  677. }
  678. func GetEdbInfoFilter(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  679. sql := ` SELECT * FROM edb_info WHERE 1=1 AND edb_info_type = 0 `
  680. if condition != "" {
  681. sql += condition
  682. }
  683. sql += ` ORDER BY create_time DESC `
  684. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  685. return
  686. }
  687. type AddEdbInfoResp struct {
  688. EdbInfoId int `description:"指标ID"`
  689. UniqueCode string `description:"指标唯一编码"`
  690. ClassifyId int `description:"分类id"`
  691. ExistEdbName []string `description:"重复指标名称"`
  692. }
  693. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  694. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  695. //calculateTableName := GetEdbInfoCalculateTableName(source)
  696. //if calculateTableName == "" {
  697. // err = errors.New("无效的表名")
  698. // return
  699. //}
  700. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  701. //sql = fmt.Sprintf(sql, calculateTableName)
  702. if condition != "" {
  703. sql += condition
  704. }
  705. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&items).Error
  706. return
  707. }
  708. // GetEdbInfoAllCalculateByEdbInfoIdList 根据指标id集合 获取基础指标对应的所有计算指标
  709. func GetEdbInfoAllCalculateByEdbInfoIdList(edbInfoIdList []int) (list []*EdbInfo, err error) {
  710. num := len(edbInfoIdList)
  711. if num <= 0 {
  712. return
  713. }
  714. sql := ` SELECT DISTINCT b.* FROM edb_info_calculate_mapping AS a
  715. INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
  716. WHERE a.from_edb_info_id in (` + utils.GetOrmInReplace(num) + `)
  717. ORDER BY b.edb_info_id ASC `
  718. err = global.DmSQL["data"].Raw(sql, edbInfoIdList).Scan(&list).Error
  719. return
  720. }
  721. func GetRefreshEdbInfoFromBase(edbInfoId, source int) (baseEdbInfoArr, calculateInfoArr []*EdbInfo, err error) {
  722. calculateList, err := GetEdbInfoCalculateMap(edbInfoId, source)
  723. if err != nil && !utils.IsErrNoRow(err) {
  724. return
  725. }
  726. for _, item := range calculateList {
  727. if item.EdbInfoId == edbInfoId { // 如果查出来关联的指标就是自己的话,那么就过滤
  728. continue
  729. }
  730. if item.EdbType == 1 {
  731. baseEdbInfoArr = append(baseEdbInfoArr, item)
  732. } else {
  733. calculateInfoArr = append(calculateInfoArr, item)
  734. newBaseEdbInfoArr, newCalculateInfoArr, _ := GetRefreshEdbInfoFromBase(item.EdbInfoId, item.Source)
  735. baseEdbInfoArr = append(baseEdbInfoArr, newBaseEdbInfoArr...)
  736. calculateInfoArr = append(calculateInfoArr, newCalculateInfoArr...)
  737. }
  738. }
  739. return
  740. }
  741. func ModifyEdbInfoDataStatus(edbInfoId int64, source, subSource int, edbCode string) (err error) {
  742. sql := ``
  743. tableName := GetEdbDataTableName(source, subSource)
  744. sql = ` UPDATE %s SET edb_info_id=?,modify_time=NOW() WHERE edb_code=? `
  745. sql = fmt.Sprintf(sql, tableName)
  746. err = global.DmSQL["data"].Exec(sql, edbInfoId, edbCode).Error
  747. return
  748. }
  749. // GetFirstEdbInfoByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  750. func GetFirstEdbInfoByClassifyId(classifyId int) (item *EdbInfo, err error) {
  751. sql := ` SELECT * FROM edb_info WHERE classify_id=? order by sort asc,edb_info_id asc limit 1`
  752. err = global.DmSQL["data"].Raw(sql, classifyId).First(&item).Error
  753. return
  754. }
  755. // UpdateEdbInfoSortByClassifyId 根据分类id更新排序
  756. func UpdateEdbInfoSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  757. sql := ` update edb_info set sort = ` + updateSort + ` WHERE classify_id=?`
  758. if prevEdbInfoId > 0 {
  759. sql += ` AND ( sort > ? or ( edb_info_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  760. } else {
  761. sql += ` AND ( sort > ? )`
  762. }
  763. err = global.DmSQL["data"].Exec(sql, classifyId, nowSort).Error
  764. return
  765. }
  766. // Update 更新指标基础信息
  767. func (edbInfo *EdbInfo) Update(cols []string) (err error) {
  768. err = global.DmSQL["data"].Select(cols).Updates(edbInfo).Error
  769. return
  770. }
  771. type EdbInfoDataResp struct {
  772. EdbInfo *EdbInfo
  773. DataList []*EdbDataList
  774. }
  775. type EdbInfoDataFullClassifyResp struct {
  776. EdbInfo *EdbInfoFullClassify
  777. DataList []*EdbDataList
  778. }
  779. type EdbInfoReplaceReq struct {
  780. OldEdbInfoId int `description:"原指标ID"`
  781. NewEdbInfoId int `description:"替换为指标ID"`
  782. }
  783. // EdbInfoReplaceV2 替换指标v2版本(更换时间:2022年01月05日15:33:42)
  784. //func EdbInfoReplaceV2(oldEdbInfo, newEdbInfo *EdbInfo, sysAdminId int, sysAdminRealName string) (mappingList,replaceChartTotal, replaceCalculateTotal int, err error) {
  785. // defer func() {
  786. // if err != nil {
  787. // fmt.Println("ERR:", err.Error())
  788. // }
  789. // }()
  790. //
  791. // // 替换关联表中的source_edb_info_id
  792. // mappingList, replaceChartTotal, replaceCalculateTotal, err := replaceChartEdb(oldEdbInfo, newEdbInfo)
  793. // if err != nil {
  794. // return
  795. // }
  796. // err = replaceRelationEdbInfo(mappingList, oldEdbInfo, newEdbInfo, sysAdminId, sysAdminRealName)
  797. // return
  798. //}
  799. // ReplaceChartEdb 替换图表中的指标
  800. func ReplaceChartEdb(oldEdbInfo, newEdbInfo *EdbInfo) (relationEdbInfoIdList []int, replaceChartTotal, replaceCalculateTotal int, err error) {
  801. var errmsg string
  802. relationEdbInfoIdList = make([]int, 0) // 关联的指标id
  803. logMsg := `` // 记录替换的日志
  804. to := global.DmSQL["data"].Begin()
  805. defer func() {
  806. if err != nil {
  807. _ = to.Rollback()
  808. } else {
  809. _ = to.Commit()
  810. if logMsg != `` {
  811. utils.FileLog.Info(fmt.Sprintf("替换指标记录,旧的指标id:%d,新的指标id:%d;%s", oldEdbInfo.EdbInfoId, newEdbInfo.EdbInfoId, logMsg))
  812. }
  813. }
  814. if errmsg != "" {
  815. fmt.Println("errmsg:" + errmsg)
  816. }
  817. if err != nil && errmsg != "" {
  818. go alarm_msg.SendAlarmMsg("替换图表中的指标失败提醒,errmsg:"+errmsg, 3)
  819. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"替换指标失败提醒", "errmsg:"+errmsg, utils.EmailSendToUsers)
  820. }
  821. }()
  822. //替换图表
  823. {
  824. chartEdbMappingList := make([]*ChartEdbInfoMapping, 0)
  825. csql := `SELECT * FROM chart_edb_mapping WHERE edb_info_id=?`
  826. err = to.Raw(csql, oldEdbInfo.EdbInfoId).Scan(&chartEdbMappingList).Error
  827. if err != nil {
  828. errmsg = "获取指标关联图表信息失败:Err:" + err.Error()
  829. return
  830. }
  831. replaceChartTotal = len(chartEdbMappingList)
  832. // 查询所有的相关的图表信息
  833. chartInfoIds := make([]string, 0)
  834. if len(chartEdbMappingList) > 0 {
  835. for _, chartEdbMapping := range chartEdbMappingList {
  836. chartInfoIds = append(chartInfoIds, strconv.Itoa(chartEdbMapping.ChartInfoId))
  837. }
  838. }
  839. if len(chartInfoIds) > 0 {
  840. chartInfoList, e := GetChartInfoListByChartIdList(chartInfoIds)
  841. if e != nil {
  842. err = e
  843. errmsg = "获取图表信息失败:Err:" + e.Error()
  844. return
  845. }
  846. for _, chartInfo := range chartInfoList {
  847. updateStr := make([]string, 0)
  848. if chartInfo.EdbInfoIds != "" && strings.Contains(chartInfo.EdbInfoIds, strconv.Itoa(oldEdbInfo.EdbInfoId)) {
  849. //需要更换
  850. //解析字符串
  851. edbInfoIds := strings.Split(chartInfo.EdbInfoIds, ",")
  852. for i, edbInfoId := range edbInfoIds {
  853. if edbInfoId == strconv.Itoa(oldEdbInfo.EdbInfoId) {
  854. edbInfoIds[i] = strconv.Itoa(newEdbInfo.EdbInfoId)
  855. }
  856. }
  857. chartInfo.EdbInfoIds = strings.Join(edbInfoIds, ",")
  858. updateStr = append(updateStr, "EdbInfoIds")
  859. }
  860. if chartInfo.ExtraConfig != "" || chartInfo.BarConfig != "" {
  861. //判断是否是拟合方程或者散点图截面图
  862. if chartInfo.Source == utils.CHART_SOURCE_FUTURE_GOOD {
  863. // 商品价格曲线图的一些配置
  864. var barConfig FutureGoodBarChartInfoReq
  865. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  866. if err != nil {
  867. errmsg = "商品价格曲线图配置异常 json.Unmarshal:Err:" + err.Error()
  868. return
  869. }
  870. if barConfig.BaseEdbInfoId == oldEdbInfo.EdbInfoId {
  871. barConfig.BaseEdbInfoId = newEdbInfo.EdbInfoId
  872. }
  873. for k, item := range barConfig.EdbInfoIdList {
  874. if item.EdbInfoId == oldEdbInfo.EdbInfoId && item.Source == 1 {
  875. barConfig.EdbInfoIdList[k].EdbInfoId = newEdbInfo.EdbInfoId
  876. }
  877. }
  878. // 更新图表配置信息
  879. // 重新序列化
  880. configJson, e := json.Marshal(barConfig)
  881. if e != nil {
  882. err = e
  883. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  884. return
  885. }
  886. configStr := string(configJson)
  887. chartInfo.BarConfig = configStr
  888. updateStr = append(updateStr, "BarConfig")
  889. } else if chartInfo.Source == utils.CHART_SOURCE_FUTURE_GOOD_PROFIT {
  890. // 商品利润曲线图的一些配置
  891. var extraConf FutureGoodProfitChartInfoReq
  892. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
  893. if err != nil {
  894. errmsg = "商品利润曲线图配置异常 json.Unmarshal:Err:" + err.Error()
  895. return
  896. }
  897. if extraConf.BaseEdbInfoId == oldEdbInfo.EdbInfoId {
  898. extraConf.BaseEdbInfoId = newEdbInfo.EdbInfoId
  899. }
  900. for k, v := range extraConf.EdbInfoIdList {
  901. if v == oldEdbInfo.EdbInfoId {
  902. extraConf.EdbInfoIdList[k] = newEdbInfo.EdbInfoId
  903. }
  904. }
  905. // 更新图表配置信息
  906. // 重新序列化
  907. configJson, e := json.Marshal(extraConf)
  908. if e != nil {
  909. err = e
  910. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  911. return
  912. }
  913. configStr := string(configJson)
  914. chartInfo.ExtraConfig = configStr
  915. updateStr = append(updateStr, "ExtraConfig")
  916. } else if chartInfo.Source == utils.CHART_SOURCE_LINE_EQUATION {
  917. //解析配置内容
  918. var lineChartInfoConfig request.LineChartInfoReq
  919. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &lineChartInfoConfig)
  920. if err != nil {
  921. errmsg = "获取图表配置信息失败 json.Unmarshal:Err:" + err.Error()
  922. return
  923. }
  924. xEdbInfoIdList := lineChartInfoConfig.XEdbInfoIdList
  925. yEdbInfoIdList := lineChartInfoConfig.YEdbInfoIdList
  926. if len(xEdbInfoIdList) > 0 {
  927. // 循环判断是否存在
  928. for k, xEdbInfoId := range xEdbInfoIdList {
  929. if xEdbInfoId == oldEdbInfo.EdbInfoId {
  930. xEdbInfoIdList[k] = newEdbInfo.EdbInfoId
  931. }
  932. }
  933. }
  934. if len(yEdbInfoIdList) > 0 {
  935. // 循环判断是否存在
  936. for k, yEdbInfoId := range yEdbInfoIdList {
  937. if yEdbInfoId == oldEdbInfo.EdbInfoId {
  938. yEdbInfoIdList[k] = newEdbInfo.EdbInfoId
  939. }
  940. }
  941. }
  942. // 替换新的指标信息
  943. lineChartInfoConfig.XEdbInfoIdList = xEdbInfoIdList
  944. lineChartInfoConfig.YEdbInfoIdList = yEdbInfoIdList
  945. // 重新序列化
  946. lineChartInfoConfigJson, e := json.Marshal(lineChartInfoConfig)
  947. if e != nil {
  948. err = e
  949. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  950. return
  951. }
  952. lineChartInfoConfigStr := string(lineChartInfoConfigJson)
  953. // 更新图表配置信息
  954. chartInfo.ExtraConfig = lineChartInfoConfigStr
  955. updateStr = append(updateStr, "ExtraConfig")
  956. } else if chartInfo.ChartType == utils.CHART_TYPE_SECTION_SCATTER {
  957. //解析配置内容
  958. var tmpExtraConfig SectionScatterReq
  959. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &tmpExtraConfig)
  960. if err != nil {
  961. errmsg = "获取截面散点图配置信息失败 json.Unmarshal:Err:" + err.Error()
  962. return
  963. }
  964. //找到指标信息,并替换
  965. if len(tmpExtraConfig.SeriesList) > 0 {
  966. for k1, series := range tmpExtraConfig.SeriesList {
  967. for k2, item := range series.EdbInfoList {
  968. if item.XEdbInfoId == oldEdbInfo.EdbInfoId {
  969. tmpExtraConfig.SeriesList[k1].EdbInfoList[k2].XEdbInfoId = newEdbInfo.EdbInfoId
  970. }
  971. if item.YEdbInfoId == oldEdbInfo.EdbInfoId {
  972. tmpExtraConfig.SeriesList[k1].EdbInfoList[k2].YEdbInfoId = newEdbInfo.EdbInfoId
  973. }
  974. }
  975. }
  976. }
  977. // 重新序列化
  978. sectionScatterConfigJson, e := json.Marshal(tmpExtraConfig)
  979. if e != nil {
  980. err = e
  981. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  982. return
  983. }
  984. sectionScatterConfigStr := string(sectionScatterConfigJson)
  985. // 更新图表配置信息
  986. chartInfo.ExtraConfig = sectionScatterConfigStr
  987. updateStr = append(updateStr, "ExtraConfig")
  988. } else if chartInfo.ChartType == utils.CHART_TYPE_SECTION_COMBINE {
  989. //解析配置内容
  990. var tmpExtraConfig ChartSectionExtraConf
  991. err = json.Unmarshal([]byte(chartInfo.ExtraConfig), &tmpExtraConfig)
  992. if err != nil {
  993. errmsg = "获取截面散点图配置信息失败 json.Unmarshal:Err:" + err.Error()
  994. return
  995. }
  996. //找到指标信息,并替换
  997. if len(tmpExtraConfig.DateConfList) > 0 {
  998. for k1, item := range tmpExtraConfig.DateConfList {
  999. if item.EdbInfoId > 0 && item.EdbInfoId == oldEdbInfo.EdbInfoId {
  1000. tmpExtraConfig.DateConfList[k1].EdbInfoId = newEdbInfo.EdbInfoId
  1001. }
  1002. }
  1003. }
  1004. // 重新序列化
  1005. configJson, e := json.Marshal(tmpExtraConfig)
  1006. if e != nil {
  1007. err = e
  1008. errmsg = "图表配置信息序列化失败:json.Marshal Err: " + e.Error()
  1009. return
  1010. }
  1011. configStr := string(configJson)
  1012. // 更新图表配置信息
  1013. chartInfo.ExtraConfig = configStr
  1014. updateStr = append(updateStr, "ExtraConfig")
  1015. }
  1016. }
  1017. if len(updateStr) > 0 {
  1018. updateStr = append(updateStr, "ModifyTime")
  1019. chartInfo.ModifyTime = time.Now()
  1020. err = chartInfo.Update(updateStr)
  1021. if err != nil {
  1022. errmsg = "更新图表信息失败:chartInfo.Update Err: " + err.Error()
  1023. return
  1024. }
  1025. }
  1026. }
  1027. }
  1028. // 查询所有的图表信息
  1029. if len(chartEdbMappingList) > 0 {
  1030. chartInfoIdList := make([]string, 0)
  1031. for _, mv := range chartEdbMappingList {
  1032. //获取图表所有指标信息
  1033. chartEdbList := make([]*ChartEdbInfoMapping, 0)
  1034. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=?`
  1035. err = to.Raw(csql, mv.ChartInfoId).Scan(&chartEdbList).Error
  1036. if err != nil {
  1037. errmsg = "获取图表所有指标信息失败:Err:" + err.Error()
  1038. return
  1039. }
  1040. // 查询图表信息
  1041. var minData, maxData float64
  1042. minData = newEdbInfo.MinValue
  1043. maxData = newEdbInfo.MaxValue
  1044. for _, cv := range chartEdbList {
  1045. if mv.IsAxis == cv.IsAxis {
  1046. if minData > cv.MinData {
  1047. minData = cv.MinData
  1048. }
  1049. if maxData < cv.MaxData {
  1050. maxData = cv.MaxData
  1051. }
  1052. }
  1053. }
  1054. //修改图表关联指标
  1055. rsql := ` UPDATE chart_edb_mapping SET edb_info_id=?,max_data=?,min_data=?,modify_time=NOW() WHERE chart_edb_mapping_id=? `
  1056. err = to.Exec(rsql, newEdbInfo.EdbInfoId, maxData, minData, mv.ChartEdbMappingId).Error
  1057. if err != nil {
  1058. errmsg = "更新图库指标信息失败:Err:" + err.Error()
  1059. return
  1060. }
  1061. rsql = ` UPDATE chart_series_edb_mapping SET edb_info_id=? WHERE edb_info_id=? and chart_info_id=?`
  1062. err = to.Exec(rsql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoType, mv.ChartInfoId).Error
  1063. if err != nil {
  1064. errmsg = "更新图库系列指标信息失败:Err:" + err.Error()
  1065. return
  1066. }
  1067. chartInfoIdList = append(chartInfoIdList, fmt.Sprint(mv.ChartInfoId))
  1068. }
  1069. logMsg += `涉及到的图表id:` + strings.Join(chartInfoIdList, ",") + ";"
  1070. }
  1071. }
  1072. //替换计算指标
  1073. {
  1074. //获取所有包含的计算指标
  1075. mappingList := make([]*EdbInfoCalculateMapping, 0)
  1076. msql := ` SELECT * FROM edb_info_calculate_mapping WHERE from_edb_info_id=? GROUP BY edb_info_id `
  1077. err = to.Raw(msql, oldEdbInfo.EdbInfoId).Scan(&mappingList).Error
  1078. if err != nil {
  1079. errmsg = "获取计算指标关联基础指标信息失败:Err:" + err.Error()
  1080. return
  1081. }
  1082. replaceCalculateTotal = len(mappingList)
  1083. //计算指标
  1084. if len(mappingList) > 0 {
  1085. edbInfoIdList := make([]string, 0)
  1086. for _, mv := range mappingList {
  1087. //如果即将替换的指标与当前指标id一致,那么就退出当前循环,进入下一循环
  1088. if mv.EdbInfoId == newEdbInfo.EdbInfoId {
  1089. continue
  1090. }
  1091. //替换原指标
  1092. 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=? `
  1093. err = to.Exec(msql, newEdbInfo.EdbInfoId, newEdbInfo.EdbCode, newEdbInfo.EdbName, newEdbInfo.Source, newEdbInfo.SourceName, mv.EdbInfoCalculateMappingId).Error
  1094. if err != nil {
  1095. return
  1096. }
  1097. relationEdbInfoIdList = append(relationEdbInfoIdList, mv.EdbInfoId)
  1098. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(mv.EdbInfoId))
  1099. }
  1100. logMsg += `涉及到的指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1101. }
  1102. }
  1103. // 替换预测指标中的来源指标
  1104. {
  1105. //获取所有包含的计算指标
  1106. predictEdbConfList := make([]*PredictEdbConf, 0)
  1107. msql := ` SELECT * FROM predict_edb_conf WHERE source_edb_info_id=? GROUP BY predict_edb_info_id `
  1108. err = to.Raw(msql, oldEdbInfo.EdbInfoId).Scan(&predictEdbConfList).Error
  1109. if err != nil {
  1110. errmsg = "获取关联的预测指标信息失败:Err:" + err.Error()
  1111. return
  1112. }
  1113. replaceCalculateTotal += len(predictEdbConfList)
  1114. //替换预测指标的来源指标
  1115. msql = `UPDATE predict_edb_conf SET source_edb_info_id=? WHERE source_edb_info_id=? `
  1116. err = to.Exec(msql, newEdbInfo.EdbInfoId, oldEdbInfo.EdbInfoId).Error
  1117. if err != nil {
  1118. return
  1119. }
  1120. if len(predictEdbConfList) > 0 {
  1121. edbInfoIdList := make([]string, 0)
  1122. for _, v := range predictEdbConfList {
  1123. relationEdbInfoIdList = append(relationEdbInfoIdList, v.PredictEdbInfoId)
  1124. edbInfoIdList = append(edbInfoIdList, fmt.Sprint(v.PredictEdbInfoId))
  1125. }
  1126. logMsg += `涉及到的预测指标id:` + strings.Join(edbInfoIdList, ",") + ";"
  1127. }
  1128. }
  1129. return
  1130. }
  1131. type EdbInfoView struct {
  1132. EdbInfoId int `orm:"column(edb_info_id);pk" gorm:"primaryKey" `
  1133. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  1134. SourceName string `description:"来源名称"`
  1135. Source int `description:"来源id"`
  1136. EdbCode string `description:"指标编码"`
  1137. EdbName string `description:"指标名称"`
  1138. EdbNameSource string `description:"指标名称来源"`
  1139. Frequency string `description:"频率"`
  1140. Unit string `description:"单位"`
  1141. StartDate string `description:"起始日期"`
  1142. EndDate string `description:"终止日期"`
  1143. ClassifyId int `description:"分类id"`
  1144. SysUserId int
  1145. SysUserRealName string
  1146. UniqueCode string `description:"指标唯一编码"`
  1147. CreateTime string
  1148. ModifyTime string
  1149. MinValue float64 `description:"指标最小值"`
  1150. MaxValue float64 `description:"指标最大值"`
  1151. CalculateFormula string `description:"计算公式"`
  1152. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  1153. Sort int `description:"排序字段"`
  1154. IsUpdate int `description:"当天是否已更新,1:未更新,2:已更新"`
  1155. LatestDate string `description:"数据最新日期(实际日期)"`
  1156. LatestValue float64 `description:"数据最新值(实际值)"`
  1157. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  1158. SubSourceName string `description:"子数据来源名称"`
  1159. IndicatorCode string `description:"指标代码"`
  1160. StockCode string `description:"证券代码"`
  1161. EndValue float64 `description:"数据的最新值(预测日期的最新值)"`
  1162. }
  1163. // EdbInfoFilterDataResp 搜索指标列表数据返回
  1164. type EdbInfoFilterDataResp struct {
  1165. Paging *paging.PagingItem
  1166. List []*EdbInfoList
  1167. }
  1168. // GetEdbInfoFilterList 获取指指标列表数据接口
  1169. func GetEdbInfoFilterList(condition string, pars []interface{}, startSize, pageSize int) (total int64, list []*EdbInfoList, err error) {
  1170. o := global.DmSQL["data"]
  1171. sql := ` SELECT count(1) total FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC `
  1172. err = o.Raw(sql, pars...).Scan(&total).Error
  1173. if err != nil {
  1174. return
  1175. }
  1176. sql = ` SELECT * FROM edb_info where 1=1 ` + condition + ` ORDER BY edb_info_id DESC LIMIT ?,? `
  1177. pars = append(pars, startSize, pageSize)
  1178. err = o.Raw(sql, pars...).Scan(&list).Error
  1179. return
  1180. }
  1181. // SetEdbInfoImageReq 设置指标图片
  1182. type SetEdbInfoImageReq struct {
  1183. EdbInfoId int `description:"指标ID"`
  1184. ImageUrl string `description:"指标图片地址"`
  1185. }
  1186. // GetNextEdbInfoByCondition 根据条件获取下一个指标
  1187. func GetNextEdbInfoByCondition(condition string, pars []interface{}) (item *EdbInfo, err error) {
  1188. o := global.DmSQL["data"]
  1189. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1190. if condition != "" {
  1191. sql += condition
  1192. }
  1193. sql += " ORDER BY sort asc , edb_info_id asc LIMIT 1 "
  1194. err = o.Raw(sql, pars...).First(&item).Error
  1195. return
  1196. }
  1197. // GetNextEdbInfo 根据分类id获取下一个 指标
  1198. func GetNextEdbInfo(classifyId, classifySort, classifyType, edbInfoType int) (item *EdbInfo, err error) {
  1199. o := global.DmSQL["data"]
  1200. sql := ` SELECT b.* FROM edb_classify AS a
  1201. INNER JOIN edb_info AS b ON a.classify_id=b.classify_id
  1202. WHERE (a.sort>? OR (a.sort=? and a.classify_id>?) )
  1203. AND a.classify_type=? AND b.edb_info_type=?
  1204. ORDER BY a.sort ASC,b.sort asc,b.edb_info_id asc
  1205. LIMIT 1 `
  1206. err = o.Raw(sql, classifySort, classifySort, classifyId, classifyType, edbInfoType).Scan(&item).Error
  1207. return
  1208. }
  1209. // GetEdbInfoByEdbCodeList 根据指标code集合获取指标列表
  1210. func GetEdbInfoByEdbCodeList(source int, edbCodeList []string) (items []*EdbInfo, err error) {
  1211. num := len(edbCodeList)
  1212. if num <= 0 {
  1213. return
  1214. }
  1215. o := global.DmSQL["data"]
  1216. sql := ` SELECT * FROM edb_info WHERE source=? AND edb_code in (` + utils.GetOrmInReplace(num) + `) `
  1217. err = o.Raw(sql, source, edbCodeList).Scan(&items).Error
  1218. return
  1219. }
  1220. // GetEdbInfoByConditionCount 根据条件获取指标列表数据总数
  1221. func GetEdbInfoByConditionCount(condition string, pars []interface{}) (count int, err error) {
  1222. o := global.DmSQL["data"]
  1223. sql := ` SELECT COUNT(1) AS count FROM edb_info WHERE 1=1 `
  1224. if condition != "" {
  1225. sql += condition
  1226. }
  1227. err = o.Raw(sql, pars...).Scan(&count).Error
  1228. return
  1229. }
  1230. // GetEdbInfoListByCondition 根据条件获取指标列表数据
  1231. func GetEdbInfoListByCondition(condition string, pars []interface{}, startSize, pageSize int, orderDesc string) (items []*EdbInfoList, err error) {
  1232. o := global.DmSQL["data"]
  1233. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1234. if condition != "" {
  1235. sql += condition
  1236. }
  1237. if orderDesc == `` {
  1238. orderDesc = ` ASC `
  1239. }
  1240. sql += ` ORDER BY edb_info_id `
  1241. sql += orderDesc
  1242. sql += ` LIMIT ?,? `
  1243. pars = append(pars, startSize)
  1244. pars = append(pars, pageSize)
  1245. err = o.Raw(sql, pars...).Scan(&items).Error
  1246. return
  1247. }
  1248. // GetEdbInfoListByEdbInfoId
  1249. // @Description: 根据指标id列表获取列表信息
  1250. // @param edbIdList
  1251. // @return items
  1252. // @return err
  1253. func GetEdbInfoListByEdbInfoId(edbIdList []string) (items []*EdbInfo, err error) {
  1254. num := len(edbIdList)
  1255. if num <= 0 {
  1256. return
  1257. }
  1258. o := global.DmSQL["data"]
  1259. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1260. err = o.Raw(sql, edbIdList).Scan(&items).Error
  1261. return
  1262. }
  1263. // GetEdbInfoListByUserId
  1264. // @Description: 根据指标id列表获取列表信息
  1265. // @param userIdList []int
  1266. // @param edbInfoType int
  1267. // @return items
  1268. // @return err
  1269. func GetEdbInfoListByUserId(userIdList []int, edbInfoType int) (items []*EdbInfo, err error) {
  1270. num := len(userIdList)
  1271. if num <= 0 {
  1272. return
  1273. }
  1274. o := global.DmSQL["data"]
  1275. sql := ` SELECT * FROM edb_info WHERE edb_info_type = ? AND sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  1276. err = o.Raw(sql, edbInfoType, userIdList).Scan(&items).Error
  1277. return
  1278. }
  1279. // ModifyEdbInfoUserIdByCodeList 根据指标code列表修改创建人
  1280. func ModifyEdbInfoUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  1281. num := len(edbIdList)
  1282. if num <= 0 {
  1283. return
  1284. }
  1285. o := global.DmSQL["data"]
  1286. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1287. err = o.Exec(sql, userId, userName, edbIdList).Error
  1288. return
  1289. }
  1290. // ModifyEdbInfoUserIdByOldUserId
  1291. // @Description: 根据旧用户id修改新用户id
  1292. // @author: Roc
  1293. // @datetime 2024-03-25 19:17:09
  1294. // @param edbIdList []string
  1295. // @param userId int
  1296. // @param userName string
  1297. // @return err error
  1298. func ModifyEdbInfoUserIdByOldUserId(oldUserIdList []int, edbInfoType, userId int, userName string) (err error) {
  1299. num := len(oldUserIdList)
  1300. if num <= 0 {
  1301. return
  1302. }
  1303. o := global.DmSQL["data"]
  1304. sql := `UPDATE edb_info SET sys_user_id=?,sys_user_real_name=? WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) and edb_info_type = ?`
  1305. err = o.Exec(sql, userId, userName, oldUserIdList, edbInfoType).Error
  1306. return
  1307. }
  1308. // MoveEdbChartList 图表/指标信息
  1309. type MoveEdbChartList struct {
  1310. DataId string `description:"指标/图表唯一id"`
  1311. Code string `description:"指标code"`
  1312. Name string `description:"指标/图表名称"`
  1313. NameEn string `description:"指标/图表名称"`
  1314. ClassifyName string `description:"分类名称"`
  1315. ClassifyNameEn string `description:"分类名称"`
  1316. CreateUserId int `description:"创建人id"`
  1317. CreateUserName string `description:"创建人名称"`
  1318. }
  1319. // MoveEdbChartListResp 更改创建人返回数据
  1320. type MoveEdbChartListResp struct {
  1321. Paging *paging.PagingItem
  1322. List []MoveEdbChartList
  1323. }
  1324. type EdbChartClassifyResp struct {
  1325. List []*EdbChartClassify
  1326. }
  1327. type EdbChartClassify struct {
  1328. ClassifyId int
  1329. ClassifyName string
  1330. ParentId int
  1331. IsJoinPermission int `description:"是否加入权限管控,0:不加入;1:加入;默认:0"`
  1332. Child []*EdbChartClassify
  1333. }
  1334. // EdbInfoDataSeasonalResp 指标季节性数据返回
  1335. type EdbInfoDataSeasonalResp struct {
  1336. EdbInfo *EdbInfo
  1337. DataList interface{}
  1338. }
  1339. // SetEdbDataInsertConfigReq 设置插入规则的请求
  1340. type SetEdbDataInsertConfigReq struct {
  1341. EdbInfoId int `description:"指标ID"`
  1342. Date string `description:"日期"`
  1343. Value string `description:"值"`
  1344. }
  1345. // GetEdbInfoByClassifyId 用于分类展示
  1346. func GetEdbInfoByClassifyId(classifyId, edbInfoType, adminId int) (items []*EdbClassifyItems, err error) {
  1347. o := global.DmSQL["data"]
  1348. 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, sort,is_join_permission FROM edb_info WHERE classify_id = ? AND edb_info_type = ?`
  1349. pars := []interface{}{classifyId, edbInfoType}
  1350. // 如果筛选了用户id
  1351. if adminId > 0 {
  1352. sql += ` AND sys_user_id = ? `
  1353. pars = append(pars, adminId)
  1354. }
  1355. sql += ` order by sort asc,edb_info_id asc `
  1356. err = o.Raw(sql, pars...).Scan(&items).Error
  1357. return
  1358. }
  1359. type EdbAndClassify struct {
  1360. EdbInfoId int `description:"指标id"`
  1361. EdbCode string `description:"指标code"`
  1362. UniqueCode string `description:"唯一code"`
  1363. EdbName string `description:"指标名称"`
  1364. ClassifyId int `description:"所属分类id"`
  1365. FirstClassifyId int `description:"第1级的分类id"`
  1366. FirstClassifyName string `description:"第1级的分类名称"`
  1367. SecondClassifyId int `description:"第2级的分类id"`
  1368. SecondClassifyName string `description:"第2级的分类名称"`
  1369. }
  1370. // GetEdbInfoAndClassifyListByEdbIdList 根据指标id列表获取 指标及分类信息
  1371. func GetEdbInfoAndClassifyListByEdbIdList(edbIdList []int) (items []*EdbAndClassify, err error) {
  1372. num := len(edbIdList)
  1373. if num <= 0 {
  1374. return
  1375. }
  1376. o := global.DmSQL["data"]
  1377. sql := ` SELECT a.edb_info_id,a.edb_code,a.unique_code,a.edb_name,a.classify_id FROM edb_info AS a
  1378. WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  1379. err = o.Raw(sql, edbIdList).Scan(&items).Error
  1380. return
  1381. }
  1382. // TraceEdbInfoResp 指标追溯数据返回
  1383. type TraceEdbInfoResp struct {
  1384. EdbInfoId int `description:"指标id"`
  1385. EdbInfoType int `description:"指标类型: 0-普通指标; 1-预测指标"`
  1386. EdbName string `description:"指标名称"`
  1387. EdbNameEn string `description:"指标英文名称"`
  1388. EdbType int `description:"指标类型: 1-基础指标; 2-计算指标"`
  1389. RuleTitle string `description:"指标规则"`
  1390. RuleTitleEn string `description:"指标规则(英文)"`
  1391. UniqueCode string `description:"唯一编码"`
  1392. ClassifyId int `description:"分类ID"`
  1393. Child []TraceEdbInfoResp `description:"下级来源"`
  1394. IsStop int8 `description:"是否终止"`
  1395. EdbInfo *EdbInfo `description:"指标信息" json:"-"`
  1396. HaveOperaAuth bool `description:"是否有数据权限,默认:false"`
  1397. }
  1398. // BeforeAndAfterDateDataResp 前后几期数据
  1399. type BeforeAndAfterDateDataResp struct {
  1400. List []*EdbDataList `description:"list"`
  1401. Date string `description:"实际日期"`
  1402. ShowValue string `description:"展示值"`
  1403. }
  1404. // GetEdbInfoAdminList
  1405. func GetEdbInfoAdminList(edbType int) (list []int, err error) {
  1406. o := global.DmSQL["data"]
  1407. sql := ` SELECT sys_user_id FROM edb_info WHERE edb_info_type = ? GROUP BY sys_user_id `
  1408. err = o.Raw(sql, edbType).Scan(&list).Error
  1409. return
  1410. }
  1411. type EdbInfoExistCheckResp struct {
  1412. IndexExist bool `description:"指标是否存在:true,存在,false:不存在"`
  1413. ExistStockCodeArr []string `description:"存在的证券代码"`
  1414. ExistEdbCodeArr []string `description:"存在的指标代码"`
  1415. ExistClassifyId []int `description:"存在的分类id"`
  1416. ExistIndexId []int `description:"存在的指标id"`
  1417. ExistEdbInfo []*EdbInfo `description:"存在的指标"`
  1418. }
  1419. type BatchAddEdbInfoReq struct {
  1420. BatchList []*BatchAddEdbInfo
  1421. }
  1422. type BatchAddEdbInfo struct {
  1423. Source int `description:"来源id"`
  1424. EdbName string `description:"指标名称"`
  1425. Frequency string `description:"频率"`
  1426. Unit string `description:"单位"`
  1427. ClassifyId int `description:"分类id"`
  1428. StockCode string `description:"证券代码"`
  1429. EdbCode string `description:"指标编码"`
  1430. ApiExtraPars string `description:"API额外参数"`
  1431. }
  1432. func GetEdbInfoWsdMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  1433. o := global.DmSQL["data"]
  1434. sql := ``
  1435. tableName := GetEdbDataTableNameAndSubSource(source, subSource)
  1436. 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=? `
  1437. sql = fmt.Sprintf(sql, tableName)
  1438. err = o.Raw(sql, edbCode).Scan(&item).Error
  1439. if err != nil {
  1440. return
  1441. }
  1442. var latest_value float64
  1443. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  1444. sql = fmt.Sprintf(sql, tableName)
  1445. err = o.Raw(sql, edbCode).Scan(&latest_value).Error
  1446. item.LatestValue = latest_value
  1447. return
  1448. }
  1449. // GetEdbInfoByNames 根据名称获取普通的指标
  1450. func GetEdbInfoByNames(edbNames string) (items []*EdbInfoList, err error) {
  1451. o := global.DmSQL["data"]
  1452. sql := ` SELECT * FROM edb_info WHERE edb_name IN (` + edbNames + `) AND edb_info_type = 0 `
  1453. err = o.Raw(sql).Scan(&items).Error
  1454. return
  1455. }
  1456. type EdbInfoSmmExistCheckResp struct {
  1457. EdbInfoExistCheckResp
  1458. ExistAll bool `description:"是否全部重复"`
  1459. }
  1460. // GetEdbBaseInfoList
  1461. // @Description: 获取指标库数据列表
  1462. // @author: Roc
  1463. // @datetime 2024-01-10 14:28:22
  1464. // @param condition string
  1465. // @param pars []interface{}
  1466. // @param orderBy string
  1467. // @param startSize int
  1468. // @param pageSize int
  1469. // @return total int
  1470. // @return items []*BaseRefreshEdbInfo
  1471. // @return err error
  1472. func GetEdbBaseInfoList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRefreshEdbInfo, err error) {
  1473. o := global.DmSQL["data"]
  1474. // 数量汇总
  1475. totalSql := ` SELECT count(1) FROM edb_info WHERE 1=1 `
  1476. if condition != "" {
  1477. totalSql += condition
  1478. }
  1479. err = o.Raw(totalSql, pars...).Scan(&total).Error
  1480. if err != nil {
  1481. return
  1482. }
  1483. // 列表数据
  1484. sql := ` SELECT edb_info_id as edb_info_id, classify_id as classify_id,edb_code as index_code,edb_name as index_name,end_date,end_value,sys_user_id,sys_user_real_name,frequency,no_update as is_stop,terminal_code FROM edb_info WHERE 1=1 `
  1485. if condition != "" {
  1486. sql += condition
  1487. }
  1488. if orderBy != "" {
  1489. sql += ` ORDER BY ` + orderBy
  1490. } else {
  1491. sql += ` ORDER BY edb_info_id ASC `
  1492. }
  1493. sql += ` LIMIT ?,? `
  1494. err = o.Raw(sql, pars...).Scan(&items).Error
  1495. return
  1496. }
  1497. // ModifyEdbInfoUpdateStatus
  1498. // @Description: 修改指标库停更状态
  1499. // @author: Roc
  1500. // @datetime 2024-01-08 16:23:31
  1501. // @param edbIdList []int
  1502. // @param indexCodeList []string
  1503. // @param isStop int
  1504. // @return err error
  1505. func ModifyEdbInfoUpdateStatus(edbIdList []int, isStop int, calculateEdbInfoIds []int) (err error) {
  1506. idNum := len(edbIdList)
  1507. if idNum <= 0 {
  1508. return
  1509. }
  1510. o := global.DmSQL["data"].Begin()
  1511. defer func() {
  1512. if err != nil {
  1513. _ = o.Rollback()
  1514. return
  1515. }
  1516. _ = o.Commit()
  1517. }()
  1518. // 更改指标的更新状态
  1519. sql := ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  1520. err = o.Exec(sql, isStop, edbIdList).Error
  1521. if err != nil {
  1522. return
  1523. }
  1524. if len(calculateEdbInfoIds) > 0 {
  1525. // 批量更新相关联的指标ID
  1526. sql = ` UPDATE edb_info SET no_update = ? WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) `
  1527. err = o.Exec(sql, isStop, calculateEdbInfoIds).Error
  1528. if err != nil {
  1529. return
  1530. }
  1531. }
  1532. return
  1533. }
  1534. func GetEdbInfoCount(source int, edbCode string) (count int, err error) {
  1535. o := global.DmSQL["data"]
  1536. sql := ` SELECT COUNT(1) AS count FROM edb_info
  1537. WHERE source=? AND edb_code = ?`
  1538. err = o.Raw(sql, source, edbCode).Scan(&count).Error
  1539. return
  1540. }
  1541. // GetEdbInfoByNameArr 根据名称获取指标
  1542. func GetEdbInfoByNameArr(names []string, edbInfoType int) (items []*EdbInfo, err error) {
  1543. if len(names) == 0 {
  1544. return
  1545. }
  1546. o := global.DmSQL["data"]
  1547. sql := fmt.Sprintf(`SELECT edb_info_id, edb_code, edb_name FROM edb_info WHERE edb_name IN (%s) AND edb_info_type = ? `, utils.GetOrmInReplace(len(names)))
  1548. err = o.Raw(sql, names, edbInfoType).Scan(&items).Error
  1549. return
  1550. }
  1551. // GetEdbCodesBySource 根据来源获取指标编码
  1552. func GetEdbCodesBySource(source int) (items []*EdbInfo, err error) {
  1553. o := global.DmSQL["data"]
  1554. sql := ` SELECT edb_info_id, edb_code, unique_code, classify_id FROM edb_info WHERE source = ? `
  1555. err = o.Raw(sql, source).Scan(&items).Error
  1556. return
  1557. }
  1558. // GetAllEdbDataListData
  1559. // @Description: 获取指标的所有数据
  1560. // @author: Roc
  1561. // @datetime 2024-05-07 15:18:27
  1562. // @param edbInfoId int
  1563. // @param source int
  1564. // @param subSource int
  1565. // @param startDataTime string
  1566. // @return dataList []*EdbData
  1567. // @return err error
  1568. func GetAllEdbDataListData(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1569. // 自有数据需要额外处理(从mongo获取)
  1570. if source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  1571. return getAllDataByMongo(edbInfoId, source, subSource, startDataTime)
  1572. }
  1573. if source == utils.DATA_SOURCE_THS && subSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
  1574. return getThsHfAllDataByMongo(edbInfoId, source, subSource, startDataTime)
  1575. }
  1576. // 默认走mysql
  1577. return getAllDataByMysql(edbInfoId, source, subSource, startDataTime)
  1578. }
  1579. // getAllDataByMysql
  1580. // @Description: 从mysql获取指标的所有数据
  1581. // @author: Roc
  1582. // @datetime 2024-05-07 15:18:41
  1583. // @param edbInfoId int
  1584. // @param source int
  1585. // @param subSource int
  1586. // @param startDataTime string
  1587. // @return dataList []*data_manage.EdbData
  1588. // @return err error
  1589. func getAllDataByMysql(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1590. dataList = make([]*EdbData, 0)
  1591. var dataCondition string
  1592. var dataPars []interface{}
  1593. dataCondition += ` AND edb_info_id=? `
  1594. dataPars = append(dataPars, edbInfoId)
  1595. // 结束日期
  1596. if startDataTime != "" {
  1597. dataCondition += ` AND data_time > ? `
  1598. dataPars = append(dataPars, startDataTime)
  1599. }
  1600. // 获取列表数据
  1601. dataList, err = GetAllEdbDataListByCondition(dataCondition, dataPars, source, subSource)
  1602. if err != nil {
  1603. return
  1604. }
  1605. return
  1606. }
  1607. // getAllDataByMongo
  1608. // @Description: 从mongo获取指标的所有数据
  1609. // @author: Roc
  1610. // @datetime 2024-05-07 15:18:53
  1611. // @param edbInfoId int
  1612. // @param source int
  1613. // @param subSource int
  1614. // @param startDataTime string
  1615. // @return dataList []*data_manage.EdbData
  1616. // @return err error
  1617. func getAllDataByMongo(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1618. dataList = make([]*EdbData, 0)
  1619. mogDataObj := mgo.EdbDataBusiness{}
  1620. // 构建查询条件
  1621. queryConditions := bson.M{
  1622. "edb_info_id": edbInfoId,
  1623. }
  1624. // 开始日期
  1625. dateCondition, err := mgo.BuildDateCondition(startDataTime, "")
  1626. if err != nil {
  1627. return
  1628. }
  1629. if len(dateCondition) > 0 {
  1630. queryConditions["data_time"] = dateCondition
  1631. }
  1632. // 获取列表数据
  1633. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"-data_time"})
  1634. if tmpErr != nil {
  1635. err = tmpErr
  1636. return
  1637. }
  1638. for k, v := range tmpDataList {
  1639. dataList = append(dataList, &EdbData{
  1640. EdbDataId: k + 1,
  1641. EdbInfoId: v.EdbInfoId,
  1642. DataTime: v.DataTime.Format(utils.FormatDate),
  1643. Value: v.Value,
  1644. })
  1645. }
  1646. return
  1647. }
  1648. // GetEdbClassifyIdListBySource 获取普通指标的分类列表
  1649. func GetEdbClassifyIdListBySource(source int) (items []int, err error) {
  1650. o := global.DmSQL["data"]
  1651. sql := `SELECT distinct classify_id FROM edb_info WHERE source = ? and edb_info_type=0`
  1652. err = o.Raw(sql, source).Scan(&items).Error
  1653. return
  1654. }
  1655. // GetEdbInfoByClassifyIdAndSource 用于分类展示
  1656. func GetEdbInfoByClassifyIdAndSource(classifyId, edbInfoType, source int) (items []*EdbClassifyItems, err error) {
  1657. o := global.DmSQL["data"]
  1658. 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, sort,is_join_permission FROM edb_info WHERE classify_id = ? AND edb_info_type = ? AND source =?`
  1659. pars := []interface{}{classifyId, edbInfoType, source}
  1660. sql += ` order by sort asc,edb_info_id asc `
  1661. err = o.Raw(sql, pars...).Scan(&items).Error
  1662. return
  1663. }
  1664. type ReplaceEdbInfoItem struct {
  1665. OldEdbInfo *EdbInfo
  1666. NewEdbInfo *EdbInfo
  1667. }
  1668. type EdbInfoEditRecord struct {
  1669. EdbInfoId int `description:"指标ID"`
  1670. EdbName string `description:"指标名称"`
  1671. Frequency string `description:"频率"`
  1672. Unit string `description:"单位"`
  1673. ClassifyId int `description:"分类id"`
  1674. CalculateFormula string `description:"计算公式"`
  1675. OperateUserId int `description:"操作人id"`
  1676. OperateUserRealName string `description:"操作人姓名"`
  1677. }
  1678. func ModifyEdbInfoBaseTimeById(edbInfoId int, cTime time.Time) (err error) {
  1679. o := global.DmSQL["data"]
  1680. // 更新修改时间
  1681. sql := ` UPDATE edb_info SET base_modify_time = ? WHERE edb_info_id = ? `
  1682. err = o.Exec(sql, cTime, edbInfoId).Error
  1683. return
  1684. }
  1685. func GetEdbInfoFieldList(cond string, pars []interface{}, fields []string) (items []*EdbInfo, err error) {
  1686. field := " * "
  1687. if len(fields) > 0 {
  1688. field = strings.Join(fields, ",")
  1689. }
  1690. o := global.DmSQL["data"]
  1691. sql := fmt.Sprintf(`SELECT %s FROM edb_info WHERE 1=1 %s `, field, cond)
  1692. err = o.Raw(sql, pars...).Scan(&items).Error
  1693. return
  1694. }
  1695. // EdbInfoExtra 指标额外数据-extra字段
  1696. type EdbInfoExtra struct {
  1697. ApiExtraPars string `description:"API-额外参数(如同花顺日期序列)"`
  1698. }
  1699. func GetEdbInfoListByCond(condition string, pars []interface{}) (list []*EdbInfoList, err error) {
  1700. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  1701. if condition != "" {
  1702. sql += condition
  1703. }
  1704. sql += ` ORDER BY create_time DESC `
  1705. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&list).Error
  1706. return
  1707. }
  1708. func GetThsHfEdbInfoMaxAndMinInfoByMongo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  1709. mogDataObj := new(mgo.EdbDataThsHf)
  1710. pipeline := []bson.M{
  1711. {"$match": bson.M{"edb_code": edbCode}},
  1712. {"$group": bson.M{
  1713. "_id": nil,
  1714. "min_date": bson.M{"$min": "$data_time"},
  1715. "max_date": bson.M{"$max": "$data_time"},
  1716. "min_value": bson.M{"$min": "$value"},
  1717. "max_value": bson.M{"$max": "$value"},
  1718. }},
  1719. {"$project": bson.M{"_id": 0}}, // 可选,如果不需要_id字段
  1720. }
  1721. result, err := mogDataObj.GetEdbInfoMaxAndMinInfo(pipeline)
  1722. if err != nil {
  1723. fmt.Println("EdbDataThsHf getEdbDataThsHfList Err:" + err.Error())
  1724. return
  1725. }
  1726. if !result.MaxDate.IsZero() {
  1727. whereQuery := bson.M{"edb_code": edbCode, "data_time": result.MaxDate}
  1728. selectParam := bson.D{{"value", 1}, {"_id", 0}}
  1729. latestValue, tmpErr := mogDataObj.GetLatestValue(whereQuery, selectParam)
  1730. if tmpErr != nil {
  1731. err = tmpErr
  1732. return
  1733. }
  1734. result.LatestValue = latestValue.Value
  1735. result.EndValue = latestValue.Value
  1736. }
  1737. item = &EdbInfoMaxAndMinInfo{
  1738. MinDate: result.MinDate.Format(utils.FormatDate),
  1739. MaxDate: result.MaxDate.Format(utils.FormatDate),
  1740. MinValue: result.MinValue,
  1741. MaxValue: result.MaxValue,
  1742. LatestValue: result.LatestValue,
  1743. }
  1744. return
  1745. }
  1746. func getThsHfAllDataByMongo(edbInfoId, source, subSource int, startDataTime string) (dataList []*EdbData, err error) {
  1747. dataList = make([]*EdbData, 0)
  1748. mogDataObj := mgo.EdbDataThsHf{}
  1749. // 构建查询条件
  1750. queryConditions := bson.M{
  1751. "edb_info_id": edbInfoId,
  1752. }
  1753. // 开始日期
  1754. dateCondition, err := mgo.BuildDateCondition(startDataTime, "")
  1755. if err != nil {
  1756. return
  1757. }
  1758. if len(dateCondition) > 0 {
  1759. queryConditions["data_time"] = dateCondition
  1760. }
  1761. // 获取列表数据
  1762. tmpDataList, tmpErr := mogDataObj.GetAllDataList(queryConditions, []string{"-data_time"})
  1763. if tmpErr != nil {
  1764. err = tmpErr
  1765. return
  1766. }
  1767. for k, v := range tmpDataList {
  1768. dataList = append(dataList, &EdbData{
  1769. EdbDataId: k + 1,
  1770. EdbInfoId: v.EdbInfoId,
  1771. DataTime: v.DataTime.Format(utils.FormatDate),
  1772. Value: v.Value,
  1773. })
  1774. }
  1775. return
  1776. }
  1777. // GetEdbInfoByTypes 根据类型获取所有指标
  1778. func GetEdbInfoByTypes(edbInfoType, edbType int) (items []*EdbClassifyItems, err error) {
  1779. o := global.DmSQL["data"]
  1780. 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 = ? AND edb_type = ? order by sort asc,edb_info_id asc`
  1781. err = o.Raw(sql, edbInfoType, edbType).Scan(&items).Error
  1782. return
  1783. }