edb_info.go 65 KB

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