manual_edb.go 14 KB

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