target.go 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. package models
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "sort"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. )
  12. type DataList struct {
  13. TradeCode string `gorm:"column:TRADE_CODE;type:varchar(255);not null" description:"指标编码"`
  14. SecName string `gorm:"column:SEC_NAME;type:varchar(255);not null" description:"指标名称"`
  15. Unit string `gorm:"column:UNIT;type:varchar(255);not null" description:"单位"`
  16. Remark string `gorm:"column:REMARK;type:varchar(255);not null" description:"备注"`
  17. Frequency string `gorm:"type:varchar(255);not null" description:"频度"`
  18. ClassifyId int `gorm:"type:int;not null" description:"分类id"`
  19. ClassifyName string `gorm:"type:varchar(255);not null" description:"分类名称"`
  20. Dt string `gorm:"column:DT;type:varchar(255);not null" description:"录入日期"`
  21. Close float64 `gorm:"column:CLOSE;type:float;not null" description:"录入值"`
  22. ModifyTime string `gorm:"type:varchar(255);not null" description:"修改时间"`
  23. }
  24. type DataListResp struct {
  25. List []*DataList
  26. Paging *paging.PagingItem `description:"分页数据"`
  27. }
  28. func GetDataList(condition string, pars []interface{}, startSize, pageSize int) (items []*DataList, err error) {
  29. 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
  30. inner join edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  31. left join edbdata_classify AS b ON a.classify_id=b.classify_id
  32. where a.classify_id>0`
  33. if condition != "" {
  34. sql += condition
  35. }
  36. sql += ` order by c.DT desc limit ?,? `
  37. pars = append(pars, startSize)
  38. pars = append(pars, pageSize)
  39. err = global.DmSQL["edb"].Raw(sql, pars...).Find(&items).Error
  40. return
  41. }
  42. func GetDataListCount(condition string, pars []interface{}) (count int, err error) {
  43. sql := ` select count(1) as count FROM edbdata AS c
  44. inner join edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  45. left join edbdata_classify AS b ON a.classify_id=b.classify_id
  46. where a.classify_id>0 `
  47. if condition != "" {
  48. sql += condition
  49. }
  50. err = global.DmSQL["edb"].Raw(sql, pars...).Scan(&count).Error
  51. return
  52. }
  53. type DataAddReq struct {
  54. TradeCode string `description:"指标唯一编码"`
  55. CreateDate string `description:"创建日期"`
  56. Close string `description:"录入值"`
  57. }
  58. type Edbdata struct {
  59. TradeCode string `gorm:"column:TRADE_CODE;primaryKey;type:varchar(255)" orm:"column(TRADE_CODE);pk" gorm:"primaryKey" description:"指标编码"`
  60. Dt string `gorm:"column:DT;type:varchar(255)" orm:"column(DT)" description:"日期"`
  61. Close string `gorm:"column:CLOSE;type:varchar(255)" orm:"column(CLOSE)" description:"值"`
  62. ModifyTime time.Time `gorm:"column:modify_time;type:timestamp" orm:"column(modify_time)" description:"修改时间"`
  63. }
  64. func GetDataInfo(tradeCode, creteDate string) (item *Edbdata, err error) {
  65. sql := " SELECT * FROM edbdata WHERE TRADE_CODE=? AND DT=? "
  66. err = global.DmSQL["edb"].Raw(sql, tradeCode, creteDate).First(&item).Error
  67. return
  68. }
  69. func AddEdbdata(item *Edbdata) (lastId int64, err error) {
  70. err = global.DmSQL["edb"].Create(item).Error
  71. if err != nil {
  72. return
  73. }
  74. return
  75. }
  76. type DataEditReq struct {
  77. TradeCode string `description:"指标唯一编码"`
  78. CreateDate string `description:"创建日期"`
  79. Close interface{} `description:"录入值"`
  80. OldCreateDate string `description:"旧的录入日期"`
  81. }
  82. // BatchDataEditReq 批量修改指标
  83. type BatchDataEditReq struct {
  84. OldCreateDate string `description:"旧的录入日期"`
  85. CreateDate string `description:"新的录入日期"`
  86. List []DataEditReq `description:"需要修改的数据"`
  87. }
  88. // 编辑数据
  89. func EditEdbdata(item *Edbdata) (err error) {
  90. sql := ` UPDATE edbdata SET CLOSE = ?,modify_time=NOW() WHERE TRADE_CODE = ? AND DT = ? `
  91. err = global.DmSQL["edb"].Exec(sql, item.Close, item.TradeCode, item.Dt).Error
  92. return
  93. }
  94. type EdbdataDeleteRecord struct {
  95. Id int `gorm:"primaryKey" `
  96. TradeCode string `orm:"column(TRADE_CODE)" description:"指标编码"`
  97. Dt string `orm:"column(DT)" description:"日期"`
  98. Close string `orm:"column(CLOSE)" description:"值"`
  99. ModifyTime time.Time `orm:"column(modify_time)" description:"修改时间"`
  100. CreateTime time.Time
  101. SysUserId int
  102. }
  103. func AddEdbdataDeleteRecord(item *EdbdataDeleteRecord) (lastId int64, err error) {
  104. err = global.DmSQL["edb"].Create(item).Error
  105. lastId = int64(item.Id)
  106. return
  107. }
  108. // DeleteEdbData 根据指标code和日期删除数据
  109. func DeleteEdbData(tradeCode, dt string) (err error) {
  110. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? AND DT = ? `
  111. err = global.DmSQL["edb"].Exec(sql, tradeCode, dt).Error
  112. return
  113. }
  114. // DeleteAllEdbData 根据指标code删除数据
  115. func DeleteAllEdbData(tradeCode string) (err error) {
  116. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? `
  117. err = global.DmSQL["edb"].Exec(sql, tradeCode).Error
  118. return
  119. }
  120. type Edbinfo struct {
  121. TradeCode string `gorm:"column:TRADE_CODE;primaryKey;type:varchar(255)" orm:"column(TRADE_CODE);pk" gorm:"primaryKey" description:"指标code"`
  122. SecName string `gorm:"column:SEC_NAME;type:varchar(255)" orm:"column(SEC_NAME);" description:"指标名称"`
  123. Unit string `gorm:"column:UNIT;type:varchar(255)" orm:"column(UNIT);" description:"单位"`
  124. Remark string `gorm:"column:REMARK;type:varchar(255)" orm:"column(REMARK);" description:"备注"`
  125. Frequency string `gorm:"column:FREQUENCY;type:varchar(255)" orm:"column(frequency)" description:"频度"`
  126. ClassifyId int `gorm:"column:CLASSIFY_ID;type:int" orm:"column(classify_id)" description:"分类id"`
  127. ClassifyName string `gorm:"-" orm:"-" description:"分类名称"`
  128. CreateDate string `gorm:"column:CREATE_DATE;type:varchar(255)" orm:"column(create_date)" description:"创建时间"`
  129. UserId int `gorm:"column:USER_ID;type:int" orm:"column(user_id)" description:"录入用户id"`
  130. UserName string `gorm:"column:USER_NAME;type:varchar(255)" orm:"column(user_name)" description:"录入用户名称"`
  131. NoticeTime string `gorm:"column:NOTICE_TIME;type:varchar(255)" orm:"column(notice_time)" description:"通知时间"`
  132. Mobile string `gorm:"column:MOBILE;type:varchar(255)" orm:"column(mobile)" description:"录入者手机号"`
  133. Sort int `gorm:"column:SORT;type:int" orm:"column(sort)" description:"排序"`
  134. ModifyTime string `gorm:"column:MODIFY_TIME;type:varchar(255)" description:"最近一次更新时间"`
  135. IsJoinEdb int8 `gorm:"column:IS_JOIN_EDB;type:tinyint" description:"指标库是否已添加:0-否;1-是"`
  136. StartDate string `gorm:"column:START_DATE;type:varchar(255)" description:"数据开始日期"`
  137. EndDate string `gorm:"column:END_DATE;type:varchar(255)" description:"数据结束日期"`
  138. LatestValue float64 `gorm:"column:LATEST_VALUE;type:decimal(10,2)" description:"指标最新值"`
  139. }
  140. func GetEdbinfoListCount(condition string, pars []interface{}, mobile string, roleType int) (count int, err error) {
  141. sql := ``
  142. if mobile != "" && roleType == 1 {
  143. sql = `SELECT COUNT(1) AS count FROM edbinfo AS a
  144. INNER JOIN edbinfo_user AS c ON a.TRADE_CODE=c.TRADE_CODE AND c.mobile=?
  145. WHERE a.classify_id>0`
  146. if condition != "" {
  147. sql += condition
  148. }
  149. err = global.DmSQL["edb"].Raw(sql, utils.ForwardPars(pars, mobile)...).Scan(&count).Error
  150. } else {
  151. sql = `SELECT COUNT(1) AS count FROM edbinfo AS a WHERE a.classify_id>0`
  152. if condition != "" {
  153. sql += condition
  154. }
  155. err = global.DmSQL["edb"].Raw(sql, pars...).Scan(&count).Error
  156. }
  157. return
  158. }
  159. type EdbinfoItem struct {
  160. TradeCode string `gorm:"column:TRADE_CODE;primaryKey;type:varchar(255)"` //`orm:"column(TRADE_CODE);pk" gorm:"primaryKey" description:"指标code"`
  161. SecName string `gorm:"column:SEC_NAME;type:varchar(255)"` //`orm:"column(SEC_NAME);" description:"指标名称"`
  162. Unit string `gorm:"column:UNIT;type:varchar(255)"` //`orm:"column(UNIT);" description:"单位"`
  163. Remark string `gorm:"column:REMARK;type:varchar(255)"` //`orm:"column(REMARK);" description:"备注"`
  164. Frequency string `gorm:"column:frequency;"` //`description:"频度"`
  165. ClassifyId int `gorm:"column:classify_id;type:int"` //`description:"分类id"`
  166. ClassifyName string `gorm:"-"` // `description:"分类名称"`
  167. CreateDate string `gorm:"column:create_date;"` // `description:"创建时间"`
  168. UserId int `gorm:"column:user_id;"` // `description:"录入用户id"`
  169. UserName string `gorm:"column:user_name;"` // `description:"录入用户名称"`
  170. NoticeTime string `gorm:"column:notice_time;"` //`description:"通知时间"`
  171. Mobile string `gorm:"column:mobile"` // `description:"录入者手机号"`
  172. ModifyTime string `gorm:"column:modify_time;"` //`// `description:"最近一次更新时间"`
  173. StartDate string `gorm:"column:start_date;"` // `description:"数据开始日期"`
  174. EndDate string `gorm:"column:end_date;"` // `description:"数据结束日期"`
  175. LatestValue float64 `gorm:"column:latest_value;"` // `description:"指标最新值"`
  176. }
  177. func GetEdbinfoItemList(condition string, pars []interface{}, startSize, pageSize int, mobile string, roleType int) (items []*EdbinfoItem, err error) {
  178. sql := ``
  179. if mobile != "" && roleType == 1 {
  180. sql = ` SELECT DISTINCT a.*,b.classify_name FROM edbinfo AS a
  181. LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  182. INNER JOIN edbinfo_user AS c ON a.TRADE_CODE=c.TRADE_CODE AND c.mobile=?
  183. WHERE a.classify_id>0`
  184. if condition != "" {
  185. sql += condition
  186. }
  187. sql += ` ORDER BY a.create_date DESC LIMIT ?,? `
  188. err = global.DmSQL["edb"].Raw(sql, mobile, pars, startSize, pageSize).Find(&items).Error
  189. } else {
  190. sql = `SELECT DISTINCT a.*,b.classify_name FROM edbinfo AS a
  191. LEFT JOIN edbdata_classify AS b on a.classify_id=b.classify_id
  192. WHERE a.classify_id>0`
  193. if condition != "" {
  194. sql += condition
  195. }
  196. sql += ` ORDER BY a.create_date DESC LIMIT ?,? `
  197. pars = append(pars, startSize, pageSize)
  198. err = global.DmSQL["edb"].Raw(sql, pars...).Find(&items).Error
  199. }
  200. return
  201. }
  202. // EdbParamsInfo 指标数据结构体
  203. type EdbParamsInfo struct {
  204. Unit string `gorm:"column:UNIT;"` //`orm:"column(UNIT);" description:"单位"`
  205. Frequency string `gorm:"column:frequency;"` //`orm:"column(frequency);" description:"单位"`
  206. }
  207. // GetEdbUnitList 获取指标单位
  208. func GetEdbUnitList() (items []*EdbParamsInfo, err error) {
  209. sql := `SELECT UNIT from edbinfo group by UNIT`
  210. err = global.DmSQL["edb"].Raw(sql).Find(&items).Error
  211. return
  212. }
  213. // GetEdbFrequencyList 获取指标频度
  214. func GetEdbFrequencyList(classifyId, userId int) (items []*EdbParamsInfo, err error) {
  215. sql := `SELECT frequency from edbinfo a
  216. join edbdata b on a.TRADE_CODE=b.TRADE_CODE
  217. where classify_id = ? `
  218. if userId > 0 {
  219. sql += ` and a.user_id = ` + fmt.Sprint(userId) + ` `
  220. }
  221. sql += ` group by a.frequency`
  222. err = global.DmSQL["edb"].Raw(sql, classifyId).Find(&items).Error
  223. return
  224. }
  225. type TargetListResp struct {
  226. List []*EdbinfoItem
  227. Paging *paging.PagingItem `description:"分页数据"`
  228. }
  229. type EdbinfoAddReq struct {
  230. SecName string `description:"指标名称"`
  231. Unit string `description:"单位"`
  232. Frequency string `description:"频度"`
  233. ClassifyId int `description:"分类id"`
  234. NoticeTime string `description:"通知时间"`
  235. }
  236. // GetMaxTradeCode 获取指标最大trade_code
  237. func GetMaxTradeCode() (max_trade_code string, err error) {
  238. sql := " SELECT MAX(TRADE_CODE) AS max_trade_code FROM edbinfo WHERE LEFT(TRADE_CODE,1)='W' "
  239. err = global.DmSQL["edb"].Raw(sql).Scan(&max_trade_code).Error
  240. if (err != nil && utils.IsErrNoRow(err)) || max_trade_code == `` {
  241. max_trade_code = "W00"
  242. }
  243. return
  244. }
  245. func GetEdbinfoBySecName(secName string) (item *Edbinfo, err error) {
  246. sql := `SELECT * FROM edbinfo WHERE SEC_NAME=? `
  247. err = global.DmSQL["edb"].Raw(sql, secName).First(&item).Error
  248. return
  249. }
  250. func GetEdbinfoByTradeCode(tradeCode string) (item *Edbinfo, err error) {
  251. sql := `SELECT * FROM edbinfo WHERE TRADE_CODE=? `
  252. err = global.DmSQL["edb"].Raw(sql, tradeCode).First(&item).Error
  253. return
  254. }
  255. func AddEdbinfo(tradeCode, secName, unit, remark, frequency, noticeTime string, classifyId int, userId int, userName string) (err error) {
  256. currTime := time.Now().Format(utils.FormatDateTime)
  257. edbInfo := &Edbinfo{
  258. TradeCode: tradeCode,
  259. SecName: secName,
  260. Unit: unit,
  261. Remark: remark,
  262. Frequency: frequency,
  263. ClassifyId: classifyId,
  264. CreateDate: currTime,
  265. UserId: userId,
  266. UserName: userName,
  267. NoticeTime: noticeTime,
  268. Mobile: "",
  269. ModifyTime: currTime,
  270. IsJoinEdb: 0,
  271. }
  272. err = global.DmSQL["edb"].Create(edbInfo).Error
  273. return
  274. }
  275. func AddEdbinfoUser(tradeCode, mobile string) (err error) {
  276. sql := `INSERT INTO edbinfo_user(TRADE_CODE, mobile) VALUES (?,?)`
  277. err = global.DmSQL["edb"].Exec(sql, tradeCode, mobile).Error
  278. return
  279. }
  280. type EdbinfoEditReq struct {
  281. TradeCode string `description:"指标code"`
  282. SecName string `description:"指标名称"`
  283. Unit string `description:"单位"`
  284. Frequency string `description:"频度"`
  285. ClassifyId int `description:"分类id"`
  286. NoticeTime string `description:"通知时间"`
  287. }
  288. func EditEdbinfo(tradeCode, secName, unit, frequency, noticeTime string, classifyId int) (err error) {
  289. sql := `UPDATE edbinfo SET SEC_NAME= ?, UNIT = ?,classify_id=?,frequency=?,notice_time=?,create_date=NOW() WHERE TRADE_CODE=? `
  290. err = global.DmSQL["edb"].Exec(sql, secName, unit, classifyId, frequency, noticeTime, tradeCode).Error
  291. return
  292. }
  293. func SearchTargetEntry(classifyId int, keyWord string) (items []*Edbinfo, err error) {
  294. where := ""
  295. pars := make([]interface{}, 0)
  296. sql := `SELECT * FROM edbinfo WHERE classify_id>0 AND classify_id=? `
  297. pars = append(pars, classifyId)
  298. if keyWord != "" {
  299. sql += `AND SEC_NAME LIKE ? `
  300. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  301. }
  302. sql += where
  303. err = global.DmSQL["edb"].Raw(sql, pars...).Find(&items).Error
  304. return
  305. }
  306. type SearchTargetListResp struct {
  307. List []*Edbinfo
  308. }
  309. type EdbdataClassify struct {
  310. ClassifyId int `gorm:"column:classify_id;type:int" xorm:"int 'classify_id'"`
  311. ClassifyName string `gorm:"column:classify_name;type:varchar(255)" xorm:"varchar(255) 'classify_name'"`
  312. ParentId int `gorm:"column:parent_id;type:int" xorm:"int 'parent_id'"`
  313. EdbInfoTotal int `gorm:"column:edb_info_total;type:int" xorm:"int 'edb_info_total'"`
  314. TradeCode string `gorm:"column:trade_code;type:varchar(255)" xorm:"varchar(255) 'trade_code'"`
  315. UniqueCode string `gorm:"column:unique_code;type:varchar(255)" xorm:"varchar(255) 'unique_code'"`
  316. }
  317. type EdbdataClassifyList struct {
  318. ClassifyId int
  319. ClassifyName string
  320. ParentId int
  321. Child []*EdbdataClassify `gorm:"-"`
  322. TradeCode string
  323. UniqueCode string
  324. }
  325. func GetEdbdataClassify(userId int64) (items []*EdbdataClassifyList, err error) {
  326. var newItems []*EdbdataClassifyList
  327. sql := ` SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE parent_id=0 `
  328. err = global.DmSQL["edb"].Raw(sql).Find(&newItems).Error
  329. if err != nil {
  330. return
  331. }
  332. classifyLen := len(newItems)
  333. for i := 0; i < classifyLen; i++ {
  334. var childItems []*EdbdataClassify
  335. parentId := newItems[i].ClassifyId
  336. childSql := ``
  337. if userId > 0 {
  338. userClassifyList, _ := GetManualUserClassify(int(userId))
  339. var userIdArr []string
  340. for _, v := range userClassifyList {
  341. userIdArr = append(userIdArr, strconv.Itoa(v.ClassifyId))
  342. }
  343. userIdStr := strings.Join(userIdArr, ",")
  344. if userIdStr != "" {
  345. 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(" + userIdStr + ") AND parent_id=? ORDER BY a.create_time ASC "
  346. // _, err = o.Raw(childSql, parentId).QueryRows(&childItems)
  347. err = global.DmSQL["edb"].Raw(childSql, parentId).Find(&childItems).Error
  348. }
  349. } else {
  350. childSql = "SELECT classify_id,classify_name,parent_id FROM edbdata_classify WHERE is_show=1 and parent_id=? ORDER BY create_time ASC "
  351. //_, err = o.Raw(childSql, parentId).QueryRows(&childItems)
  352. err = global.DmSQL["edb"].Raw(childSql, parentId).Find(&childItems).Error
  353. }
  354. if err != nil {
  355. return
  356. }
  357. newItems[i].Child = childItems
  358. }
  359. for _, v := range newItems {
  360. childLen := len(v.Child)
  361. if childLen > 0 {
  362. items = append(items, v)
  363. }
  364. }
  365. return
  366. }
  367. type ManualUserClassify struct {
  368. ManualUserClassifyId int `gorm:"column:manual_user_classify_id"` //`orm:"column(manual_user_classify_id);pk" gorm:"primaryKey" `
  369. AdminId int `gorm:"column:admin_id"`
  370. ClassifyId int `gorm:"column:classify_id"`
  371. CreateTime time.Time `gorm:"column:create_time"`
  372. }
  373. func GetManualUserClassify(sysUserId int) (list []*ManualUserClassify, err error) {
  374. sql := `SELECT * FROM manual_user_classify WHERE admin_id=? `
  375. err = global.DmSQL["data"].Raw(sql, sysUserId).Find(&list).Error
  376. return
  377. }
  378. type EdbdataClassifyResp struct {
  379. List []*EdbdataClassifyList
  380. }
  381. func GetTargetBySecName(secName string) (item *Edbinfo, err error) {
  382. sql := `SELECT * FROM edbinfo WHERE SEC_NAME=? `
  383. err = global.DmSQL["edb"].Raw(sql, secName).First(&item).Error
  384. return
  385. }
  386. // 更新指标数据信息
  387. func (edbinfo *Edbinfo) Update(cols []string) (err error) {
  388. err = global.DmSQL["edb"].Select(cols).Updates(edbinfo).Error
  389. return
  390. }
  391. // GetTargetsDataList 根据code获取指标数据列表
  392. func GetTargetsDataList(tradeCode string) (items []*Edbdata, err error) {
  393. sql := `SELECT * FROM edbdata WHERE TRADE_CODE=? ORDER BY DT ASC `
  394. err = global.DmSQL["edb"].Raw(sql, tradeCode).Find(&items).Error
  395. return
  396. }
  397. func ModifyTargetsDataByImport(tradeCode, dt, close string) (err error) {
  398. sql := `UPDATE edbdata SET CLOSE=?,modify_time=NOW() WHERE TRADE_CODE=? AND DT=? `
  399. err = global.DmSQL["edb"].Exec(sql, close, tradeCode, dt).Error
  400. return
  401. }
  402. func AddTargetsDataByImport(tradeCode, dt, close string) (err error) {
  403. sql := `INSERT INTO edbdata(TRADE_CODE, DT,CLOSE, modify_time)VALUES(?,?,?,NOW()) `
  404. err = global.DmSQL["edb"].Exec(sql, tradeCode, dt, close).Error
  405. return
  406. }
  407. type EdbdataImportResp struct {
  408. Status int
  409. Msg string
  410. SuccessCount int
  411. FailCount int
  412. }
  413. func GetFailList(sysUserId int) (items []*EdbdataImportFail, err error) {
  414. sql := ` SELECT * FROM edbdata_import_fail WHERE sys_user_id=? `
  415. err = global.DmSQL["edb"].Raw(sql, sysUserId).Find(&items).Error
  416. return
  417. }
  418. type DataListForExport struct {
  419. TradeCode string `gorm:"column:TRADE_CODE"` //`orm:"column(TRADE_CODE)" description:"指标code"`
  420. SecName string `gorm:"column:SEC_NAME"` //`orm:"column(SEC_NAME)" description:"指标名称"`
  421. Unit string `gorm:"column:UNIT"` //`orm:"column(UNIT)" description:"单位"`
  422. Frequency string `gorm:"column:frequency"` //`description:"频度"`
  423. ClassifyId int `gorm:"column:classify_id"` //`description:"分类id"`
  424. NoticeTime string `gorm:"column:notice_time"` //`description:"通知时间"`
  425. ClassifyName string `gorm:"column:classify_name"`
  426. Dt string `gorm:"column:DT"` //`orm:"column(DT)" description:"日期"`
  427. Close float64 `gorm:"column:CLOSE"` //`orm:"column(CLOSE)" description:"值"`
  428. }
  429. type DataDeleteReq struct {
  430. TradeCode string `description:"指标唯一编码"`
  431. CreateDate string `description:"数据录入日期"`
  432. }
  433. func DataDelete(tradeCode, createDate, close string, modifyTime time.Time, sysUserId int) (err error) {
  434. to := global.DmSQL["edb"].Begin()
  435. defer func() {
  436. if err != nil {
  437. _ = to.Rollback()
  438. } else {
  439. _ = to.Commit()
  440. }
  441. }()
  442. recordSql := ` INSERT INTO edbdata_delete_record(TRADE_CODE,DT,CLOSE,modify_time,create_time,sys_user_id)
  443. VALUES(?,?,?,?,?,?)`
  444. err = to.Exec(recordSql, tradeCode, createDate, close, modifyTime, time.Now(), sysUserId).Error
  445. sql := ` DELETE FROM edbdata WHERE TRADE_CODE = ? AND DT = ? `
  446. err = to.Exec(sql, tradeCode, createDate).Error
  447. return
  448. }
  449. type TargetDeleteReq struct {
  450. TradeCode string `description:"指标唯一编码"`
  451. }
  452. func TargetDelete(tradeCode string) (err error) {
  453. to := global.DmSQL["edb"].Begin()
  454. defer func() {
  455. if err != nil {
  456. _ = to.Rollback()
  457. } else {
  458. _ = to.Commit()
  459. }
  460. }()
  461. sql := " DELETE FROM edbinfo WHERE TRADE_CODE = ? "
  462. //_, err = to.Raw(sql, tradeCode).Exec()
  463. err = to.Exec(sql, tradeCode).Error
  464. sql = " DELETE FROM edbdata WHERE TRADE_CODE = ? "
  465. //_, err = to.Raw(sql, tradeCode).Exec()
  466. err = to.Exec(sql, tradeCode).Error
  467. return
  468. }
  469. type Researcher struct {
  470. AdminId int `gorm:"column:admin_id"` //`description:"系统用户id"`
  471. AdminName string `gorm:"column:admin_name"` //`description:"系统用户名称"`
  472. RealName string `gorm:"column:real_name"` //`description:"系统用户姓名"`
  473. Role string `gorm:"column:role"` //`description:"系统用户角色"`
  474. Mobile string `gorm:"column:mobile"` //`description:"手机号"`
  475. TargetCount int `gorm:"column:target_count"` //`description:"指标数量"`
  476. }
  477. type ResearcherListResp struct {
  478. List []*Researcher
  479. }
  480. func GetResearcherEntry() (items []*Researcher, err error) {
  481. sql := ` SELECT admin_id,admin_name,real_name,mobile,0 as target_count FROM "admin" WHERE role_type=1 `
  482. err = global.DmSQL["edb"].Raw(sql).Find(&items).Error
  483. researchLen := len(items)
  484. for i := 0; i < researchLen; i++ {
  485. var count int
  486. mobile := items[i].Mobile
  487. sqlCount := ` SELECT COUNT(DISTINCT a.TRADE_CODE) AS count FROM edbinfo_user AS a
  488. INNER JOIN edbinfo AS b ON a.TRADE_CODE=b.TRADE_CODE
  489. WHERE a.mobile=? AND b.classify_id>0 `
  490. err = global.DmSQL["edb"].Raw(sqlCount, mobile).Scan(&count).Error
  491. items[i].TargetCount = count
  492. }
  493. return
  494. }
  495. func GetResearcherEntryByMobile(mobile string) (items []*Researcher, err error) {
  496. sql := ` SELECT admin_id,admin_name,real_name,mobile,0 as target_count FROM "admin" WHERE role_type=1 `
  497. if mobile != "" {
  498. sql += ` AND mobile IN(` + mobile + `)`
  499. }
  500. err = global.DmSQL["edb"].Raw(sql).Find(&items).Error
  501. researchLen := len(items)
  502. for i := 0; i < researchLen; i++ {
  503. var count int
  504. mobile := items[i].Mobile
  505. sqlCount := ` SELECT COUNT(DISTINCT a.TRADE_CODE) AS count FROM edbinfo_user AS a
  506. INNER JOIN edbinfo AS b ON a.TRADE_CODE=b.TRADE_CODE
  507. WHERE a.mobile=? AND b.classify_id>0 `
  508. err = global.DmSQL["edb"].Raw(sqlCount, mobile).Scan(&count).Error
  509. items[i].TargetCount = count
  510. }
  511. return
  512. }
  513. type EdbinfoItems struct {
  514. TradeCode string `gorm:"column:TRADE_CODE"` //`orm:"column(TRADE_CODE);pk" gorm:"primaryKey" description:"指标code"`
  515. SecName string `gorm:"column:SEC_NAME"` //`orm:"column(SEC_NAME);" description:"指标名称"`
  516. Unit string `gorm:"column:UNIT"` //`orm:"column(UNIT);" description:"单位"`
  517. Remark string `gorm:"column:REMARK"` //`orm:"column(REMARK);" description:"备注"`
  518. Frequency string `gorm:"column:frequency"` //`description:"频度"`
  519. ClassifyId int `gorm:"column:classify_id"` //`description:"分类id"`
  520. ClassifyName string `gorm:"column:classify_name"` //`description:"分类名称"`
  521. CreateDate string `gorm:"column:create_date"` //`description:"创建时间"`
  522. UserId int `gorm:"column:user_id"` //`description:"录入用户id"`
  523. NoticeTime string `gorm:"column:notice_time"` //`description:"通知时间"`
  524. Mobile string `gorm:"column:mobile"` //`description:"录入者手机号"`
  525. ModifyDate string `gorm:"column:modify_date"` //`description:"待更新日期"`
  526. Status string `gorm:"column:status"` // `description:"状态:未完成/完成"`
  527. }
  528. type TargetItemsResp struct {
  529. List SortEdbInfo
  530. }
  531. type SortEdbInfo []EdbinfoItems
  532. func GetTargetItems(mobile string, classifyId int) (lastItems SortEdbInfo, err error) {
  533. var items []*EdbinfoItems
  534. sql := ` SELECT *,'' modify_date,'' STATUS FROM edbinfo AS a
  535. WHERE a.classify_id>0
  536. `
  537. if classifyId > 0 {
  538. sql += ` AND a.classify_id=` + strconv.Itoa(classifyId) + ``
  539. }
  540. sql += ` GROUP BY a.TRADE_CODE `
  541. sql = sql + ` ORDER BY CONVERT(a.SEC_NAME USING gbk ) COLLATE gbk_chinese_ci ASC `
  542. err = global.DmSQL["edb"].Raw(sql).Find(&items).Error
  543. if err != nil {
  544. return
  545. }
  546. itemsLen := len(items)
  547. nowWeek := time.Now().Weekday().String()
  548. fmt.Println(nowWeek)
  549. finishEdbInfo := SortEdbInfo{}
  550. unFinishEdbInfo := SortEdbInfo{}
  551. for i := 0; i < itemsLen; i++ {
  552. noticeTime := items[i].NoticeTime
  553. frequency := items[i].Frequency
  554. tradeCode := items[i].TradeCode
  555. if noticeTime != "" {
  556. if frequency == "周度" {
  557. noticeArr := strings.Split(noticeTime, " ")
  558. noticeWeek := noticeArr[0]
  559. fmt.Println(noticeWeek)
  560. addDay := 0
  561. if nowWeek == "Sunday" {
  562. if noticeWeek == "周日" {
  563. addDay = 0
  564. } else if noticeWeek == "周一" {
  565. addDay = 1
  566. } else if noticeWeek == "周二" {
  567. addDay = 2
  568. } else if noticeWeek == "周三" {
  569. addDay = 3
  570. } else if noticeWeek == "周四" {
  571. addDay = 4
  572. } else if noticeWeek == "周五" {
  573. addDay = 5
  574. } else if noticeWeek == "周六" {
  575. addDay = 6
  576. } else {
  577. addDay = 0
  578. }
  579. } else if nowWeek == "Monday" {
  580. if noticeWeek == "周日" {
  581. addDay = 6
  582. } else if noticeWeek == "周一" {
  583. addDay = 0
  584. } else if noticeWeek == "周二" {
  585. addDay = 1
  586. } else if noticeWeek == "周三" {
  587. addDay = 2
  588. } else if noticeWeek == "周四" {
  589. addDay = 3
  590. } else if noticeWeek == "周五" {
  591. addDay = 4
  592. } else if noticeWeek == "周六" {
  593. addDay = 5
  594. } else {
  595. addDay = 0
  596. }
  597. } else if nowWeek == "Tuesday" {
  598. if noticeWeek == "周日" {
  599. addDay = 5
  600. } else if noticeWeek == "周一" {
  601. addDay = 6
  602. } else if noticeWeek == "周二" {
  603. addDay = 0
  604. } else if noticeWeek == "周三" {
  605. addDay = 1
  606. } else if noticeWeek == "周四" {
  607. addDay = 2
  608. } else if noticeWeek == "周五" {
  609. addDay = 3
  610. } else if noticeWeek == "周六" {
  611. addDay = 4
  612. } else {
  613. addDay = 0
  614. }
  615. } else if nowWeek == "Wednesday" {
  616. if noticeWeek == "周日" {
  617. addDay = 4
  618. } else if noticeWeek == "周一" {
  619. addDay = 5
  620. } else if noticeWeek == "周二" {
  621. addDay = 6
  622. } else if noticeWeek == "周三" {
  623. addDay = 0
  624. } else if noticeWeek == "周四" {
  625. addDay = 1
  626. } else if noticeWeek == "周五" {
  627. addDay = 2
  628. } else if noticeWeek == "周六" {
  629. addDay = 3
  630. } else {
  631. addDay = 0
  632. }
  633. } else if nowWeek == "Thursday" {
  634. if noticeWeek == "周日" {
  635. addDay = 3
  636. } else if noticeWeek == "周一" {
  637. addDay = 4
  638. } else if noticeWeek == "周二" {
  639. addDay = 5
  640. } else if noticeWeek == "周三" {
  641. addDay = 6
  642. } else if noticeWeek == "周四" {
  643. addDay = 0
  644. } else if noticeWeek == "周五" {
  645. addDay = 1
  646. } else if noticeWeek == "周六" {
  647. addDay = 2
  648. } else {
  649. addDay = 0
  650. }
  651. } else if nowWeek == "Friday" {
  652. if noticeWeek == "周日" {
  653. addDay = 2
  654. } else if noticeWeek == "周一" {
  655. addDay = 3
  656. } else if noticeWeek == "周二" {
  657. addDay = 4
  658. } else if noticeWeek == "周三" {
  659. addDay = 5
  660. } else if noticeWeek == "周四" {
  661. addDay = 6
  662. } else if noticeWeek == "周五" {
  663. addDay = 0
  664. } else if noticeWeek == "周六" {
  665. addDay = 1
  666. } else {
  667. addDay = 0
  668. }
  669. } else if nowWeek == "Saturday" {
  670. if noticeWeek == "周日" {
  671. addDay = 1
  672. } else if noticeWeek == "周一" {
  673. addDay = 2
  674. } else if noticeWeek == "周二" {
  675. addDay = 3
  676. } else if noticeWeek == "周三" {
  677. addDay = 4
  678. } else if noticeWeek == "周四" {
  679. addDay = 5
  680. } else if noticeWeek == "周五" {
  681. addDay = 6
  682. } else if noticeWeek == "周六" {
  683. addDay = 0
  684. } else {
  685. addDay = 0
  686. }
  687. }
  688. modifyDate := time.Now().AddDate(0, 0, addDay)
  689. modifyDateStr := modifyDate.Format(utils.FormatDate)
  690. items[i].ModifyDate = modifyDateStr
  691. modifyDateEndStr := modifyDate.AddDate(0, 0, -7).Format(utils.FormatDate)
  692. fmt.Println("addDay:", addDay)
  693. fmt.Println("modifyDateEndStr:", modifyDateEndStr)
  694. count := 0
  695. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  696. err = global.DmSQL["edb"].Raw(sqlCount, tradeCode, modifyDateEndStr, modifyDateStr).Scan(&count).Error
  697. if err != nil {
  698. return nil, err
  699. }
  700. if count > 0 {
  701. items[i].Status = "完成"
  702. finishEdbInfo = append(finishEdbInfo, *items[i])
  703. } else {
  704. items[i].Status = "未完成"
  705. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  706. }
  707. } else if frequency == "日度" {
  708. items[i].Status = "完成"
  709. finishEdbInfo = append(finishEdbInfo, *items[i])
  710. } else if frequency == "月度" {
  711. myYear := time.Now().Year()
  712. myMonth := time.Now().Format("01")
  713. startDate, endDate := utils.GetMonthStartAndEnd(strconv.Itoa(myYear), myMonth)
  714. count := 0
  715. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  716. err = global.DmSQL["edb"].Raw(sqlCount, tradeCode, startDate, endDate).Scan(&count).Error
  717. if err != nil {
  718. return nil, err
  719. }
  720. if noticeTime != "" {
  721. var modifyDateStr string
  722. strArr := strings.Split(noticeTime, "日")
  723. myYear := time.Now().Year()
  724. myMonth := time.Now().Format("01")
  725. modifyDateStr = strconv.Itoa(myYear) + "-" + myMonth + "-" + strArr[0]
  726. items[i].ModifyDate = modifyDateStr
  727. }
  728. if count > 0 {
  729. items[i].Status = "完成"
  730. finishEdbInfo = append(finishEdbInfo, *items[i])
  731. } else {
  732. items[i].Status = "未完成"
  733. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  734. }
  735. } else {
  736. items[i].Status = "完成"
  737. finishEdbInfo = append(finishEdbInfo, *items[i])
  738. }
  739. } else {
  740. if frequency == "月度" {
  741. myYear := time.Now().Year()
  742. myMonth := time.Now().Format("01")
  743. startDate, endDate := utils.GetMonthStartAndEnd(strconv.Itoa(myYear), myMonth)
  744. count := 0
  745. sqlCount := ` SELECT COUNT(1) AS num FROM edbdata WHERE TRADE_CODE=? AND DT >= ? AND DT <= ? `
  746. //err = o.Raw(sqlCount, tradeCode, startDate, endDate).QueryRow(&count)
  747. err = global.DmSQL["edb"].Raw(sqlCount, tradeCode, startDate, endDate).Scan(&count).Error
  748. if err != nil {
  749. return nil, err
  750. }
  751. if count > 0 {
  752. items[i].Status = "完成"
  753. finishEdbInfo = append(finishEdbInfo, *items[i])
  754. } else {
  755. items[i].Status = "未完成"
  756. unFinishEdbInfo = append(unFinishEdbInfo, *items[i])
  757. }
  758. } else {
  759. items[i].Status = "完成"
  760. finishEdbInfo = append(finishEdbInfo, *items[i])
  761. }
  762. }
  763. }
  764. sort.Sort(SortByModifyDate{finishEdbInfo})
  765. sort.Sort(SortByModifyDate{unFinishEdbInfo})
  766. lastItems = append(lastItems, unFinishEdbInfo...)
  767. lastItems = append(lastItems, finishEdbInfo...)
  768. return
  769. }
  770. // 获取此 slice 的长度
  771. func (p SortEdbInfo) Len() int { return len(p) }
  772. // 根据元素的状态降序排序
  773. func (p SortEdbInfo) Less(i, j int) bool {
  774. return p[i].Status > p[j].Status
  775. }
  776. // 交换数据
  777. func (p SortEdbInfo) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  778. // 嵌套结构体 将继承 SortEdbInfo 的所有属性和方法
  779. // 所以相当于SortByName 也实现了 Len() 和 Swap() 方法
  780. type SortByStatus struct{ SortEdbInfo }
  781. // 根据元素的姓名长度降序排序 (此处按照自己的业务逻辑写)
  782. func (p SortByStatus) Less(i, j int) bool {
  783. return len(p.SortEdbInfo[i].Status) > len(p.SortEdbInfo[j].Status)
  784. }
  785. type SortByModifyDate struct{ SortEdbInfo }
  786. // 根据元素的年龄降序排序 (此处按照自己的业务逻辑写)
  787. func (p SortByModifyDate) Less(i, j int) bool {
  788. return p.SortEdbInfo[i].ModifyDate > p.SortEdbInfo[j].ModifyDate
  789. }
  790. type DataCheckResp struct {
  791. Status int `description:"状态:1:该日期已存在数据,是否确认修改?,0:数据不存在"`
  792. Close string `description:"值"`
  793. }
  794. type TargetCheckResp struct {
  795. Status int `description:"状态:1:该指标有关联数据,请先删除数据,0:指标不存在关联数据,可直接删除"`
  796. }
  797. type EdbdataExportList struct {
  798. TradeCode string `gorm:"column:TRADE_CODE;primaryKey;not null" description:"指标code"`
  799. SecName string `gorm:"column:SEC_NAME" description:"指标名称"`
  800. Unit string `gorm:"column:UNIT" description:"单位"`
  801. Remark string `gorm:"column:REMARK" description:"备注"`
  802. Frequency string `gorm:"column:frequency" description:"频度"` //`description:"频度"`
  803. ClassifyId int `gorm:"column:classify_id" description:"分类id"` //`description:"分类id"`
  804. ClassifyName string `gorm:"column:classify_name" description:"分类名称"` //`description:"分类名称"`
  805. CreateDate string `gorm:"column:create_date" description:"创建时间"` //`description:"创建时间"`
  806. Dt string `gorm:"column:DT" description:"最新一次录入时间"`
  807. }
  808. func GetEdbdataSecName(condition string, pars []interface{}) (items []*EdbdataExportList, err error) {
  809. sql := `SELECT a.TRADE_CODE,a.SEC_NAME,a.frequency,a.UNIT,MAX(c.DT) AS Dt,b.classify_name
  810. FROM edbdata AS c
  811. INNER JOIN edbinfo AS a ON a.TRADE_CODE=c.TRADE_CODE
  812. LEFT JOIN edbdata_classify AS b ON a.classify_id=b.classify_id
  813. WHERE a.classify_id>0`
  814. if condition != "" {
  815. sql += condition
  816. }
  817. sql += " GROUP BY a.TRADE_CODE ORDER BY a.TRADE_CODE ASC "
  818. err = global.DmSQL["edb"].Raw(sql, pars...).Find(&items).Error
  819. return
  820. }
  821. // GetEdbDataFrequencyByClassifyIdList
  822. // @Description: 根据分类列表获取所有的频度
  823. // @author: Roc
  824. // @datetime 2024-08-15 17:51:13
  825. // @param classifyIdList []int
  826. // @return items []*string
  827. // @return err error
  828. func GetEdbDataFrequencyByClassifyIdList(classifyIdList []int) (items []string, err error) {
  829. num := len(classifyIdList)
  830. if num <= 0 {
  831. return
  832. }
  833. sql := `SELECT DISTINCT frequency FROM edbinfo where classify_id in (` + utils.GetOrmInReplace(num) + `) AND frequency IS NOT NULL ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  834. err = global.DmSQL["edb"].Raw(sql, classifyIdList).Find(&items).Error
  835. return
  836. }
  837. func GetEdbDataFrequencyByKeyord(keyword string) (items []string, err error) {
  838. sql := `SELECT DISTINCT frequency FROM edbinfo where SEC_NAME=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年度','年度') `
  839. err = global.DmSQL["edb"].Raw(sql, keyword).Find(&items).Error
  840. return
  841. }
  842. type EdbdataList struct {
  843. Dt string `gorm:"column:DT" description:"录入时间"` //`orm:"column(DT);" description:"录入时间"`
  844. }
  845. func GetEdbdataList(tradeCode string) (items []*EdbdataList, err error) {
  846. sql := ` SELECT DT FROM edbdata WHERE TRADE_CODE IN(` + tradeCode + `) GROUP BY DT ORDER BY DT DESC `
  847. err = global.DmSQL["edb"].Raw(sql).Find(&items).Error
  848. return
  849. }
  850. type EdbdataItem struct {
  851. TradeCode string `gorm:"column:TRADE_CODE" description:"指标code"` //`orm:"column(TRADE_CODE);" description:"指标code"`
  852. Dt string `gorm:"column:DT" description:"最新一次录入时间"` // `orm:"column(DT);" description:"最新一次录入时间"`
  853. Close float64 `gorm:"column:CLOSE" description:"值"` //`orm:"column(CLOSE);" description:"值"`
  854. }
  855. func GetEdbdataAllByTradeCode(tradeCode string) (items []*EdbdataItem, err error) {
  856. sql := ` SELECT * FROM edbdata WHERE TRADE_CODE=? `
  857. err = global.DmSQL["edb"].Raw(sql, tradeCode).Find(&items).Error
  858. return
  859. }
  860. // EdbInfoItem
  861. type EdbInfoItem struct {
  862. TradeCode string `gorm:"column:TRADE_CODE;primaryKey;type:varchar(255)" orm:"column(TRADE_CODE);pk" gorm:"primaryKey" description:"指标code"`
  863. SecName string `gorm:"column:SEC_NAME;type:varchar(255)" orm:"column(SEC_NAME);" description:"指标名称"`
  864. Unit string `gorm:"column:UNIT;type:varchar(255)" orm:"column(UNIT);" description:"单位"`
  865. Remark string `gorm:"column:REMARK;type:varchar(255)" orm:"column(REMARK);" description:"备注"`
  866. Frequency string `gorm:"column:FREQUENCY;type:varchar(255)" description:"频度"`
  867. ClassifyId int `gorm:"column:CLASSIFY_ID;type:int" description:"分类id"`
  868. ClassifyName string `gorm:"column:CLASSIFY_NAME;type:varchar(255)" description:"分类名称"`
  869. CreateDate string `gorm:"column:CREATE_DATE;type:varchar(255)" description:"创建时间"`
  870. UserId int `gorm:"column:USER_ID;type:int" description:"录入用户id"`
  871. UserName string `gorm:"column:USER_NAME;type:varchar(255)" description:"录入用户名称"`
  872. NoticeTime string `gorm:"column:NOTICE_TIME;type:varchar(255)" description:"通知时间"`
  873. Mobile string `gorm:"column:MOBILE;type:varchar(255)" description:"录入者手机号"`
  874. ModifyDate string `gorm:"column:MODIFY_DATE;type:varchar(255)" description:"待更新日期"`
  875. ModifyTime string `gorm:"column:MODIFY_TIME;type:varchar(255)" description:"最近一次更新时间"`
  876. Status string `gorm:"column:STATUS;type:varchar(255)" description:"状态:未完成/完成"`
  877. IsJoinEdb int8 `gorm:"column:IS_JOIN_EDB;type:tinyint" description:"指标库是否已添加:0-否;1-是"`
  878. StartDate string `gorm:"column:START_DATE;type:varchar(255)" description:"数据开始日期"`
  879. EndDate string `gorm:"column:END_DATE;type:varchar(255)" description:"数据结束日期"`
  880. LatestValue float64 `gorm:"column:LATEST_VALUE;type:decimal(10,2)" description:"指标最新值"`
  881. DataList []*Edbdata `gorm:"-" description:"指标数据列表"`
  882. }
  883. // GetTargetItemList 获取指标列表数据
  884. func GetTargetItemList(classifyId, edbShowType int, frequency, keyword, tradeCode string, classifyIdStrList []string) (items []*EdbInfoItem, err error) {
  885. pars := make([]interface{}, 0)
  886. sql := ` SELECT a.*,'' modify_date,'' STATUS FROM edbinfo AS a `
  887. if edbShowType != 0 {
  888. sql = ` SELECT a.*,b.DT,'' modify_date,'' STATUS FROM edbinfo AS a
  889. left join edbdata b on a.TRADE_CODE=b.TRADE_CODE `
  890. }
  891. sql += ` WHERE a.classify_id>0 `
  892. //如果没有分类id集合列表,那么就没有数据了,不用往下执行了,直接返回好了
  893. if len(classifyIdStrList) <= 0 {
  894. return
  895. }
  896. if len(classifyIdStrList) > 0 {
  897. sql += ` AND a.classify_id in (` + strings.Join(classifyIdStrList, ",") + `) `
  898. }
  899. if classifyId > 0 {
  900. sql += ` AND a.classify_id=` + strconv.Itoa(classifyId) + ` `
  901. }
  902. //频度
  903. if frequency != "" {
  904. sql += ` AND a.frequency="` + frequency + `" `
  905. }
  906. //关键字
  907. if keyword != "" {
  908. sql += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
  909. pars = utils.GetLikeKeywordPars(pars, keyword, 2)
  910. }
  911. //指定指标
  912. if tradeCode != "" {
  913. sql += ` AND a.TRADE_CODE = "` + tradeCode + `" `
  914. }
  915. //指标里面是否有数据
  916. switch edbShowType {
  917. case 1:
  918. sql += ` AND b.CLOSE is not null `
  919. case 2:
  920. sql += ` AND b.CLOSE is null `
  921. }
  922. sql += ` GROUP BY a.TRADE_CODE `
  923. sql = sql + ` ORDER BY CONVERT(a.SEC_NAME USING gbk ) COLLATE gbk_chinese_ci ASC `
  924. err = global.DmSQL["edb"].Raw(sql, pars...).Find(&items).Error
  925. return
  926. }
  927. // GetEdbDataListByCodes 通过指标ID获取所有数据
  928. func GetEdbDataListByCodes(tradeCode string) (items []*Edbdata, err error) {
  929. 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 `
  930. err = global.DmSQL["edb"].Raw(sql).Find(&items).Error
  931. return
  932. }
  933. // TargetItemListResp 指标数据结构体
  934. type TargetItemListResp struct {
  935. List []*EdbInfoItem
  936. FrequencyList []string
  937. }
  938. // BatchDataDeleteReq 批量删除某日的指标数据请求结构体
  939. type BatchDataDeleteReq struct {
  940. CreateDate string `description:"创建日期"`
  941. TradeCodeList []string `description:"指标唯一编码列表"`
  942. }
  943. // BatchDeleteEdbDataByDate 批量删除某日的指标数据
  944. func BatchDeleteEdbDataByDate(tradeCodes, dt string, opUserId int) (err error) {
  945. var list []*Edbdata
  946. sql := ` select * FROM edbdata WHERE TRADE_CODE in (` + tradeCodes + `) AND DT = ? `
  947. err = global.DmSQL["edb"].Raw(sql, dt).Find(&list).Error
  948. if err != nil {
  949. return
  950. }
  951. deleteRecordList := make([]*EdbdataDeleteRecord, 0)
  952. for _, edbDataInfo := range list {
  953. deleteRecord := &EdbdataDeleteRecord{
  954. TradeCode: edbDataInfo.TradeCode,
  955. Dt: edbDataInfo.Dt,
  956. Close: edbDataInfo.Close,
  957. ModifyTime: time.Now(),
  958. CreateTime: time.Now(),
  959. SysUserId: opUserId,
  960. }
  961. deleteRecordList = append(deleteRecordList, deleteRecord)
  962. }
  963. if len(deleteRecordList) > 0 {
  964. tmpErr := global.DmSQL["edb"].CreateInBatches(deleteRecordList, utils.MultiAddNum).Error
  965. if tmpErr != nil {
  966. err = tmpErr
  967. return
  968. }
  969. }
  970. sql = ` DELETE FROM edbdata WHERE TRADE_CODE in (` + tradeCodes + `) AND DT = ? `
  971. //_, err = o.Raw(sql, dt).Exec()
  972. err = global.DmSQL["edb"].Exec(sql, dt).Error
  973. return
  974. }
  975. // BatchDeleteEdbData 批量删除指标数据
  976. func BatchDeleteEdbData(tradeCode string, opUserId int) (err error) {
  977. var list []*Edbdata
  978. sql := ` select * FROM edbdata WHERE TRADE_CODE = ? `
  979. err = global.DmSQL["edb"].Raw(sql, tradeCode).Find(&list).Error
  980. if err != nil {
  981. return
  982. }
  983. deleteRecordList := make([]*EdbdataDeleteRecord, 0)
  984. for _, edbDataInfo := range list {
  985. deleteRecord := &EdbdataDeleteRecord{
  986. TradeCode: edbDataInfo.TradeCode,
  987. Dt: edbDataInfo.Dt,
  988. Close: edbDataInfo.Close,
  989. ModifyTime: time.Now(),
  990. CreateTime: time.Now(),
  991. SysUserId: opUserId,
  992. }
  993. deleteRecordList = append(deleteRecordList, deleteRecord)
  994. }
  995. err = global.DmSQL["edb"].CreateInBatches(deleteRecordList, utils.MultiAddNum).Error
  996. if err != nil {
  997. return
  998. }
  999. sql = ` DELETE FROM edbdata WHERE TRADE_CODE = ? `
  1000. err = global.DmSQL["edb"].Exec(sql, tradeCode).Error
  1001. return
  1002. }
  1003. // EdbInfoGroupCount 指标分类id获取当前分类下的指标数量
  1004. type EdbInfoGroupCount struct {
  1005. Count int `gorm:"column:count"`
  1006. ClassifyId int `gorm:"column:classify_id"`
  1007. }
  1008. // GetEdbInfoGroupCountByClassifyIds 根据指标分类id获取当前分类下的指标数量
  1009. func GetEdbInfoGroupCountByClassifyIds(classifyIds string) (list []*EdbInfoGroupCount, err error) {
  1010. sql := `SELECT COUNT(1) AS count,classify_id FROM ( SELECT a.*,b.CLOSE FROM edbinfo AS a
  1011. INNER JOIN edbdata AS b ON a.TRADE_CODE=b.TRADE_CODE
  1012. WHERE a.classify_id in (` + classifyIds + `) group by a.TRADE_CODE) d
  1013. GROUP BY classify_id `
  1014. err = global.DmSQL["edb"].Raw(sql).Find(&list).Error
  1015. return
  1016. }
  1017. type EdbdataFloat struct {
  1018. TradeCode string `gorm:"column:TRADE_CODE"` //`orm:"column(TRADE_CODE);pk" gorm:"primaryKey" description:"指标编码"`
  1019. Dt string `gorm:"column:DT"` //`orm:"column(DT)" description:"日期"`
  1020. Close float64 `gorm:"column:CLOSE"` //`orm:"column(CLOSE)" description:"值"`
  1021. ModifyTime time.Time `gorm:"column:modify_time"` //`orm:"column(modify_time)" description:"修改时间"`
  1022. }
  1023. func ModifyEdbinfo(tradeCode, unit, frequency string, classifyId int) (err error) {
  1024. sql := `UPDATE edbinfo SET UNIT = ?,frequency=?, classify_id=?, create_date=NOW() WHERE TRADE_CODE=? `
  1025. err = global.DmSQL["edb"].Exec(sql, unit, frequency, classifyId, tradeCode).Error
  1026. return
  1027. }
  1028. func DeleteTargetsDataByImport(tradeCode, dt string) (err error) {
  1029. sql := `DELETE FROM edbdata WHERE TRADE_CODE=? AND DT=? `
  1030. err = global.DmSQL["edb"].Exec(sql, tradeCode, dt).Error
  1031. return
  1032. }
  1033. // GetEdbinfoListByCodeListByCodeIdList
  1034. // @Description: 根据指标code列表获取列表信息
  1035. // @param edbCodeList
  1036. // @return items
  1037. // @return err
  1038. func GetEdbinfoListByCodeListByCodeIdList(edbCodeList []string) (items []*Edbinfo, err error) {
  1039. num := len(edbCodeList)
  1040. if num <= 0 {
  1041. return
  1042. }
  1043. sql := `SELECT * FROM edbinfo WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) `
  1044. err = global.DmSQL["edb"].Raw(sql, edbCodeList).Find(&items).Error
  1045. return
  1046. }
  1047. // GetEdbinfoListByCodeListByUserId
  1048. // @Description: 根据用户id列表获取指标列表信息
  1049. // @param userIdList
  1050. // @return items
  1051. // @return err
  1052. func GetEdbinfoListByCodeListByUserId(userIdList []int) (items []*Edbinfo, err error) {
  1053. num := len(userIdList)
  1054. if num <= 0 {
  1055. return
  1056. }
  1057. sql := `SELECT * FROM edbinfo WHERE user_id in (` + utils.GetOrmInReplace(num) + `) `
  1058. err = global.DmSQL["edb"].Raw(sql, userIdList).Find(&items).Error
  1059. return
  1060. }
  1061. // ModifyEdbinfoUserIdByCodeList 根据指标code列表修改创建人
  1062. func ModifyEdbinfoUserIdByCodeList(edbCodeList []string, userId int, userName string) (err error) {
  1063. num := len(edbCodeList)
  1064. if num <= 0 {
  1065. return
  1066. }
  1067. sql := `UPDATE edbinfo SET user_id = ?,user_name = ? WHERE TRADE_CODE in (` + utils.GetOrmInReplace(num) + `) `
  1068. err = global.DmSQL["edb"].Exec(sql, userId, userName, edbCodeList).Error
  1069. return
  1070. }
  1071. func GetEdbInfoAdminList() (list []int, err error) {
  1072. sql := `SELECT user_id FROM edbinfo GROUP BY user_id `
  1073. err = global.DmSQL["edb"].Raw(sql).Find(&list).Error
  1074. return
  1075. }
  1076. // EdbinfoMaxMinDate
  1077. // @Description: 手工指标的最小最大日期
  1078. type EdbinfoMaxMinDate struct {
  1079. MinDate string `gorm:"column:min_date"`
  1080. MaxDate string `gorm:"column:max_date"`
  1081. }
  1082. // GetEdbdataMaxMinDate
  1083. // @Description: 获取手工指标的最小最大日期
  1084. // @author: Roc
  1085. // @datetime 2024-08-01 14:27:20
  1086. // @param tradeCode string
  1087. // @return item EdbinfoMaxMinDate
  1088. // @return err error
  1089. func GetEdbdataMaxMinDate(tradeCode string) (item EdbinfoMaxMinDate, err error) {
  1090. sql := ` SELECT MIN(DT) min_date,MAX(DT) max_date FROM edbdata WHERE TRADE_CODE = ? `
  1091. err = global.DmSQL["edb"].Raw(sql, tradeCode).Find(&item).Error
  1092. return
  1093. }
  1094. // GetEdbdataLatestValue
  1095. // @Description: 获取手工数据的最新值
  1096. // @author: Roc
  1097. // @datetime 2024-08-02 10:33:22
  1098. // @param tradeCode string
  1099. // @return latestValue float64
  1100. // @return err error
  1101. func GetEdbdataLatestValue(tradeCode string) (latestValue float64, err error) {
  1102. sql := ` SELECT CLOSE FROM edbdata WHERE TRADE_CODE = ? ORDER BY DT DESC LIMIT 1`
  1103. err = global.DmSQL["edb"].Raw(sql, tradeCode).Scan(&latestValue).Error
  1104. return
  1105. }
  1106. // ModifyEdbinfoMaxMinDate
  1107. // @Description: 修改手工指标的最小最大日期
  1108. // @author: Roc
  1109. // @datetime 2024-08-01 15:33:45
  1110. // @param startDate string
  1111. // @param endDate string
  1112. // @param tradeCode string
  1113. // @return err error
  1114. func ModifyEdbinfoMaxMinDate(tradeCode, startDate, endDate string, latestValue float64) (err error) {
  1115. sql := ` UPDATE edbinfo SET start_date = ?, end_date = ?, latest_value = ? , modify_time = now() WHERE TRADE_CODE = ? `
  1116. err = global.DmSQL["edb"].Exec(sql, startDate, endDate, latestValue, tradeCode).Error
  1117. return
  1118. }