edb_info.go 65 KB

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