target.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  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 `orm:"column(TRADE_CODE)" description:"指标编码"`
  16. SecName string `orm:"column(SEC_NAME)" description:"指标名称"`
  17. Unit string `orm:"column(UNIT)" description:"单位"`
  18. Remark string `orm:"column(REMARK)" description:"备注"`
  19. Frequency string `description:"频度"`
  20. ClassifyId int `description:"分类id"`
  21. ClassifyName string `description:"分类名称"`
  22. Dt string `orm:"column(DT)" description:"录入日期"`
  23. Close float64 `orm:"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 `orm:"column(TRADE_CODE);pk" description:"指标编码"`
  70. Dt string `orm:"column(DT)" description:"日期"`
  71. Close string `orm:"column(CLOSE)" description:"值"`
  72. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  73. }
  74. type EdbDataNextDateTime struct {
  75. TradeCode string `orm:"column(TRADE_CODE);pk" description:"指标编码"`
  76. Dt string `orm:"column(DT)" description:"日期"`
  77. Close string `orm:"column(CLOSE)" description:"值"`
  78. ModifyTime time.Time `orm:"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 `orm:"column(TRADE_CODE)" description:"指标编码"`
  114. Dt string `orm:"column(DT)" description:"日期"`
  115. Close string `orm:"column(CLOSE)" description:"值"`
  116. ModifyTime time.Time `orm:"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 `orm:"column(TRADE_CODE);pk" description:"指标code"`
  145. SecName string `orm:"column(SEC_NAME);" description:"指标名称"`
  146. Unit string `orm:"column(UNIT);" description:"单位"`
  147. Remark string `orm:"column(REMARK);" description:"备注"`
  148. Frequency string `orm:"column(frequency)" description:"频度"`
  149. ClassifyId int `orm:"column(classify_id)" description:"分类id"`
  150. ClassifyName string `orm:"-" description:"分类名称"`
  151. CreateDate string `orm:"column(create_date)" description:"创建时间"`
  152. UserId int `orm:"column(user_id)" description:"录入用户id"`
  153. UserName string `orm:"column(user_name)" description:"录入用户名称"`
  154. NoticeTime string `orm:"column(notice_time)" description:"通知时间"`
  155. Mobile string `orm:"column(mobile)" description:"录入者手机号"`
  156. Sort int `orm:"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" 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 `orm:"column(UNIT);" description:"单位" gorm:"column:UNIT"`
  268. Frequency string `orm:"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 `orm:"column(manual_user_classify_id);pk"`
  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 `orm:"column(TRADE_CODE)" description:"指标code"`
  531. SecName string `orm:"column(SEC_NAME)" description:"指标名称"`
  532. Unit string `orm:"column(UNIT)" description:"单位"`
  533. Frequency string `description:"频度"`
  534. ClassifyId int `description:"分类id"`
  535. NoticeTime string `description:"通知时间"`
  536. ClassifyName string
  537. Dt string `orm:"column(DT)" description:"日期"`
  538. Close float64 `orm:"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 `orm:"column(TRADE_CODE);pk" description:"指标code"`
  688. SecName string `orm:"column(SEC_NAME);" description:"指标名称"`
  689. Unit string `orm:"column(UNIT);" description:"单位"`
  690. Remark string `orm:"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. sql = sql + ` ORDER BY CONVERT(a.SEC_NAME USING gbk ) COLLATE gbk_chinese_ci ASC `
  722. err = o.Raw(sql).Find(&items).Error
  723. if err != nil {
  724. return
  725. }
  726. itemsLen := len(items)
  727. nowWeek := time.Now().Weekday().String()
  728. fmt.Println(nowWeek)
  729. finishEdbInfo := SortEdbInfo{}
  730. unFinishEdbInfo := SortEdbInfo{}
  731. for i := 0; i < itemsLen; i++ {
  732. noticeTime := items[i].NoticeTime
  733. frequency := items[i].Frequency
  734. tradeCode := items[i].TradeCode
  735. if noticeTime != "" {
  736. if frequency == "周度" {
  737. noticeArr := strings.Split(noticeTime, " ")
  738. noticeWeek := noticeArr[0]
  739. fmt.Println(noticeWeek)
  740. addDay := 0
  741. if nowWeek == "Sunday" {
  742. if noticeWeek == "周日" {
  743. addDay = 0
  744. } else if noticeWeek == "周一" {
  745. addDay = 1
  746. } else if noticeWeek == "周二" {
  747. addDay = 2
  748. } else if noticeWeek == "周三" {
  749. addDay = 3
  750. } else if noticeWeek == "周四" {
  751. addDay = 4
  752. } else if noticeWeek == "周五" {
  753. addDay = 5
  754. } else if noticeWeek == "周六" {
  755. addDay = 6
  756. } else {
  757. addDay = 0
  758. }
  759. } else if nowWeek == "Monday" {
  760. if noticeWeek == "周日" {
  761. addDay = 6
  762. } else if noticeWeek == "周一" {
  763. addDay = 0
  764. } else if noticeWeek == "周二" {
  765. addDay = 1
  766. } else if noticeWeek == "周三" {
  767. addDay = 2
  768. } else if noticeWeek == "周四" {
  769. addDay = 3
  770. } else if noticeWeek == "周五" {
  771. addDay = 4
  772. } else if noticeWeek == "周六" {
  773. addDay = 5
  774. } else {
  775. addDay = 0
  776. }
  777. } else if nowWeek == "Tuesday" {
  778. if noticeWeek == "周日" {
  779. addDay = 5
  780. } else if noticeWeek == "周一" {
  781. addDay = 6
  782. } else if noticeWeek == "周二" {
  783. addDay = 0
  784. } else if noticeWeek == "周三" {
  785. addDay = 1
  786. } else if noticeWeek == "周四" {
  787. addDay = 2
  788. } else if noticeWeek == "周五" {
  789. addDay = 3
  790. } else if noticeWeek == "周六" {
  791. addDay = 4
  792. } else {
  793. addDay = 0
  794. }
  795. } else if nowWeek == "Wednesday" {
  796. if noticeWeek == "周日" {
  797. addDay = 4
  798. } else if noticeWeek == "周一" {
  799. addDay = 5
  800. } else if noticeWeek == "周二" {
  801. addDay = 6
  802. } else if noticeWeek == "周三" {
  803. addDay = 0
  804. } else if noticeWeek == "周四" {
  805. addDay = 1
  806. } else if noticeWeek == "周五" {
  807. addDay = 2
  808. } else if noticeWeek == "周六" {
  809. addDay = 3
  810. } else {
  811. addDay = 0
  812. }
  813. } else if nowWeek == "Thursday" {
  814. if noticeWeek == "周日" {
  815. addDay = 3
  816. } else if noticeWeek == "周一" {
  817. addDay = 4
  818. } else if noticeWeek == "周二" {
  819. addDay = 5
  820. } else if noticeWeek == "周三" {
  821. addDay = 6
  822. } else if noticeWeek == "周四" {
  823. addDay = 0
  824. } else if noticeWeek == "周五" {
  825. addDay = 1
  826. } else if noticeWeek == "周六" {
  827. addDay = 2
  828. } else {
  829. addDay = 0
  830. }
  831. } else if nowWeek == "Friday" {
  832. if noticeWeek == "周日" {
  833. addDay = 2
  834. } else if noticeWeek == "周一" {
  835. addDay = 3
  836. } else if noticeWeek == "周二" {
  837. addDay = 4
  838. } else if noticeWeek == "周三" {
  839. addDay = 5
  840. } else if noticeWeek == "周四" {
  841. addDay = 6
  842. } else if noticeWeek == "周五" {
  843. addDay = 0
  844. } else if noticeWeek == "周六" {
  845. addDay = 1
  846. } else {
  847. addDay = 0
  848. }
  849. } else if nowWeek == "Saturday" {
  850. if noticeWeek == "周日" {
  851. addDay = 1
  852. } else if noticeWeek == "周一" {
  853. addDay = 2
  854. } else if noticeWeek == "周二" {
  855. addDay = 3
  856. } else if noticeWeek == "周三" {
  857. addDay = 4
  858. } else if noticeWeek == "周四" {
  859. addDay = 5
  860. } else if noticeWeek == "周五" {
  861. addDay = 6
  862. } else if noticeWeek == "周六" {
  863. addDay = 0
  864. } else {
  865. addDay = 0
  866. }
  867. }
  868. modifyDate := time.Now().AddDate(0, 0, addDay)
  869. modifyDateStr := modifyDate.Format(utils.FormatDate)
  870. items[i].ModifyDate = modifyDateStr
  871. modifyDateEndStr := modifyDate.AddDate(0, 0, -7).Format(utils.FormatDate)
  872. fmt.Println("addDay:", addDay)
  873. fmt.Println("modifyDateEndStr:", modifyDateEndStr)
  874. count := 0
  875. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  876. //err = o.Raw(sqlCount, tradeCode, modifyDateEndStr, modifyDateStr).QueryRow(&count)
  877. var countNull sql2.NullInt64
  878. err = o.Raw(sqlCount, tradeCode, modifyDateEndStr, modifyDateStr).Scan(&countNull).Error
  879. if err != nil {
  880. return nil, err
  881. }
  882. if countNull.Valid {
  883. count = int(countNull.Int64)
  884. }
  885. if count > 0 {
  886. items[i].Status = "完成"
  887. finishEdbInfo = append(finishEdbInfo, *items[i])
  888. } else {
  889. items[i].Status = "未完成"
  890. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  891. }
  892. } else if frequency == "日度" {
  893. items[i].Status = "完成"
  894. finishEdbInfo = append(finishEdbInfo, *items[i])
  895. } else if frequency == "月度" {
  896. myYear := time.Now().Year()
  897. myMonth := time.Now().Format("01")
  898. startDate, endDate := utils.GetMonthStartAndEnd(strconv.Itoa(myYear), myMonth)
  899. count := 0
  900. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  901. //err = o.Raw(sqlCount, tradeCode, startDate, endDate).QueryRow(&count)
  902. var countNull sql2.NullInt64
  903. err = o.Raw(sqlCount, tradeCode, startDate, endDate).Scan(&countNull).Error
  904. if err != nil {
  905. return nil, err
  906. }
  907. if countNull.Valid {
  908. count = int(countNull.Int64)
  909. }
  910. if noticeTime != "" {
  911. var modifyDateStr string
  912. strArr := strings.Split(noticeTime, "日")
  913. myYear := time.Now().Year()
  914. myMonth := time.Now().Format("01")
  915. modifyDateStr = strconv.Itoa(myYear) + "-" + myMonth + "-" + strArr[0]
  916. items[i].ModifyDate = modifyDateStr
  917. }
  918. if count > 0 {
  919. items[i].Status = "完成"
  920. finishEdbInfo = append(finishEdbInfo, *items[i])
  921. } else {
  922. items[i].Status = "未完成"
  923. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  924. }
  925. } else {
  926. items[i].Status = "完成"
  927. finishEdbInfo = append(finishEdbInfo, *items[i])
  928. }
  929. } else {
  930. if frequency == "月度" {
  931. myYear := time.Now().Year()
  932. myMonth := time.Now().Format("01")
  933. startDate, endDate := utils.GetMonthStartAndEnd(strconv.Itoa(myYear), myMonth)
  934. count := 0
  935. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  936. //err = o.Raw(sqlCount, tradeCode, startDate, endDate).QueryRow(&count)
  937. var countNull sql2.NullInt64
  938. err = o.Raw(sqlCount, tradeCode, startDate, endDate).Scan(&countNull).Error
  939. if err != nil {
  940. return nil, err
  941. }
  942. if countNull.Valid {
  943. count = int(countNull.Int64)
  944. }
  945. if count > 0 {
  946. items[i].Status = "完成"
  947. finishEdbInfo = append(finishEdbInfo, *items[i])
  948. } else {
  949. items[i].Status = "未完成"
  950. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  951. }
  952. } else {
  953. items[i].Status = "完成"
  954. finishEdbInfo = append(finishEdbInfo, *items[i])
  955. }
  956. }
  957. }
  958. sort.Sort(SortByModifyDate{finishEdbInfo})
  959. sort.Sort(SortByModifyDate{unFinishEdbInfo})
  960. lastItems = append(lastItems, unFinishEdbInfo...)
  961. lastItems = append(lastItems, finishEdbInfo...)
  962. return
  963. }
  964. // 获取此 slice 的长度
  965. func (p SortEdbInfo) Len() int { return len(p) }
  966. // 根据元素的状态降序排序
  967. func (p SortEdbInfo) Less(i, j int) bool {
  968. return p[i].Status > p[j].Status
  969. }
  970. // 交换数据
  971. func (p SortEdbInfo) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  972. // 嵌套结构体 将继承 SortEdbInfo 的所有属性和方法
  973. // 所以相当于SortByName 也实现了 Len() 和 Swap() 方法
  974. type SortByStatus struct{ SortEdbInfo }
  975. // 根据元素的姓名长度降序排序 (此处按照自己的业务逻辑写)
  976. func (p SortByStatus) Less(i, j int) bool {
  977. return len(p.SortEdbInfo[i].Status) > len(p.SortEdbInfo[j].Status)
  978. }
  979. type SortByModifyDate struct{ SortEdbInfo }
  980. // 根据元素的年龄降序排序 (此处按照自己的业务逻辑写)
  981. func (p SortByModifyDate) Less(i, j int) bool {
  982. return p.SortEdbInfo[i].ModifyDate > p.SortEdbInfo[j].ModifyDate
  983. }
  984. type DataCheckResp struct {
  985. Status int `description:"状态:1:该日期已存在数据,是否确认修改?,0:数据不存在"`
  986. Close string `description:"值"`
  987. }
  988. type TargetCheckResp struct {
  989. Status int `description:"状态:1:该指标有关联数据,请先删除数据,0:指标不存在关联数据,可直接删除"`
  990. }
  991. type EdbdataExportList struct {
  992. TradeCode string `orm:"column(TRADE_CODE);" description:"指标code"`
  993. SecName string `orm:"column(SEC_NAME);" description:"指标名称"`
  994. Unit string `orm:"column(UNIT);" description:"单位"`
  995. Remark string `orm:"column(REMARK);" description:"备注"`
  996. Frequency string `description:"频度"`
  997. ClassifyId int `description:"分类id"`
  998. ClassifyName string `description:"分类名称"`
  999. CreateDate string `description:"创建时间"`
  1000. Dt string `orm:"column(Dt);" description:"最新一次录入时间"`
  1001. }
  1002. func GetEdbdataSecName(condition string, pars []interface{}) (items []*EdbdataExportList, err error) {
  1003. //sql := `SELECT a.TRADE_CODE,a.SEC_NAME,a.frequency,a.UNIT,MAX(c.DT) AS Dt
  1004. // FROM edbdata AS c
  1005. // INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  1006. // INNER JOIN edbinfo_user AS d ON a.TRADE_CODE=d.TRADE_CODE
  1007. // LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  1008. // WHERE a.classify_id>0`
  1009. sql := `SELECT a.TRADE_CODE,a.SEC_NAME,a.frequency,a.UNIT,MAX(c.DT) AS Dt,b.classify_name
  1010. FROM edbdata AS c
  1011. INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  1012. LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  1013. WHERE a.classify_id>0`
  1014. if condition != "" {
  1015. sql += condition
  1016. }
  1017. sql += " GROUP BY a.TRADE_CODE ORDER BY a.TRADE_CODE ASC "
  1018. o := global.DbMap[utils.DbNameManualIndex]
  1019. err = o.Raw(sql, pars...).Find(&items).Error
  1020. return
  1021. }
  1022. func GetEdbDataFrequency(classifyId int) (items []*string, err error) {
  1023. sql := `SELECT DISTINCT frequency FROM edbinfo where classify_id=? AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  1024. o := global.DbMap[utils.DbNameManualIndex]
  1025. err = o.Raw(sql, classifyId).Find(&items).Error
  1026. return
  1027. }
  1028. // GetEdbDataFrequencyByClassifyIdList
  1029. // @Description: 根据分类列表获取所有的频度
  1030. // @author: Roc
  1031. // @datetime 2024-08-15 17:51:13
  1032. // @param classifyIdList []int
  1033. // @return items []*string
  1034. // @return err error
  1035. func GetEdbDataFrequencyByClassifyIdList(classifyIdList []int) (items []string, err error) {
  1036. num := len(classifyIdList)
  1037. if num <= 0 {
  1038. return
  1039. }
  1040. sql := `SELECT DISTINCT frequency FROM edbinfo where classify_id in (` + utils.GetOrmInReplace(num) + `) AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  1041. o := global.DbMap[utils.DbNameManualIndex]
  1042. err = o.Raw(sql, classifyIdList).Find(&items).Error
  1043. return
  1044. }
  1045. func GetEdbDataFrequencyByKeyord(keyword string) (items []string, err error) {
  1046. sql := `SELECT DISTINCT frequency FROM edbinfo where SEC_NAME=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  1047. o := global.DbMap[utils.DbNameManualIndex]
  1048. err = o.Raw(sql, keyword).Find(&items).Error
  1049. return
  1050. }
  1051. type EdbdataList struct {
  1052. Dt string `orm:"column(DT);" description:"录入时间"`
  1053. }
  1054. func GetEdbdataList(tradeCode string) (items []*EdbdataList, err error) {
  1055. sql := ` SELECT DT FROM edbdata WHERE TRADE_CODE IN(` + tradeCode + `) GROUP BY DT ORDER BY DT DESC `
  1056. o := global.DbMap[utils.DbNameManualIndex]
  1057. err = o.Raw(sql).Find(&items).Error
  1058. return
  1059. }
  1060. type EdbdataItem struct {
  1061. TradeCode string `orm:"column(TRADE_CODE);" description:"指标code"`
  1062. Dt string `orm:"column(DT);" description:"最新一次录入时间"`
  1063. Close float64 `orm:"column(CLOSE);" description:"值"`
  1064. }
  1065. func GetEdbdataValueByTradeCode(tradeCode, dt string) (item *EdbdataItem, err error) {
  1066. sql := ` SELECT TRADE_CODE,DT,CLOSE FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1067. o := global.DbMap[utils.DbNameManualIndex]
  1068. err = o.Raw(sql, tradeCode, dt).First(&item).Error
  1069. return
  1070. }
  1071. func GetEdbdataAllByTradeCode(tradeCode string) (items []*EdbdataItem, err error) {
  1072. sql := ` SELECT * FROM edbdata WHERE TRADE_CODE=? `
  1073. o := global.DbMap[utils.DbNameManualIndex]
  1074. err = o.Raw(sql, tradeCode).Find(&items).Error
  1075. return
  1076. }
  1077. func GetEdbdataClassifyByParentId(parentId int) (items []*EdbdataClassify, err error) {
  1078. sql := ` SELECT * FROM edbdata_classify WHERE parent_id=? `
  1079. o := global.DbMap[utils.DbNameManualIndex]
  1080. err = o.Raw(sql, parentId).Find(&items).Error
  1081. return
  1082. }
  1083. type LzPriceClassify struct {
  1084. ProductName string
  1085. }
  1086. func GetLzPriceClassify() (items []*LzPriceClassify, err error) {
  1087. sql := ` SELECT product_name FROM longzhongpriceinfo GROUP BY product_name ORDER BY product_name DESC `
  1088. o := global.DbMap[utils.DbNameManualIndex]
  1089. err = o.Raw(sql).Find(&items).Error
  1090. return
  1091. }
  1092. type Longzhongpriceinfo struct {
  1093. LongzhongpriceinfoId int `orm:"column(longzhongpriceinfo_id);pk"`
  1094. Standard string
  1095. ModelName string
  1096. Unit string
  1097. AreaName string
  1098. PriceType string
  1099. Memo string
  1100. PriceId string
  1101. ProductName string
  1102. InfoType string
  1103. InfoTypeRemark string
  1104. MarketName string
  1105. ManufactureName string
  1106. }
  1107. func GetLongzhongpriceinfoByClassifyName(productName string) (items []*Longzhongpriceinfo, err error) {
  1108. sql := `SELECT * FROM longzhongpriceinfo WHERE product_name=? ORDER BY longzhongpriceinfo_id ASC `
  1109. o := global.DbMap[utils.DbNameManualIndex]
  1110. err = o.Raw(sql, productName).Find(&items).Error
  1111. return
  1112. }
  1113. func GetLongzhongPriceDataMaxCount(productName string) (count int, err error) {
  1114. o := global.DbMap[utils.DbNameManualIndex]
  1115. sql := `SELECT MAX(t.num) AS count FROM (
  1116. SELECT COUNT(1) AS num FROM longzhongpriceinfo AS a
  1117. INNER JOIN longzhongpricedata AS b ON a.longzhongpriceinfo_id=b.longzhongpriceinfo_id
  1118. WHERE a.product_name=?
  1119. GROUP BY a.product_name
  1120. )AS t `
  1121. //err = o.Raw(sql, productName).QueryRow(&count)
  1122. var countNull sql2.NullInt64
  1123. err = o.Raw(sql, productName).Scan(&countNull).Error
  1124. if err != nil {
  1125. return
  1126. }
  1127. if countNull.Valid {
  1128. count = int(countNull.Int64)
  1129. }
  1130. return
  1131. }
  1132. type LongzhongpricedataItems struct {
  1133. LongzhongpricedataId int `orm:"column(longzhongpricedata_id);pk"`
  1134. LongzhongpriceinfoId int
  1135. PriceDate string
  1136. Memo string
  1137. Price float64
  1138. CnyPrice float64
  1139. ZsyPrice float64
  1140. ZshPrice float64
  1141. LowPrice float64
  1142. HighPrice float64
  1143. RisePrice float64
  1144. TonPrice float64
  1145. PriceType string
  1146. UpdateDate string
  1147. }
  1148. func GetLongzhongPriceDataById(lzPriceInfoId int) (items []*LongzhongpricedataItems, err error) {
  1149. o := global.DbMap[utils.DbNameManualIndex]
  1150. 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
  1151. FROM longzhongpricedata AS a
  1152. WHERE longzhongpriceinfo_id=? ORDER BY price_date DESC `
  1153. err = o.Raw(sql, lzPriceInfoId).Find(&items).Error
  1154. return
  1155. }
  1156. func GetLzSurveyClassify() (items []*LzPriceClassify, err error) {
  1157. sql := ` SELECT breed_name AS product_name FROM longzhong_survey_product GROUP BY breed_name ORDER BY breed_name DESC `
  1158. o := global.DbMap[utils.DbNameManualIndex]
  1159. err = o.Raw(sql).Find(&items).Error
  1160. return
  1161. }
  1162. type LongzhongSurveyProduct struct {
  1163. SurveyProductId int `orm:"column(survey_product_id);pk"`
  1164. ProjectQuotaId int64
  1165. BreedId string
  1166. BreedName string
  1167. QuotaId string
  1168. QuotaName string
  1169. UnitId string
  1170. UnitName string
  1171. SampleType int64
  1172. SampleId string
  1173. SampleName string
  1174. DeviceId string
  1175. Device string
  1176. ProductCraftId string
  1177. ProductCraft string
  1178. ProductLine string
  1179. InputMode int64
  1180. Frequency int64
  1181. InputValue string
  1182. TaskShouldFinishTime int
  1183. CustomId string
  1184. CustomType int64
  1185. Custom string
  1186. QuotaSampleId int64
  1187. StartDate string
  1188. EndDate string
  1189. LzCode string
  1190. }
  1191. func GetLongzhongSurveyProductByClassifyName(productName string) (items []*LongzhongSurveyProduct, err error) {
  1192. sql := `SELECT * FROM longzhong_survey_product WHERE breed_name=? ORDER BY survey_product_id ASC `
  1193. o := global.DbMap[utils.DbNameManualIndex]
  1194. err = o.Raw(sql, productName).Find(&items).Error
  1195. return
  1196. }
  1197. func GetLzSurveyProductByNameAndFrequency(productName string, frequency int) (items []*LongzhongSurveyProduct, err error) {
  1198. sql := `SELECT * FROM longzhong_survey_product WHERE breed_name=? AND frequency=? ORDER BY survey_product_id ASC `
  1199. o := global.DbMap[utils.DbNameManualIndex]
  1200. err = o.Raw(sql, productName, frequency).Find(&items).Error
  1201. return
  1202. }
  1203. func GetExportLzSurveyProductByBreedIds(breedIds []string) (items []*LongzhongSurveyProduct, err error) {
  1204. if len(breedIds) == 0 {
  1205. return
  1206. }
  1207. field := ` survey_product_id, breed_id, breed_name, sample_name, custom, quota_name, lz_code, frequency, unit_name, end_date, input_value `
  1208. 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 `
  1209. o := global.DbMap[utils.DbNameManualIndex]
  1210. err = o.Raw(sql, breedIds).Find(&items).Error
  1211. return
  1212. }
  1213. func GetLzFrequency(productName string) (items []*int, err error) {
  1214. sql := `SELECT DISTINCT frequency FROM longzhong_survey_product WHERE breed_name=? ORDER BY frequency`
  1215. o := global.DbMap[utils.DbNameManualIndex]
  1216. err = o.Raw(sql, productName).Find(&items).Error
  1217. return
  1218. }
  1219. // EdbInfoItem
  1220. type EdbInfoItem struct {
  1221. TradeCode string `orm:"column(TRADE_CODE);pk" description:"指标code"`
  1222. SecName string `orm:"column(SEC_NAME);" description:"指标名称"`
  1223. Unit string `orm:"column(UNIT);" description:"单位"`
  1224. Remark string `orm:"column(REMARK);" description:"备注"`
  1225. Frequency string `description:"频度"`
  1226. ClassifyId int `description:"分类id"`
  1227. ClassifyName string `description:"分类名称"`
  1228. CreateDate string `description:"创建时间"`
  1229. UserId int `description:"录入用户id"`
  1230. UserName string `description:"录入用户名称"`
  1231. NoticeTime string `description:"通知时间"`
  1232. Mobile string `description:"录入者手机号"`
  1233. ModifyDate string `description:"待更新日期"`
  1234. ModifyTime string `description:"最近一次更新时间"`
  1235. Status string `description:"状态:未完成/完成"`
  1236. IsJoinEdb int8 `description:"指标库是否已添加:0-否;1-是"`
  1237. StartDate string `description:"数据开始日期"`
  1238. EndDate string `description:"数据结束日期"`
  1239. LatestValue float64 `description:"指标最新值"`
  1240. DataList []*Edbdata `description:"指标数据列表"`
  1241. NextDataTimeList []*EdbDataNextDateTime `description:"下期数据时间列表"`
  1242. }
  1243. // GetTargetItemList 获取指标列表数据
  1244. func GetTargetItemList(classifyId, edbShowType int, frequency, keyword, tradeCode string, classifyIdStrList []string) (items []*EdbInfoItem, err error) {
  1245. o := global.DbMap[utils.DbNameManualIndex]
  1246. pars := make([]interface{}, 0)
  1247. sql := ` SELECT a.*,'' modify_date,'' STATUS FROM edbinfo AS a `
  1248. if edbShowType != 0 {
  1249. sql = ` SELECT a.*,b.DT,'' modify_date,'' STATUS FROM edbinfo AS a
  1250. left join edbdata b on a.TRADE_CODE=b.TRADE_CODE `
  1251. }
  1252. sql += ` WHERE a.classify_id>0 `
  1253. //如果没有分类id集合列表,那么就没有数据了,不用往下执行了,直接返回好了
  1254. if len(classifyIdStrList) <= 0 {
  1255. return
  1256. }
  1257. if len(classifyIdStrList) > 0 {
  1258. sql += ` AND a.classify_id in (` + strings.Join(classifyIdStrList, ",") + `) `
  1259. }
  1260. if classifyId > 0 {
  1261. sql += ` AND a.classify_id=` + strconv.Itoa(classifyId) + ` `
  1262. }
  1263. //频度
  1264. if frequency != "" {
  1265. sql += ` AND a.frequency="` + frequency + `" `
  1266. }
  1267. //关键字
  1268. if keyword != "" {
  1269. sql += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  1270. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  1271. }
  1272. //指定指标
  1273. if tradeCode != "" {
  1274. sql += ` AND a.TRADE_CODE = "` + tradeCode + `" `
  1275. }
  1276. //指标里面是否有数据
  1277. switch edbShowType {
  1278. case 1:
  1279. sql += ` AND b.CLOSE is not null `
  1280. case 2:
  1281. sql += ` AND b.CLOSE is null `
  1282. }
  1283. sql += ` GROUP BY a.TRADE_CODE `
  1284. sql = sql + ` ORDER BY CONVERT(a.SEC_NAME USING gbk ) COLLATE gbk_chinese_ci ASC `
  1285. err = o.Raw(sql, pars...).Find(&items).Error
  1286. return
  1287. }
  1288. // GetLzItemList 模糊查询隆众数据库指标列表
  1289. //func GetLzItemList(keyword string) (items []*data_manage.LongzhongSurveyProduct, err error) {
  1290. // o := global.DbMap[utils.DbNameManualIndex]
  1291. // sql := "SELECT * FROM longzhong_survey_product WHERE CONCAT(sample_name,breed_name,custom,quota_name,lz_code) LIKE ?"
  1292. // err = o.Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error
  1293. // return
  1294. //}
  1295. type lzSurveyData struct {
  1296. DataTime string `orm:"column(data_time)" description:"日期"`
  1297. InputValue string `orm:"column(input_value)" description:"值"`
  1298. }
  1299. // GetLzItemListByCode 根据code查询隆众数据列表
  1300. //func GetLzItemListByCode(lzCode string) (items []*lzSurveyData, err error) {
  1301. // o := global.DbMap[utils.DbNameManualIndex]
  1302. // sql := "SELECT * FROM longzhong_survey_data WHERE survey_product_id=? GROUP BY data_time DESC"
  1303. // err = o.Raw(sql, lzCode).Find(&items).Error
  1304. // return
  1305. //}
  1306. // GetEdbDataListByCodes 通过指标ID获取所有数据
  1307. func GetEdbDataListByCodes(tradeCode string) (items []*Edbdata, err error) {
  1308. 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 `
  1309. o := global.DbMap[utils.DbNameManualIndex]
  1310. err = o.Raw(sql).Find(&items).Error
  1311. return
  1312. }
  1313. // TargetItemListResp 指标数据结构体
  1314. type TargetItemListResp struct {
  1315. List []*EdbInfoItem
  1316. FrequencyList []string
  1317. }
  1318. // BatchDataDeleteReq 批量删除某日的指标数据请求结构体
  1319. type BatchDataDeleteReq struct {
  1320. CreateDate string `description:"创建日期"`
  1321. TradeCodeList []string `description:"指标唯一编码列表"`
  1322. }
  1323. // BatchDeleteEdbDataByDate 批量删除某日的指标数据
  1324. func BatchDeleteEdbDataByDate(tradeCodes, dt string, opUserId int) (err error) {
  1325. o := global.DbMap[utils.DbNameManualIndex]
  1326. var list []*Edbdata
  1327. sql := ` select * FROM edbdata WHERE TRADE_CODE in (` + tradeCodes + `) AND DT = ? `
  1328. err = o.Raw(sql, dt).Find(&list).Error
  1329. if err != nil {
  1330. return
  1331. }
  1332. deleteRecordList := make([]*EdbdataDeleteRecord, 0)
  1333. for _, edbDataInfo := range list {
  1334. deleteRecord := &EdbdataDeleteRecord{
  1335. TradeCode: edbDataInfo.TradeCode,
  1336. Dt: edbDataInfo.Dt,
  1337. Close: edbDataInfo.Close,
  1338. ModifyTime: time.Now(),
  1339. CreateTime: time.Now(),
  1340. SysUserId: opUserId,
  1341. }
  1342. deleteRecordList = append(deleteRecordList, deleteRecord)
  1343. }
  1344. if len(deleteRecordList) > 0 {
  1345. tmpErr := o.CreateInBatches(deleteRecordList, utils.MultiAddNum).Error
  1346. if tmpErr != nil {
  1347. err = tmpErr
  1348. return
  1349. }
  1350. }
  1351. sql = ` DELETE FROM edbdata WHERE TRADE_CODE in (` + tradeCodes + `) AND DT = ? `
  1352. err = o.Exec(sql, dt).Error
  1353. return
  1354. }
  1355. // BatchDeleteEdbData 批量删除指标数据
  1356. func BatchDeleteEdbData(tradeCode string, opUserId int) (err error) {
  1357. o := global.DbMap[utils.DbNameManualIndex]
  1358. var list []*Edbdata
  1359. sql := ` select * FROM edbdata WHERE TRADE_CODE = ? `
  1360. err = o.Raw(sql, tradeCode).Find(&list).Error
  1361. if err != nil {
  1362. return
  1363. }
  1364. deleteRecordList := make([]*EdbdataDeleteRecord, 0)
  1365. for _, edbDataInfo := range list {
  1366. deleteRecord := &EdbdataDeleteRecord{
  1367. TradeCode: edbDataInfo.TradeCode,
  1368. Dt: edbDataInfo.Dt,
  1369. Close: edbDataInfo.Close,
  1370. ModifyTime: time.Now(),
  1371. CreateTime: time.Now(),
  1372. SysUserId: opUserId,
  1373. }
  1374. deleteRecordList = append(deleteRecordList, deleteRecord)
  1375. }
  1376. err = o.CreateInBatches(deleteRecordList, utils.MultiAddNum).Error
  1377. if err != nil {
  1378. return
  1379. }
  1380. sql = ` DELETE FROM edbdata WHERE TRADE_CODE = ? `
  1381. err = o.Exec(sql, tradeCode).Error
  1382. return
  1383. }
  1384. // GetEdbInfoCountByClassifyId 根据指标分类id获取当前分类下的指标数量
  1385. func GetEdbInfoCountByClassifyId(classifyId int) (count int, err error) {
  1386. o := global.DbMap[utils.DbNameManualIndex]
  1387. sql := `SELECT COUNT(1) AS count FROM ( SELECT a.*,b.CLOSE FROM edbinfo AS a
  1388. INNER JOIN edbdata AS b ON a.TRADE_CODE=b.TRADE_CODE
  1389. WHERE a.classify_id=? group by a.TRADE_CODE) d `
  1390. var countNull sql2.NullInt64
  1391. err = o.Raw(sql, classifyId).Scan(&countNull).Error
  1392. if err != nil {
  1393. return
  1394. }
  1395. if countNull.Valid {
  1396. count = int(countNull.Int64)
  1397. }
  1398. return
  1399. }
  1400. // EdbInfoGroupCount 指标分类id获取当前分类下的指标数量
  1401. type EdbInfoGroupCount struct {
  1402. Count int
  1403. ClassifyId int
  1404. }
  1405. // GetEdbInfoGroupCountByClassifyIds 根据指标分类id获取当前分类下的指标数量
  1406. //func GetEdbInfoGroupCountByClassifyIds(classifyIdList []int) (list []*EdbInfoGroupCount, err error) {
  1407. // num := len(classifyIdList)
  1408. // if num <= 0 {
  1409. // return
  1410. // }
  1411. // o := global.DbMap[utils.DbNameManualIndex]
  1412. // sql := `SELECT COUNT(1) AS count,classify_id FROM ( SELECT a.*,b.CLOSE FROM edbinfo AS a
  1413. // INNER JOIN edbdata AS b ON a.TRADE_CODE=b.TRADE_CODE
  1414. // WHERE a.classify_id in (` + utils.GetOrmInReplace(num) + `) group by a.TRADE_CODE) d
  1415. // GROUP BY classify_id `
  1416. // err = o.Raw(sql, classifyIdList).Find(&list).Error
  1417. // return
  1418. //}
  1419. // GetExcelData 获取excel样式数据
  1420. //func GetExcelData() (list []*data_manage.ExcelStyle, err error) {
  1421. // o := global.DbMap[utils.DbNameManualIndex]
  1422. // sql := `SELECT * FROM excel_style `
  1423. // err = o.Raw(sql).QueryRows(&list)
  1424. // return
  1425. //}
  1426. // AddExcelData 添加excel样式数据
  1427. //func AddExcelData(item *data_manage.ExcelStyle) (id int64, err error) {
  1428. // o := global.DbMap[utils.DbNameManualIndex]
  1429. // id, err = o.Insert(item)
  1430. // return
  1431. //}
  1432. type EdbdataFloat struct {
  1433. TradeCode string `orm:"column(TRADE_CODE);pk" description:"指标编码"`
  1434. Dt string `orm:"column(DT)" description:"日期"`
  1435. Close float64 `orm:"column(CLOSE)" description:"值"`
  1436. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  1437. }
  1438. //func GetTargetsDataFloat(tradeCode, dt string) (item *EdbdataFloat, err error) {
  1439. // sql := `SELECT * FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1440. // o := global.DbMap[utils.DbNameManualIndex]
  1441. // err = o.Raw(sql, tradeCode, dt).First(&item).Error
  1442. // return
  1443. //}
  1444. func ModifyEdbinfo(tradeCode, unit, frequency string, classifyId int) (err error) {
  1445. sql := `UPDATE edbinfo SET UNIT = ?,frequency=?, classify_id=?, create_date=NOW() WHERE TRADE_CODE=? `
  1446. o := global.DbMap[utils.DbNameManualIndex]
  1447. err = o.Exec(sql, unit, frequency, classifyId, tradeCode).Error
  1448. return
  1449. }
  1450. func DeleteTargetsDataByImport(tradeCode, dt string) (err error) {
  1451. sql := `DELETE FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1452. o := global.DbMap[utils.DbNameManualIndex]
  1453. err = o.Exec(sql, tradeCode, dt).Error
  1454. return
  1455. }
  1456. // GetEdbinfoListByCodeListGroupByUserId 根据指标code列表、用户分组获取指标信息
  1457. //func GetEdbinfoListByCodeListGroupByUserId(edbCodeList []string) (items []*Edbinfo, err error) {
  1458. // num := len(edbCodeList)
  1459. // if num <= 0 {
  1460. // return
  1461. // }
  1462. // o := global.DbMap[utils.DbNameManualIndex]
  1463. // sql := `SELECT * FROM edbinfo WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) GROUP BY user_id `
  1464. // err = o.Raw(sql, edbCodeList).Find(&items).Error
  1465. // return
  1466. //}
  1467. // GetEdbinfoListByCodeListByCodeIdList
  1468. // @Description: 根据指标code列表获取列表信息
  1469. // @param edbCodeList
  1470. // @return items
  1471. // @return err
  1472. func GetEdbinfoListByCodeListByCodeIdList(edbCodeList []string) (items []*Edbinfo, err error) {
  1473. num := len(edbCodeList)
  1474. if num <= 0 {
  1475. return
  1476. }
  1477. o := global.DbMap[utils.DbNameManualIndex]
  1478. sql := `SELECT * FROM edbinfo WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) `
  1479. err = o.Raw(sql, edbCodeList).Find(&items).Error
  1480. return
  1481. }
  1482. // GetEdbinfoListByCodeListByUserId
  1483. // @Description: 根据用户id列表获取指标列表信息
  1484. // @param userIdList
  1485. // @return items
  1486. // @return err
  1487. func GetEdbinfoListByCodeListByUserId(userIdList []int) (items []*Edbinfo, err error) {
  1488. num := len(userIdList)
  1489. if num <= 0 {
  1490. return
  1491. }
  1492. o := global.DbMap[utils.DbNameManualIndex]
  1493. sql := `SELECT * FROM edbinfo WHERE user_id in (` + utils.GetOrmInReplace(num) + `) `
  1494. err = o.Raw(sql, userIdList).Find(&items).Error
  1495. return
  1496. }
  1497. // ModifyEdbinfoUserIdByCodeList 根据指标code列表修改创建人
  1498. func ModifyEdbinfoUserIdByCodeList(edbCodeList []string, userId int, userName string) (err error) {
  1499. num := len(edbCodeList)
  1500. if num <= 0 {
  1501. return
  1502. }
  1503. o := global.DbMap[utils.DbNameManualIndex]
  1504. sql := `UPDATE edbinfo SET user_id = ?,user_name = ? WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) `
  1505. err = o.Exec(sql, userId, userName, edbCodeList).Error
  1506. return
  1507. }
  1508. // ModifyEdbinfoUserIdByOldUserId
  1509. // @Description: 根据旧用户id修改新用户id
  1510. // @author: Roc
  1511. // @datetime 2024-03-25 17:59:32
  1512. // @param oldUserId int
  1513. // @param userId int
  1514. // @return err error
  1515. func ModifyEdbinfoUserIdByOldUserId(oldUserIdList []int, userId int) (err error) {
  1516. num := len(oldUserIdList)
  1517. if num <= 0 {
  1518. return
  1519. }
  1520. o := global.DbMap[utils.DbNameManualIndex]
  1521. sql := `UPDATE edbinfo SET user_id=? WHERE user_id in (` + utils.GetOrmInReplace(num) + `) `
  1522. err = o.Exec(sql, userId, oldUserIdList).Error
  1523. return
  1524. }
  1525. func GetEdbInfoAdminList() (list []int, err error) {
  1526. sql := `SELECT user_id FROM edbinfo GROUP BY user_id `
  1527. o := global.DbMap[utils.DbNameManualIndex]
  1528. err = o.Raw(sql).Find(&list).Error
  1529. return
  1530. }
  1531. // GetAllChildManualEdbClassify
  1532. // @Description: 获取手工数据中所有的子分类
  1533. // @author: Roc
  1534. // @datetime 2024-07-16 13:27:28
  1535. // @return items []*EdbdataClassify
  1536. // @return err error
  1537. func GetAllChildManualEdbClassify() (items []*EdbdataClassify, err error) {
  1538. o := global.DbMap[utils.DbNameManualIndex]
  1539. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id > 0 `
  1540. err = o.Raw(sql).Find(&items).Error
  1541. return
  1542. }
  1543. // GetChildManualEdbClassifyByIdList
  1544. // @Description: 获取手工数据中所有的子分类
  1545. // @author: Roc
  1546. // @datetime 2024-07-16 13:33:57
  1547. // @param idList []int
  1548. // @return items []*EdbdataClassify
  1549. // @return err error
  1550. func GetChildManualEdbClassifyByIdList(idList []int) (items []*EdbdataClassify, err error) {
  1551. num := len(idList)
  1552. if num <= 0 {
  1553. return
  1554. }
  1555. o := global.DbMap[utils.DbNameManualIndex]
  1556. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE classify_id in (` + utils.GetOrmInReplace(num) + `) `
  1557. err = o.Raw(sql, idList).Find(&items).Error
  1558. return
  1559. }
  1560. // EdbinfoMaxMinDate
  1561. // @Description: 手工指标的最小最大日期
  1562. type EdbinfoMaxMinDate struct {
  1563. MinDate string
  1564. MaxDate string
  1565. }
  1566. func (mmDate *EdbinfoMaxMinDate) AfterFind(tx *gorm.DB) (err error) {
  1567. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  1568. if mmDate.MinDate != "" {
  1569. mmDate.MinDate = utils.GormDateStrToDateStr(mmDate.MinDate)
  1570. }
  1571. if mmDate.MaxDate != "" {
  1572. mmDate.MaxDate = utils.GormDateStrToDateStr(mmDate.MaxDate)
  1573. }
  1574. }
  1575. return
  1576. }
  1577. // GetEdbdataMaxMinDate
  1578. // @Description: 获取手工指标的最小最大日期
  1579. // @author: Roc
  1580. // @datetime 2024-08-01 14:27:20
  1581. // @param tradeCode string
  1582. // @return item EdbinfoMaxMinDate
  1583. // @return err error
  1584. func GetEdbdataMaxMinDate(tradeCode string) (item EdbinfoMaxMinDate, err error) {
  1585. o := global.DbMap[utils.DbNameManualIndex]
  1586. sql := ` SELECT MIN(DT) min_date,MAX(DT) max_date FROM edbdata WHERE TRADE_CODE = ? `
  1587. err = o.Raw(sql, tradeCode).First(&item).Error
  1588. return
  1589. }
  1590. // GetEdbdataLatestValue
  1591. // @Description: 获取手工数据的最新值
  1592. // @author: Roc
  1593. // @datetime 2024-08-02 10:33:22
  1594. // @param tradeCode string
  1595. // @return latestValue float64
  1596. // @return err error
  1597. func GetEdbdataLatestValue(tradeCode string) (latestValue float64, err error) {
  1598. o := global.DbMap[utils.DbNameManualIndex]
  1599. sql := ` SELECT CLOSE FROM edbdata WHERE TRADE_CODE = ? ORDER BY DT DESC LIMIT 1`
  1600. //err = o.Raw(sql, tradeCode).QueryRow(&latestValue)
  1601. var valueNull sql2.NullFloat64
  1602. err = o.Raw(sql, tradeCode).Scan(&valueNull).Error
  1603. if err != nil {
  1604. return
  1605. }
  1606. if valueNull.Valid {
  1607. latestValue = valueNull.Float64
  1608. }
  1609. return
  1610. }
  1611. // ModifyEdbinfoMaxMinDate
  1612. // @Description: 修改手工指标的最小最大日期
  1613. // @author: Roc
  1614. // @datetime 2024-08-01 15:33:45
  1615. // @param startDate string
  1616. // @param endDate string
  1617. // @param tradeCode string
  1618. // @return err error
  1619. func ModifyEdbinfoMaxMinDate(tradeCode, startDate, endDate string, latestValue float64) (err error) {
  1620. o := global.DbMap[utils.DbNameManualIndex]
  1621. sql := ` UPDATE edbinfo SET start_date = ?, end_date = ?, latest_value = ? , modify_time = now() WHERE TRADE_CODE = ? `
  1622. err = o.Exec(sql, startDate, endDate, latestValue, tradeCode).Error
  1623. return
  1624. }