manual_edb.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. // TargetDetailResp 指标数据结构体
  11. type TargetDetailResp struct {
  12. Detail *EdbInfoItem
  13. ClassifyList []*EdbdataClassify
  14. }
  15. // GetTargetByTradeCode
  16. // @Description: 根据手工指标编码获取指标信息
  17. // @author: Roc
  18. // @datetime 2024-07-16 14:18:10
  19. // @param tradeCode string
  20. // @return item *Edbinfo
  21. // @return err error
  22. func GetTargetByTradeCode(tradeCode string) (item *EdbInfoItem, err error) {
  23. sql := `SELECT * FROM edbinfo WHERE TRADE_CODE = ? `
  24. o := global.DbMap[utils.DbNameManualIndex]
  25. err = o.Raw(sql, tradeCode).First(&item).Error
  26. if err != nil {
  27. return
  28. }
  29. item.ConvDate()
  30. return
  31. }
  32. // GetCountManualUserClassify
  33. // @Description: 根据用户ID和分类ID获取关系数量
  34. // @author: Roc
  35. // @datetime 2024-07-16 14:27:58
  36. // @param sysUserId int
  37. // @param classifyId int
  38. // @return total int
  39. // @return err error
  40. func GetCountManualUserClassify(sysUserId, classifyId int) (total int, err error) {
  41. o := global.DbMap[utils.DbNameIndex]
  42. sql := `SELECT count(1) ct FROM manual_user_classify WHERE admin_id=? AND classify_id = ? `
  43. var totalNull sql2.NullInt64
  44. err = o.Raw(sql, sysUserId, classifyId).Scan(&totalNull).Error
  45. if err != nil {
  46. return
  47. }
  48. if totalNull.Valid {
  49. total = int(totalNull.Int64)
  50. }
  51. return
  52. }
  53. func GetManualClassifyByClassifyId(classifyId int) (item *EdbdataClassify, err error) {
  54. o := global.DbMap[utils.DbNameManualIndex]
  55. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE classify_id = ? `
  56. err = o.Raw(sql, classifyId).First(&item).Error
  57. return
  58. }
  59. // GetEdbDataListByCode
  60. // @Description: 通过指标ID获取所有数据
  61. // @author: Roc
  62. // @datetime 2024-07-16 15:32:41
  63. // @param tradeCode string
  64. // @return items []*Edbdata
  65. // @return err error
  66. func GetEdbDataListByCode(tradeCode string) (items []*Edbdata, err error) {
  67. sql := ` SELECT TRADE_CODE,DT,round(CLOSE,4) CLOSE,modify_time FROM edbdata WHERE TRADE_CODE = ? GROUP BY TRADE_CODE,DT ORDER BY DT DESC `
  68. o := global.DbMap[utils.DbNameManualIndex]
  69. err = o.Raw(sql, tradeCode).Find(&items).Error
  70. return
  71. }
  72. type EdbInfoListItem struct {
  73. TradeCode string `gorm:"primaryKey;autoIncrement:false;column:TRADE_CODE" description:"指标code"`
  74. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  75. Unit string `gorm:"column:UNIT;" description:"单位"`
  76. Remark string `gorm:"column:REMARK;" description:"备注"`
  77. Frequency string `description:"频度"`
  78. ClassifyId int `description:"分类id"`
  79. ClassifyName string `description:"分类名称"`
  80. CreateDate string `description:"创建时间"`
  81. UserId int `description:"录入用户id"`
  82. NoticeTime string `description:"通知时间"`
  83. Mobile string `description:"录入者手机号"`
  84. ModifyDate string `description:"待更新日期"`
  85. ModifyTime string `description:"数据更新时间"`
  86. Status string `description:"状态:未完成/完成"`
  87. UniqueCode string
  88. IsJoinEdb int8 `description:"指标库是否已添加:0-否;1-是"`
  89. UserName string `description:"录入用户名称"`
  90. StartDate string `description:"数据开始日期"`
  91. EndDate string `description:"数据结束日期"`
  92. LatestValue float64 `description:"指标最新值"`
  93. NextDateTime string `description:"下期时间"`
  94. Source int `description:"来源"`
  95. SourceName string `description:"数据源名称"`
  96. SearchText string `description:"搜索结果(含高亮)"`
  97. ClassifyUniqueCode string `description:"分类唯一编码(前端定位用)"`
  98. }
  99. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  100. func (m *EdbInfoListItem) AfterFind(db *gorm.DB) (err error) {
  101. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  102. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  103. m.ModifyDate = utils.GormDateStrToDateStr(m.ModifyDate)
  104. m.NextDateTime = utils.GormDateStrToDateStr(m.NextDateTime)
  105. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  106. m.CreateDate = utils.GormDateStrToDateTimeStr(m.CreateDate)
  107. return
  108. }
  109. // EdbListResp 指标数据结构体
  110. type EdbListResp struct {
  111. List []*EdbInfoListItem
  112. Paging *paging.PagingItem `description:"分页数据"`
  113. }
  114. // GetEdbInfoList
  115. // @Description: 根据条件获取指标列表
  116. // @author: Roc
  117. // @datetime 2024-07-17 10:34:05
  118. // @param condition string
  119. // @param pars []interface{}
  120. // @return items []*Edbinfo
  121. // @return err error
  122. func GetEdbInfoList(condition string, pars []interface{}, startSize, pageSize int) (items []*EdbInfoListItem, err error) {
  123. o := global.DbMap[utils.DbNameManualIndex]
  124. sql := `SELECT DISTINCT a.* FROM edbinfo AS a WHERE a.classify_id > 0 `
  125. if condition != "" {
  126. sql += condition
  127. }
  128. sql += ` ORDER BY a.create_date DESC `
  129. if pageSize > 0 {
  130. sql += ` LIMIT ?,? `
  131. pars = append(pars, startSize, pageSize)
  132. }
  133. err = o.Raw(sql, pars...).Find(&items).Error
  134. return
  135. }
  136. // GetEdbInfoSortList
  137. // @Description: 根据条件获取指标列表 排序
  138. // @author: gmy
  139. // @datetime 2024-09-05 15:25:05
  140. // @param condition string
  141. // @param pars []interface{}
  142. // @return items []*Edbinfo
  143. // @return err error
  144. func GetEdbInfoSortList(condition string, pars []interface{}, startSize, pageSize int, orderField, orderType string) (items []*EdbInfoListItem, err error) {
  145. o := global.DbMap[utils.DbNameManualIndex]
  146. sql := `SELECT a.* FROM edbinfo AS a WHERE 1=1 `
  147. if condition != "" {
  148. sql += condition
  149. }
  150. if orderField == "" {
  151. orderField = "end_date"
  152. }
  153. if orderType == "" {
  154. orderType = "DESC"
  155. }
  156. //sql += ` ORDER BY a.` + orderField + ` ` + orderType
  157. sql += ` ORDER BY ? ?`
  158. pars = append(pars, orderField)
  159. pars = append(pars, orderType)
  160. if pageSize > 0 {
  161. sql += ` LIMIT ?,? `
  162. pars = append(pars, startSize, pageSize)
  163. }
  164. err = o.Raw(sql, pars...).Find(&items).Error
  165. return
  166. }
  167. // GetCountEdbInfoList
  168. // @Description: 根据条件获取指标数量
  169. // @author: Roc
  170. // @datetime 2024-07-17 14:51:00
  171. // @param condition string
  172. // @param pars []interface{}
  173. // @return total int
  174. // @return err error
  175. func GetCountEdbInfoList(condition string, pars []interface{}) (total int, err error) {
  176. o := global.DbMap[utils.DbNameManualIndex]
  177. sql := `SELECT COUNT(1) ct FROM edbinfo AS a WHERE a.classify_id > 0 `
  178. if condition != "" {
  179. sql += condition
  180. }
  181. sql += ` ORDER BY a.create_date DESC `
  182. err = o.Raw(sql, pars...).Scan(&total).Error
  183. return
  184. }
  185. // OnlyMultiAddEdbdata
  186. // @Description: 批量添加指标数据
  187. // @author: Roc
  188. // @datetime 2024-07-18 16:48:55
  189. // @param items []*Edbdata
  190. // @return err error
  191. func OnlyMultiAddEdbdata(items []*Edbdata) (err error) {
  192. o := global.DbMap[utils.DbNameManualIndex]
  193. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  194. return
  195. }
  196. // DelEdbdataByCodeAndDateList
  197. // @Description: 根据指标编码和日期列表批量删除指标的明细数据
  198. // @author: Roc
  199. // @datetime 2024-07-18 16:54:27
  200. // @param tradeCode string
  201. // @param dateList []string
  202. // @return err error
  203. func DelEdbdataByCodeAndDateList(tradeCode string, dateList []string) (err error) {
  204. num := len(dateList)
  205. if num <= 0 {
  206. return
  207. }
  208. o := global.DbMap[utils.DbNameManualIndex]
  209. sql := `delete from edbdata WHERE TRADE_CODE=? AND DT in (?) `
  210. err = o.Exec(sql, tradeCode, dateList).Error
  211. return
  212. }
  213. // UpdateManualIsJoinEdbStatus
  214. // @Description: 根据手工数据加入指标的状态
  215. // @author: Roc
  216. // @datetime 2024-07-22 11:29:13
  217. // @param edbCode string
  218. // @param isJoinEdb int 是否加入指标库
  219. // @return err error
  220. func UpdateManualIsJoinEdbStatus(edbCode string, isJoinEdb int8) (err error) {
  221. o := global.DbMap[utils.DbNameManualIndex]
  222. sql := ` UPDATE edbinfo SET is_join_edb = ? WHERE TRADE_CODE =? `
  223. err = o.Exec(sql, isJoinEdb, edbCode).Error
  224. return
  225. }
  226. // BatchManualEdbReq 指标数据结构体
  227. type BatchManualEdbReq struct {
  228. //IsJoinEdb int `form:"IsJoinEdb" description:"是否加到指标库,0:未加到指标库"`
  229. FrequencyList []string `description:"频度;枚举值:日度、周度、月度、季度、半年度、年度"`
  230. Keyword string `description:"关键字"`
  231. ClassifyIdList []int `description:"所选品种id列表"`
  232. UserIdList []int `description:"所选用户id列表"`
  233. ListAll bool `form:"ListAll" json:"ListAll" description:"列表全选"`
  234. TradeCodeList []string `form:"TradeCodeList" json:"TradeCodeList" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  235. PageSize int `form:"PageSize" json:"PageSize"`
  236. CurrentIndex int `form:"CurrentIndex" form:"CurrentIndex"`
  237. }
  238. // DelManualIndexByCodeList
  239. // @Description: 根据指标编码列表删除指标
  240. // @author: Roc
  241. // @datetime 2024-07-30 14:10:12
  242. // @param codeList []string
  243. // @return err error
  244. func DelManualIndexByCodeList(codeList []string) (err error) {
  245. num := len(codeList)
  246. if num <= 0 {
  247. return
  248. }
  249. to := global.DbMap[utils.DbNameManualIndex].Begin()
  250. defer func() {
  251. if err != nil {
  252. _ = to.Rollback()
  253. } else {
  254. _ = to.Commit()
  255. }
  256. }()
  257. // 删除指标
  258. sql := `DELETE FROM edbinfo WHERE TRADE_CODE in (?) `
  259. err = to.Exec(sql, codeList).Error
  260. if err != nil {
  261. return
  262. }
  263. // 删除指标数据
  264. sql = `DELETE FROM edbdata WHERE TRADE_CODE in (?) `
  265. err = to.Exec(sql, codeList).Error
  266. if err != nil {
  267. return
  268. }
  269. return
  270. }
  271. // GetEdbinfoListBySecNameList
  272. // @Description: 根据指标名称获取列表数据
  273. // @author: Roc
  274. // @datetime 2024-07-30 19:14:25
  275. // @param secNameList []string
  276. // @return item []*Edbinfo
  277. // @return err error
  278. func GetEdbinfoListBySecNameList(secNameList []string) (items []*Edbinfo, err error) {
  279. num := len(secNameList)
  280. if num <= 0 {
  281. return
  282. }
  283. sql := `SELECT * FROM edbinfo WHERE SEC_NAME in (?) AND REMARK='手动' `
  284. o := global.DbMap[utils.DbNameManualIndex]
  285. err = o.Raw(sql, secNameList).Find(&items).Error
  286. return
  287. }
  288. // GetTargetsDataListByCodeList
  289. // @Description: 根据code列表获取指标数据列表
  290. // @author: Roc
  291. // @datetime 2024-07-30 19:19:36
  292. // @param tradeCodeList []string
  293. // @return items []*Edbdata
  294. // @return err error
  295. func GetTargetsDataListByCodeList(tradeCodeList []string) (items []*Edbdata, err error) {
  296. num := len(tradeCodeList)
  297. if num <= 0 {
  298. return
  299. }
  300. o := global.DbMap[utils.DbNameManualIndex]
  301. sql := `SELECT * FROM edbdata WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + ` ) ORDER BY DT ASC `
  302. err = o.Raw(sql, tradeCodeList).Find(&items).Error
  303. return
  304. }
  305. // EdbinfoOpRecord
  306. // @Description: 手工数据的操作日志
  307. type EdbinfoOpRecord struct {
  308. EdbinfoOpRecordId int `gorm:"column:edbinfo_op_record_id"`
  309. TradeCode string `gorm:"column:TRADE_CODE" description:"指标编码"`
  310. Remark string `gorm:"column:remark" description:"操作信息"`
  311. UserId int `gorm:"column:user_id" description:"用户id"`
  312. UserName string `gorm:"column:user_name" description:"用户姓名"`
  313. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  314. }
  315. // Remark备注:
  316. // 1、创建指标
  317. // 2、编辑指标
  318. // 3、更新数据
  319. // 4、数据资产由<xxx>转移给<xxx>
  320. // Create
  321. // @Description: 添加手工数据的操作日志
  322. // @author: Roc
  323. // @receiver m
  324. // @datetime 2024-07-30 16:25:55
  325. // @return err error
  326. func (m *EdbinfoOpRecord) Create() (err error) {
  327. o := global.DbMap[utils.DbNameManualIndex]
  328. err = o.Create(m).Error
  329. return
  330. }
  331. // MulCreate
  332. // @Description: 批量添加手工数据的操作日志
  333. // @author: Roc
  334. // @receiver m
  335. // @datetime 2024-08-01 11:05:02
  336. // @param list []*EdbinfoOpRecord
  337. // @return err error
  338. func (m *EdbinfoOpRecord) MulCreate(list []*EdbinfoOpRecord) (err error) {
  339. o := global.DbMap[utils.DbNameManualIndex]
  340. err = o.CreateInBatches(list, utils.MultiAddNum).Error
  341. return
  342. }
  343. // EdbinfoOpRecordItem
  344. // @Description: 手工数据的操作日志
  345. type EdbinfoOpRecordItem struct {
  346. TradeCode string `gorm:"primaryKey;autoIncrement:false;column:TRADE_CODE" description:"指标编码"`
  347. Remark string `gorm:"column:remark" description:"操作信息"`
  348. UserId int `gorm:"column:user_id" description:"用户id"`
  349. UserName string `gorm:"column:user_name" description:"用户姓名"`
  350. CreateTime string `gorm:"column:create_time" description:"创建时间"`
  351. }
  352. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  353. func (m *EdbinfoOpRecordItem) AfterFind(db *gorm.DB) (err error) {
  354. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  355. return
  356. }
  357. // EdbinfoOpRecordListResp 指标数据结构体
  358. type EdbinfoOpRecordListResp struct {
  359. List []*EdbinfoOpRecordItem
  360. Paging *paging.PagingItem `description:"分页数据"`
  361. }
  362. // GetEdbinfoOpRecordPageList
  363. // @Description: 根据指标编码获取手工数据的操作日志
  364. // @author: Roc
  365. // @datetime 2024-07-30 17:32:33
  366. // @param tradeCode string
  367. // @param startSize int
  368. // @param total int
  369. // @return pageSize int
  370. // @return items []*EdbinfoOpRecordItem
  371. // @return err error
  372. func GetEdbinfoOpRecordPageList(tradeCode string, startSize, pageSize int) (total int, items []*EdbinfoOpRecordItem, err error) {
  373. o := global.DbMap[utils.DbNameManualIndex]
  374. sql := `SELECT count(1) FROM edbinfo_op_record AS a WHERE TRADE_CODE = ? `
  375. var totalNull sql2.NullInt64
  376. err = o.Raw(sql, tradeCode).Scan(&totalNull).Error
  377. if err != nil {
  378. return
  379. }
  380. if totalNull.Valid {
  381. total = int(totalNull.Int64)
  382. }
  383. sql = `SELECT a.* FROM edbinfo_op_record AS a WHERE TRADE_CODE = ? ORDER BY a.create_time DESC LIMIT ?,?`
  384. err = o.Raw(sql, tradeCode, startSize, pageSize).Find(&items).Error
  385. return
  386. }
  387. // GetManualEdbCountByCondition
  388. // @Description: 根据获取手工数据条数
  389. // @author: Roc
  390. // @datetime 2024-08-05 09:37:16
  391. // @param condition string
  392. // @param pars []interface{}
  393. // @return count int
  394. // @return err error
  395. func GetManualEdbCountByCondition(condition string, pars []interface{}) (count int, err error) {
  396. o := global.DbMap[utils.DbNameManualIndex]
  397. sql := ` SELECT COUNT(1) AS count FROM edbinfo WHERE 1=1 `
  398. if condition != "" {
  399. sql += condition
  400. }
  401. var countNull sql2.NullInt64
  402. err = o.Raw(sql, pars...).Scan(&countNull).Error
  403. if err != nil {
  404. return
  405. }
  406. if countNull.Valid {
  407. count = int(countNull.Int64)
  408. }
  409. return
  410. }
  411. // DelEdbinfoOpRecordByTradeCode
  412. // @Description: 根据指标编码删除操作日志
  413. // @author: Roc
  414. // @datetime 2024-08-05 10:27:29
  415. // @param tradeCode string
  416. // @return err error
  417. func DelEdbinfoOpRecordByTradeCode(tradeCode string) (err error) {
  418. o := global.DbMap[utils.DbNameManualIndex]
  419. sql := ` DELETE FROM edbinfo_op_record WHERE TRADE_CODE = ? `
  420. err = o.Exec(sql, tradeCode).Error
  421. return
  422. }