target.go 58 KB

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