manual_edb.go 15 KB

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