target.go 43 KB

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