target.go 59 KB

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