target.go 62 KB

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