mysteel_chemical_index.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "gorm.io/gorm"
  7. "strings"
  8. "time"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. )
  11. // BaseFromMysteelChemicalIndex 上海钢联指标表
  12. type BaseFromMysteelChemicalIndex struct {
  13. BaseFromMysteelChemicalIndexId int `orm:"column(base_from_mysteel_chemical_index_id);pk" gorm:"primaryKey"`
  14. BaseFromMysteelChemicalClassifyId int `orm:"column(base_from_mysteel_chemical_classify_id)" description:"上海钢联指标分类id"`
  15. IndexCode string `description:"指标编码"`
  16. IndexName string `description:"指标名称"`
  17. Unit string `description:"单位"`
  18. Source string `description:"数据来源"`
  19. Frequency string `description:"频度"`
  20. StartDate string `description:"开始日期"`
  21. EndDate string `description:"结束日期"`
  22. Describe string `description:"指标描述"`
  23. UpdateWeek string `description:"更新周期"`
  24. UpdateTime string `description:"更新时间1"`
  25. UpdateTime2 string `description:"更新时间2"`
  26. SysUserId int `description:"创建人id"`
  27. SysUserRealName string `description:"创建人姓名"`
  28. ModifyTime time.Time `description:"修改时间"`
  29. CreateTime time.Time `description:"创建时间"`
  30. Sort int `description:"排序字段"`
  31. FilePath string `description:"文件"`
  32. MergeFilePath string `description:"合并文件"`
  33. TerminalCode string `description:"终端编码"`
  34. IsStop int `description:"是否停更:1:停更,0:未停更"`
  35. EndValue float64 `description:"指标的最新值"`
  36. IsSupplierStop int `description:"是否供应商停更:1:停更,0:未停更"`
  37. }
  38. type BaseFromMysteelChemicalIndexItem struct {
  39. BaseFromMysteelChemicalIndexId int64 `gorm:"column:base_from_mysteel_chemical_index_id);primaryKey"`
  40. IndexCode string `description:"指标编码"`
  41. IndexName string `description:"指标名称"`
  42. Unit string `description:"单位"`
  43. Frequency string `description:"频度"`
  44. Source string `description:"数据来源"`
  45. StartDate string `description:"开始日期"`
  46. EndDate string `description:"结束日期"`
  47. CreateTime string `description:"创建时间"`
  48. ModifyTime string `description:"修改时间"`
  49. EdbInfoId int `description:"eta指标库的id"`
  50. EdbUniqueCode string `description:"指标库唯一编码"`
  51. EdbClassifyId int `description:"指标库分类ID"`
  52. IsStop int `description:"是否停更:1:停更,0:未停更"`
  53. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  54. }
  55. func (m *BaseFromMysteelChemicalIndexItem) AfterFind(tx *gorm.DB) (err error) {
  56. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  57. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  58. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  59. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  60. return
  61. }
  62. var BaseFromMysteelChemicalIndexCols = struct {
  63. BaseFromMysteelChemicalIndexId string
  64. IndexCode string
  65. IndexName string
  66. Unit string
  67. Frequency string
  68. Source string
  69. StartDate string
  70. EndDate string
  71. CreateTime string
  72. ModifyTime string
  73. }{
  74. BaseFromMysteelChemicalIndexId: "base_from_mysteel_chemical_index_id",
  75. IndexCode: "index_code",
  76. IndexName: "index_name",
  77. Unit: "unit",
  78. Frequency: "frequency",
  79. Source: "source",
  80. StartDate: "start_date",
  81. EndDate: "end_date",
  82. CreateTime: "create_time",
  83. ModifyTime: "modify_time",
  84. }
  85. // Update 更新上海钢联指标基础信息
  86. func (item *BaseFromMysteelChemicalIndex) Update(cols []string) (err error) {
  87. o := global.DbMap[utils.DbNameIndex]
  88. err = o.Select(cols).Updates(item).Error
  89. return
  90. }
  91. func (m *BaseFromMysteelChemicalIndex) GeItemsByCondition(condition string, pars []interface{}, limitSize int) (items []*BaseFromMysteelChemicalIndexItem, err error) {
  92. if pars == nil {
  93. pars = make([]interface{}, 0)
  94. }
  95. //pars = append(pars, utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  96. pars = utils.ForwardPars(pars, utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  97. sql := `select a.*,b.edb_info_id,b.unique_code as edb_unique_code,b.classify_id as edb_classify_id FROM base_from_mysteel_chemical_index AS a LEFT JOIN edb_info b on a.index_code=b.edb_code AND b.source=? WHERE 1=1 `
  98. if condition != "" {
  99. sql += condition
  100. }
  101. sql += ` order by a.base_from_mysteel_chemical_index_id desc `
  102. if limitSize > 0 {
  103. sql += " limit ? "
  104. pars = append(pars, limitSize)
  105. }
  106. o := global.DbMap[utils.DbNameIndex]
  107. err = o.Raw(sql, pars...).Find(&items).Error
  108. return
  109. }
  110. // AddBaseFromMysteelChemicalIndex 添加上海钢联指标
  111. func AddBaseFromMysteelChemicalIndex(item *BaseFromMysteelChemicalIndex) (lastId int64, err error) {
  112. o := global.DbMap[utils.DbNameIndex]
  113. err = o.Create(item).Error
  114. if err != nil {
  115. return
  116. }
  117. lastId = int64(item.BaseFromMysteelChemicalIndexId)
  118. return
  119. }
  120. // AddBaseFromMysteelChemicalIndex 添加上海钢联指标
  121. func BatchAddBaseFromMysteelChemicalIndex(items []*BaseFromMysteelChemicalIndex) (lastId int64, err error) {
  122. o := global.DbMap[utils.DbNameIndex]
  123. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  124. return
  125. }
  126. // BaseFromMysteelChemicalData 上海钢联指标数据表
  127. type BaseFromMysteelChemicalData struct {
  128. BaseFromMysteelChemicalDataId int `orm:"column(base_from_mysteel_chemical_data_id);pk" gorm:"primaryKey"`
  129. BaseFromMysteelChemicalIndexId int `orm:"column(base_from_mysteel_chemical_index_id)" description:"上海钢联指标id"`
  130. IndexCode string `description:"指标编码"`
  131. DataTime time.Time `description:"数据日期"`
  132. Value float64 `description:"数据值"`
  133. ModifyTime time.Time `description:"修改时间"`
  134. CreateTime time.Time `description:"创建时间"`
  135. }
  136. // MysteelChemicalFrequency 上海钢联频度
  137. type MysteelChemicalFrequency struct {
  138. Frequency string `description:"频度:1-日度 2-周度 3-月度 4-季度 5-年度 99-无固定频率"`
  139. }
  140. // GetMysteelChemicalIndexByClassifyId 用于分类展示
  141. func GetMysteelChemicalIndexByClassifyId(classifyId int) (items []*BaseFromMysteelChemicalClassifyItems, err error) {
  142. o := global.DbMap[utils.DbNameIndex]
  143. sql := ` SELECT base_from_mysteel_chemical_index_id,base_from_mysteel_chemical_classify_id,index_name AS classify_name,
  144. sys_user_id,sys_user_real_name,sort,index_code
  145. FROM base_from_mysteel_chemical_index where base_from_mysteel_chemical_classify_id=? ORDER BY sort asc, create_time ASC `
  146. err = o.Raw(sql, classifyId).Find(&items).Error
  147. return
  148. }
  149. // MysteelChemicalFrequencyByClassifyId 根据分类id获取上海钢联频度数据列表
  150. func MysteelChemicalFrequencyByClassifyId(classifyId int) (items []*MysteelChemicalFrequency, err error) {
  151. o := global.DbMap[utils.DbNameIndex]
  152. if classifyId == 0 {
  153. sql := ` SELECT frequency FROM base_from_mysteel_chemical_index WHERE frequency != '' GROUP BY frequency ORDER BY frequency ASC `
  154. err = o.Raw(sql).Find(&items).Error
  155. return
  156. } else {
  157. sql := ` SELECT frequency FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_classify_id = ? AND frequency != '' GROUP BY frequency ORDER BY frequency ASC `
  158. err = o.Raw(sql, classifyId).Find(&items).Error
  159. return
  160. }
  161. }
  162. // GetMysteelChemicalFrequency 获取上海钢联频度数据列表
  163. func GetMysteelChemicalFrequency(condition string, pars []interface{}) (items []*MysteelChemicalFrequency, err error) {
  164. o := global.DbMap[utils.DbNameIndex]
  165. sql := ` SELECT frequency FROM base_from_mysteel_chemical_index WHERE 1=1 AND frequency != '' `
  166. if condition != "" {
  167. sql += condition
  168. }
  169. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  170. err = o.Raw(sql, pars...).Find(&items).Error
  171. return
  172. }
  173. // MysteelChemicalList 上海钢联指标列表
  174. type MysteelChemicalList struct {
  175. Id int `gorm:"column:base_from_mysteel_chemical_index_id"`
  176. BaseFromMysteelChemicalClassifyId int `gorm:"column:base_from_mysteel_chemical_classify_id" description:"钢联化工指标分类id"`
  177. ParentClassifyId int `description:"上海钢联指标父级分类id"`
  178. IndexCode string `description:"指标编码"`
  179. IndexName string `description:"指标名称"`
  180. UnitName string `gorm:"column:unit"`
  181. UniqueCode string `description:"唯一编码"`
  182. FrequencyName string `gorm:"column:frequency"`
  183. EdbInfoId int `description:"指标库的id"`
  184. UpdateTime string `grom:"column:modify_time"`
  185. IsStop int `description:"是否停更:1:停更,0:未停更"`
  186. IsSupplierStop int `description:"是否供应商停更:1:停更,0:未停更"`
  187. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  188. EndDate string `description:"结束日期"`
  189. EndValue float64 `description:"最新值"`
  190. DataList []*MysteelChemicalData `gorm:"-"`
  191. }
  192. func (m *MysteelChemicalList) AfterFind(tx *gorm.DB) (err error) {
  193. m.UpdateTime = utils.GormDateStrToDateTimeStr(m.UpdateTime)
  194. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  195. return
  196. }
  197. // MysteelChemicalData 上海钢联数据列表
  198. type MysteelChemicalData struct {
  199. InputValue string `gorm:"column:value" description:"值"`
  200. DataTime string `gorm:"column:data_time" description:"日期"`
  201. }
  202. func (m *MysteelChemicalData) AfterFind(tx *gorm.DB) (err error) {
  203. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  204. return
  205. }
  206. // GetMysteelChemicalIndex 根据分类id获取上海钢联频度数据列表
  207. func GetMysteelChemicalIndex(condition string, pars []interface{}) (items []*MysteelChemicalList, err error) {
  208. o := global.DbMap[utils.DbNameIndex]
  209. sql := ` SELECT * FROM base_from_mysteel_chemical_index WHERE 1=1 `
  210. if condition != "" {
  211. sql += condition
  212. }
  213. sql += ` ORDER BY base_from_mysteel_chemical_index_id ASC `
  214. err = o.Raw(sql, pars...).Find(&items).Error
  215. return
  216. }
  217. // GetMysteelChemicalIndexData 根据指标code获取上海钢联数据列表
  218. func GetMysteelChemicalIndexData(indexCode string, startSize, pageSize int) (items []*MysteelChemicalData, err error) {
  219. sql := ` SELECT * FROM (
  220. SELECT DISTINCT a.index_code,a.value,a.data_time FROM base_from_mysteel_chemical_data AS a WHERE index_code=?
  221. ORDER BY data_time DESC
  222. )AS t
  223. GROUP BY t.data_time
  224. ORDER BY t.data_time DESC LIMIT ?,? `
  225. sql = utils.ReplaceDriverKeywords(utils.DbDriverName, sql)
  226. o := global.DbMap[utils.DbNameIndex]
  227. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  228. return
  229. }
  230. // GetMysteelChemicalIndexDataCount 根据指标code获取上海钢联数据列表 获取钢联数据总数
  231. func GetMysteelChemicalIndexDataCount(indexCode string) (count int, err error) {
  232. sql := `SELECT COUNT(1) AS count FROM (
  233. SELECT * FROM (
  234. SELECT DISTINCT a.index_code,a.value,a.data_time FROM base_from_mysteel_chemical_data AS a WHERE index_code=?
  235. ORDER BY data_time DESC
  236. )AS t
  237. GROUP BY t.data_time
  238. ORDER BY t.data_time DESC
  239. )AS n `
  240. o := global.DbMap[utils.DbNameIndex]
  241. err = o.Raw(sql, indexCode).Scan(&count).Error
  242. return
  243. }
  244. // GetBaseFromMysteelChemicalIndexByIndexId 根据指标id获取指标信息
  245. func GetBaseFromMysteelChemicalIndexByIndexId(indexId int) (item *BaseFromMysteelChemicalIndex, err error) {
  246. o := global.DbMap[utils.DbNameIndex]
  247. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_index_id=? `
  248. err = o.Raw(sql, indexId).Find(&item).Error
  249. return
  250. }
  251. // GetBaseFromMysteelChemicalIndexByCode 根据指标code获取指标信息
  252. func GetBaseFromMysteelChemicalIndexByCode(indexCode string) (item *BaseFromMysteelChemicalIndex, err error) {
  253. o := global.DbMap[utils.DbNameIndex]
  254. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE index_code=? `
  255. err = o.Raw(sql, indexCode).Find(&item).Error
  256. return
  257. }
  258. // GetBaseFromMysteelChemicalIndexByCodeList 根据指标code获取指标信息
  259. func GetBaseFromMysteelChemicalIndexByCodeList(indexCodeList []string) (items []*BaseFromMysteelChemicalIndex, err error) {
  260. if len(indexCodeList) <= 0 {
  261. return
  262. }
  263. o := global.DbMap[utils.DbNameIndex]
  264. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE index_code IN (%s) `
  265. holder := make([]string, 0)
  266. for range indexCodeList {
  267. holder = append(holder, "?")
  268. }
  269. sql = fmt.Sprintf(sql, strings.Join(holder, ","))
  270. err = o.Raw(sql, indexCodeList).Find(&items).Error
  271. return
  272. }
  273. // GetBaseFromMysteelChemicalIndexByClassifyIdAndName 根据分类id和指标名名获取指标信息
  274. func GetBaseFromMysteelChemicalIndexByClassifyIdAndName(classifyId int, chartName string) (item *BaseFromMysteelChemicalIndex, err error) {
  275. o := global.DbMap[utils.DbNameIndex]
  276. sql := ` SELECT * FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_classify_id = ? and index_name = ? `
  277. err = o.Raw(sql, classifyId, chartName).Find(&item).Error
  278. return
  279. }
  280. // GetBaseFromMysteelChemicalIndexListByClassifyId 根据指标分类id获取指标列表信息
  281. func GetBaseFromMysteelChemicalIndexListByClassifyId(classifyId int) (items []*BaseFromMysteelChemicalIndex, err error) {
  282. o := global.DbMap[utils.DbNameIndex]
  283. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_classify_id=? `
  284. err = o.Raw(sql, classifyId).Find(&items).Error
  285. return
  286. }
  287. // GetBaseFromMysteelChemicalIndexListByClassifyIdList 根据指标分类id集合获取指标列表信息
  288. func GetBaseFromMysteelChemicalIndexListByClassifyIdList(classifyIdList []int) (items []*BaseFromMysteelChemicalIndex, err error) {
  289. num := len(classifyIdList)
  290. if num <= 0 {
  291. return
  292. }
  293. o := global.DbMap[utils.DbNameIndex]
  294. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  295. err = o.Raw(sql, classifyIdList).Find(&items).Error
  296. return
  297. }
  298. // GetBaseFromMysteelChemicalDataMaxCount 获取分类下指标的最大数量
  299. func GetBaseFromMysteelChemicalDataMaxCount(classifyId int) (count int, err error) {
  300. o := global.DbMap[utils.DbNameIndex]
  301. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM (
  302. SELECT COUNT(1) AS num FROM base_from_mysteel_chemical_index AS a
  303. INNER JOIN base_from_mysteel_chemical_data AS b ON a.base_from_mysteel_chemical_index_id=b.base_from_mysteel_chemical_index_id
  304. WHERE a.base_from_mysteel_chemical_classify_id=?
  305. GROUP BY a.base_from_mysteel_chemical_index_id
  306. )AS t `
  307. err = o.Raw(sql, classifyId).Scan(&count).Error
  308. return
  309. }
  310. // GetBaseFromMysteelChemicalDataTimeByIndexId 根据指标id获取指标数据的日期列表
  311. func GetBaseFromMysteelChemicalDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  312. if len(indexIdList) == 0 {
  313. return
  314. }
  315. o := global.DbMap[utils.DbNameIndex]
  316. sql := ` SELECT DISTINCT data_time FROM base_from_mysteel_chemical_data WHERE base_from_mysteel_chemical_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
  317. err = o.Raw(sql, indexIdList).Find(&items).Error
  318. if err != nil {
  319. return
  320. }
  321. for i := range items {
  322. items[i] = utils.GormDateStrToDateStr(items[i])
  323. }
  324. return
  325. }
  326. // GetMysteelChemicalIndexDataByCode 通过上海钢联指标code获取所有数据列表
  327. func GetMysteelChemicalIndexDataByCode(indexCode string) (items []*MysteelChemicalData, err error) {
  328. sql := ` SELECT * FROM (
  329. SELECT DISTINCT a.index_code,a.value,a.data_time FROM base_from_mysteel_chemical_data AS a WHERE index_code=?
  330. ORDER BY data_time DESC
  331. )AS t
  332. GROUP BY t.data_time
  333. ORDER BY t.data_time DESC `
  334. o := global.DbMap[utils.DbNameIndex]
  335. err = o.Raw(sql, indexCode).Find(&items).Error
  336. return
  337. }
  338. // MoveBaseFromMysteelChemicalIndex 移动上海钢联指标分类
  339. func MoveBaseFromMysteelChemicalIndex(chartInfoId, classifyId int) (err error) {
  340. o := global.DbMap[utils.DbNameIndex]
  341. sql := ` UPDATE base_from_mysteel_chemical_index
  342. SET
  343. base_from_mysteel_chemical_classify_id = ?
  344. WHERE base_from_mysteel_chemical_index_id = ?`
  345. err = o.Exec(sql, classifyId, chartInfoId).Error
  346. return
  347. }
  348. // UpdateBaseFromMysteelChemicalIndexByClassifyId 根据指标id更新排序
  349. func UpdateBaseFromMysteelChemicalIndexByClassifyId(classifyId, nowSort, prevIndexInfoId int, updateSort string) (err error) {
  350. o := global.DbMap[utils.DbNameIndex]
  351. sql := ` update base_from_mysteel_chemical_index set sort = ` + updateSort + ` WHERE base_from_mysteel_chemical_classify_id=? AND `
  352. if prevIndexInfoId > 0 {
  353. sql += ` ( sort > ? or (base_from_mysteel_chemical_index_id > ` + fmt.Sprint(prevIndexInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  354. }
  355. err = o.Exec(sql, classifyId, nowSort).Error
  356. return
  357. }
  358. // GetFirstBaseFromMysteelChemicalIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  359. func GetFirstBaseFromMysteelChemicalIndexByClassifyId(classifyId int) (item *BaseFromMysteelChemicalIndex, err error) {
  360. o := global.DbMap[utils.DbNameIndex]
  361. sql := ` SELECT * FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_classify_id=? order by sort asc,base_from_mysteel_chemical_index_id asc limit 1`
  362. err = o.Raw(sql, classifyId).Find(&item).Error
  363. return
  364. }
  365. // GetMysteelChemicalIndexCount 根据条件获取上海钢联数据
  366. func GetMysteelChemicalIndexCount(condition string, pars []interface{}) (count int, err error) {
  367. o := global.DbMap[utils.DbNameIndex]
  368. sql := ` SELECT COUNT(1) AS count FROM base_from_mysteel_chemical_index WHERE 1=1 `
  369. if condition != "" {
  370. sql += condition
  371. }
  372. sql += ` ORDER BY base_from_mysteel_chemical_index_id ASC `
  373. err = o.Raw(sql, pars...).Scan(&count).Error
  374. return
  375. }
  376. // GetMysteelChemicalIndexList 根据分类id获取上海钢联频度数据列表
  377. func GetMysteelChemicalIndexList(condition string, pars []interface{}, startSize, pageSize int, orderDesc string) (items []*BaseFromMysteelChemicalIndex, err error) {
  378. o := global.DbMap[utils.DbNameIndex]
  379. sql := ` SELECT * FROM base_from_mysteel_chemical_index WHERE 1=1 `
  380. if condition != "" {
  381. sql += condition
  382. }
  383. if orderDesc == `` {
  384. orderDesc = ` ASC `
  385. }
  386. sql += ` ORDER BY base_from_mysteel_chemical_index_id `
  387. sql += orderDesc
  388. sql += ` LIMIT ?,? `
  389. pars = append(pars, startSize, pageSize)
  390. err = o.Raw(sql, pars...).Find(&items).Error
  391. return
  392. }
  393. // GetMysteelChemicalIndexListGroupByUserId 根据指标id列表、用户分组获取指标信息
  394. func GetMysteelChemicalIndexListGroupByUserId(edbIdList []string) (items []*BaseFromMysteelChemicalIndex, err error) {
  395. num := len(edbIdList)
  396. if num <= 0 {
  397. return
  398. }
  399. o := global.DbMap[utils.DbNameIndex]
  400. sql := ` SELECT * FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_index_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY sys_user_id `
  401. err = o.Raw(sql, edbIdList).Find(&items).Error
  402. return
  403. }
  404. // GetMysteelChemicalIndexListByIndexId
  405. // @Description: 根据指标id列表获取列表信息
  406. // @param edbIdList
  407. // @return items
  408. // @return err
  409. func GetMysteelChemicalIndexListByIndexId(edbIdList []string) (items []*BaseFromMysteelChemicalIndex, err error) {
  410. num := len(edbIdList)
  411. if num <= 0 {
  412. return
  413. }
  414. o := global.DbMap[utils.DbNameIndex]
  415. sql := ` SELECT * FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_index_id in (` + utils.GetOrmInReplace(num) + `) `
  416. err = o.Raw(sql, edbIdList).Find(&items).Error
  417. return
  418. }
  419. // GetMysteelChemicalIndexListByUserId
  420. // @Description: 根据用户id列表获取列表信息
  421. // @param userIdList
  422. // @return items
  423. // @return err
  424. func GetMysteelChemicalIndexListByUserId(userIdList []int) (items []*BaseFromMysteelChemicalIndex, err error) {
  425. num := len(userIdList)
  426. if num <= 0 {
  427. return
  428. }
  429. o := global.DbMap[utils.DbNameIndex]
  430. sql := ` SELECT * FROM base_from_mysteel_chemical_index WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  431. err = o.Raw(sql, userIdList).Find(&items).Error
  432. return
  433. }
  434. // ModifyMysteelChemicalIndexUserIdByCodeList 根据指标code列表修改创建人
  435. func ModifyMysteelChemicalIndexUserIdByCodeList(edbIdList []string, userId int, userName string) (err error) {
  436. num := len(edbIdList)
  437. if num <= 0 {
  438. return
  439. }
  440. o := global.DbMap[utils.DbNameIndex]
  441. sql := `UPDATE base_from_mysteel_chemical_index SET sys_user_id=?,sys_user_real_name=? WHERE base_from_mysteel_chemical_index_id in (` + utils.GetOrmInReplace(num) + `) `
  442. err = o.Exec(sql, userId, userName, edbIdList).Error
  443. return
  444. }
  445. // ModifyMysteelChemicalIndexUserIdByOldUserId
  446. // @Description: 根据旧用户id修改新用户id
  447. // @author: Roc
  448. // @datetime 2024-03-25 17:59:08
  449. // @param oldUserId int
  450. // @param userId int
  451. // @param userName string
  452. // @return err error
  453. func ModifyMysteelChemicalIndexUserIdByOldUserId(oldUserIdList []int, userId int, userName string) (err error) {
  454. num := len(oldUserIdList)
  455. if num <= 0 {
  456. return
  457. }
  458. o := global.DbMap[utils.DbNameIndex]
  459. sql := `UPDATE base_from_mysteel_chemical_index SET sys_user_id=?,sys_user_real_name=? WHERE sys_user_id in (` + utils.GetOrmInReplace(num) + `) `
  460. err = o.Exec(sql, userId, userName, oldUserIdList).Error
  461. return
  462. }
  463. // GetMysteelChemicalIndexAdminList 获取所有指标创建人
  464. func GetMysteelChemicalIndexAdminList() (list []int, err error) {
  465. o := global.DbMap[utils.DbNameIndex]
  466. sql := ` SELECT sys_user_id FROM base_from_mysteel_chemical_index GROUP BY sys_user_id `
  467. err = o.Raw(sql).Find(&list).Error
  468. return
  469. }
  470. type TerminalNum struct {
  471. TerminalCode string `description:"terminal_code"`
  472. Num int `description:"num"`
  473. }
  474. // GetMysteelChemicalGroupTerminalNum 获取上海钢联指标的终端分布
  475. func GetMysteelChemicalGroupTerminalNum() (items []*TerminalNum, err error) {
  476. o := global.DbMap[utils.DbNameIndex]
  477. sql := ` SELECT terminal_code,count(1) num FROM base_from_mysteel_chemical_index GROUP BY terminal_code ORDER BY num ASC `
  478. err = o.Raw(sql).Find(&items).Error
  479. return
  480. }
  481. // BaseRefreshEdbInfo
  482. // @Description: 刷新配置的基础指标信息结构体
  483. type BaseRefreshEdbInfo struct {
  484. EdbInfoId int
  485. ClassifyId int `description:"上海钢联指标分类id"`
  486. IndexCode string `description:"指标编码"`
  487. IndexName string `description:"指标名称"`
  488. EndDate string `description:"最新日期"`
  489. EndValue string `description:"最新值"`
  490. SysUserId int `description:"创建人id"`
  491. SysUserRealName string `description:"创建人姓名"`
  492. Frequency string `description:"频度"`
  493. IsStop int `description:"是否停更:1:停更,0:未停更"`
  494. TerminalCode string `description:"终端编码"`
  495. RefreshTime string `description:"刷新时间"`
  496. }
  497. func (obj *BaseRefreshEdbInfo) AfterFind(tx *gorm.DB) (err error) {
  498. obj.EndDate = utils.GormDateStrToDateStr(obj.EndDate)
  499. return
  500. }
  501. // RefreshBaseEdbInfoResp
  502. // @Description: 刷新数据源的数据返回
  503. type RefreshBaseEdbInfoResp struct {
  504. Paging *paging.PagingItem
  505. List []*BaseRefreshEdbInfo
  506. }
  507. // GetMysteelChemicalBaseInfoList
  508. // @Description: 获取上海钢联数据列表
  509. // @author: Roc
  510. // @datetime 2024-01-10 14:28:35
  511. // @param condition string
  512. // @param pars []interface{}
  513. // @param orderBy string
  514. // @param startSize int
  515. // @param pageSize int
  516. // @return total int
  517. // @return items []*BaseRefreshEdbInfo
  518. // @return err error
  519. func GetMysteelChemicalBaseInfoList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRefreshEdbInfo, err error) {
  520. o := global.DbMap[utils.DbNameIndex]
  521. // 数量汇总
  522. totalSql := ` SELECT count(1) FROM base_from_mysteel_chemical_index WHERE 1=1 `
  523. if condition != "" {
  524. totalSql += condition
  525. }
  526. err = o.Raw(totalSql, pars...).Scan(&total).Error
  527. if err != nil {
  528. return
  529. }
  530. // 列表数据
  531. sql := ` SELECT base_from_mysteel_chemical_index_id as edb_info_id, base_from_mysteel_chemical_classify_id as classify_id,index_code,index_name,end_date,end_value,sys_user_id,sys_user_real_name,frequency,is_stop,terminal_code FROM base_from_mysteel_chemical_index WHERE 1=1 `
  532. if condition != "" {
  533. sql += condition
  534. }
  535. if orderBy != "" {
  536. sql += ` ORDER BY ` + orderBy
  537. } else {
  538. sql += ` ORDER BY base_from_mysteel_chemical_index_id ASC `
  539. }
  540. sql += ` LIMIT ?,? `
  541. pars = append(pars, startSize, pageSize)
  542. err = o.Raw(sql, pars...).Find(&items).Error
  543. return
  544. }
  545. // ModifyMysteelChemicalUpdateStatus
  546. // @Description: 修改上海钢联数据停更状态
  547. // @author: Roc
  548. // @datetime 2024-01-08 16:23:31
  549. // @param edbIdList []int
  550. // @param indexCodeList []string
  551. // @param isStop int
  552. // @return err error
  553. func ModifyMysteelChemicalUpdateStatus(edbIdList []int, indexCodeList []string, isStop int) (err error) {
  554. idNum := len(edbIdList)
  555. if idNum <= 0 {
  556. return
  557. }
  558. o := global.DbMap[utils.DbNameIndex].Begin()
  559. defer func() {
  560. if err != nil {
  561. _ = o.Rollback()
  562. return
  563. }
  564. _ = o.Commit()
  565. }()
  566. // 更改数据源的更新状态
  567. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = ? WHERE base_from_mysteel_chemical_index_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  568. err = o.Exec(sql, isStop, edbIdList).Error
  569. if err != nil {
  570. return
  571. }
  572. codeNum := len(indexCodeList)
  573. if codeNum <= 0 {
  574. // 需要通过指标id列表查找code列表
  575. sql := ` SELECT index_code FROM base_from_mysteel_chemical_index WHERE base_from_mysteel_chemical_index_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  576. err = o.Raw(sql, edbIdList).Find(&indexCodeList).Error
  577. if err != nil {
  578. return
  579. }
  580. }
  581. codeNum = len(indexCodeList)
  582. // 查出来的编码是空的话,那么就直接返回了
  583. if codeNum <= 0 {
  584. return
  585. }
  586. // 更改指标的更新状态
  587. sql = ` UPDATE edb_info SET no_update = ?, set_update_time=? WHERE source = ? AND sub_source= ? AND edb_code IN (` + utils.GetOrmInReplace(codeNum) + `) `
  588. err = o.Exec(sql, isStop, time.Now(), utils.DATA_SOURCE_MYSTEEL_CHEMICAL, 0, indexCodeList).Error
  589. if err != nil {
  590. return
  591. }
  592. return
  593. }
  594. // ModifyMysteelChemicalUpdateStatusByEdbInfoId
  595. // @Description: 修改单个上海钢联指标停更状态,同时停更依赖于该指标的计算指标
  596. // @author: Roc
  597. // @datetime 2024-01-08 16:23:31
  598. // @param edbIdList []int
  599. // @param indexCodeList []string
  600. // @param isStop int
  601. // @return err error
  602. func ModifyMysteelChemicalUpdateStatusByEdbInfoId(edbInfoId, isStop int, edbCode string, calculateEdbInfoIds []int) (err error) {
  603. o := global.DbMap[utils.DbNameIndex].Begin()
  604. defer func() {
  605. if err != nil {
  606. _ = o.Rollback()
  607. return
  608. }
  609. _ = o.Commit()
  610. }()
  611. // 更改数据源的更新状态
  612. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = ? WHERE index_code = ? `
  613. err = o.Exec(sql, isStop, edbCode).Error
  614. if err != nil {
  615. return
  616. }
  617. // 更改指标的更新状态
  618. sql = ` UPDATE edb_info SET no_update = ?, set_update_time=? WHERE source = ? AND sub_source= ? AND edb_info_id=? `
  619. err = o.Exec(sql, isStop, time.Now(), utils.DATA_SOURCE_MYSTEEL_CHEMICAL, 0, edbInfoId).Error
  620. if err != nil {
  621. return
  622. }
  623. if len(calculateEdbInfoIds) > 0 {
  624. // 批量更新相关联的指标ID
  625. sql = ` UPDATE edb_info SET no_update = ?, set_update_time=? WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) `
  626. err = o.Exec(sql, isStop, time.Now(), calculateEdbInfoIds).Error
  627. if err != nil {
  628. return
  629. }
  630. }
  631. return
  632. }
  633. func ModifyMysteelChemicalUpdateStatusByEdbInfoIds(edbInfoIds []int, isStop int, edbCodes []string, calculateEdbInfoIds []int) (err error) {
  634. o := global.DbMap[utils.DbNameIndex].Begin()
  635. defer func() {
  636. if err != nil {
  637. _ = o.Rollback()
  638. return
  639. }
  640. _ = o.Commit()
  641. }()
  642. // 更改数据源的更新状态
  643. sql := ` UPDATE base_from_mysteel_chemical_index SET is_stop = ? WHERE index_code IN (` + utils.GetOrmInReplace(len(edbCodes)) + `) `
  644. err = o.Exec(sql, isStop, edbCodes).Error
  645. if err != nil {
  646. return
  647. }
  648. // 更改指标的更新状态
  649. sql = ` UPDATE edb_info SET no_update = ?, set_update_time=? WHERE source = ? AND edb_info_id IN (` + utils.GetOrmInReplace(len(edbInfoIds)) + `) `
  650. err = o.Exec(sql, isStop, time.Now(), utils.DATA_SOURCE_MYSTEEL_CHEMICAL, edbInfoIds).Error
  651. if err != nil {
  652. return
  653. }
  654. if len(calculateEdbInfoIds) > 0 {
  655. // 批量更新相关联的指标ID
  656. sql = ` UPDATE edb_info SET no_update = ?, set_update_time=? WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) `
  657. err = o.Exec(sql, isStop, time.Now(), calculateEdbInfoIds).Error
  658. if err != nil {
  659. return
  660. }
  661. }
  662. return
  663. }
  664. // GetNotIsSupplierStopIndexByCodeList
  665. // @Description: 获取未被供应商停更的指标
  666. // @author: Roc
  667. // @datetime 2024-08-28 18:15:03
  668. // @param codeList []string
  669. // @param isStop int
  670. // @return items []*BaseFromMysteelChemicalIndex
  671. // @return err error
  672. func GetNotIsSupplierStopIndexByCodeList(codeList []string, isStop int) (items []*BaseFromMysteelChemicalIndex, err error) {
  673. num := len(codeList)
  674. if num <= 0 {
  675. return
  676. }
  677. o := global.DbMap[utils.DbNameIndex]
  678. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE is_supplier_stop = ? AND index_code in (` + utils.GetOrmInReplace(num) + `) `
  679. err = o.Raw(sql, isStop, codeList).Find(&items).Error
  680. return
  681. }
  682. type MysteelChemicalPageListResp struct {
  683. List []*MysteelChemicalList
  684. Paging *paging.PagingItem `description:"分页数据"`
  685. }
  686. // GetNoEdbMysteelChemicalIndexCount 获取未加入指标库的钢联原始指标-分页
  687. func GetNoEdbMysteelChemicalIndexCount(condition string, pars []interface{}) (count int, err error) {
  688. o := global.DbMap[utils.DbNameIndex]
  689. sql := fmt.Sprintf(`SELECT COUNT(1) FROM base_from_mysteel_chemical_index WHERE 1=1 AND index_code NOT IN (
  690. SELECT DISTINCT edb_code FROM edb_info WHERE source = %d
  691. ) `, utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  692. if condition != "" {
  693. sql += condition
  694. }
  695. err = o.Raw(sql, pars...).Scan(&count).Error
  696. return
  697. }
  698. // GetNoEdbMysteelChemicalIndexPageList 获取未加入指标库的钢联原始指标-分页
  699. func GetNoEdbMysteelChemicalIndexPageList(condition string, pars []interface{}, startSize, pageSize int) (items []*MysteelChemicalList, err error) {
  700. o := global.DbMap[utils.DbNameIndex]
  701. sql := fmt.Sprintf(`SELECT * FROM base_from_mysteel_chemical_index WHERE 1=1 AND index_code NOT IN (
  702. SELECT DISTINCT edb_code FROM edb_info WHERE source = %d
  703. ) `, utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
  704. if condition != "" {
  705. sql += condition
  706. }
  707. sql += ` ORDER BY base_from_mysteel_chemical_index_id ASC LIMIT ?,?`
  708. pars = append(pars, startSize, pageSize)
  709. err = o.Raw(sql, pars...).Find(&items).Error
  710. return
  711. }