edb_info.go 69 KB

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