target.go 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "gorm.io/gorm"
  8. "sort"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. )
  14. type DataList struct {
  15. TradeCode string `gorm:"column:TRADE_CODE" description:"指标编码"`
  16. SecName string `gorm:"column:SEC_NAME" description:"指标名称"`
  17. Unit string `gorm:"column:UNIT" description:"单位"`
  18. Remark string `gorm:"column(REMARK)" description:"备注"`
  19. Frequency string `description:"频度"`
  20. ClassifyId int `description:"分类id"`
  21. ClassifyName string `description:"分类名称"`
  22. Dt string `gorm:"column:DT" description:"录入日期"`
  23. Close float64 `gorm:"column:CLOSE" description:"录入值"`
  24. ModifyTime string `description:"修改时间"`
  25. }
  26. type DataListResp struct {
  27. List []*DataList
  28. Paging *paging.PagingItem `description:"分页数据"`
  29. }
  30. func GetDataList(condition string, pars []interface{}, startSize, pageSize int) (items []*DataList, err error) {
  31. sql := `select a.TRADE_CODE,a.SEC_NAME,a.UNIT,a.frequency,a.classify_id,b.classify_name,c.DT,c.CLOSE,c.modify_time FROM edbdata AS c
  32. inner join edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  33. left join edbdata_classify AS b ON a.classify_id=b.classify_id
  34. where a.classify_id>0`
  35. if condition != "" {
  36. sql += condition
  37. }
  38. sql += ` order by c.DT desc limit ?,? `
  39. o := global.DbMap[utils.DbNameManualIndex]
  40. pars = append(pars, startSize, pageSize)
  41. err = o.Raw(sql, pars...).Find(&items).Error
  42. return
  43. }
  44. func GetDataListCount(condition string, pars []interface{}) (count int, err error) {
  45. sql := ` select count(1) as count FROM edbdata AS c
  46. inner join edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  47. left join edbdata_classify AS b ON a.classify_id=b.classify_id
  48. where a.classify_id>0 `
  49. if condition != "" {
  50. sql += condition
  51. }
  52. o := global.DbMap[utils.DbNameManualIndex]
  53. var countNull sql2.NullInt64
  54. err = o.Raw(sql, pars...).Scan(&countNull).Error
  55. if err != nil {
  56. return
  57. }
  58. if countNull.Valid {
  59. count = int(countNull.Int64)
  60. }
  61. return
  62. }
  63. type DataAddReq struct {
  64. TradeCode string `description:"指标唯一编码"`
  65. CreateDate string `description:"创建日期"`
  66. Close string `description:"录入值"`
  67. }
  68. type Edbdata struct {
  69. TradeCode string `gorm:"column:TRADE_CODE;pk" description:"指标编码"`
  70. Dt string `gorm:"column:DT" description:"日期"`
  71. Close string `gorm:"column:CLOSE" description:"值"`
  72. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  73. }
  74. type EdbDataNextDateTime struct {
  75. TradeCode string `gorm:"column:TRADE_CODE;pk" description:"指标编码"`
  76. Dt string `gorm:"column:DT" description:"日期"`
  77. Close string `gorm:"column:CLOSE" description:"值"`
  78. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  79. NextDateTime string `description:"下期日期"`
  80. }
  81. func GetDataInfo(tradeCode, creteDate string) (item *Edbdata, err error) {
  82. sql := " SELECT * FROM edbdata WHERE TRADE_CODE=? AND DT=? "
  83. o := global.DbMap[utils.DbNameManualIndex]
  84. err = o.Raw(sql, tradeCode, creteDate).First(&item).Error
  85. return
  86. }
  87. func AddEdbdata(item *Edbdata) (lastId int64, err error) {
  88. o := global.DbMap[utils.DbNameManualIndex]
  89. err = o.Create(item).Error
  90. return
  91. }
  92. type DataEditReq struct {
  93. TradeCode string `description:"指标唯一编码"`
  94. CreateDate string `description:"创建日期"`
  95. Close interface{} `description:"录入值"`
  96. OldCreateDate string `description:"旧的录入日期"`
  97. }
  98. // BatchDataEditReq 批量修改指标
  99. type BatchDataEditReq struct {
  100. OldCreateDate string `description:"旧的录入日期"`
  101. CreateDate string `description:"新的录入日期"`
  102. List []DataEditReq `description:"需要修改的数据"`
  103. }
  104. // EditEdbdata 编辑数据
  105. func EditEdbdata(item *Edbdata) (err error) {
  106. o := global.DbMap[utils.DbNameManualIndex]
  107. sql := ` UPDATE edbdata SET CLOSE = ?,modify_time=NOW() WHERE TRADE_CODE = ? AND DT = ? `
  108. err = o.Exec(sql, item.Close, item.TradeCode, item.Dt).Error
  109. return
  110. }
  111. type EdbdataDeleteRecord struct {
  112. Id int `gorm:"column:id;primaryKey;autoIncrement"`
  113. TradeCode string `gorm:"column:TRADE_CODE" description:"指标编码"`
  114. Dt string `gorm:"column:DT" description:"日期"`
  115. Close string `gorm:"column:CLOSE" description:"值"`
  116. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  117. CreateTime time.Time
  118. SysUserId int
  119. }
  120. func AddEdbdataDeleteRecord(item *EdbdataDeleteRecord) (lastId int64, err error) {
  121. o := global.DbMap[utils.DbNameManualIndex]
  122. err = o.Create(item).Error
  123. if err != nil {
  124. return
  125. }
  126. lastId = int64(item.Id)
  127. return
  128. }
  129. // DeleteEdbData 根据指标code和日期删除数据
  130. func DeleteEdbData(tradeCode, dt string) (err error) {
  131. o := global.DbMap[utils.DbNameManualIndex]
  132. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? AND DT = ? `
  133. err = o.Exec(sql, tradeCode, dt).Error
  134. return
  135. }
  136. // DeleteAllEdbData 根据指标code删除数据
  137. func DeleteAllEdbData(tradeCode string) (err error) {
  138. o := global.DbMap[utils.DbNameManualIndex]
  139. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? `
  140. err = o.Exec(sql, tradeCode).Error
  141. return
  142. }
  143. type Edbinfo struct {
  144. TradeCode string `gorm:"column:TRADE_CODE;autoIncrement:false;primaryKey" description:"指标code"`
  145. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  146. Unit string `gorm:"column:UNIT;" description:"单位"`
  147. Remark string `gorm:"column:REMARK;" description:"备注"`
  148. Frequency string `gorm:"column:frequency" description:"频度"`
  149. ClassifyId int `gorm:"column:classify_id" description:"分类id"`
  150. ClassifyName string `gorm:"-" description:"分类名称"`
  151. CreateDate string `gorm:"column:create_date" description:"创建时间"`
  152. UserId int `gorm:"column:user_id" description:"录入用户id"`
  153. UserName string `gorm:"column:user_name" description:"录入用户名称"`
  154. NoticeTime string `gorm:"column:notice_time" description:"通知时间"`
  155. Mobile string `gorm:"column:mobile" description:"录入者手机号"`
  156. Sort int `gorm:"column:sort" description:"排序"`
  157. ModifyTime string `description:"最近一次更新时间"`
  158. IsJoinEdb int8 `description:"指标库是否已添加:0-否;1-是"`
  159. StartDate string `description:"数据开始日期"`
  160. EndDate string `description:"数据结束日期"`
  161. LatestValue float64 `description:"指标最新值"`
  162. }
  163. //func DeleteEdbinfoByTraceCodeList(tradeCodeList []string) (err error) {
  164. // if len(tradeCodeList) == 0 {
  165. // return
  166. // }
  167. // o := global.DbMap[utils.DbNameManualIndex]
  168. // err = o.DoTx(func(ctx context.Context, txOrm orm.TxOrmer) error {
  169. // var holder []string
  170. // for range tradeCodeList {
  171. // holder = append(holder, "?")
  172. // }
  173. // sql := ` DELETE FROM edbdata WHERE TRADE_CODE in (` + strings.Join(holder, ",") + `) `
  174. // err := txOrm.Raw(sql, tradeCodeList).Error
  175. // if err != nil {
  176. // return err
  177. // }
  178. // sql = ` DELETE FROM edbinfo WHERE TRADE_CODE in (` + strings.Join(holder, ",") + `)`
  179. // err = txOrm.Raw(sql, tradeCodeList).Error
  180. // if err != nil {
  181. // return err
  182. // }
  183. // return nil
  184. // })
  185. // return
  186. //}
  187. func GetEdbinfoListCount(condition string, pars []interface{}, mobile string, roleType int) (count int, err error) {
  188. o := global.DbMap[utils.DbNameManualIndex]
  189. sql := ``
  190. var countNull sql2.NullInt64
  191. if mobile != "" && roleType == 1 {
  192. sql = `SELECT COUNT(1) AS count FROM edbinfo AS a
  193. INNER JOIN edbinfo_user AS c ON a.TRADE_CODE=c.TRADE_CODE AND c.mobile=?
  194. WHERE a.classify_id>0`
  195. if condition != "" {
  196. sql += condition
  197. }
  198. err = o.Raw(sql, utils.ForwardPars(pars, mobile)...).Scan(&countNull).Error
  199. if err != nil {
  200. return
  201. }
  202. if countNull.Valid {
  203. count = int(countNull.Int64)
  204. }
  205. } else {
  206. sql := `SELECT COUNT(1) AS count FROM edbinfo AS a WHERE a.classify_id>0`
  207. if condition != "" {
  208. sql += condition
  209. }
  210. err = o.Raw(sql, pars...).Scan(&countNull).Error
  211. if err != nil {
  212. return
  213. }
  214. if countNull.Valid {
  215. count = int(countNull.Int64)
  216. }
  217. }
  218. return
  219. }
  220. type EdbinfoItem struct {
  221. TradeCode string `gorm:"column:TRADE_CODE;primaryKey;autoIncrement:false;" orm:"column(TRADE_CODE);pk" description:"指标code"`
  222. SecName string `gorm:"column:SEC_NAME" orm:"column(SEC_NAME);" description:"指标名称"`
  223. Unit string `gorm:"column:UNIT" orm:"column(UNIT);" description:"单位"`
  224. Remark string `gorm:"column:REMARK" orm:"column(REMARK);" description:"备注"`
  225. Frequency string `description:"频度"`
  226. ClassifyId int `description:"分类id"`
  227. ClassifyName string `description:"分类名称"`
  228. CreateDate string `description:"创建时间"`
  229. UserId int `description:"录入用户id"`
  230. UserName string `description:"录入用户名称"`
  231. NoticeTime string `description:"通知时间"`
  232. Mobile string `description:"录入者手机号"`
  233. ModifyTime string `description:"最近一次更新时间"`
  234. StartDate string `description:"数据开始日期"`
  235. EndDate string `description:"数据结束日期"`
  236. LatestValue float64 `description:"指标最新值"`
  237. }
  238. func GetEdbinfoItemList(condition string, pars []interface{}, startSize, pageSize int, mobile string, roleType int) (items []*EdbinfoItem, err error) {
  239. o := global.DbMap[utils.DbNameManualIndex]
  240. sql := ``
  241. if mobile != "" && roleType == 1 {
  242. sql = ` SELECT DISTINCT a.*,b.classify_name FROM edbinfo AS a
  243. LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  244. INNER JOIN edbinfo_user AS c ON a.TRADE_CODE=c.TRADE_CODE AND c.mobile=?
  245. WHERE a.classify_id>0`
  246. if condition != "" {
  247. sql += condition
  248. }
  249. sql += ` ORDER BY a.create_date DESC LIMIT ?,? `
  250. pars = append(pars, startSize, pageSize)
  251. err = o.Raw(sql, utils.ForwardPars(pars, mobile)...).Find(&items).Error
  252. } else {
  253. sql = `SELECT DISTINCT a.*,b.classify_name FROM edbinfo AS a
  254. LEFT JOIN edbdata_classify AS b on a.classify_id=b.classify_id
  255. WHERE a.classify_id>0`
  256. if condition != "" {
  257. sql += condition
  258. }
  259. sql += ` ORDER BY a.create_date DESC LIMIT ?,? `
  260. pars = append(pars, startSize, pageSize)
  261. err = o.Raw(sql, pars...).Find(&items).Error
  262. }
  263. return
  264. }
  265. // EdbParamsInfo 指标数据结构体
  266. type EdbParamsInfo struct {
  267. Unit string `gorm:"column(UNIT);" description:"单位" gorm:"column:UNIT"`
  268. Frequency string `gorm:"column:frequency;" description:"单位"`
  269. }
  270. // GetEdbUnitList 获取指标单位
  271. func GetEdbUnitList() (items []*EdbParamsInfo, err error) {
  272. o := global.DbMap[utils.DbNameManualIndex]
  273. sql := `SELECT UNIT,Frequency from edbinfo group by UNIT`
  274. err = o.Raw(sql).Find(&items).Error
  275. return
  276. }
  277. // GetEdbFrequencyList 获取指标频度
  278. func GetEdbFrequencyList(classifyId, userId int) (items []*EdbParamsInfo, err error) {
  279. o := global.DbMap[utils.DbNameManualIndex]
  280. sql := `SELECT frequency from edbinfo a
  281. join edbdata b on a.TRADE_CODE=b.TRADE_CODE
  282. where classify_id = ? `
  283. if userId > 0 {
  284. sql += ` and a.user_id = ` + fmt.Sprint(userId) + ` `
  285. }
  286. sql += ` group by a.frequency`
  287. err = o.Raw(sql, classifyId).Find(&items).Error
  288. return
  289. }
  290. type TargetListResp struct {
  291. List []*EdbinfoItem
  292. Paging *paging.PagingItem `description:"分页数据"`
  293. }
  294. type EdbinfoAddReq struct {
  295. SecName string `description:"指标名称"`
  296. Unit string `description:"单位"`
  297. Frequency string `description:"频度"`
  298. ClassifyId int `description:"分类id"`
  299. NoticeTime string `description:"通知时间"`
  300. }
  301. // GetMaxTradeCode 获取指标最大trade_code
  302. func GetMaxTradeCode() (max_trade_code string, err error) {
  303. sql := " SELECT MAX(TRADE_CODE) AS max_trade_code FROM edbinfo WHERE LEFT(TRADE_CODE,1)='W' "
  304. o := global.DbMap[utils.DbNameManualIndex]
  305. var codeNull sql2.NullString
  306. err = o.Raw(sql).Scan(&codeNull).Error
  307. max_trade_code = "W00"
  308. if codeNull.Valid {
  309. max_trade_code = codeNull.String
  310. }
  311. return
  312. }
  313. func GetEdbinfoBySecName(secName string) (item *Edbinfo, err error) {
  314. sql := `SELECT * FROM edbinfo WHERE SEC_NAME=? `
  315. o := global.DbMap[utils.DbNameManualIndex]
  316. err = o.Raw(sql, secName).First(&item).Error
  317. return
  318. }
  319. func GetEdbinfoByTradeCode(tradeCode string) (item *Edbinfo, err error) {
  320. sql := `SELECT * FROM edbinfo WHERE TRADE_CODE=? `
  321. o := global.DbMap[utils.DbNameManualIndex]
  322. err = o.Raw(sql, tradeCode).First(&item).Error
  323. return
  324. }
  325. func AddEdbinfo(tradeCode, secName, unit, remark, frequency, noticeTime string, classifyId int, userId int, userName string) (err error) {
  326. o := global.DbMap[utils.DbNameManualIndex]
  327. currTime := time.Now().Format(utils.FormatDateTime)
  328. edbInfo := &Edbinfo{
  329. TradeCode: tradeCode,
  330. SecName: secName,
  331. Unit: unit,
  332. Remark: remark,
  333. Frequency: frequency,
  334. ClassifyId: classifyId,
  335. CreateDate: currTime,
  336. UserId: userId,
  337. UserName: userName,
  338. NoticeTime: noticeTime,
  339. Mobile: "",
  340. ModifyTime: currTime,
  341. IsJoinEdb: 0,
  342. }
  343. err = o.Create(edbInfo).Error
  344. return
  345. }
  346. func AddEdbinfoUser(tradeCode, mobile string) (err error) {
  347. o := global.DbMap[utils.DbNameManualIndex]
  348. sql := `INSERT INTO edbinfo_user(TRADE_CODE, mobile) VALUES (?,?)`
  349. err = o.Exec(sql, tradeCode, mobile).Error
  350. return
  351. }
  352. type EdbinfoEditReq struct {
  353. TradeCode string `description:"指标code"`
  354. SecName string `description:"指标名称"`
  355. Unit string `description:"单位"`
  356. Frequency string `description:"频度"`
  357. ClassifyId int `description:"分类id"`
  358. NoticeTime string `description:"通知时间"`
  359. }
  360. func EditEdbinfo(tradeCode, secName, unit, frequency, noticeTime string, classifyId int) (err error) {
  361. sql := `UPDATE edbinfo SET SEC_NAME= ?, UNIT = ?,classify_id=?,frequency=?,notice_time=?,create_date=NOW() WHERE TRADE_CODE=? `
  362. o := global.DbMap[utils.DbNameManualIndex]
  363. err = o.Exec(sql, secName, unit, classifyId, frequency, noticeTime, tradeCode).Error
  364. return
  365. }
  366. func SearchTargetEntry(classifyId int, keyWord string) (items []*Edbinfo, err error) {
  367. where := ""
  368. pars := make([]interface{}, 0)
  369. sql := `SELECT * FROM edbinfo WHERE classify_id>0 AND classify_id=? `
  370. pars = append(pars, classifyId)
  371. if keyWord != "" {
  372. sql += `AND SEC_NAME LIKE ? `
  373. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  374. }
  375. sql += where
  376. o := global.DbMap[utils.DbNameManualIndex]
  377. err = o.Raw(sql, pars...).Find(&items).Error
  378. return
  379. }
  380. type SearchTargetListResp struct {
  381. List []*Edbinfo
  382. }
  383. type EdbdataClassify struct {
  384. ClassifyId int
  385. ClassifyName string
  386. ParentId int
  387. EdbInfoTotal int
  388. TradeCode string
  389. UniqueCode string
  390. }
  391. //func GetEdbdataClassifyByClassifyName(classifyName string) (item *EdbdataClassify, err error) {
  392. // sql := `SELECT * FROM edbdata_classify WHERE classify_name=? `
  393. // o := global.DbMap[utils.DbNameManualIndex]
  394. // err = o.Raw(sql, classifyName).First(&item).Error
  395. // return
  396. //}
  397. type EdbdataClassifyList struct {
  398. ClassifyId int
  399. ClassifyName string
  400. ParentId int
  401. Child []*EdbdataClassify `gorm:"-"`
  402. TradeCode string
  403. UniqueCode string
  404. }
  405. func GetEdbdataClassify(userId int64) (items []*EdbdataClassifyList, err error) {
  406. var newItems []*EdbdataClassifyList
  407. o := global.DbMap[utils.DbNameManualIndex]
  408. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=0 `
  409. err = o.Raw(sql).Find(&newItems).Error
  410. if err != nil {
  411. return
  412. }
  413. classifyLen := len(newItems)
  414. // 获取子集分类
  415. var allChildItems []*EdbdataClassify
  416. if userId > 0 {
  417. userClassifyList, _ := GetManualUserClassify(int(userId))
  418. var userIdArr []int
  419. for _, v := range userClassifyList {
  420. userIdArr = append(userIdArr, v.ClassifyId)
  421. }
  422. num := len(userClassifyList)
  423. if num > 0 {
  424. childSql := "SELECT a.classify_id,a.classify_name,a.parent_id FROM edbdata_classify AS a WHERE a.is_show=1 and a.classify_id IN(" + utils.GetOrmInReplace(num) + ") ORDER BY a.create_time ASC "
  425. err = o.Raw(childSql, userIdArr).Find(&allChildItems).Error
  426. }
  427. } else {
  428. childSql := "SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE is_show=1 ORDER BY create_time ASC "
  429. err = o.Raw(childSql).Find(&allChildItems).Error
  430. }
  431. if err != nil {
  432. return
  433. }
  434. for i := 0; i < classifyLen; i++ {
  435. var childItems []*EdbdataClassify
  436. parentId := newItems[i].ClassifyId
  437. for _, v := range allChildItems {
  438. if v.ParentId == parentId {
  439. childItems = append(childItems, v)
  440. }
  441. }
  442. newItems[i].Child = childItems
  443. }
  444. for _, v := range newItems {
  445. childLen := len(v.Child)
  446. if childLen > 0 {
  447. items = append(items, v)
  448. }
  449. }
  450. return
  451. }
  452. type ManualUserClassify struct {
  453. ManualUserClassifyId int `gorm:"column:manual_user_classify_id;primaryKey"`
  454. AdminId int
  455. ClassifyId int
  456. CreateTime time.Time
  457. }
  458. func GetManualUserClassify(sysUserId int) (list []*ManualUserClassify, err error) {
  459. o := global.DbMap[utils.DbNameIndex]
  460. sql := `SELECT * FROM manual_user_classify WHERE admin_id=? `
  461. err = o.Raw(sql, sysUserId).Find(&list).Error
  462. return
  463. }
  464. type EdbdataClassifyResp struct {
  465. List []*EdbdataClassifyList
  466. }
  467. func GetTargetBySecName(secName string) (item *Edbinfo, err error) {
  468. sql := `SELECT * FROM edbinfo WHERE SEC_NAME=? `
  469. o := global.DbMap[utils.DbNameManualIndex]
  470. err = o.Raw(sql, secName).First(&item).Error
  471. return
  472. }
  473. // 更新指标数据信息
  474. func (edbinfo *Edbinfo) Update(cols []string) (err error) {
  475. o := global.DbMap[utils.DbNameManualIndex]
  476. err = o.Select(cols).Updates(edbinfo).Error
  477. return
  478. }
  479. //func ModifyTargetClassifyId(tradeCode string, classifyId int) (err error) {
  480. // sql := `UPDATE edbinfo SET classify_id=? WHERE TRADE_CODE=? `
  481. // o := global.DbMap[utils.DbNameManualIndex]
  482. // err = o.Exec(sql, classifyId, tradeCode).Error
  483. // return
  484. //}
  485. //func GetTargetsDataCount(tradeCode, dt string) (count int, err error) {
  486. // sql := `SELECT COUNT(1) AS count FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  487. // o := global.DbMap[utils.DbNameManualIndex]
  488. // err = o.Raw(sql, tradeCode, dt).QueryRow(&count)
  489. // return
  490. //}
  491. // GetTargetsDataList 根据code获取指标数据列表
  492. func GetTargetsDataList(tradeCode string) (items []*Edbdata, err error) {
  493. o := global.DbMap[utils.DbNameManualIndex]
  494. sql := `SELECT * FROM edbdata WHERE TRADE_CODE=? ORDER BY DT ASC `
  495. err = o.Raw(sql, tradeCode).Find(&items).Error
  496. return
  497. }
  498. //func GetTargetsData(tradeCode, dt string) (item *Edbdata, err error) {
  499. // sql := `SELECT * FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  500. // o := global.DbMap[utils.DbNameManualIndex]
  501. // err = o.Raw(sql, tradeCode, dt).First(&item).Error
  502. // return
  503. //}
  504. func ModifyTargetsDataByImport(tradeCode, dt, close string) (err error) {
  505. sql := `UPDATE edbdata SET CLOSE=?,modify_time=NOW() WHERE TRADE_CODE=? AND DT=? `
  506. o := global.DbMap[utils.DbNameManualIndex]
  507. err = o.Exec(sql, close, tradeCode, dt).Error
  508. return
  509. }
  510. func AddTargetsDataByImport(tradeCode, dt, close string) (err error) {
  511. sql := `INSERT INTO edbdata(TRADE_CODE, DT,CLOSE, modify_time)VALUES(?,?,?,NOW()) `
  512. o := global.DbMap[utils.DbNameManualIndex]
  513. err = o.Exec(sql, tradeCode, dt, close).Error
  514. return
  515. }
  516. type EdbdataImportResp struct {
  517. Status int
  518. Msg string
  519. SuccessCount int
  520. FailCount int
  521. IndexCount int `description:"指标数"`
  522. }
  523. func GetFailList(sysUserId int) (items []*EdbdataImportFail, err error) {
  524. o := global.DbMap[utils.DbNameManualIndex]
  525. sql := ` SELECT * FROM edbdata_import_fail WHERE sys_user_id=? `
  526. err = o.Raw(sql, sysUserId).Find(&items).Error
  527. return
  528. }
  529. type DataListForExport struct {
  530. TradeCode string `gorm:"column:TRADE_CODE" description:"指标code"`
  531. SecName string `gorm:"column:SEC_NAME" description:"指标名称"`
  532. Unit string `gorm:"column:UNIT" description:"单位"`
  533. Frequency string `description:"频度"`
  534. ClassifyId int `description:"分类id"`
  535. NoticeTime string `description:"通知时间"`
  536. ClassifyName string
  537. Dt string `gorm:"column:DT" description:"日期"`
  538. Close float64 `gorm:"column:CLOSE" description:"值"`
  539. }
  540. //func GetDataListForExport(startDate, endDate, frequency, keyWord string, classifyId int) (items []*DataListForExport, err error) {
  541. // where := ``
  542. // var pars []interface{}
  543. // if keyWord != "" {
  544. // where = ` AND SEC_NAME LIKE ? `
  545. // pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  546. // }
  547. // if startDate != "" {
  548. // where += ` AND create_date>=? `
  549. // pars = append(pars, startDate)
  550. // }
  551. // if endDate != "" {
  552. // where += ` AND create_date<=? `
  553. // pars = append(pars, endDate)
  554. // }
  555. // if frequency != "" {
  556. // where += ` AND frequency=? `
  557. // pars = append(pars, frequency)
  558. // }
  559. // if classifyId > 0 {
  560. // where += ` AND classify_id=? `
  561. // pars = append(pars, classifyId)
  562. // }
  563. //
  564. // sql := ` SELECT a.TRADE_CODE,a.SEC_NAME,a.UNIT,a.frequency,a.classify_id,b.classify_name,c.DT,c.CLOSE FROM edbdata AS c
  565. // INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  566. // LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  567. // WHERE a.classify_id>0 `
  568. // if where != "" {
  569. // sql += where
  570. // }
  571. // sql = sql + " ORDER BY c.DT DESC "
  572. // o := global.DbMap[utils.DbNameManualIndex]
  573. // err = o.Raw(sql, pars...).Find(&items).Error
  574. // return
  575. //}
  576. type DataDeleteReq struct {
  577. TradeCode string `description:"指标唯一编码"`
  578. CreateDate string `description:"数据录入日期"`
  579. }
  580. func DataDelete(tradeCode, createDate, close string, modifyTime time.Time, sysUserId int) (err error) {
  581. to := global.DbMap[utils.DbNameManualIndex].Begin()
  582. defer func() {
  583. if err != nil {
  584. _ = to.Rollback()
  585. } else {
  586. _ = to.Commit()
  587. }
  588. }()
  589. recordSql := ` INSERT INTO edbdata_delete_record(TRADE_CODE,DT,CLOSE,modify_time,create_time,sys_user_id)
  590. VALUES(?,?,?,?,?,?)`
  591. err = to.Exec(recordSql, tradeCode, createDate, close, modifyTime, time.Now(), sysUserId).Error
  592. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? AND DT = ? `
  593. err = to.Exec(sql, tradeCode, createDate).Error
  594. return
  595. }
  596. //func GetTargetInfoCount(tradeCode string) (count int, err error) {
  597. // sql := ` SELECT COUNT(1) AS count FROM edbdata AS c
  598. // INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  599. // WHERE a.TRADE_CODE=? `
  600. // o := global.DbMap[utils.DbNameManualIndex]
  601. // err = o.Raw(sql, tradeCode).QueryRow(&count)
  602. // return
  603. //}
  604. type TargetDeleteReq struct {
  605. TradeCode string `description:"指标唯一编码"`
  606. }
  607. func TargetDelete(tradeCode string) (err error) {
  608. to := global.DbMap[utils.DbNameManualIndex].Begin()
  609. defer func() {
  610. if err != nil {
  611. _ = to.Rollback()
  612. } else {
  613. _ = to.Commit()
  614. }
  615. }()
  616. sql := " DELETE FROM edbinfo WHERE TRADE_CODE = ? "
  617. err = to.Exec(sql, tradeCode).Error
  618. sql = " DELETE FROM edbdata WHERE TRADE_CODE = ? "
  619. err = to.Exec(sql, tradeCode).Error
  620. return
  621. }
  622. type Researcher struct {
  623. AdminId int `description:"系统用户id"`
  624. AdminName string `description:"系统用户名称"`
  625. RealName string `description:"系统用户姓名"`
  626. Role string `description:"系统用户角色"`
  627. Mobile string `description:"手机号"`
  628. TargetCount int `description:"指标数量"`
  629. }
  630. type ResearcherListResp struct {
  631. List []*Researcher
  632. }
  633. func GetResearcherEntry() (items []*Researcher, err error) {
  634. sql := ` SELECT admin_id,admin_name,real_name,mobile,0 as target_count FROM admin WHERE role_type=1 `
  635. o := global.DEFAULT_DB
  636. sql = utils.ReplaceDriverKeywords("", sql)
  637. err = o.Raw(sql).Find(&items).Error
  638. researchLen := len(items)
  639. edbO := global.DbMap[utils.DbNameManualIndex]
  640. for i := 0; i < researchLen; i++ {
  641. var count int
  642. mobile := items[i].Mobile
  643. sqlCount := ` SELECT COUNT(DISTINCT a.TRADE_CODE) AS count FROM edbinfo_user AS a
  644. INNER JOIN edbinfo AS b ON a.TRADE_CODE=b.TRADE_CODE
  645. WHERE a.mobile=? AND b.classify_id>0 `
  646. var countNull sql2.NullInt64
  647. err = edbO.Raw(sqlCount, mobile).Scan(&countNull).Error
  648. if err != nil {
  649. return
  650. }
  651. if countNull.Valid {
  652. count = int(countNull.Int64)
  653. }
  654. items[i].TargetCount = count
  655. }
  656. return
  657. }
  658. func GetResearcherEntryByMobile(mobile string) (items []*Researcher, err error) {
  659. sql := ` SELECT admin_id,admin_name,real_name,mobile,0 as target_count FROM admin WHERE role_type=1 `
  660. if mobile != "" {
  661. sql += ` AND mobile IN(` + mobile + `)`
  662. }
  663. sql = utils.ReplaceDriverKeywords("", sql)
  664. o := global.DEFAULT_DB
  665. err = o.Raw(sql).Find(&items).Error
  666. researchLen := len(items)
  667. edbO := global.DbMap[utils.DbNameManualIndex]
  668. for i := 0; i < researchLen; i++ {
  669. var count int
  670. mobile := items[i].Mobile
  671. sqlCount := ` SELECT COUNT(DISTINCT a.TRADE_CODE) AS count FROM edbinfo_user AS a
  672. INNER JOIN edbinfo AS b ON a.TRADE_CODE=b.TRADE_CODE
  673. WHERE a.mobile=? AND b.classify_id>0 `
  674. var countNull sql2.NullInt64
  675. err = edbO.Raw(sqlCount, mobile).Scan(&countNull).Error
  676. if err != nil {
  677. return
  678. }
  679. if countNull.Valid {
  680. count = int(countNull.Int64)
  681. }
  682. items[i].TargetCount = count
  683. }
  684. return
  685. }
  686. type EdbinfoItems struct {
  687. TradeCode string `gorm:"column:TRADE_CODE;autoIncrement:false;primaryKey" description:"指标编码"`
  688. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  689. Unit string `gorm:"column:UNIT;" description:"单位"`
  690. Remark string `gorm:"column:REMARK;" description:"备注"`
  691. Frequency string `description:"频度"`
  692. ClassifyId int `description:"分类id"`
  693. ClassifyName string `description:"分类名称"`
  694. CreateDate string `description:"创建时间"`
  695. UserId int `description:"录入用户id"`
  696. NoticeTime string `description:"通知时间"`
  697. Mobile string `description:"录入者手机号"`
  698. ModifyDate string `description:"待更新日期"`
  699. Status string `description:"状态:未完成/完成"`
  700. }
  701. type TargetItemsResp struct {
  702. List SortEdbInfo
  703. }
  704. type SortEdbInfo []EdbinfoItems
  705. func GetTargetItems(mobile string, classifyId int) (lastItems SortEdbInfo, err error) {
  706. var items []*EdbinfoItems
  707. o := global.DbMap[utils.DbNameManualIndex]
  708. //sql := ` SELECT *,'' modify_date,'' status FROM edbinfo AS a WHERE a.classify_id>0 `
  709. sql := ` SELECT *,'' modify_date,'' STATUS FROM edbinfo AS a
  710. WHERE a.classify_id>0
  711. `
  712. if classifyId > 0 {
  713. sql += ` AND a.classify_id=` + strconv.Itoa(classifyId) + ``
  714. }
  715. sql += ` GROUP BY a.TRADE_CODE `
  716. //if classifyId > 0 {
  717. // sql = ` SELECT *,'' modify_date,'' status FROM edbinfo AS a
  718. // WHERE a.classify_id=` + strconv.Itoa(classifyId) + ` AND a.classify_id>0
  719. // GROUP BY a.TRADE_CODE `
  720. //}
  721. //` ORDER BY CONVERT(a.SEC_NAME USING gbk ) COLLATE gbk_chinese_ci ASC `
  722. sql = sql + fmt.Sprintf(` ORDER BY %s COLLATE gbk_chinese_ci ASC `, utils.GenerateQuerySql(utils.ConvertColumn, &utils.QueryParam{
  723. ConvertColumn: "a.SEC_NAME",
  724. }))
  725. err = o.Raw(sql).Find(&items).Error
  726. if err != nil {
  727. return
  728. }
  729. itemsLen := len(items)
  730. nowWeek := time.Now().Weekday().String()
  731. fmt.Println(nowWeek)
  732. finishEdbInfo := SortEdbInfo{}
  733. unFinishEdbInfo := SortEdbInfo{}
  734. for i := 0; i < itemsLen; i++ {
  735. noticeTime := items[i].NoticeTime
  736. frequency := items[i].Frequency
  737. tradeCode := items[i].TradeCode
  738. if noticeTime != "" {
  739. if frequency == "周度" {
  740. noticeArr := strings.Split(noticeTime, " ")
  741. noticeWeek := noticeArr[0]
  742. fmt.Println(noticeWeek)
  743. addDay := 0
  744. if nowWeek == "Sunday" {
  745. if noticeWeek == "周日" {
  746. addDay = 0
  747. } else if noticeWeek == "周一" {
  748. addDay = 1
  749. } else if noticeWeek == "周二" {
  750. addDay = 2
  751. } else if noticeWeek == "周三" {
  752. addDay = 3
  753. } else if noticeWeek == "周四" {
  754. addDay = 4
  755. } else if noticeWeek == "周五" {
  756. addDay = 5
  757. } else if noticeWeek == "周六" {
  758. addDay = 6
  759. } else {
  760. addDay = 0
  761. }
  762. } else if nowWeek == "Monday" {
  763. if noticeWeek == "周日" {
  764. addDay = 6
  765. } else if noticeWeek == "周一" {
  766. addDay = 0
  767. } else if noticeWeek == "周二" {
  768. addDay = 1
  769. } else if noticeWeek == "周三" {
  770. addDay = 2
  771. } else if noticeWeek == "周四" {
  772. addDay = 3
  773. } else if noticeWeek == "周五" {
  774. addDay = 4
  775. } else if noticeWeek == "周六" {
  776. addDay = 5
  777. } else {
  778. addDay = 0
  779. }
  780. } else if nowWeek == "Tuesday" {
  781. if noticeWeek == "周日" {
  782. addDay = 5
  783. } else if noticeWeek == "周一" {
  784. addDay = 6
  785. } else if noticeWeek == "周二" {
  786. addDay = 0
  787. } else if noticeWeek == "周三" {
  788. addDay = 1
  789. } else if noticeWeek == "周四" {
  790. addDay = 2
  791. } else if noticeWeek == "周五" {
  792. addDay = 3
  793. } else if noticeWeek == "周六" {
  794. addDay = 4
  795. } else {
  796. addDay = 0
  797. }
  798. } else if nowWeek == "Wednesday" {
  799. if noticeWeek == "周日" {
  800. addDay = 4
  801. } else if noticeWeek == "周一" {
  802. addDay = 5
  803. } else if noticeWeek == "周二" {
  804. addDay = 6
  805. } else if noticeWeek == "周三" {
  806. addDay = 0
  807. } else if noticeWeek == "周四" {
  808. addDay = 1
  809. } else if noticeWeek == "周五" {
  810. addDay = 2
  811. } else if noticeWeek == "周六" {
  812. addDay = 3
  813. } else {
  814. addDay = 0
  815. }
  816. } else if nowWeek == "Thursday" {
  817. if noticeWeek == "周日" {
  818. addDay = 3
  819. } else if noticeWeek == "周一" {
  820. addDay = 4
  821. } else if noticeWeek == "周二" {
  822. addDay = 5
  823. } else if noticeWeek == "周三" {
  824. addDay = 6
  825. } else if noticeWeek == "周四" {
  826. addDay = 0
  827. } else if noticeWeek == "周五" {
  828. addDay = 1
  829. } else if noticeWeek == "周六" {
  830. addDay = 2
  831. } else {
  832. addDay = 0
  833. }
  834. } else if nowWeek == "Friday" {
  835. if noticeWeek == "周日" {
  836. addDay = 2
  837. } else if noticeWeek == "周一" {
  838. addDay = 3
  839. } else if noticeWeek == "周二" {
  840. addDay = 4
  841. } else if noticeWeek == "周三" {
  842. addDay = 5
  843. } else if noticeWeek == "周四" {
  844. addDay = 6
  845. } else if noticeWeek == "周五" {
  846. addDay = 0
  847. } else if noticeWeek == "周六" {
  848. addDay = 1
  849. } else {
  850. addDay = 0
  851. }
  852. } else if nowWeek == "Saturday" {
  853. if noticeWeek == "周日" {
  854. addDay = 1
  855. } else if noticeWeek == "周一" {
  856. addDay = 2
  857. } else if noticeWeek == "周二" {
  858. addDay = 3
  859. } else if noticeWeek == "周三" {
  860. addDay = 4
  861. } else if noticeWeek == "周四" {
  862. addDay = 5
  863. } else if noticeWeek == "周五" {
  864. addDay = 6
  865. } else if noticeWeek == "周六" {
  866. addDay = 0
  867. } else {
  868. addDay = 0
  869. }
  870. }
  871. modifyDate := time.Now().AddDate(0, 0, addDay)
  872. modifyDateStr := modifyDate.Format(utils.FormatDate)
  873. items[i].ModifyDate = modifyDateStr
  874. modifyDateEndStr := modifyDate.AddDate(0, 0, -7).Format(utils.FormatDate)
  875. fmt.Println("addDay:", addDay)
  876. fmt.Println("modifyDateEndStr:", modifyDateEndStr)
  877. count := 0
  878. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  879. //err = o.Raw(sqlCount, tradeCode, modifyDateEndStr, modifyDateStr).QueryRow(&count)
  880. var countNull sql2.NullInt64
  881. err = o.Raw(sqlCount, tradeCode, modifyDateEndStr, modifyDateStr).Scan(&countNull).Error
  882. if err != nil {
  883. return nil, err
  884. }
  885. if countNull.Valid {
  886. count = int(countNull.Int64)
  887. }
  888. if count > 0 {
  889. items[i].Status = "完成"
  890. finishEdbInfo = append(finishEdbInfo, *items[i])
  891. } else {
  892. items[i].Status = "未完成"
  893. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  894. }
  895. } else if frequency == "日度" {
  896. items[i].Status = "完成"
  897. finishEdbInfo = append(finishEdbInfo, *items[i])
  898. } else if frequency == "月度" {
  899. myYear := time.Now().Year()
  900. myMonth := time.Now().Format("01")
  901. startDate, endDate := utils.GetMonthStartAndEnd(strconv.Itoa(myYear), myMonth)
  902. count := 0
  903. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  904. //err = o.Raw(sqlCount, tradeCode, startDate, endDate).QueryRow(&count)
  905. var countNull sql2.NullInt64
  906. err = o.Raw(sqlCount, tradeCode, startDate, endDate).Scan(&countNull).Error
  907. if err != nil {
  908. return nil, err
  909. }
  910. if countNull.Valid {
  911. count = int(countNull.Int64)
  912. }
  913. if noticeTime != "" {
  914. var modifyDateStr string
  915. strArr := strings.Split(noticeTime, "日")
  916. myYear := time.Now().Year()
  917. myMonth := time.Now().Format("01")
  918. modifyDateStr = strconv.Itoa(myYear) + "-" + myMonth + "-" + strArr[0]
  919. items[i].ModifyDate = modifyDateStr
  920. }
  921. if count > 0 {
  922. items[i].Status = "完成"
  923. finishEdbInfo = append(finishEdbInfo, *items[i])
  924. } else {
  925. items[i].Status = "未完成"
  926. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  927. }
  928. } else {
  929. items[i].Status = "完成"
  930. finishEdbInfo = append(finishEdbInfo, *items[i])
  931. }
  932. } else {
  933. if frequency == "月度" {
  934. myYear := time.Now().Year()
  935. myMonth := time.Now().Format("01")
  936. startDate, endDate := utils.GetMonthStartAndEnd(strconv.Itoa(myYear), myMonth)
  937. count := 0
  938. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  939. //err = o.Raw(sqlCount, tradeCode, startDate, endDate).QueryRow(&count)
  940. var countNull sql2.NullInt64
  941. err = o.Raw(sqlCount, tradeCode, startDate, endDate).Scan(&countNull).Error
  942. if err != nil {
  943. return nil, err
  944. }
  945. if countNull.Valid {
  946. count = int(countNull.Int64)
  947. }
  948. if count > 0 {
  949. items[i].Status = "完成"
  950. finishEdbInfo = append(finishEdbInfo, *items[i])
  951. } else {
  952. items[i].Status = "未完成"
  953. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  954. }
  955. } else {
  956. items[i].Status = "完成"
  957. finishEdbInfo = append(finishEdbInfo, *items[i])
  958. }
  959. }
  960. }
  961. sort.Sort(SortByModifyDate{finishEdbInfo})
  962. sort.Sort(SortByModifyDate{unFinishEdbInfo})
  963. lastItems = append(lastItems, unFinishEdbInfo...)
  964. lastItems = append(lastItems, finishEdbInfo...)
  965. return
  966. }
  967. // 获取此 slice 的长度
  968. func (p SortEdbInfo) Len() int { return len(p) }
  969. // 根据元素的状态降序排序
  970. func (p SortEdbInfo) Less(i, j int) bool {
  971. return p[i].Status > p[j].Status
  972. }
  973. // 交换数据
  974. func (p SortEdbInfo) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  975. // 嵌套结构体 将继承 SortEdbInfo 的所有属性和方法
  976. // 所以相当于SortByName 也实现了 Len() 和 Swap() 方法
  977. type SortByStatus struct{ SortEdbInfo }
  978. // 根据元素的姓名长度降序排序 (此处按照自己的业务逻辑写)
  979. func (p SortByStatus) Less(i, j int) bool {
  980. return len(p.SortEdbInfo[i].Status) > len(p.SortEdbInfo[j].Status)
  981. }
  982. type SortByModifyDate struct{ SortEdbInfo }
  983. // 根据元素的年龄降序排序 (此处按照自己的业务逻辑写)
  984. func (p SortByModifyDate) Less(i, j int) bool {
  985. return p.SortEdbInfo[i].ModifyDate > p.SortEdbInfo[j].ModifyDate
  986. }
  987. type DataCheckResp struct {
  988. Status int `description:"状态:1:该日期已存在数据,是否确认修改?,0:数据不存在"`
  989. Close string `description:"值"`
  990. }
  991. type TargetCheckResp struct {
  992. Status int `description:"状态:1:该指标有关联数据,请先删除数据,0:指标不存在关联数据,可直接删除"`
  993. }
  994. type EdbdataExportList struct {
  995. TradeCode string `gorm:"column:TRADE_CODE;" description:"指标code"`
  996. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  997. Unit string `gorm:"column:UNIT;" description:"单位"`
  998. Remark string `gorm:"column:REMARK;" description:"备注"`
  999. Frequency string `description:"频度"`
  1000. ClassifyId int `description:"分类id"`
  1001. ClassifyName string `description:"分类名称"`
  1002. CreateDate string `description:"创建时间"`
  1003. Dt string `gorm:"column:DT;" description:"最新一次录入时间"`
  1004. }
  1005. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  1006. func (m *EdbdataExportList) AfterFind(db *gorm.DB) (err error) {
  1007. m.Dt = utils.GormDateStrToDateStr(m.Dt)
  1008. return
  1009. }
  1010. func GetEdbdataSecName(condition string, pars []interface{}) (items []*EdbdataExportList, err error) {
  1011. //sql := `SELECT a.TRADE_CODE,a.SEC_NAME,a.frequency,a.UNIT,MAX(c.DT) AS Dt
  1012. // FROM edbdata AS c
  1013. // INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  1014. // INNER JOIN edbinfo_user AS d ON a.TRADE_CODE=d.TRADE_CODE
  1015. // LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  1016. // WHERE a.classify_id>0`
  1017. sql := `SELECT a.TRADE_CODE,a.SEC_NAME,a.frequency,a.UNIT,MAX(c.DT) AS Dt,b.classify_name
  1018. FROM edbdata AS c
  1019. INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  1020. LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  1021. WHERE a.classify_id>0`
  1022. if condition != "" {
  1023. sql += condition
  1024. }
  1025. sql += " GROUP BY a.TRADE_CODE ORDER BY a.TRADE_CODE ASC "
  1026. o := global.DbMap[utils.DbNameManualIndex]
  1027. err = o.Raw(sql, pars...).Find(&items).Error
  1028. return
  1029. }
  1030. func GetEdbDataFrequency(classifyId int) (items []*string, err error) {
  1031. sql := `SELECT DISTINCT frequency FROM edbinfo where classify_id=? AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  1032. o := global.DbMap[utils.DbNameManualIndex]
  1033. err = o.Raw(sql, classifyId).Find(&items).Error
  1034. return
  1035. }
  1036. // GetEdbDataFrequencyByClassifyIdList
  1037. // @Description: 根据分类列表获取所有的频度
  1038. // @author: Roc
  1039. // @datetime 2024-08-15 17:51:13
  1040. // @param classifyIdList []int
  1041. // @return items []*string
  1042. // @return err error
  1043. func GetEdbDataFrequencyByClassifyIdList(classifyIdList []int) (items []string, err error) {
  1044. num := len(classifyIdList)
  1045. if num <= 0 {
  1046. return
  1047. }
  1048. sql := `SELECT DISTINCT frequency FROM edbinfo where classify_id in (` + utils.GetOrmInReplace(num) + `) AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  1049. o := global.DbMap[utils.DbNameManualIndex]
  1050. err = o.Raw(sql, classifyIdList).Find(&items).Error
  1051. return
  1052. }
  1053. func GetEdbDataFrequencyByKeyord(keyword string) (items []string, err error) {
  1054. sql := `SELECT DISTINCT frequency FROM edbinfo where SEC_NAME=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  1055. o := global.DbMap[utils.DbNameManualIndex]
  1056. err = o.Raw(sql, keyword).Find(&items).Error
  1057. return
  1058. }
  1059. type EdbdataList struct {
  1060. Dt string `gorm:"column:DT;" description:"录入时间"`
  1061. }
  1062. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  1063. func (m *EdbdataList) AfterFind(db *gorm.DB) (err error) {
  1064. m.Dt = utils.GormDateStrToDateStr(m.Dt)
  1065. return
  1066. }
  1067. func GetEdbdataList(tradeCode string) (items []*EdbdataList, err error) {
  1068. sql := ` SELECT DT FROM edbdata WHERE TRADE_CODE IN(` + tradeCode + `) GROUP BY DT ORDER BY DT DESC `
  1069. o := global.DbMap[utils.DbNameManualIndex]
  1070. err = o.Raw(sql).Find(&items).Error
  1071. return
  1072. }
  1073. type EdbdataItem struct {
  1074. TradeCode string `gorm:"column:TRADE_CODE;" description:"指标code"`
  1075. Dt string `gorm:"column:DT;" description:"最新一次录入时间"`
  1076. Close float64 `gorm:"column:CLOSE;" description:"值"`
  1077. }
  1078. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  1079. func (m *EdbdataItem) AfterFind(db *gorm.DB) (err error) {
  1080. m.Dt = utils.GormDateStrToDateStr(m.Dt)
  1081. return
  1082. }
  1083. func GetEdbdataValueByTradeCode(tradeCode, dt string) (item *EdbdataItem, err error) {
  1084. sql := ` SELECT TRADE_CODE,DT,CLOSE FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1085. o := global.DbMap[utils.DbNameManualIndex]
  1086. err = o.Raw(sql, tradeCode, dt).First(&item).Error
  1087. return
  1088. }
  1089. func GetEdbdataAllByTradeCode(tradeCode string) (items []*EdbdataItem, err error) {
  1090. sql := ` SELECT * FROM edbdata WHERE TRADE_CODE=? `
  1091. o := global.DbMap[utils.DbNameManualIndex]
  1092. err = o.Raw(sql, tradeCode).Find(&items).Error
  1093. return
  1094. }
  1095. func GetEdbdataClassifyByParentId(parentId int) (items []*EdbdataClassify, err error) {
  1096. sql := ` SELECT * FROM edbdata_classify WHERE parent_id=? `
  1097. o := global.DbMap[utils.DbNameManualIndex]
  1098. err = o.Raw(sql, parentId).Find(&items).Error
  1099. return
  1100. }
  1101. type LzPriceClassify struct {
  1102. ProductName string
  1103. }
  1104. func GetLzPriceClassify() (items []*LzPriceClassify, err error) {
  1105. sql := ` SELECT product_name FROM longzhongpriceinfo GROUP BY product_name ORDER BY product_name DESC `
  1106. o := global.DbMap[utils.DbNameManualIndex]
  1107. err = o.Raw(sql).Find(&items).Error
  1108. return
  1109. }
  1110. type Longzhongpriceinfo struct {
  1111. LongzhongpriceinfoId int `gorm:"column:longzhongpriceinfo_id;primaryKey"`
  1112. Standard string
  1113. ModelName string
  1114. Unit string
  1115. AreaName string
  1116. PriceType string
  1117. Memo string
  1118. PriceId string
  1119. ProductName string
  1120. InfoType string
  1121. InfoTypeRemark string
  1122. MarketName string
  1123. ManufactureName string
  1124. }
  1125. func GetLongzhongpriceinfoByClassifyName(productName string) (items []*Longzhongpriceinfo, err error) {
  1126. sql := `SELECT * FROM longzhongpriceinfo WHERE product_name=? ORDER BY longzhongpriceinfo_id ASC `
  1127. o := global.DbMap[utils.DbNameManualIndex]
  1128. err = o.Raw(sql, productName).Find(&items).Error
  1129. return
  1130. }
  1131. func GetLongzhongPriceDataMaxCount(productName string) (count int, err error) {
  1132. o := global.DbMap[utils.DbNameManualIndex]
  1133. sql := `SELECT MAX(t.num) AS count FROM (
  1134. SELECT COUNT(1) AS num FROM longzhongpriceinfo AS a
  1135. INNER JOIN longzhongpricedata AS b ON a.longzhongpriceinfo_id=b.longzhongpriceinfo_id
  1136. WHERE a.product_name=?
  1137. GROUP BY a.product_name
  1138. )AS t `
  1139. //err = o.Raw(sql, productName).QueryRow(&count)
  1140. var countNull sql2.NullInt64
  1141. err = o.Raw(sql, productName).Scan(&countNull).Error
  1142. if err != nil {
  1143. return
  1144. }
  1145. if countNull.Valid {
  1146. count = int(countNull.Int64)
  1147. }
  1148. return
  1149. }
  1150. type LongzhongpricedataItems struct {
  1151. LongzhongpricedataId int `gorm:"column:longzhongpricedata_id;primaryKey"`
  1152. LongzhongpriceinfoId int
  1153. PriceDate string
  1154. Memo string
  1155. Price float64
  1156. CnyPrice float64
  1157. ZsyPrice float64
  1158. ZshPrice float64
  1159. LowPrice float64
  1160. HighPrice float64
  1161. RisePrice float64
  1162. TonPrice float64
  1163. PriceType string
  1164. UpdateDate string
  1165. }
  1166. func GetLongzhongPriceDataById(lzPriceInfoId int) (items []*LongzhongpricedataItems, err error) {
  1167. o := global.DbMap[utils.DbNameManualIndex]
  1168. sql := ` SELECT DISTINCT a.longzhongpriceinfo_id,a.price_date,a.memo,a.price,a.cny_price,a.zsy_price,a.zsh_price,a.low_price,a.high_price,a.rise_price,a.ton_price,a.price_type,a.update_date
  1169. FROM longzhongpricedata AS a
  1170. WHERE longzhongpriceinfo_id=? ORDER BY price_date DESC `
  1171. err = o.Raw(sql, lzPriceInfoId).Find(&items).Error
  1172. return
  1173. }
  1174. func GetLzSurveyClassify() (items []*LzPriceClassify, err error) {
  1175. sql := ` SELECT breed_name AS product_name FROM longzhong_survey_product GROUP BY breed_name ORDER BY breed_name DESC `
  1176. o := global.DbMap[utils.DbNameManualIndex]
  1177. err = o.Raw(sql).Find(&items).Error
  1178. return
  1179. }
  1180. type LongzhongSurveyProduct struct {
  1181. SurveyProductId int `gorm:"column:survey_product_id;primaryKey"`
  1182. ProjectQuotaId int64
  1183. BreedId string
  1184. BreedName string
  1185. QuotaId string
  1186. QuotaName string
  1187. UnitId string
  1188. UnitName string
  1189. SampleType int64
  1190. SampleId string
  1191. SampleName string
  1192. DeviceId string
  1193. Device string
  1194. ProductCraftId string
  1195. ProductCraft string
  1196. ProductLine string
  1197. InputMode int64
  1198. Frequency int64
  1199. InputValue string
  1200. TaskShouldFinishTime int
  1201. CustomId string
  1202. CustomType int64
  1203. Custom string
  1204. QuotaSampleId int64
  1205. StartDate string
  1206. EndDate string
  1207. LzCode string
  1208. }
  1209. func (obj *LongzhongSurveyProduct) AfterFind(tx *gorm.DB) (err error) {
  1210. obj.StartDate = utils.GormDateStrToDateStr(obj.StartDate)
  1211. obj.EndDate = utils.GormDateStrToDateStr(obj.EndDate)
  1212. return
  1213. }
  1214. func GetLongzhongSurveyProductByClassifyName(productName string) (items []*LongzhongSurveyProduct, err error) {
  1215. sql := `SELECT * FROM longzhong_survey_product WHERE breed_name=? ORDER BY survey_product_id ASC `
  1216. o := global.DbMap[utils.DbNameManualIndex]
  1217. err = o.Raw(sql, productName).Find(&items).Error
  1218. return
  1219. }
  1220. func GetLzSurveyProductByNameAndFrequency(productName string, frequency int) (items []*LongzhongSurveyProduct, err error) {
  1221. sql := `SELECT * FROM longzhong_survey_product WHERE breed_name=? AND frequency=? ORDER BY survey_product_id ASC `
  1222. o := global.DbMap[utils.DbNameManualIndex]
  1223. err = o.Raw(sql, productName, frequency).Find(&items).Error
  1224. return
  1225. }
  1226. func GetExportLzSurveyProductByBreedIds(breedIds []string) (items []*LongzhongSurveyProduct, err error) {
  1227. if len(breedIds) == 0 {
  1228. return
  1229. }
  1230. field := ` survey_product_id, breed_id, breed_name, sample_name, custom, quota_name, lz_code, frequency, unit_name, end_date, input_value `
  1231. sql := `SELECT ` + field + ` FROM longzhong_survey_product WHERE breed_id IN (` + utils.GetOrmInReplace(len(breedIds)) + `) ORDER BY breed_id ASC, frequency ASC, survey_product_id ASC `
  1232. o := global.DbMap[utils.DbNameManualIndex]
  1233. err = o.Raw(sql, breedIds).Find(&items).Error
  1234. return
  1235. }
  1236. func GetLzFrequency(productName string) (items []*int, err error) {
  1237. sql := `SELECT DISTINCT frequency FROM longzhong_survey_product WHERE breed_name=? ORDER BY frequency`
  1238. o := global.DbMap[utils.DbNameManualIndex]
  1239. err = o.Raw(sql, productName).Find(&items).Error
  1240. return
  1241. }
  1242. // EdbInfoItem
  1243. type EdbInfoItem struct {
  1244. TradeCode string `gorm:"column:TRADE_CODE;autoIncrement:false;primaryKey" description:"指标编码"`
  1245. SecName string `gorm:"column:SEC_NAME;" description:"指标名称"`
  1246. Unit string `gorm:"column:UNIT;" description:"单位"`
  1247. Remark string `gorm:"column:REMARK;" description:"备注"`
  1248. Frequency string `description:"频度"`
  1249. ClassifyId int `description:"分类id"`
  1250. ClassifyName string `description:"分类名称"`
  1251. CreateDate string `description:"创建时间"`
  1252. UserId int `description:"录入用户id"`
  1253. UserName string `description:"录入用户名称"`
  1254. NoticeTime string `description:"通知时间"`
  1255. Mobile string `description:"录入者手机号"`
  1256. ModifyDate string `description:"待更新日期"`
  1257. ModifyTime string `description:"最近一次更新时间"`
  1258. Status string `description:"状态:未完成/完成"`
  1259. IsJoinEdb int8 `description:"指标库是否已添加:0-否;1-是"`
  1260. StartDate string `description:"数据开始日期"`
  1261. EndDate string `description:"数据结束日期"`
  1262. LatestValue float64 `description:"指标最新值"`
  1263. DataList []*Edbdata `gorm:"-" description:"指标数据列表"`
  1264. NextDataTimeList []*EdbDataNextDateTime `gorm:"-" description:"下期数据时间列表"`
  1265. }
  1266. // AfterFind 在该模型上设置钩子函数,把日期转成正确的string,所以查询函数只能用Find函数,First或者Scan是不会触发该函数的来获取数据
  1267. func (m *EdbInfoItem) AfterFind(db *gorm.DB) (err error) {
  1268. m.CreateDate = utils.GormDateStrToDateTimeStr(m.CreateDate)
  1269. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  1270. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  1271. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  1272. return
  1273. }
  1274. // ConvDate
  1275. func (m *EdbInfoItem) ConvDate() {
  1276. m.CreateDate = utils.GormDateStrToDateTimeStr(m.CreateDate)
  1277. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  1278. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  1279. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  1280. return
  1281. }
  1282. // GetTargetItemList 获取指标列表数据
  1283. func GetTargetItemList(classifyId, edbShowType int, frequency, keyword, tradeCode string, classifyIdStrList []string) (items []*EdbInfoItem, err error) {
  1284. o := global.DbMap[utils.DbNameManualIndex]
  1285. pars := make([]interface{}, 0)
  1286. sql := ` SELECT a.*,'' modify_date,'' STATUS FROM edbinfo AS a `
  1287. if edbShowType != 0 {
  1288. sql = ` SELECT a.*,b.DT,'' modify_date,'' STATUS FROM edbinfo AS a
  1289. left join edbdata b on a.TRADE_CODE=b.TRADE_CODE `
  1290. }
  1291. sql += ` WHERE a.classify_id>0 `
  1292. //如果没有分类id集合列表,那么就没有数据了,不用往下执行了,直接返回好了
  1293. if len(classifyIdStrList) <= 0 {
  1294. return
  1295. }
  1296. if len(classifyIdStrList) > 0 {
  1297. sql += ` AND a.classify_id in (` + strings.Join(classifyIdStrList, ",") + `) `
  1298. }
  1299. if classifyId > 0 {
  1300. sql += ` AND a.classify_id=` + strconv.Itoa(classifyId) + ` `
  1301. }
  1302. //频度
  1303. if frequency != "" {
  1304. sql += ` AND a.frequency="` + frequency + `" `
  1305. }
  1306. //关键字
  1307. if keyword != "" {
  1308. sql += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  1309. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  1310. }
  1311. //指定指标
  1312. if tradeCode != "" {
  1313. sql += ` AND a.TRADE_CODE = "` + tradeCode + `" `
  1314. }
  1315. //指标里面是否有数据
  1316. switch edbShowType {
  1317. case 1:
  1318. sql += ` AND b.CLOSE is not null `
  1319. case 2:
  1320. sql += ` AND b.CLOSE is null `
  1321. }
  1322. sql += ` GROUP BY a.TRADE_CODE `
  1323. //sql = sql + ` ORDER BY CONVERT(a.SEC_NAME USING gbk ) COLLATE gbk_chinese_ci ASC `
  1324. sql = sql + fmt.Sprintf(` ORDER BY %s COLLATE gbk_chinese_ci ASC `, utils.GenerateQuerySql(utils.ConvertColumn, &utils.QueryParam{ConvertColumn: "a.SEC_NAME"}))
  1325. err = o.Raw(sql, pars...).Find(&items).Error
  1326. return
  1327. }
  1328. // GetLzItemList 模糊查询隆众数据库指标列表
  1329. //func GetLzItemList(keyword string) (items []*data_manage.LongzhongSurveyProduct, err error) {
  1330. // o := global.DbMap[utils.DbNameManualIndex]
  1331. // sql := "SELECT * FROM longzhong_survey_product WHERE CONCAT(sample_name,breed_name,custom,quota_name,lz_code) LIKE ?"
  1332. // err = o.Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error
  1333. // return
  1334. //}
  1335. type lzSurveyData struct {
  1336. DataTime string `gorm:"column:data_time" description:"日期"`
  1337. InputValue string `gorm:"column:input_value" description:"值"`
  1338. }
  1339. // GetLzItemListByCode 根据code查询隆众数据列表
  1340. //func GetLzItemListByCode(lzCode string) (items []*lzSurveyData, err error) {
  1341. // o := global.DbMap[utils.DbNameManualIndex]
  1342. // sql := "SELECT * FROM longzhong_survey_data WHERE survey_product_id=? GROUP BY data_time DESC"
  1343. // err = o.Raw(sql, lzCode).Find(&items).Error
  1344. // return
  1345. //}
  1346. // GetEdbDataListByCodes 通过指标ID获取所有数据
  1347. func GetEdbDataListByCodes(tradeCode string) (items []*Edbdata, err error) {
  1348. sql := ` SELECT TRADE_CODE,DT,round(CLOSE,4) CLOSE,modify_time FROM edbdata WHERE TRADE_CODE IN(` + tradeCode + `) GROUP BY TRADE_CODE,DT ORDER BY DT DESC `
  1349. o := global.DbMap[utils.DbNameManualIndex]
  1350. err = o.Raw(sql).Find(&items).Error
  1351. return
  1352. }
  1353. // TargetItemListResp 指标数据结构体
  1354. type TargetItemListResp struct {
  1355. List []*EdbInfoItem
  1356. FrequencyList []string
  1357. }
  1358. // BatchDataDeleteReq 批量删除某日的指标数据请求结构体
  1359. type BatchDataDeleteReq struct {
  1360. CreateDate string `description:"创建日期"`
  1361. TradeCodeList []string `description:"指标唯一编码列表"`
  1362. }
  1363. // BatchDeleteEdbDataByDate 批量删除某日的指标数据
  1364. func BatchDeleteEdbDataByDate(tradeCodes, dt string, opUserId int) (err error) {
  1365. o := global.DbMap[utils.DbNameManualIndex]
  1366. var list []*Edbdata
  1367. sql := ` select * FROM edbdata WHERE TRADE_CODE in (` + tradeCodes + `) AND DT = ? `
  1368. err = o.Raw(sql, dt).Find(&list).Error
  1369. if err != nil {
  1370. return
  1371. }
  1372. deleteRecordList := make([]*EdbdataDeleteRecord, 0)
  1373. for _, edbDataInfo := range list {
  1374. deleteRecord := &EdbdataDeleteRecord{
  1375. TradeCode: edbDataInfo.TradeCode,
  1376. Dt: edbDataInfo.Dt,
  1377. Close: edbDataInfo.Close,
  1378. ModifyTime: time.Now(),
  1379. CreateTime: time.Now(),
  1380. SysUserId: opUserId,
  1381. }
  1382. deleteRecordList = append(deleteRecordList, deleteRecord)
  1383. }
  1384. if len(deleteRecordList) > 0 {
  1385. tmpErr := o.CreateInBatches(deleteRecordList, utils.MultiAddNum).Error
  1386. if tmpErr != nil {
  1387. err = tmpErr
  1388. return
  1389. }
  1390. }
  1391. sql = ` DELETE FROM edbdata WHERE TRADE_CODE in (` + tradeCodes + `) AND DT = ? `
  1392. err = o.Exec(sql, dt).Error
  1393. return
  1394. }
  1395. // BatchDeleteEdbData 批量删除指标数据
  1396. func BatchDeleteEdbData(tradeCode string, opUserId int) (err error) {
  1397. o := global.DbMap[utils.DbNameManualIndex]
  1398. var list []*Edbdata
  1399. sql := ` select * FROM edbdata WHERE TRADE_CODE = ? `
  1400. err = o.Raw(sql, tradeCode).Find(&list).Error
  1401. if err != nil {
  1402. return
  1403. }
  1404. deleteRecordList := make([]*EdbdataDeleteRecord, 0)
  1405. for _, edbDataInfo := range list {
  1406. deleteRecord := &EdbdataDeleteRecord{
  1407. TradeCode: edbDataInfo.TradeCode,
  1408. Dt: edbDataInfo.Dt,
  1409. Close: edbDataInfo.Close,
  1410. ModifyTime: time.Now(),
  1411. CreateTime: time.Now(),
  1412. SysUserId: opUserId,
  1413. }
  1414. deleteRecordList = append(deleteRecordList, deleteRecord)
  1415. }
  1416. err = o.CreateInBatches(deleteRecordList, utils.MultiAddNum).Error
  1417. if err != nil {
  1418. return
  1419. }
  1420. sql = ` DELETE FROM edbdata WHERE TRADE_CODE = ? `
  1421. err = o.Exec(sql, tradeCode).Error
  1422. return
  1423. }
  1424. // GetEdbInfoCountByClassifyId 根据指标分类id获取当前分类下的指标数量
  1425. func GetEdbInfoCountByClassifyId(classifyId int) (count int, err error) {
  1426. o := global.DbMap[utils.DbNameManualIndex]
  1427. sql := `SELECT COUNT(1) AS count FROM ( SELECT a.*,b.CLOSE FROM edbinfo AS a
  1428. INNER JOIN edbdata AS b ON a.TRADE_CODE=b.TRADE_CODE
  1429. WHERE a.classify_id=? group by a.TRADE_CODE) d `
  1430. var countNull sql2.NullInt64
  1431. err = o.Raw(sql, classifyId).Scan(&countNull).Error
  1432. if err != nil {
  1433. return
  1434. }
  1435. if countNull.Valid {
  1436. count = int(countNull.Int64)
  1437. }
  1438. return
  1439. }
  1440. // EdbInfoGroupCount 指标分类id获取当前分类下的指标数量
  1441. type EdbInfoGroupCount struct {
  1442. Count int
  1443. ClassifyId int
  1444. }
  1445. // GetEdbInfoGroupCountByClassifyIds 根据指标分类id获取当前分类下的指标数量
  1446. //func GetEdbInfoGroupCountByClassifyIds(classifyIdList []int) (list []*EdbInfoGroupCount, err error) {
  1447. // num := len(classifyIdList)
  1448. // if num <= 0 {
  1449. // return
  1450. // }
  1451. // o := global.DbMap[utils.DbNameManualIndex]
  1452. // sql := `SELECT COUNT(1) AS count,classify_id FROM ( SELECT a.*,b.CLOSE FROM edbinfo AS a
  1453. // INNER JOIN edbdata AS b ON a.TRADE_CODE=b.TRADE_CODE
  1454. // WHERE a.classify_id in (` + utils.GetOrmInReplace(num) + `) group by a.TRADE_CODE) d
  1455. // GROUP BY classify_id `
  1456. // err = o.Raw(sql, classifyIdList).Find(&list).Error
  1457. // return
  1458. //}
  1459. // GetExcelData 获取excel样式数据
  1460. //func GetExcelData() (list []*data_manage.ExcelStyle, err error) {
  1461. // o := global.DbMap[utils.DbNameManualIndex]
  1462. // sql := `SELECT * FROM excel_style `
  1463. // err = o.Raw(sql).QueryRows(&list)
  1464. // return
  1465. //}
  1466. // AddExcelData 添加excel样式数据
  1467. //func AddExcelData(item *data_manage.ExcelStyle) (id int64, err error) {
  1468. // o := global.DbMap[utils.DbNameManualIndex]
  1469. // id, err = o.Insert(item)
  1470. // return
  1471. //}
  1472. type EdbdataFloat struct {
  1473. TradeCode string `gorm:"column:TRADE_CODE;autoIncrement:false;primaryKey" description:"指标编码"`
  1474. Dt string `gorm:"column:DT" description:"日期"`
  1475. Close float64 `gorm:"column:CLOSE" description:"值"`
  1476. ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"`
  1477. }
  1478. //func GetTargetsDataFloat(tradeCode, dt string) (item *EdbdataFloat, err error) {
  1479. // sql := `SELECT * FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1480. // o := global.DbMap[utils.DbNameManualIndex]
  1481. // err = o.Raw(sql, tradeCode, dt).First(&item).Error
  1482. // return
  1483. //}
  1484. func ModifyEdbinfo(tradeCode, unit, frequency string, classifyId int) (err error) {
  1485. sql := `UPDATE edbinfo SET UNIT = ?,frequency=?, classify_id=?, create_date=NOW() WHERE TRADE_CODE=? `
  1486. o := global.DbMap[utils.DbNameManualIndex]
  1487. err = o.Exec(sql, unit, frequency, classifyId, tradeCode).Error
  1488. return
  1489. }
  1490. func DeleteTargetsDataByImport(tradeCode, dt string) (err error) {
  1491. sql := `DELETE FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1492. o := global.DbMap[utils.DbNameManualIndex]
  1493. err = o.Exec(sql, tradeCode, dt).Error
  1494. return
  1495. }
  1496. // GetEdbinfoListByCodeListGroupByUserId 根据指标code列表、用户分组获取指标信息
  1497. //func GetEdbinfoListByCodeListGroupByUserId(edbCodeList []string) (items []*Edbinfo, err error) {
  1498. // num := len(edbCodeList)
  1499. // if num <= 0 {
  1500. // return
  1501. // }
  1502. // o := global.DbMap[utils.DbNameManualIndex]
  1503. // sql := `SELECT * FROM edbinfo WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) GROUP BY user_id `
  1504. // err = o.Raw(sql, edbCodeList).Find(&items).Error
  1505. // return
  1506. //}
  1507. // GetEdbinfoListByCodeListByCodeIdList
  1508. // @Description: 根据指标code列表获取列表信息
  1509. // @param edbCodeList
  1510. // @return items
  1511. // @return err
  1512. func GetEdbinfoListByCodeListByCodeIdList(edbCodeList []string) (items []*Edbinfo, err error) {
  1513. num := len(edbCodeList)
  1514. if num <= 0 {
  1515. return
  1516. }
  1517. o := global.DbMap[utils.DbNameManualIndex]
  1518. sql := `SELECT * FROM edbinfo WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) `
  1519. err = o.Raw(sql, edbCodeList).Find(&items).Error
  1520. return
  1521. }
  1522. // GetEdbinfoListByCodeListByUserId
  1523. // @Description: 根据用户id列表获取指标列表信息
  1524. // @param userIdList
  1525. // @return items
  1526. // @return err
  1527. func GetEdbinfoListByCodeListByUserId(userIdList []int) (items []*Edbinfo, err error) {
  1528. num := len(userIdList)
  1529. if num <= 0 {
  1530. return
  1531. }
  1532. o := global.DbMap[utils.DbNameManualIndex]
  1533. sql := `SELECT * FROM edbinfo WHERE user_id in (` + utils.GetOrmInReplace(num) + `) `
  1534. err = o.Raw(sql, userIdList).Find(&items).Error
  1535. return
  1536. }
  1537. // ModifyEdbinfoUserIdByCodeList 根据指标code列表修改创建人
  1538. func ModifyEdbinfoUserIdByCodeList(edbCodeList []string, userId int, userName string) (err error) {
  1539. num := len(edbCodeList)
  1540. if num <= 0 {
  1541. return
  1542. }
  1543. o := global.DbMap[utils.DbNameManualIndex]
  1544. sql := `UPDATE edbinfo SET user_id = ?,user_name = ? WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) `
  1545. err = o.Exec(sql, userId, userName, edbCodeList).Error
  1546. return
  1547. }
  1548. // ModifyEdbinfoUserIdByOldUserId
  1549. // @Description: 根据旧用户id修改新用户id
  1550. // @author: Roc
  1551. // @datetime 2024-03-25 17:59:32
  1552. // @param oldUserId int
  1553. // @param userId int
  1554. // @return err error
  1555. func ModifyEdbinfoUserIdByOldUserId(oldUserIdList []int, userId int) (err error) {
  1556. num := len(oldUserIdList)
  1557. if num <= 0 {
  1558. return
  1559. }
  1560. o := global.DbMap[utils.DbNameManualIndex]
  1561. sql := `UPDATE edbinfo SET user_id=? WHERE user_id in (` + utils.GetOrmInReplace(num) + `) `
  1562. err = o.Exec(sql, userId, oldUserIdList).Error
  1563. return
  1564. }
  1565. func GetEdbInfoAdminList() (list []int, err error) {
  1566. sql := `SELECT user_id FROM edbinfo GROUP BY user_id `
  1567. o := global.DbMap[utils.DbNameManualIndex]
  1568. err = o.Raw(sql).Find(&list).Error
  1569. return
  1570. }
  1571. // GetAllChildManualEdbClassify
  1572. // @Description: 获取手工数据中所有的子分类
  1573. // @author: Roc
  1574. // @datetime 2024-07-16 13:27:28
  1575. // @return items []*EdbdataClassify
  1576. // @return err error
  1577. func GetAllChildManualEdbClassify() (items []*EdbdataClassify, err error) {
  1578. o := global.DbMap[utils.DbNameManualIndex]
  1579. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id > 0 `
  1580. err = o.Raw(sql).Find(&items).Error
  1581. return
  1582. }
  1583. // GetChildManualEdbClassifyByIdList
  1584. // @Description: 获取手工数据中所有的子分类
  1585. // @author: Roc
  1586. // @datetime 2024-07-16 13:33:57
  1587. // @param idList []int
  1588. // @return items []*EdbdataClassify
  1589. // @return err error
  1590. func GetChildManualEdbClassifyByIdList(idList []int) (items []*EdbdataClassify, err error) {
  1591. num := len(idList)
  1592. if num <= 0 {
  1593. return
  1594. }
  1595. o := global.DbMap[utils.DbNameManualIndex]
  1596. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1597. err = o.Raw(sql, idList).Find(&items).Error
  1598. return
  1599. }
  1600. // EdbinfoMaxMinDate
  1601. // @Description: 手工指标的最小最大日期
  1602. type EdbinfoMaxMinDate struct {
  1603. MinDate string
  1604. MaxDate string
  1605. }
  1606. func (mmDate *EdbinfoMaxMinDate) AfterFind(tx *gorm.DB) (err error) {
  1607. mmDate.MinDate = utils.GormDateStrToDateStr(mmDate.MinDate)
  1608. mmDate.MaxDate = utils.GormDateStrToDateStr(mmDate.MaxDate)
  1609. return
  1610. }
  1611. // GetEdbdataMaxMinDate
  1612. // @Description: 获取手工指标的最小最大日期
  1613. // @author: Roc
  1614. // @datetime 2024-08-01 14:27:20
  1615. // @param tradeCode string
  1616. // @return item EdbinfoMaxMinDate
  1617. // @return err error
  1618. func GetEdbdataMaxMinDate(tradeCode string) (item EdbinfoMaxMinDate, err error) {
  1619. o := global.DbMap[utils.DbNameManualIndex]
  1620. sql := ` SELECT MIN(DT) min_date,MAX(DT) max_date FROM edbdata WHERE TRADE_CODE = ? `
  1621. err = o.Raw(sql, tradeCode).First(&item).Error
  1622. return
  1623. }
  1624. // GetEdbdataLatestValue
  1625. // @Description: 获取手工数据的最新值
  1626. // @author: Roc
  1627. // @datetime 2024-08-02 10:33:22
  1628. // @param tradeCode string
  1629. // @return latestValue float64
  1630. // @return err error
  1631. func GetEdbdataLatestValue(tradeCode string) (latestValue float64, err error) {
  1632. o := global.DbMap[utils.DbNameManualIndex]
  1633. sql := ` SELECT CLOSE FROM edbdata WHERE TRADE_CODE = ? ORDER BY DT DESC LIMIT 1`
  1634. //err = o.Raw(sql, tradeCode).QueryRow(&latestValue)
  1635. var valueNull sql2.NullFloat64
  1636. err = o.Raw(sql, tradeCode).Scan(&valueNull).Error
  1637. if err != nil {
  1638. return
  1639. }
  1640. if valueNull.Valid {
  1641. latestValue = valueNull.Float64
  1642. }
  1643. return
  1644. }
  1645. // ModifyEdbinfoMaxMinDate
  1646. // @Description: 修改手工指标的最小最大日期
  1647. // @author: Roc
  1648. // @datetime 2024-08-01 15:33:45
  1649. // @param startDate string
  1650. // @param endDate string
  1651. // @param tradeCode string
  1652. // @return err error
  1653. func ModifyEdbinfoMaxMinDate(tradeCode, startDate, endDate string, latestValue float64) (err error) {
  1654. o := global.DbMap[utils.DbNameManualIndex]
  1655. sql := ` UPDATE edbinfo SET start_date = ?, end_date = ?, latest_value = ? , modify_time = now() WHERE TRADE_CODE = ? `
  1656. err = o.Exec(sql, startDate, endDate, latestValue, tradeCode).Error
  1657. return
  1658. }