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. //
  159. //pars = append(pars, orderField)
  160. //pars = append(pars, orderType)
  161. if pageSize > 0 {
  162. sql += ` LIMIT ?,? `
  163. pars = append(pars, startSize, pageSize)
  164. }
  165. err = o.Raw(sql, pars...).Find(&items).Error
  166. return
  167. }
  168. // GetCountEdbInfoList
  169. // @Description: 根据条件获取指标数量
  170. // @author: Roc
  171. // @datetime 2024-07-17 14:51:00
  172. // @param condition string
  173. // @param pars []interface{}
  174. // @return total int
  175. // @return err error
  176. func GetCountEdbInfoList(condition string, pars []interface{}) (total int, err error) {
  177. o := global.DbMap[utils.DbNameManualIndex]
  178. sql := `SELECT COUNT(1) ct FROM edbinfo AS a WHERE a.classify_id > 0 `
  179. if condition != "" {
  180. sql += condition
  181. }
  182. sql += ` ORDER BY a.create_date DESC `
  183. err = o.Raw(sql, pars...).Scan(&total).Error
  184. return
  185. }
  186. // OnlyMultiAddEdbdata
  187. // @Description: 批量添加指标数据
  188. // @author: Roc
  189. // @datetime 2024-07-18 16:48:55
  190. // @param items []*Edbdata
  191. // @return err error
  192. func OnlyMultiAddEdbdata(items []*Edbdata) (err error) {
  193. o := global.DbMap[utils.DbNameManualIndex]
  194. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  195. return
  196. }
  197. // DelEdbdataByCodeAndDateList
  198. // @Description: 根据指标编码和日期列表批量删除指标的明细数据
  199. // @author: Roc
  200. // @datetime 2024-07-18 16:54:27
  201. // @param tradeCode string
  202. // @param dateList []string
  203. // @return err error
  204. func DelEdbdataByCodeAndDateList(tradeCode string, dateList []string) (err error) {
  205. num := len(dateList)
  206. if num <= 0 {
  207. return
  208. }
  209. o := global.DbMap[utils.DbNameManualIndex]
  210. sql := `delete from edbdata WHERE TRADE_CODE=? AND DT in (?) `
  211. err = o.Exec(sql, tradeCode, dateList).Error
  212. return
  213. }
  214. // UpdateManualIsJoinEdbStatus
  215. // @Description: 根据手工数据加入指标的状态
  216. // @author: Roc
  217. // @datetime 2024-07-22 11:29:13
  218. // @param edbCode string
  219. // @param isJoinEdb int 是否加入指标库
  220. // @return err error
  221. func UpdateManualIsJoinEdbStatus(edbCode string, isJoinEdb int8) (err error) {
  222. o := global.DbMap[utils.DbNameManualIndex]
  223. sql := ` UPDATE edbinfo SET is_join_edb = ? WHERE TRADE_CODE =? `
  224. err = o.Exec(sql, isJoinEdb, edbCode).Error
  225. return
  226. }
  227. // BatchManualEdbReq 指标数据结构体
  228. type BatchManualEdbReq struct {
  229. //IsJoinEdb int `form:"IsJoinEdb" description:"是否加到指标库,0:未加到指标库"`
  230. FrequencyList []string `description:"频度;枚举值:日度、周度、月度、季度、半年度、年度"`
  231. Keyword string `description:"关键字"`
  232. ClassifyIdList []int `description:"所选品种id列表"`
  233. UserIdList []int `description:"所选用户id列表"`
  234. ListAll bool `form:"ListAll" json:"ListAll" description:"列表全选"`
  235. TradeCodeList []string `form:"TradeCodeList" json:"TradeCodeList" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  236. PageSize int `form:"PageSize" json:"PageSize"`
  237. CurrentIndex int `form:"CurrentIndex" form:"CurrentIndex"`
  238. }
  239. // DelManualIndexByCodeList
  240. // @Description: 根据指标编码列表删除指标
  241. // @author: Roc
  242. // @datetime 2024-07-30 14:10:12
  243. // @param codeList []string
  244. // @return err error
  245. func DelManualIndexByCodeList(codeList []string) (err error) {
  246. num := len(codeList)
  247. if num <= 0 {
  248. return
  249. }
  250. to := global.DbMap[utils.DbNameManualIndex].Begin()
  251. defer func() {
  252. if err != nil {
  253. _ = to.Rollback()
  254. } else {
  255. _ = to.Commit()
  256. }
  257. }()
  258. // 删除指标
  259. sql := `DELETE FROM edbinfo WHERE TRADE_CODE in (?) `
  260. err = to.Exec(sql, codeList).Error
  261. if err != nil {
  262. return
  263. }
  264. // 删除指标数据
  265. sql = `DELETE FROM edbdata WHERE TRADE_CODE in (?) `
  266. err = to.Exec(sql, codeList).Error
  267. if err != nil {
  268. return
  269. }
  270. return
  271. }
  272. // GetEdbinfoListBySecNameList
  273. // @Description: 根据指标名称获取列表数据
  274. // @author: Roc
  275. // @datetime 2024-07-30 19:14:25
  276. // @param secNameList []string
  277. // @return item []*Edbinfo
  278. // @return err error
  279. func GetEdbinfoListBySecNameList(secNameList []string) (items []*Edbinfo, err error) {
  280. num := len(secNameList)
  281. if num <= 0 {
  282. return
  283. }
  284. sql := `SELECT * FROM edbinfo WHERE SEC_NAME in (?) AND REMARK='手动' `
  285. o := global.DbMap[utils.DbNameManualIndex]
  286. err = o.Raw(sql, secNameList).Find(&items).Error
  287. return
  288. }
  289. // GetTargetsDataListByCodeList
  290. // @Description: 根据code列表获取指标数据列表
  291. // @author: Roc
  292. // @datetime 2024-07-30 19:19:36
  293. // @param tradeCodeList []string
  294. // @return items []*Edbdata
  295. // @return err error
  296. func GetTargetsDataListByCodeList(tradeCodeList []string) (items []*Edbdata, err error) {
  297. num := len(tradeCodeList)
  298. if num <= 0 {
  299. return
  300. }
  301. o := global.DbMap[utils.DbNameManualIndex]
  302. sql := `SELECT * FROM edbdata WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + ` ) ORDER BY DT ASC `
  303. err = o.Raw(sql, tradeCodeList).Find(&items).Error
  304. return
  305. }
  306. // EdbinfoOpRecord
  307. // @Description: 手工数据的操作日志
  308. type EdbinfoOpRecord struct {
  309. EdbinfoOpRecordId int `gorm:"column:edbinfo_op_record_id"`
  310. TradeCode string `gorm:"column:TRADE_CODE" description:"指标编码"`
  311. Remark string `gorm:"column:remark" description:"操作信息"`
  312. UserId int `gorm:"column:user_id" description:"用户id"`
  313. UserName string `gorm:"column:user_name" description:"用户姓名"`
  314. CreateTime time.Time `gorm:"column:create_time" description:"创建时间"`
  315. }
  316. // Remark备注:
  317. // 1、创建指标
  318. // 2、编辑指标
  319. // 3、更新数据
  320. // 4、数据资产由<xxx>转移给<xxx>
  321. // Create
  322. // @Description: 添加手工数据的操作日志
  323. // @author: Roc
  324. // @receiver m
  325. // @datetime 2024-07-30 16:25:55
  326. // @return err error
  327. func (m *EdbinfoOpRecord) Create() (err error) {
  328. o := global.DbMap[utils.DbNameManualIndex]
  329. err = o.Create(m).Error
  330. return
  331. }
  332. // MulCreate
  333. // @Description: 批量添加手工数据的操作日志
  334. // @author: Roc
  335. // @receiver m
  336. // @datetime 2024-08-01 11:05:02
  337. // @param list []*EdbinfoOpRecord
  338. // @return err error
  339. func (m *EdbinfoOpRecord) MulCreate(list []*EdbinfoOpRecord) (err error) {
  340. o := global.DbMap[utils.DbNameManualIndex]
  341. err = o.CreateInBatches(list, utils.MultiAddNum).Error
  342. return
  343. }
  344. // EdbinfoOpRecordItem
  345. // @Description: 手工数据的操作日志
  346. type EdbinfoOpRecordItem struct {
  347. TradeCode string `gorm:"primaryKey;autoIncrement:false;column:TRADE_CODE" description:"指标编码"`
  348. Remark string `gorm:"column:remark" description:"操作信息"`
  349. UserId int `gorm:"column:user_id" description:"用户id"`
  350. UserName string `gorm:"column:user_name" description:"用户姓名"`
  351. CreateTime string `gorm:"column:create_time" description:"创建时间"`
  352. }
  353. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  354. func (m *EdbinfoOpRecordItem) AfterFind(db *gorm.DB) (err error) {
  355. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  356. return
  357. }
  358. // EdbinfoOpRecordListResp 指标数据结构体
  359. type EdbinfoOpRecordListResp struct {
  360. List []*EdbinfoOpRecordItem
  361. Paging *paging.PagingItem `description:"分页数据"`
  362. }
  363. // GetEdbinfoOpRecordPageList
  364. // @Description: 根据指标编码获取手工数据的操作日志
  365. // @author: Roc
  366. // @datetime 2024-07-30 17:32:33
  367. // @param tradeCode string
  368. // @param startSize int
  369. // @param total int
  370. // @return pageSize int
  371. // @return items []*EdbinfoOpRecordItem
  372. // @return err error
  373. func GetEdbinfoOpRecordPageList(tradeCode string, startSize, pageSize int) (total int, items []*EdbinfoOpRecordItem, err error) {
  374. o := global.DbMap[utils.DbNameManualIndex]
  375. sql := `SELECT count(1) FROM edbinfo_op_record AS a WHERE TRADE_CODE = ? `
  376. var totalNull sql2.NullInt64
  377. err = o.Raw(sql, tradeCode).Scan(&totalNull).Error
  378. if err != nil {
  379. return
  380. }
  381. if totalNull.Valid {
  382. total = int(totalNull.Int64)
  383. }
  384. sql = `SELECT a.* FROM edbinfo_op_record AS a WHERE TRADE_CODE = ? ORDER BY a.create_time DESC LIMIT ?,?`
  385. err = o.Raw(sql, tradeCode, startSize, pageSize).Find(&items).Error
  386. return
  387. }
  388. // GetManualEdbCountByCondition
  389. // @Description: 根据获取手工数据条数
  390. // @author: Roc
  391. // @datetime 2024-08-05 09:37:16
  392. // @param condition string
  393. // @param pars []interface{}
  394. // @return count int
  395. // @return err error
  396. func GetManualEdbCountByCondition(condition string, pars []interface{}) (count int, err error) {
  397. o := global.DbMap[utils.DbNameManualIndex]
  398. sql := ` SELECT COUNT(1) AS count FROM edbinfo WHERE 1=1 `
  399. if condition != "" {
  400. sql += condition
  401. }
  402. var countNull sql2.NullInt64
  403. err = o.Raw(sql, pars...).Scan(&countNull).Error
  404. if err != nil {
  405. return
  406. }
  407. if countNull.Valid {
  408. count = int(countNull.Int64)
  409. }
  410. return
  411. }
  412. // DelEdbinfoOpRecordByTradeCode
  413. // @Description: 根据指标编码删除操作日志
  414. // @author: Roc
  415. // @datetime 2024-08-05 10:27:29
  416. // @param tradeCode string
  417. // @return err error
  418. func DelEdbinfoOpRecordByTradeCode(tradeCode string) (err error) {
  419. o := global.DbMap[utils.DbNameManualIndex]
  420. sql := ` DELETE FROM edbinfo_op_record WHERE TRADE_CODE = ? `
  421. err = o.Exec(sql, tradeCode).Error
  422. return
  423. }