target.go 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859
  1. /*
  2. * Copyright (c) 2023. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  3. * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
  4. * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
  5. * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
  6. * Vestibulum commodo. Ut rhoncus gravida arcu.
  7. */
  8. package controllers
  9. import (
  10. "encoding/json"
  11. "eta/eta_api/models"
  12. "eta/eta_api/models/data_manage"
  13. "eta/eta_api/models/data_source"
  14. "eta/eta_api/models/system"
  15. "eta/eta_api/services/alarm_msg"
  16. "eta/eta_api/services/data"
  17. "eta/eta_api/utils"
  18. "fmt"
  19. "github.com/rdlucklib/rdluck_tools/paging"
  20. "github.com/shopspring/decimal"
  21. "github.com/tealeg/xlsx"
  22. "os"
  23. "path/filepath"
  24. "reflect"
  25. "sort"
  26. "strconv"
  27. "strings"
  28. "time"
  29. )
  30. // TargetController 手工指标服务(鉴权)
  31. type TargetController struct {
  32. BaseAuthController
  33. }
  34. // TargetCommonController 手工指标服务(不鉴权)
  35. type TargetCommonController struct {
  36. BaseCommonController
  37. }
  38. // DataList
  39. // @Title 获取指标列表
  40. // @Description 获取指标列表接口
  41. // @Param PageSize query int true "每页数据条数"
  42. // @Param CurrentIndex query int true "当前页页码,从1开始"
  43. // @Param StartDate query string true "开始日期"
  44. // @Param EndDate query string true "结束日期"
  45. // @Param Frequency query string true "频度"
  46. // @Param ClassifyId query int true "分类"
  47. // @Param KeyWord query string true "名称关键词"
  48. // @Param TradeCode query string true "指标唯一编码"
  49. // @Success 200 {object} models.DataListResp
  50. // @router /data/list [get]
  51. func (this *TargetController) DataList() {
  52. br := new(models.BaseResponse).Init()
  53. defer func() {
  54. this.Data["json"] = br
  55. this.ServeJSON()
  56. }()
  57. pageSize, _ := this.GetInt("PageSize")
  58. currentIndex, _ := this.GetInt("CurrentIndex")
  59. keyWord := this.GetString("KeyWord")
  60. startDate := this.GetString("StartDate")
  61. endDate := this.GetString("EndDate")
  62. frequency := this.GetString("Frequency")
  63. tradeCode := this.GetString("TradeCode")
  64. classifyId, _ := this.GetInt("ClassifyId") //分类
  65. var startSize int
  66. if pageSize <= 0 {
  67. pageSize = utils.PageSize20
  68. }
  69. if currentIndex <= 0 {
  70. currentIndex = 1
  71. }
  72. startSize = utils.StartIndex(currentIndex, pageSize)
  73. var condition string
  74. var pars []interface{}
  75. if keyWord != "" {
  76. condition += ` AND a.SEC_NAME LIKE '%` + keyWord + `%' `
  77. }
  78. if startDate != "" {
  79. condition += ` AND c.DT >= ? `
  80. pars = append(pars, startDate)
  81. }
  82. if endDate != "" {
  83. condition += ` AND c.DT <= ? `
  84. pars = append(pars, endDate)
  85. }
  86. if frequency != "" {
  87. condition += ` AND a.frequency = ? `
  88. pars = append(pars, frequency)
  89. }
  90. if classifyId > 0 {
  91. condition += ` AND a.classify_id = ? `
  92. pars = append(pars, classifyId)
  93. }
  94. if tradeCode != "" {
  95. condition += ` AND c.TRADE_CODE = ? `
  96. pars = append(pars, tradeCode)
  97. }
  98. total, err := models.GetDataListCount(condition, pars)
  99. if err != nil {
  100. br.Msg = "获取失败"
  101. br.ErrMsg = "获取失败,Err:" + err.Error()
  102. return
  103. }
  104. list, err := models.GetDataList(condition, pars, startSize, pageSize)
  105. if err != nil {
  106. br.Msg = "获取失败"
  107. br.ErrMsg = "获取失败,Err:" + err.Error()
  108. return
  109. }
  110. page := paging.GetPaging(currentIndex, pageSize, total)
  111. resp := new(models.DataListResp)
  112. resp.Paging = page
  113. if len(list) <= 0 {
  114. list = make([]*models.DataList, 0)
  115. }
  116. resp.List = list
  117. br.Ret = 200
  118. br.Success = true
  119. br.Msg = "获取成功"
  120. br.Data = resp
  121. }
  122. // DataAdd
  123. // @Title 录入数据
  124. // @Description 录入数据接口
  125. // @Param request body models.DataAddReq true "type json string"
  126. // @Success 200 Ret=200 录入成功
  127. // @router /data/add [post]
  128. func (this *TargetController) DataAdd() {
  129. br := new(models.BaseResponse).Init()
  130. defer func() {
  131. this.Data["json"] = br
  132. this.ServeJSON()
  133. }()
  134. var req models.DataAddReq
  135. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  136. if err != nil {
  137. br.Msg = "参数解析异常!"
  138. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  139. return
  140. }
  141. if req.TradeCode == "" {
  142. br.Msg = "指标编码不可为空!"
  143. return
  144. }
  145. if req.CreateDate == "" {
  146. br.Msg = "请选择日期"
  147. return
  148. }
  149. item, err := models.GetDataInfo(req.TradeCode, req.CreateDate)
  150. if err != nil && err.Error() != utils.ErrNoRow() {
  151. br.Msg = "获取信息失败!"
  152. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  153. return
  154. }
  155. if item != nil {
  156. br.Msg = "该指标当前日期下,数据已录入"
  157. return
  158. }
  159. edbdata := new(models.Edbdata)
  160. edbdata.TradeCode = req.TradeCode
  161. edbdata.Dt = req.CreateDate
  162. edbdata.Close = req.Close
  163. edbdata.ModifyTime = time.Now()
  164. _, err = models.AddEdbdata(edbdata)
  165. if err != nil {
  166. br.Msg = "新增失败"
  167. br.ErrMsg = "新增失败,Err:" + err.Error()
  168. return
  169. }
  170. br.Ret = 200
  171. br.Success = true
  172. br.Msg = "新增成功"
  173. }
  174. // DataEdit
  175. // @Title 编辑数据
  176. // @Description 编辑数据接口
  177. // @Param request body models.DataEditReq true "type json string"
  178. // @Success 200 Ret=200 编辑数据成功
  179. // @router /data/edit [post]
  180. func (this *TargetController) DataEdit() {
  181. br := new(models.BaseResponse).Init()
  182. defer func() {
  183. this.Data["json"] = br
  184. this.ServeJSON()
  185. }()
  186. var req models.DataEditReq
  187. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  188. if err != nil {
  189. br.Msg = "参数解析异常!"
  190. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  191. return
  192. }
  193. if req.TradeCode == "" {
  194. br.Msg = "指标编码不可为空!"
  195. return
  196. }
  197. if req.CreateDate == "" {
  198. br.Msg = "请选择日期"
  199. return
  200. }
  201. close := ""
  202. loc := reflect.ValueOf(req.Close).Kind().String()
  203. if loc == "string" {
  204. close = req.Close.(string)
  205. } else if loc == "int" {
  206. closeInt := req.Close.(int)
  207. close = strconv.Itoa(closeInt)
  208. } else if loc == "float64" {
  209. close64 := req.Close.(float64)
  210. close = strconv.FormatFloat(close64, 'g', 30, 64)
  211. } else {
  212. close = ""
  213. }
  214. edbdata := new(models.Edbdata)
  215. edbdata.TradeCode = req.TradeCode
  216. edbdata.Dt = req.CreateDate
  217. edbdata.Close = close
  218. edbdata.ModifyTime = time.Now()
  219. if req.OldCreateDate == "" {
  220. item, err := models.GetDataInfo(req.TradeCode, req.CreateDate)
  221. if err != nil && err.Error() != utils.ErrNoRow() {
  222. br.Msg = "获取信息失败!"
  223. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  224. return
  225. }
  226. if item == nil { //新增
  227. _, err = models.AddEdbdata(edbdata)
  228. if err != nil {
  229. br.Msg = "新增失败"
  230. br.ErrMsg = "新增失败,Err:" + err.Error()
  231. return
  232. }
  233. } else { //修改
  234. err = models.EditEdbdata(edbdata)
  235. if err != nil {
  236. br.Msg = "编辑失败"
  237. br.ErrMsg = "编辑失败,Err:" + err.Error()
  238. return
  239. }
  240. }
  241. } else {
  242. if req.CreateDate == req.OldCreateDate { //修改
  243. err = models.EditEdbdata(edbdata)
  244. if err != nil {
  245. br.Msg = "编辑失败"
  246. br.ErrMsg = "编辑失败,Err:" + err.Error()
  247. return
  248. }
  249. } else {
  250. item, err := models.GetDataInfo(req.TradeCode, req.CreateDate)
  251. if err != nil && err.Error() != utils.ErrNoRow() {
  252. br.Msg = "获取信息失败!"
  253. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  254. return
  255. }
  256. if item == nil {
  257. _, err = models.AddEdbdata(edbdata)
  258. if err != nil {
  259. br.Msg = "新增失败"
  260. br.ErrMsg = "新增失败,Err:" + err.Error()
  261. return
  262. }
  263. } else {
  264. err = models.EditEdbdata(edbdata)
  265. if err != nil {
  266. br.Msg = "编辑失败"
  267. br.ErrMsg = "编辑失败,Err:" + err.Error()
  268. return
  269. }
  270. }
  271. oldItem, err := models.GetDataInfo(req.TradeCode, req.OldCreateDate)
  272. if err != nil && err.Error() != utils.ErrNoRow() {
  273. br.Msg = "获取信息失败!"
  274. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  275. return
  276. }
  277. if oldItem != nil {
  278. record := new(models.EdbdataDeleteRecord)
  279. record.TradeCode = oldItem.TradeCode
  280. record.Dt = oldItem.Dt
  281. record.Close = oldItem.Close
  282. record.ModifyTime = oldItem.ModifyTime
  283. record.CreateTime = time.Now()
  284. record.SysUserId = this.SysUser.AdminId
  285. _, err = models.AddEdbdataDeleteRecord(record)
  286. if err != nil {
  287. br.Msg = "编辑失败"
  288. br.ErrMsg = "新增删除记录失败,Err:" + err.Error()
  289. return
  290. }
  291. //删除数据
  292. models.DeleteEdbData(req.TradeCode, req.OldCreateDate)
  293. }
  294. }
  295. }
  296. br.Ret = 200
  297. br.Success = true
  298. br.Msg = "编辑成功"
  299. }
  300. // BatchDataEdit
  301. // @Title 批量编辑数据
  302. // @Description 批量编辑数据接口
  303. // @Param request body models.BatchDataEditReq true "type json string"
  304. // @Success 200 Ret=200 编辑数据成功
  305. // @router /data/batch_edit [post]
  306. func (this *TargetController) BatchDataEdit() {
  307. br := new(models.BaseResponse).Init()
  308. defer func() {
  309. this.Data["json"] = br
  310. this.ServeJSON()
  311. }()
  312. var req models.BatchDataEditReq
  313. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  314. if err != nil {
  315. br.Msg = "参数解析异常!"
  316. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  317. return
  318. }
  319. if req.CreateDate == req.OldCreateDate { //修改
  320. br.Msg = "开始日期与结束日期一致!"
  321. return
  322. }
  323. edbDataEditList := make([]data.EdbDataEdit, 0)
  324. for _, trade := range req.List {
  325. if trade.TradeCode == "" {
  326. br.Msg = "指标编码不可为空!"
  327. return
  328. }
  329. close := ""
  330. loc := reflect.ValueOf(trade.Close).Kind().String()
  331. if loc == "string" {
  332. close = trade.Close.(string)
  333. } else if loc == "int" {
  334. closeInt := trade.Close.(int)
  335. close = strconv.Itoa(closeInt)
  336. } else if loc == "float64" {
  337. close64 := trade.Close.(float64)
  338. close = strconv.FormatFloat(close64, 'g', 30, 64)
  339. } else {
  340. close = ""
  341. }
  342. edbDataEdit := data.EdbDataEdit{
  343. OldCreateDate: req.OldCreateDate,
  344. CreateDate: req.CreateDate,
  345. TradeCode: trade.TradeCode,
  346. Close: close,
  347. }
  348. edbDataEditList = append(edbDataEditList, edbDataEdit)
  349. }
  350. //批量修改
  351. failEdbDataList, err := data.BatchEdbData(req.OldCreateDate, req.CreateDate, edbDataEditList, this.SysUser.AdminId)
  352. if err != nil {
  353. br.Msg = "批量修改失败"
  354. br.ErrMsg = "批量修改失败:" + err.Error()
  355. return
  356. }
  357. br.Ret = 200
  358. br.Success = true
  359. br.Data = failEdbDataList
  360. br.Msg = "编辑成功"
  361. }
  362. // TargetList
  363. // @Title 指标列表
  364. // @Description 指标列表接口
  365. // @Param PageSize query int true "每页数据条数"
  366. // @Param CurrentIndex query int true "当前页页码,从1开始"
  367. // @Param StartDate query string true "开始日期"
  368. // @Param EndDate query string true "结束日期"
  369. // @Param Frequency query string true "频度"
  370. // @Param ClassifyId query int true "分类"
  371. // @Param KeyWord query string true "名称关键词"
  372. // @Success 200 {object} models.TargetListResp
  373. // @router /target/list [get]
  374. func (this *TargetController) TargetList() {
  375. br := new(models.BaseResponse).Init()
  376. defer func() {
  377. this.Data["json"] = br
  378. this.ServeJSON()
  379. }()
  380. sysUser := this.SysUser
  381. if sysUser == nil {
  382. br.Msg = "请登录"
  383. br.Ret = 408
  384. return
  385. }
  386. resp := new(models.TargetListResp)
  387. pageSize, _ := this.GetInt("PageSize")
  388. currentIndex, _ := this.GetInt("CurrentIndex")
  389. keyWord := this.GetString("KeyWord")
  390. startDate := this.GetString("StartDate")
  391. endDate := this.GetString("EndDate")
  392. frequency := this.GetString("Frequency")
  393. classifyId, _ := this.GetInt("ClassifyId") //分类
  394. var startSize int
  395. if pageSize <= 0 {
  396. pageSize = utils.PageSize20
  397. }
  398. if currentIndex <= 0 {
  399. currentIndex = 1
  400. }
  401. startSize = utils.StartIndex(currentIndex, pageSize)
  402. var condition string
  403. var pars []interface{}
  404. userId := sysUser.AdminId
  405. //超管账号可以查看分类下的所有频度数据
  406. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  407. userId = 0
  408. }
  409. //获取账户所拥有权限的分类id集合
  410. classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  411. if err != nil {
  412. br.Msg = "获取分类数据失败"
  413. return
  414. }
  415. total := 0
  416. list := make([]*models.Edbinfo, 0)
  417. //有分类数据权限才查询
  418. if len(classifyIdStrList) > 0 {
  419. condition += ` AND a.classify_id in (` + strings.Join(classifyIdStrList, ",") + `) `
  420. //关键字
  421. if keyWord != "" {
  422. condition += ` AND (a.SEC_NAME LIKE '%` + keyWord + `%' or a.TRADE_CODE LIKE '%` + keyWord + `%' )`
  423. }
  424. if startDate != "" {
  425. startDate = startDate + " 00:00:00"
  426. condition += ` AND a.create_date >= ? `
  427. pars = append(pars, startDate)
  428. }
  429. if endDate != "" {
  430. endDate = endDate + " 23:59:59"
  431. condition += ` AND a.create_date <= ? `
  432. pars = append(pars, endDate)
  433. }
  434. if frequency != "" {
  435. condition += ` AND a.frequency = ? `
  436. pars = append(pars, frequency)
  437. }
  438. if classifyId > 0 {
  439. condition += ` AND a.classify_id = ? `
  440. pars = append(pars, classifyId)
  441. }
  442. mobile := sysUser.Mobile
  443. total, err = models.GetEdbinfoListCount(condition, pars, mobile, sysUser.RoleType)
  444. if err != nil {
  445. br.Msg = "获取失败"
  446. br.ErrMsg = "获取失败,Err:" + err.Error()
  447. return
  448. }
  449. tmpList, err := models.GetEdbinfoList(condition, pars, startSize, pageSize, mobile, sysUser.RoleType)
  450. if err != nil {
  451. br.Msg = "获取失败"
  452. br.ErrMsg = "获取失败,Err:" + err.Error()
  453. return
  454. }
  455. if len(tmpList) <= 0 {
  456. list = make([]*models.Edbinfo, 0)
  457. } else {
  458. list = tmpList
  459. }
  460. systemUserMap := make(map[int]string)
  461. for k, v := range list {
  462. userName, ok := systemUserMap[v.UserId]
  463. if ok == false {
  464. systemUser, _ := system.GetSysAdminById(v.UserId)
  465. if systemUser != nil {
  466. userName = systemUser.RealName
  467. } else {
  468. userName = `无`
  469. }
  470. systemUserMap[v.UserId] = userName
  471. }
  472. list[k].UserName = userName
  473. }
  474. }
  475. page := paging.GetPaging(currentIndex, pageSize, total)
  476. resp.Paging = page
  477. resp.List = list
  478. br.Ret = 200
  479. br.Success = true
  480. br.Msg = "获取成功"
  481. br.Data = resp
  482. }
  483. // TargetAdd
  484. // @Title 录入指标
  485. // @Description 录入指标接口
  486. // @Param request body models.EdbinfoAddReq true "type json string"
  487. // @Success 200 Ret=200 录入成功
  488. // @router /target/add [post]
  489. func (this *TargetController) TargetAdd() {
  490. br := new(models.BaseResponse).Init()
  491. defer func() {
  492. this.Data["json"] = br
  493. this.ServeJSON()
  494. }()
  495. var req models.EdbinfoAddReq
  496. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  497. if err != nil {
  498. br.Msg = "参数解析异常!"
  499. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  500. return
  501. }
  502. sysUser := this.SysUser
  503. if sysUser == nil {
  504. br.Msg = "请重新登录"
  505. return
  506. }
  507. tradeCode, err := models.GetMaxTradeCode()
  508. if err != nil {
  509. br.Msg = "获取数据失败"
  510. br.ErrMsg = "获取最大交易编码失败,Err:" + err.Error()
  511. return
  512. }
  513. maxTradeCode, err := utils.GetMaxTradeCode(tradeCode)
  514. if err != nil {
  515. br.Msg = "数据异常"
  516. br.ErrMsg = "获取编码信息失败,Err:" + err.Error()
  517. return
  518. }
  519. if maxTradeCode == "" {
  520. br.Msg = "编码为空,请联系技术"
  521. br.ErrMsg = "编码为空,请联系技术"
  522. return
  523. }
  524. if req.SecName == "" {
  525. br.Msg = "指标名称不能为空"
  526. return
  527. }
  528. item, err := models.GetEdbinfoBySecName(req.SecName)
  529. if err != nil && err.Error() != utils.ErrNoRow() {
  530. br.Msg = "获取信息失败!"
  531. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  532. return
  533. }
  534. if item != nil {
  535. br.Msg = "指标名称已存在"
  536. return
  537. }
  538. err = models.AddEdbinfo(maxTradeCode, req.SecName, req.Unit, "手动", req.Frequency, req.NoticeTime, req.ClassifyId, sysUser.AdminId)
  539. if err != nil {
  540. br.Msg = "新增失败"
  541. br.ErrMsg = "新增失败,Err:" + err.Error()
  542. return
  543. }
  544. err = models.AddEdbinfoUser(maxTradeCode, sysUser.Mobile)
  545. if err != nil {
  546. br.Msg = "新增失败"
  547. br.ErrMsg = "新增失败,Err:" + err.Error()
  548. return
  549. }
  550. br.Ret = 200
  551. br.Success = true
  552. br.Msg = "新增成功"
  553. }
  554. // TargetEdit
  555. // @Title 编辑指标
  556. // @Description 编辑指标
  557. // @Param request body models.EdbinfoEditReq true "type json string"
  558. // @Success 200 Ret=200 录入成功
  559. // @router /target/edit [post]
  560. func (this *TargetController) TargetEdit() {
  561. br := new(models.BaseResponse).Init()
  562. defer func() {
  563. this.Data["json"] = br
  564. this.ServeJSON()
  565. }()
  566. var req models.EdbinfoEditReq
  567. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  568. if err != nil {
  569. br.Msg = "参数解析异常!"
  570. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  571. return
  572. }
  573. sysUser := this.SysUser
  574. if sysUser == nil {
  575. br.Msg = "请重新登录"
  576. return
  577. }
  578. if req.TradeCode == "" {
  579. br.Msg = "数据异常"
  580. br.ErrMsg = "最大编码为空"
  581. return
  582. }
  583. if req.SecName == "" {
  584. br.Msg = "指标名称不能为空"
  585. return
  586. }
  587. item, err := models.GetEdbinfoBySecName(req.SecName)
  588. if err != nil && err.Error() != utils.ErrNoRow() {
  589. br.Msg = "获取信息失败!"
  590. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  591. return
  592. }
  593. if item != nil {
  594. if req.TradeCode != item.TradeCode {
  595. br.Msg = "指标名称已存在"
  596. return
  597. }
  598. }
  599. err = models.EditEdbinfo(req.TradeCode, req.SecName, req.Unit, req.Frequency, req.NoticeTime, req.ClassifyId)
  600. if err != nil {
  601. br.Msg = "编辑失败"
  602. br.ErrMsg = "编辑失败,Err:" + err.Error()
  603. return
  604. }
  605. br.Ret = 200
  606. br.Success = true
  607. br.Msg = "编辑成功"
  608. }
  609. // TargetSearch
  610. // @Title 搜索指标
  611. // @Description 搜索指标
  612. // @Param ClassifyId query int true "分类"
  613. // @Param KeyWord query string true "名称关键词"
  614. // @Success 200 {object} models.SearchTargetListResp
  615. // @router /target/search [get]
  616. func (this *TargetController) TargetSearch() {
  617. br := new(models.BaseResponse).Init()
  618. defer func() {
  619. this.Data["json"] = br
  620. this.ServeJSON()
  621. }()
  622. sysUser := this.SysUser
  623. if sysUser == nil {
  624. br.Msg = "请登录"
  625. br.Ret = 408
  626. return
  627. }
  628. classifyId, _ := this.GetInt("ClassifyId")
  629. keyWord := this.GetString("KeyWord")
  630. list, err := models.SearchTargetEntry(classifyId, keyWord)
  631. if err != nil {
  632. br.Msg = "获取失败"
  633. br.ErrMsg = "获取失败,Err:" + err.Error()
  634. return
  635. }
  636. resp := new(models.SearchTargetListResp)
  637. resp.List = list
  638. br.Ret = 200
  639. br.Success = true
  640. br.Msg = "获取成功"
  641. br.Data = resp
  642. }
  643. // ClassifyList
  644. // @Title 获取分类
  645. // @Description 获取分类
  646. // @Success 200 {object} models.EdbdataClassifyResp
  647. // @router /classify [get]
  648. func (this *TargetController) ClassifyList() {
  649. br := new(models.BaseResponse).Init()
  650. defer func() {
  651. this.Data["json"] = br
  652. this.ServeJSON()
  653. }()
  654. sysUser := this.SysUser
  655. if sysUser == nil {
  656. br.Msg = "请登录"
  657. br.Ret = 408
  658. return
  659. }
  660. //mobile := sysUser.Mobile
  661. //var wxUserId int64
  662. //if mobile != "" {
  663. // wxUser, err := models.GetUserByMobile(mobile)
  664. // if err != nil && err.Error() != utils.ErrNoRow() {
  665. // br.Msg = "获取用户信息失败"
  666. // br.ErrMsg = "获取用户信息失败,err:" + err.Error()
  667. // return
  668. // }
  669. // if wxUser != nil {
  670. // wxUserId = wxUser.UserId
  671. // }
  672. //}
  673. var wxUserId int64
  674. wxUserId = int64(sysUser.AdminId)
  675. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  676. wxUserId = 0
  677. }
  678. list, err := models.GetEdbdataClassify(wxUserId)
  679. if err != nil {
  680. br.Msg = "获取失败"
  681. br.ErrMsg = "获取失败,Err:" + err.Error()
  682. return
  683. }
  684. classifyIdStrList := make([]string, 0)
  685. for _, classifyList := range list {
  686. if classifyList.Child != nil {
  687. for _, classify := range classifyList.Child {
  688. classifyIdStrList = append(classifyIdStrList, strconv.Itoa(classify.ClassifyId))
  689. }
  690. }
  691. }
  692. if len(classifyIdStrList) > 0 {
  693. edbInfoGroupCountList, err := models.GetEdbInfoGroupCountByClassifyIds(strings.Join(classifyIdStrList, ","))
  694. if err != nil {
  695. br.Msg = "获取失败"
  696. br.ErrMsg = "获取分类下有数据的指标数量失败,Err:" + err.Error()
  697. return
  698. }
  699. edbInfoGroupCountMap := make(map[int]int)
  700. for _, edbInfoGroupCount := range edbInfoGroupCountList {
  701. edbInfoGroupCountMap[edbInfoGroupCount.ClassifyId] = edbInfoGroupCount.Count
  702. }
  703. for _, classifyList := range list {
  704. if classifyList.Child != nil {
  705. for _, classify := range classifyList.Child {
  706. if total, ok := edbInfoGroupCountMap[classify.ClassifyId]; ok {
  707. classify.EdbInfoTotal = total
  708. }
  709. }
  710. }
  711. }
  712. }
  713. resp := new(models.EdbdataClassifyResp)
  714. resp.List = list
  715. br.Ret = 200
  716. br.Success = true
  717. br.Msg = "获取成功"
  718. br.Data = resp
  719. }
  720. // TemplateDownload
  721. // @Title 下载模板
  722. // @Description 下载模板
  723. // @Success 200 {object} models.EdbdataClassifyResp
  724. // @router /template [get]
  725. func (this *TargetCommonController) TemplateDownload() {
  726. br := new(models.BaseResponse).Init()
  727. defer func() {
  728. this.Data["json"] = br
  729. this.ServeJSON()
  730. }()
  731. this.Ctx.Output.Download("./static/数据导入模板.xlsx", "数据导入模板.xlsx")
  732. br.Ret = 200
  733. br.Success = true
  734. br.Msg = "下载成功"
  735. }
  736. // ImportData
  737. // @Title 导入数据
  738. // @Description 导入数据
  739. // @Param EntryFile query file true "文件"
  740. // @Success 200 Ret=200 录入成功
  741. // @router /import/data [post]
  742. func (this *TargetController) ImportData() {
  743. br := new(models.BaseResponse).Init()
  744. defer func() {
  745. this.Data["json"] = br
  746. this.ServeJSON()
  747. }()
  748. sysUser := this.SysUser
  749. if sysUser == nil {
  750. br.Msg = "请重新登录"
  751. return
  752. }
  753. file, _, err := this.GetFile("EntryFile")
  754. if err != nil {
  755. br.Msg = "获取文件失败"
  756. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  757. return
  758. }
  759. path := "./static/数据导入_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  760. defer file.Close()
  761. err = this.SaveToFile("EntryFile", path)
  762. if err != nil {
  763. br.Msg = "文件保存失败"
  764. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  765. return
  766. }
  767. if utils.RunMode == "debug" {
  768. defer os.Remove(path)
  769. }
  770. xlFile, err := xlsx.OpenFile(path)
  771. if err != nil {
  772. fmt.Println(err.Error())
  773. return
  774. }
  775. //超管账号可以查看分类下的所有频度数据
  776. userId := sysUser.AdminId
  777. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  778. userId = 0
  779. }
  780. //获取账户所拥有权限的分类id集合
  781. classifyNameStrList, edbDataClassifyMap, err := data.GetEdbClassifyNameListByAdminId(int64(userId))
  782. if err != nil {
  783. br.Msg = "获取分类数据失败"
  784. return
  785. }
  786. //导入成功数量
  787. successCount := 0
  788. //导入失败数据
  789. failDatas := make([]*models.EdbdataImportFail, 0)
  790. //指标map
  791. targetMap := make(map[string]*models.Edbinfo)
  792. defer func() {
  793. for _, target := range targetMap {
  794. //结束后,清除掉对应的缓存
  795. key := "import:edbinfo:data:" + target.TradeCode
  796. utils.Rc.Delete(key)
  797. //将该指标的code加入到 “手工数据导入后刷新” 缓存
  798. if utils.Re == nil {
  799. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, target.TradeCode)
  800. if err != nil {
  801. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  802. }
  803. }
  804. }
  805. }()
  806. // 所有的指标数据map
  807. edbCodeDataMap := make(map[string]map[string]string)
  808. // 遍历sheet页读取
  809. for _, sheet := range xlFile.Sheets {
  810. fmt.Println("sheet name: ", sheet.Name)
  811. //遍历行读取
  812. maxRow := sheet.MaxRow
  813. fmt.Println("maxRow:", maxRow)
  814. fmt.Println("maxRow")
  815. for i := 0; i < maxRow; i++ {
  816. if i == 1 {
  817. row := sheet.Row(i)
  818. cells := row.Cells
  819. if len(cells) < 6 {
  820. br.ErrMsg = "导入文件异常,请下载最新导入模板文件"
  821. br.Msg = "导入文件异常,请下载最新导入模板文件"
  822. return
  823. }
  824. templateFail := false
  825. if cells[0].Value != "品种分类" {
  826. templateFail = true
  827. }
  828. if cells[1].Value != "录入日期" {
  829. templateFail = true
  830. }
  831. if cells[2].Value != "指标名称" {
  832. templateFail = true
  833. }
  834. if cells[3].Value != "值" {
  835. templateFail = true
  836. }
  837. if cells[4].Value != "频度" {
  838. templateFail = true
  839. }
  840. if cells[5].Value != "单位" {
  841. templateFail = true
  842. }
  843. if templateFail {
  844. br.ErrMsg = "导入文件异常,请下载最新导入模板文件"
  845. br.Msg = "导入文件异常,请下载最新导入模板文件"
  846. return
  847. }
  848. }
  849. if i > 1 {
  850. row := sheet.Row(i)
  851. cells := row.Cells
  852. if len(cells) >= 6 {
  853. classifyName := cells[0].Value //分类
  854. if classifyName == "" { //过滤空白行
  855. continue
  856. }
  857. cell1 := cells[1].Value
  858. //createDate := utils.ConvertToFormatDay(cell1) //录入日期
  859. createDate := cell1
  860. cell2 := cells[2].Value //指标名称
  861. secName := utils.TrimStr(cell2)
  862. frequency := cells[4].Value //频度
  863. unit := cells[5].Value //单位
  864. closeVal := cells[3].Value //值
  865. if strings.Contains(closeVal, "#N/A") {
  866. continue
  867. }
  868. closeValFloat, err := cells[3].Float() //值
  869. if err != nil {
  870. failItem := new(models.EdbdataImportFail)
  871. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  872. failItem.ClassifyName = classifyName
  873. failItem.CreateDate = createDate
  874. failItem.SecName = secName
  875. failItem.Close = cells[3].Value
  876. failItem.Remark = "值类型异常"
  877. failItem.Frequency = frequency
  878. failItem.Unit = unit
  879. failDatas = append(failDatas, failItem)
  880. continue
  881. }
  882. newDecimal := decimal.NewFromFloat(closeValFloat)
  883. newDecimal.Round(4)
  884. closeVal = newDecimal.String()
  885. if strings.Contains(closeVal, "#N/A") {
  886. continue
  887. }
  888. //closeVal := closeValFloat.
  889. //判断指标,类型,等数据是否正常
  890. classifyName = strings.Trim(classifyName, " ")
  891. frequency = utils.TrimStr(frequency)
  892. unit = utils.TrimStr(unit)
  893. // 成功数量超过20个指标,那就不导入了
  894. if len(targetMap) >= 150 {
  895. failItem := new(models.EdbdataImportFail)
  896. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  897. failItem.ClassifyName = classifyName
  898. failItem.CreateDate = createDate
  899. failItem.SecName = secName
  900. failItem.Close = closeVal
  901. failItem.Remark = "导入指标数量过多"
  902. failItem.Frequency = frequency
  903. failItem.Unit = unit
  904. failDatas = append(failDatas, failItem)
  905. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
  906. continue
  907. }
  908. if !strings.Contains(strings.Join(classifyNameStrList, ","), classifyName) {
  909. failItem := new(models.EdbdataImportFail)
  910. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  911. failItem.ClassifyName = classifyName
  912. failItem.CreateDate = createDate
  913. failItem.SecName = secName
  914. failItem.Close = closeVal
  915. failItem.Remark = "没有该品种分类权限"
  916. failItem.Frequency = frequency
  917. failItem.Unit = unit
  918. failDatas = append(failDatas, failItem)
  919. continue
  920. }
  921. //fmt.Println(classifyName, createDate, secName, closeVal)
  922. //校验表格中的日期格式
  923. if strings.Contains(createDate, "-") {
  924. //如果是带有 - 的普通日期格式文本
  925. _, timeErr := time.Parse("2006-1-2", createDate)
  926. if timeErr != nil {
  927. failItem := new(models.EdbdataImportFail)
  928. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  929. failItem.ClassifyName = classifyName
  930. failItem.CreateDate = createDate
  931. failItem.SecName = secName
  932. failItem.Close = closeVal
  933. failItem.Remark = "日期格式异常"
  934. failItem.Frequency = frequency
  935. failItem.Unit = unit
  936. failDatas = append(failDatas, failItem)
  937. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
  938. continue
  939. }
  940. } else if strings.Contains(createDate, "/") {
  941. //如果是带有 / 的普通日期格式文本
  942. createDateTime, timeErr := time.Parse("2006/1/2", createDate)
  943. if timeErr != nil {
  944. failItem := new(models.EdbdataImportFail)
  945. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  946. failItem.ClassifyName = classifyName
  947. failItem.CreateDate = createDate
  948. failItem.SecName = secName
  949. failItem.Close = closeVal
  950. failItem.Remark = "日期格式异常"
  951. failItem.Frequency = frequency
  952. failItem.Unit = unit
  953. failDatas = append(failDatas, failItem)
  954. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
  955. continue
  956. }
  957. createDate = createDateTime.Format("2006-01-02")
  958. } else {
  959. //可能是excel的日期格式
  960. _, tmpErr := strconv.Atoi(createDate)
  961. if tmpErr != nil {
  962. failItem := new(models.EdbdataImportFail)
  963. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  964. failItem.ClassifyName = classifyName
  965. failItem.CreateDate = createDate
  966. failItem.SecName = secName
  967. failItem.Close = closeVal
  968. failItem.Remark = "日期格式异常"
  969. failItem.Frequency = frequency
  970. failItem.Unit = unit
  971. failDatas = append(failDatas, failItem)
  972. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
  973. continue
  974. }
  975. createDate = utils.ConvertToFormatDay(createDate) //录入日期
  976. }
  977. //获取指标分类信息
  978. classify, ok := edbDataClassifyMap[classifyName]
  979. if !ok {
  980. failItem := new(models.EdbdataImportFail)
  981. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  982. failItem.ClassifyName = classifyName
  983. failItem.CreateDate = createDate
  984. failItem.SecName = secName
  985. failItem.Close = closeVal
  986. failItem.Remark = "指标分类不存在"
  987. failItem.Frequency = frequency
  988. failItem.Unit = unit
  989. failDatas = append(failDatas, failItem)
  990. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
  991. continue
  992. }
  993. //判断指标是否存在
  994. target, ok := targetMap[secName]
  995. if !ok {
  996. tmpTarget, err := models.GetTargetBySecName(secName)
  997. if err != nil {
  998. //如果是找不到该指标,那么新增指标
  999. if err.Error() == utils.ErrNoRow() {
  1000. if frequency == "" {
  1001. failItem := new(models.EdbdataImportFail)
  1002. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  1003. failItem.ClassifyName = classifyName
  1004. failItem.CreateDate = createDate
  1005. failItem.SecName = secName
  1006. failItem.Close = closeVal
  1007. failItem.Remark = "新增指标失败,频度字段为空"
  1008. failItem.Frequency = frequency
  1009. failItem.Unit = unit
  1010. failDatas = append(failDatas, failItem)
  1011. continue
  1012. }
  1013. if unit == "" {
  1014. failItem := new(models.EdbdataImportFail)
  1015. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  1016. failItem.ClassifyName = classifyName
  1017. failItem.CreateDate = createDate
  1018. failItem.SecName = secName
  1019. failItem.Close = closeVal
  1020. failItem.Remark = "新增指标失败,单位字段为空"
  1021. failItem.Frequency = frequency
  1022. failItem.Unit = unit
  1023. failDatas = append(failDatas, failItem)
  1024. continue
  1025. }
  1026. tmpErr := data.AddEdbInfo(secName, unit, frequency, "", sysUser.Mobile, classify.ClassifyId, sysUser.AdminId)
  1027. if tmpErr != nil {
  1028. fmt.Println("line 158")
  1029. failItem := new(models.EdbdataImportFail)
  1030. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  1031. failItem.ClassifyName = classifyName
  1032. failItem.CreateDate = createDate
  1033. failItem.SecName = secName
  1034. failItem.Close = closeVal
  1035. failItem.Remark = "新增指标失败"
  1036. failItem.Frequency = frequency
  1037. failItem.Unit = unit
  1038. failDatas = append(failDatas, failItem)
  1039. continue
  1040. }
  1041. tmpTarget, tmpErr := models.GetTargetBySecName(secName)
  1042. target = tmpTarget
  1043. targetMap[secName] = target
  1044. } else {
  1045. fmt.Println("导入数据 获取指标:Err:" + err.Error())
  1046. }
  1047. } else {
  1048. target = tmpTarget
  1049. targetMap[secName] = target
  1050. }
  1051. //设置10分钟缓存,不允许其他地方删除
  1052. key := "import:edbinfo:data:" + target.TradeCode
  1053. utils.Rc.SetNX(key, 1, time.Second*600)
  1054. }
  1055. if target == nil {
  1056. fmt.Println("指标不存在")
  1057. failItem := new(models.EdbdataImportFail)
  1058. failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  1059. failItem.ClassifyName = classifyName
  1060. failItem.CreateDate = createDate
  1061. failItem.SecName = secName
  1062. failItem.Close = closeVal
  1063. failItem.Remark = "指标不存在"
  1064. failItem.Frequency = frequency
  1065. failItem.Unit = unit
  1066. failDatas = append(failDatas, failItem)
  1067. continue
  1068. }
  1069. //更新指标信息
  1070. updateCols := make([]string, 0)
  1071. //更新指标分类
  1072. if target.ClassifyId <= 0 && classify.ClassifyId > 0 {
  1073. target.ClassifyId = classify.ClassifyId
  1074. updateCols = append(updateCols, "ClassifyId")
  1075. }
  1076. if target.Frequency != frequency {
  1077. target.Frequency = frequency
  1078. target.NoticeTime = ""
  1079. updateCols = append(updateCols, "Frequency", "NoticeTime")
  1080. }
  1081. if target.Unit != unit {
  1082. target.Unit = unit
  1083. updateCols = append(updateCols, "Unit")
  1084. }
  1085. if len(updateCols) > 0 {
  1086. _ = target.Update(updateCols)
  1087. }
  1088. // 判断指标数据列表是否已经存在
  1089. tmpDataMap, ok := edbCodeDataMap[target.TradeCode]
  1090. if !ok {
  1091. tmpDataMap = make(map[string]string)
  1092. dataList, tmpErr := models.GetTargetsDataList(target.TradeCode)
  1093. if tmpErr != nil {
  1094. go alarm_msg.SendAlarmMsg("导入数据"+target.TradeCode+" 获取指标的数据失败,Err:"+err.Error(), 3)
  1095. }
  1096. for _, tmpData := range dataList {
  1097. tmpDataMap[tmpData.Dt] = tmpData.Close
  1098. }
  1099. edbCodeDataMap[target.TradeCode] = tmpDataMap
  1100. }
  1101. //判断数据是否已经存在
  1102. tmpVal, ok := tmpDataMap[createDate]
  1103. //数据已存在,进行更新操作
  1104. if ok {
  1105. if tmpVal != closeVal {
  1106. err = models.ModifyTargetsDataByImport(target.TradeCode, createDate, closeVal)
  1107. if err != nil {
  1108. go alarm_msg.SendAlarmMsg("导入数据 修改数据失败,Err:"+err.Error(), 3)
  1109. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  1110. }
  1111. }
  1112. } else { //数据不存在,进行新增操作
  1113. if target.TradeCode != "" && createDate != "" && closeVal != "" {
  1114. models.AddTargetsDataByImport(target.TradeCode, createDate, closeVal)
  1115. if err != nil {
  1116. go alarm_msg.SendAlarmMsg("导入数据 新增数据失败,Err:"+err.Error(), 3)
  1117. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 新增数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  1118. }
  1119. tmpDataMap[createDate] = closeVal
  1120. edbCodeDataMap[target.TradeCode] = tmpDataMap
  1121. }
  1122. }
  1123. successCount++
  1124. } else {
  1125. //br.ErrMsg = "请填写导入内容"
  1126. //br.Msg = "请填写导入内容"
  1127. //return
  1128. }
  1129. }
  1130. }
  1131. }
  1132. resp := models.EdbdataImportResp{
  1133. SuccessCount: successCount,
  1134. FailCount: len(failDatas),
  1135. }
  1136. fmt.Println("failDatas:", len(failDatas))
  1137. if len(failDatas) > 0 {
  1138. //先删除导入失败记录
  1139. _ = models.DelEdbDataImportFail(sysUser.AdminId)
  1140. //错误信息字符串切片,最后作为发送邮件通知使用
  1141. failContents := make([]string, 0)
  1142. for _, v := range failDatas {
  1143. failContents = append(failContents, fmt.Sprint(v.SecName, "导入失败:", v.Remark))
  1144. err = models.AddEdbdataImportFail(v)
  1145. if err != nil {
  1146. go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
  1147. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 新增失败记录失败:Err:"+err.Error(), utils.EmailSendToUsers)
  1148. }
  1149. }
  1150. //导入失败的话,最后统一邮件提醒就好啦,不需要每次都去提醒
  1151. go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), 3)
  1152. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), utils.EmailSendToUsers)
  1153. if successCount == 0 {
  1154. resp.Status = -1
  1155. resp.Msg = "导入失败"
  1156. } else {
  1157. resp.Status = 1
  1158. resp.Msg = "存在部分导入失败"
  1159. }
  1160. } else {
  1161. resp.Status = 0
  1162. }
  1163. br.Msg = "导入成功"
  1164. br.Ret = 200
  1165. br.Success = true
  1166. br.Data = resp
  1167. }
  1168. // ImportFailListDownload
  1169. // @Title 下载失败列表
  1170. // @Description 下载失败列表
  1171. // @Success 200 {object} models.EdbdataClassifyResp
  1172. // @router /import/failList [get]
  1173. func (this *TargetController) ImportFailListDownload() {
  1174. br := new(models.BaseResponse).Init()
  1175. defer func() {
  1176. this.Data["json"] = br
  1177. this.ServeJSON()
  1178. }()
  1179. sysUser := this.SysUser
  1180. if sysUser == nil {
  1181. br.Msg = "请重新登录"
  1182. return
  1183. }
  1184. item, err := models.GetFailList(sysUser.AdminId)
  1185. if err != nil {
  1186. br.Msg = "获取数据失败"
  1187. br.Msg = "获取数据失败,Err:" + err.Error()
  1188. return
  1189. }
  1190. xlsxFile := xlsx.NewFile()
  1191. sheet, err := xlsxFile.AddSheet("导入失败数据")
  1192. headRow := sheet.AddRow()
  1193. headRow.AddCell().SetValue("品种分类")
  1194. headRow.AddCell().SetValue("录入日期")
  1195. headRow.AddCell().SetValue("指标名称")
  1196. headRow.AddCell().SetValue("值")
  1197. headRow.AddCell().SetValue("频度")
  1198. headRow.AddCell().SetValue("单位")
  1199. headRow.AddCell().SetValue("备注")
  1200. for _, v := range item {
  1201. row := sheet.AddRow()
  1202. row.AddCell().SetValue(v.ClassifyName)
  1203. row.AddCell().SetValue(v.CreateDate)
  1204. row.AddCell().SetValue(v.SecName)
  1205. row.AddCell().SetValue(v.Close)
  1206. row.AddCell().SetValue(v.Frequency)
  1207. row.AddCell().SetValue(v.Unit)
  1208. row.AddCell().SetValue(v.Remark)
  1209. }
  1210. fileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1211. savePath := "./static/" + fileName
  1212. err = xlsxFile.Save(savePath)
  1213. if err != nil {
  1214. br.Msg = "文件保存失败"
  1215. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  1216. return
  1217. }
  1218. defer func() {
  1219. os.Remove(savePath)
  1220. }()
  1221. this.Ctx.Output.Download(savePath, "失败列表.xlsx")
  1222. }
  1223. // ExportDataList
  1224. // @Title 导出数据
  1225. // @Description 导出数据列表
  1226. // @Param StartDate query string true "开始日期"
  1227. // @Param EndDate query string true "结束日期"
  1228. // @Param Frequency query string true "频度"
  1229. // @Param ClassifyId query int true "分类"
  1230. // @Param KeyWord query string true "名称关键词"
  1231. // @Param TradeCode query string true "指标唯一编码"
  1232. // @Param Mobile query string true "手机号"
  1233. // @Success 200 导出成功
  1234. // @router /export/dataList [get]
  1235. func (this *TargetController) ExportDataList() {
  1236. br := new(models.BaseResponse).Init()
  1237. defer func() {
  1238. this.Data["json"] = br
  1239. this.ServeJSON()
  1240. }()
  1241. sysUser := this.SysUser
  1242. if sysUser == nil {
  1243. br.Msg = "请重新登录"
  1244. return
  1245. }
  1246. keyWord := this.GetString("KeyWord")
  1247. startDate := this.GetString("StartDate")
  1248. endDate := this.GetString("EndDate")
  1249. //frequency := this.GetString("Frequency")
  1250. classifyId, _ := this.GetInt("ClassifyId") //分类
  1251. mobile := this.GetString("Mobile") //分类
  1252. tradeCode := this.GetString("TradeCode") //指标唯一编码
  1253. var condition string
  1254. var pars []interface{}
  1255. //分类ID
  1256. if keyWord != "" {
  1257. condition += ` AND a.SEC_NAME LIKE '%` + keyWord + `%'`
  1258. }
  1259. //分类ID
  1260. if tradeCode != "" {
  1261. condition += ` AND a.TRADE_CODE = ? `
  1262. pars = append(pars, tradeCode)
  1263. }
  1264. if startDate != "" {
  1265. condition += ` AND c.DT >= ? `
  1266. pars = append(pars, startDate)
  1267. }
  1268. if endDate != "" {
  1269. condition += ` AND c.DT <= ? `
  1270. pars = append(pars, endDate)
  1271. }
  1272. //if frequency != "" {
  1273. // condition += ` AND a.frequency = ? `
  1274. // pars = append(pars, frequency)
  1275. //}
  1276. if classifyId > 0 {
  1277. condition += ` AND a.classify_id = ? `
  1278. pars = append(pars, classifyId)
  1279. }
  1280. if mobile != "" {
  1281. condition += ` AND d.mobile=? `
  1282. pars = append(pars, mobile)
  1283. }
  1284. userId := sysUser.AdminId
  1285. //超管账号可以查看分类下的所有频度数据
  1286. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  1287. userId = 0
  1288. }
  1289. //获取账户所拥有权限的分类id集合
  1290. classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  1291. if err != nil {
  1292. br.Msg = "获取分类数据失败"
  1293. return
  1294. }
  1295. dir, _ := os.Executable()
  1296. exPath := filepath.Dir(dir)
  1297. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1298. xlsxFile := xlsx.NewFile()
  1299. if err != nil {
  1300. br.Msg = "生成文件失败"
  1301. br.ErrMsg = "生成文件失败"
  1302. return
  1303. }
  1304. var frequencies []*string
  1305. if keyWord == "" {
  1306. frequencies, err = models.GetEdbDataFrequency(classifyId)
  1307. if err != nil {
  1308. br.Msg = "查询频度失败"
  1309. br.ErrMsg = "查询频度失败"
  1310. return
  1311. }
  1312. } else {
  1313. frequencies, err = models.GetEdbDataFrequencyByKeyord(keyWord)
  1314. if err != nil {
  1315. br.Msg = "查询频度失败"
  1316. br.ErrMsg = "查询频度失败"
  1317. return
  1318. }
  1319. }
  1320. fileName := `手工数据`
  1321. if frequencies == nil {
  1322. sheet, err := xlsxFile.AddSheet("无数据")
  1323. if err != nil {
  1324. br.Msg = "新增Sheet失败"
  1325. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1326. return
  1327. }
  1328. rowSecName := sheet.AddRow()
  1329. celSecName := rowSecName.AddCell()
  1330. celSecName.SetValue("")
  1331. }
  1332. for _, frequency := range frequencies {
  1333. secNameList := make([]*models.EdbdataExportList, 0)
  1334. if len(classifyIdStrList) > 0 {
  1335. newCondition := condition
  1336. newCondition += ` AND a.classify_id in (` + strings.Join(classifyIdStrList, ",") + `) `
  1337. newCondition += ` AND a.frequency = ? `
  1338. pars := append(pars, frequency)
  1339. tmpSecNameList, err := models.GetEdbdataSecName(newCondition, pars)
  1340. if err != nil {
  1341. br.Msg = "获取数据失败"
  1342. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1343. return
  1344. }
  1345. secNameList = tmpSecNameList
  1346. }
  1347. var sheet *xlsx.Sheet
  1348. if len(secNameList) > 0 {
  1349. sheet, err = xlsxFile.AddSheet(*frequency)
  1350. if err != nil {
  1351. br.Msg = "新增Sheet失败"
  1352. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1353. return
  1354. }
  1355. fileName = secNameList[0].ClassifyName
  1356. } else {
  1357. continue
  1358. }
  1359. //获取指标数据
  1360. rowSecName := sheet.AddRow()
  1361. celSecName := rowSecName.AddCell()
  1362. celSecName.SetValue("指标名称")
  1363. rowFrequency := sheet.AddRow()
  1364. celFrequency := rowFrequency.AddCell()
  1365. celFrequency.SetValue("频率")
  1366. rowUnit := sheet.AddRow()
  1367. celUnit := rowUnit.AddCell()
  1368. celUnit.SetValue("单位")
  1369. rowModifyDate := sheet.AddRow()
  1370. rowModifyCell := rowModifyDate.AddCell()
  1371. rowModifyCell.SetValue("更新时间")
  1372. dataMap := make(map[string]map[string]*models.EdbdataItem)
  1373. var tradeCodeList []string
  1374. for _, v := range secNameList {
  1375. cellSenName := rowSecName.AddCell()
  1376. cellSenName.SetValue(v.SecName)
  1377. celFrequency := rowFrequency.AddCell()
  1378. celFrequency.SetValue(v.Frequency)
  1379. celUnit := rowUnit.AddCell()
  1380. celUnit.SetValue(v.Unit)
  1381. rowModifyCell := rowModifyDate.AddCell()
  1382. rowModifyCell.SetValue(v.Dt)
  1383. tradeCodeList = append(tradeCodeList, v.TradeCode)
  1384. items, err := models.GetEdbdataAllByTradeCode(v.TradeCode)
  1385. if err != nil && err.Error() != utils.ErrNoRow() {
  1386. br.ErrMsg = "GetEdbdataAllByTradeCode,Err:" + err.Error()
  1387. br.Msg = "获取数据失败"
  1388. return
  1389. }
  1390. for _, item := range items {
  1391. if dataMap[item.TradeCode] == nil {
  1392. dataMap[item.TradeCode] = make(map[string]*models.EdbdataItem)
  1393. }
  1394. dataMap[item.TradeCode][item.Dt] = item
  1395. fmt.Println(item.TradeCode, item.Dt)
  1396. }
  1397. }
  1398. tradeCodeStr := strings.Join(tradeCodeList, "','")
  1399. tradeCodeStr = "'" + tradeCodeStr + "'"
  1400. dateList, err := models.GetEdbdataList(tradeCodeStr)
  1401. if err != nil {
  1402. br.Msg = "获取数据失败"
  1403. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1404. return
  1405. }
  1406. for _, n := range dateList {
  1407. rowData := sheet.AddRow()
  1408. celDate := rowData.AddCell()
  1409. celDate.SetValue(n.Dt)
  1410. for _, m := range secNameList {
  1411. celData := rowData.AddCell()
  1412. fmt.Println(m.TradeCode)
  1413. fmt.Println(n.Dt)
  1414. if dataMap[m.TradeCode][n.Dt] != nil {
  1415. celData.SetValue(dataMap[m.TradeCode][n.Dt].Close)
  1416. }
  1417. }
  1418. }
  1419. }
  1420. err = xlsxFile.Save(downLoadnFilePath)
  1421. if err != nil {
  1422. //有指标无数据时先导出一遍空表
  1423. sheet, err := xlsxFile.AddSheet("无数据")
  1424. if err != nil {
  1425. br.Msg = "新增Sheet失败"
  1426. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1427. return
  1428. }
  1429. rowSecName := sheet.AddRow()
  1430. celSecName := rowSecName.AddCell()
  1431. celSecName.SetValue("")
  1432. err = xlsxFile.Save(downLoadnFilePath)
  1433. if err != nil {
  1434. br.Msg = "保存文件失败"
  1435. br.ErrMsg = "保存文件失败"
  1436. return
  1437. }
  1438. }
  1439. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  1440. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1441. defer func() {
  1442. os.Remove(downLoadnFilePath)
  1443. }()
  1444. br.Ret = 200
  1445. br.Success = true
  1446. br.Msg = "success"
  1447. }
  1448. // DataList
  1449. // @Title 导出数据
  1450. // @Description 导出数据列表
  1451. // @Param StartDate query string true "开始日期"
  1452. // @Param EndDate query string true "结束日期"
  1453. // @Param Frequency query string true "频度"
  1454. // @Param ClassifyId query int true "分类"
  1455. // @Param KeyWord query string true "名称关键词"
  1456. // @Param Mobile query string true "手机号"
  1457. // @Success 200 导出成功
  1458. // @router /dataList [get]
  1459. func (this *TargetCommonController) DataList() {
  1460. br := new(models.BaseResponse).Init()
  1461. defer func() {
  1462. this.Data["json"] = br
  1463. this.ServeJSON()
  1464. }()
  1465. br.Msg = "成功"
  1466. br.Ret = 200
  1467. }
  1468. // DataDelete
  1469. // @Title 数据删除
  1470. // @Description 数据删除
  1471. // @Param request body models.DataDeleteReq true "type json string"
  1472. // @Success 200 Ret=200 录入成功
  1473. // @router /data/delete [post]
  1474. func (this *TargetController) DataDelete() {
  1475. br := new(models.BaseResponse).Init()
  1476. defer func() {
  1477. this.Data["json"] = br
  1478. this.ServeJSON()
  1479. }()
  1480. var req models.DataDeleteReq
  1481. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1482. if err != nil {
  1483. br.Msg = "参数解析异常!"
  1484. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1485. return
  1486. }
  1487. sysUser := this.SysUser
  1488. if sysUser == nil {
  1489. br.Msg = "请重新登录"
  1490. return
  1491. }
  1492. if req.TradeCode == "" {
  1493. br.Msg = "指标编码不可为空"
  1494. br.ErrMsg = "指标编码不可为空"
  1495. return
  1496. }
  1497. if req.CreateDate == "" {
  1498. br.Msg = "数据录入日期不可为空"
  1499. return
  1500. }
  1501. item, err := models.GetDataInfo(req.TradeCode, req.CreateDate)
  1502. if err != nil && err.Error() != utils.ErrNoRow() {
  1503. br.Msg = "获取信息失败!"
  1504. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  1505. return
  1506. }
  1507. if item == nil {
  1508. br.Msg = "数据不存在"
  1509. return
  1510. }
  1511. err = models.DataDelete(req.TradeCode, req.CreateDate, item.Close, item.ModifyTime, sysUser.AdminId)
  1512. if err != nil {
  1513. br.Msg = "删除失败"
  1514. br.ErrMsg = "删除失败,Err:" + err.Error()
  1515. return
  1516. }
  1517. br.Ret = 200
  1518. br.Success = true
  1519. br.Msg = "删除成功"
  1520. }
  1521. // DataCheck
  1522. // @Title 数据删除校验
  1523. // @Description 数据删除校验
  1524. // @Param TradeCode query string true "指标唯一编码"
  1525. // @Param CreateDate query string true "数据录入日期"
  1526. // @Success Ret=200 可删除
  1527. // @router /data/check [get]
  1528. func (this *TargetController) DataCheck() {
  1529. br := new(models.BaseResponse).Init()
  1530. defer func() {
  1531. this.Data["json"] = br
  1532. this.ServeJSON()
  1533. }()
  1534. tradeCode := this.GetString("TradeCode")
  1535. createDate := this.GetString("CreateDate")
  1536. sysUser := this.SysUser
  1537. if sysUser == nil {
  1538. br.Msg = "请重新登录"
  1539. return
  1540. }
  1541. if tradeCode == "" {
  1542. br.Msg = "指标编码不可为空"
  1543. return
  1544. }
  1545. if createDate == "" {
  1546. br.Msg = "请选择日期"
  1547. return
  1548. }
  1549. resp := new(models.DataCheckResp)
  1550. item, err := models.GetDataInfo(tradeCode, createDate)
  1551. if err != nil && err.Error() != utils.ErrNoRow() {
  1552. br.Msg = "获取数据失败"
  1553. br.Msg = "获取数据失败,Err:" + err.Error()
  1554. return
  1555. }
  1556. if item != nil {
  1557. resp.Status = 1
  1558. resp.Close = item.Close
  1559. } else {
  1560. resp.Status = 0
  1561. }
  1562. br.Ret = 200
  1563. br.Success = true
  1564. br.Msg = "检测成功"
  1565. br.Data = resp
  1566. }
  1567. // TargetCheck
  1568. // @Title 指标删除校验
  1569. // @Description 指标删除校验
  1570. // @Param TradeCode query string true "指标唯一编码"
  1571. // @Success Ret=200 可删除
  1572. // @router /target/check [get]
  1573. func (this *TargetController) TargetCheck() {
  1574. br := new(models.BaseResponse).Init()
  1575. defer func() {
  1576. this.Data["json"] = br
  1577. this.ServeJSON()
  1578. }()
  1579. tradeCode := this.GetString("TradeCode")
  1580. sysUser := this.SysUser
  1581. if sysUser == nil {
  1582. br.Msg = "请重新登录"
  1583. return
  1584. }
  1585. if tradeCode == "" {
  1586. br.Msg = "指标编码不可为空"
  1587. return
  1588. }
  1589. resp := new(models.DataCheckResp)
  1590. count, err := models.GetTargetInfoCount(tradeCode)
  1591. if err != nil && err.Error() != utils.ErrNoRow() {
  1592. br.Msg = "获取数据失败"
  1593. br.Msg = "获取数据失败,Err:" + err.Error()
  1594. return
  1595. }
  1596. if count > 0 {
  1597. resp.Status = 1
  1598. } else {
  1599. resp.Status = 0
  1600. }
  1601. br.Ret = 200
  1602. br.Success = true
  1603. br.Msg = "指标不存在关联数据,可直接删除"
  1604. br.Data = resp
  1605. }
  1606. // TargetDelete
  1607. // @Title 指标删除
  1608. // @Description 指标删除
  1609. // @Param request body models.TargetDeleteReq true "type json string"
  1610. // @Success Ret=200 删除成功
  1611. // @router /target/delete [post]
  1612. func (this *TargetController) TargetDelete() {
  1613. br := new(models.BaseResponse).Init()
  1614. defer func() {
  1615. this.Data["json"] = br
  1616. this.ServeJSON()
  1617. }()
  1618. var req models.TargetDeleteReq
  1619. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1620. if err != nil {
  1621. br.Msg = "参数解析异常!"
  1622. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1623. return
  1624. }
  1625. if req.TradeCode == "" {
  1626. br.Msg = "指标编码不可为空"
  1627. return
  1628. }
  1629. //判断是否在指标数据导入的过程,如果是在该过程,那么就不允许用户操作
  1630. key := "import:edbinfo:data:" + req.TradeCode
  1631. if utils.Rc.IsExist(key) {
  1632. br.Msg = "该指标正在导入数据中,请稍后操作"
  1633. return
  1634. }
  1635. err = models.TargetDelete(req.TradeCode)
  1636. if err != nil {
  1637. br.Msg = "删除失败"
  1638. br.Msg = "删除失败,Err:" + err.Error()
  1639. return
  1640. }
  1641. br.Ret = 200
  1642. br.Success = true
  1643. br.IsAddLog = true
  1644. br.Msg = "删除成功"
  1645. }
  1646. // GetResearcher
  1647. // @Title 获取研究员
  1648. // @Description 获取研究员
  1649. // @Success 200 {object} models.ResearcherListResp
  1650. // @router /getResearcher [get]
  1651. func (this *TargetController) GetResearcher() {
  1652. br := new(models.BaseResponse).Init()
  1653. defer func() {
  1654. this.Data["json"] = br
  1655. this.ServeJSON()
  1656. }()
  1657. sysUser := this.SysUser
  1658. if sysUser == nil {
  1659. br.Msg = "请重新登录"
  1660. return
  1661. }
  1662. var err error
  1663. list := make([]*models.Researcher, 0)
  1664. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN ||
  1665. sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  1666. sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  1667. list, err = models.GetResearcherEntry()
  1668. if err != nil {
  1669. br.Msg = "获取数据失败"
  1670. br.Msg = "获取数据失败,Err:" + err.Error()
  1671. return
  1672. }
  1673. } else if sysUser.Role == "hggroup" {
  1674. mobileStr := "'15888577277','15888856487','15158848535','13651627324' "
  1675. list, err = models.GetResearcherEntryByMobile(mobileStr)
  1676. if err != nil {
  1677. br.Msg = "获取数据失败"
  1678. br.Msg = "获取数据失败,Err:" + err.Error()
  1679. return
  1680. }
  1681. } else if sysUser.AdminName == "dfma" {
  1682. list, err = models.GetResearcherEntry()
  1683. if err != nil {
  1684. br.Msg = "获取数据失败"
  1685. br.Msg = "获取数据失败,Err:" + err.Error()
  1686. return
  1687. }
  1688. }
  1689. resp := new(models.ResearcherListResp)
  1690. resp.List = list
  1691. br.Ret = 200
  1692. br.Success = true
  1693. br.Msg = "获取成功"
  1694. br.Data = resp
  1695. }
  1696. // GetTargetItems
  1697. // @Title 获取研究员指标
  1698. // @Description 获取研究员指标
  1699. // @Param Mobile query string true "手机号"
  1700. // @Param ClassifyId query int true "分类id"
  1701. // @Success 200 {object} models.ResearcherListResp
  1702. // @router /getTargetItems [get]
  1703. func (this *TargetController) GetTargetItems() {
  1704. br := new(models.BaseResponse).Init()
  1705. defer func() {
  1706. this.Data["json"] = br
  1707. this.ServeJSON()
  1708. }()
  1709. sysUser := this.SysUser
  1710. if sysUser == nil {
  1711. br.Msg = "请重新登录"
  1712. return
  1713. }
  1714. mobile := this.GetString("Mobile")
  1715. if mobile == "" {
  1716. mobile = sysUser.Mobile
  1717. }
  1718. classifyId, _ := this.GetInt("ClassifyId")
  1719. items, err := models.GetTargetItems(mobile, classifyId)
  1720. if err != nil {
  1721. br.ErrMsg = "获取失败,Err:" + err.Error()
  1722. br.Msg = "获取失败"
  1723. return
  1724. }
  1725. resp := new(models.TargetItemsResp)
  1726. resp.List = items
  1727. br.Ret = 200
  1728. br.Success = true
  1729. br.Msg = "获取成功"
  1730. br.Data = resp
  1731. }
  1732. // ExportLongzhong
  1733. // @Title 隆众数据下载
  1734. // @Description 隆众数据下载接口
  1735. // @Success 200 {object} models.ResearcherListResp
  1736. // @router /export/longzhong [get]
  1737. func (this *TargetCommonController) ExportLongzhong() {
  1738. br := new(models.BaseResponse).Init()
  1739. defer func() {
  1740. this.Data["json"] = br
  1741. this.ServeJSON()
  1742. }()
  1743. parentId := 4
  1744. classifyList, err := models.GetEdbdataClassifyByParentId(parentId)
  1745. if err != nil {
  1746. return
  1747. }
  1748. xlsxFile := xlsx.NewFile()
  1749. if err != nil {
  1750. fmt.Println("生成文件失败" + err.Error())
  1751. return
  1752. }
  1753. dir, _ := os.Executable()
  1754. exPath := filepath.Dir(dir)
  1755. downLoadnFilePath := exPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1756. for _, v := range classifyList {
  1757. fmt.Println(v.ClassifyId, v.ClassifyName)
  1758. //获取指标
  1759. secNameList, err := data_source.GetLongzhonginfoByClassifyId(v.ClassifyId)
  1760. if err != nil {
  1761. fmt.Println("获取数据失败,Err:" + err.Error())
  1762. return
  1763. }
  1764. if len(secNameList) <= 0 {
  1765. continue
  1766. }
  1767. sheetNew, err := xlsxFile.AddSheet(v.ClassifyName)
  1768. if err != nil {
  1769. fmt.Println("新增Sheet失败", err.Error())
  1770. return
  1771. }
  1772. //sheetNew.SetColWidth()
  1773. //获取指标数据
  1774. windRow := sheetNew.AddRow()
  1775. secNameRow := sheetNew.AddRow()
  1776. frequencyRow := sheetNew.AddRow()
  1777. unitRow := sheetNew.AddRow()
  1778. lastModifyDateRow := sheetNew.AddRow()
  1779. //获取分类下指标最大数据量
  1780. dataMax, err := data_source.GetLongzhongDataMaxCount(v.ClassifyId)
  1781. if err != nil {
  1782. fmt.Println("获取指标最大数据量失败", err.Error())
  1783. return
  1784. }
  1785. for k, sv := range secNameList {
  1786. fmt.Println("line 399:", v.ClassifyId, v.ClassifyName, sv.SecName, sv.Frequency)
  1787. windRow.AddCell().SetValue("Wind")
  1788. secNameRow.AddCell().SetValue("指标名称")
  1789. frequencyRow.AddCell().SetValue("频率")
  1790. unitRow.AddCell().SetValue("单位")
  1791. lastModifyDateRow.AddCell().SetValue("更新时间")
  1792. secNameRow.AddCell().SetValue(sv.SecName)
  1793. frequencyRow.AddCell().SetValue(sv.Frequency)
  1794. unitRow.AddCell().SetValue(sv.Unit)
  1795. lastModifyDateRow.AddCell().SetValue(sv.LastModifyDate)
  1796. windRow.AddCell()
  1797. windRow.AddCell()
  1798. secNameRow.AddCell()
  1799. frequencyRow.AddCell()
  1800. unitRow.AddCell()
  1801. lastModifyDateRow.AddCell()
  1802. min := k * 3
  1803. sheetNew.SetColWidth(min, min, 15)
  1804. //获取数据
  1805. dataList, err := data_source.GetLongzhongDataById(sv.LongzhonginfoId)
  1806. if err != nil {
  1807. br.Msg = "获取数据失败"
  1808. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1809. return
  1810. }
  1811. if len(dataList) <= 0 {
  1812. for n := 0; n < dataMax; n++ {
  1813. rowIndex := 5 + n
  1814. row := sheetNew.Row(rowIndex)
  1815. row.AddCell()
  1816. row.AddCell()
  1817. row.AddCell()
  1818. }
  1819. } else {
  1820. endRowIndex := 0
  1821. for rk, dv := range dataList {
  1822. rowIndex := 5 + rk
  1823. row := sheetNew.Row(rowIndex)
  1824. displayDate, _ := time.Parse(utils.FormatDate, dv.DisplayTime)
  1825. displayDateCell := row.AddCell()
  1826. style := new(xlsx.Style)
  1827. style.ApplyAlignment = true
  1828. style.Alignment.WrapText = true
  1829. displayDateCell.SetStyle(style)
  1830. displayDateCell.SetDate(displayDate)
  1831. row.AddCell().SetFloat(dv.Close)
  1832. row.AddCell()
  1833. endRowIndex = rowIndex
  1834. }
  1835. if len(dataList) < dataMax {
  1836. dataLen := dataMax - len(dataList)
  1837. for n := 0; n < dataLen; n++ {
  1838. rowIndex := (endRowIndex + 1) + n
  1839. row := sheetNew.Row(rowIndex)
  1840. row.AddCell()
  1841. row.AddCell()
  1842. row.AddCell()
  1843. }
  1844. }
  1845. }
  1846. }
  1847. }
  1848. err = xlsxFile.Save(downLoadnFilePath)
  1849. if err != nil {
  1850. br.Msg = "保存文件失败"
  1851. br.ErrMsg = "保存文件失败"
  1852. return
  1853. }
  1854. fileName := "隆众石化数据" + time.Now().Format("200601021504") + ".xlsx"
  1855. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  1856. defer func() {
  1857. os.Remove(downLoadnFilePath)
  1858. }()
  1859. br.Ret = 200
  1860. br.Success = true
  1861. br.Msg = "success"
  1862. }
  1863. // ExportLongzhongPrice
  1864. // @Title 隆众价格数据下载
  1865. // @Description 隆众价格数据下载接口
  1866. // @Success 200 {object} models.ResearcherListResp
  1867. // @router /export/longzhong/price [get]
  1868. func (this *TargetCommonController) ExportLongzhongPrice() {
  1869. br := new(models.BaseResponse).Init()
  1870. defer func() {
  1871. this.Data["json"] = br
  1872. this.ServeJSON()
  1873. }()
  1874. classifyList, err := models.GetLzPriceClassify()
  1875. if err != nil {
  1876. return
  1877. }
  1878. xlsxFile := xlsx.NewFile()
  1879. if err != nil {
  1880. fmt.Println("生成文件失败" + err.Error())
  1881. return
  1882. }
  1883. dir, _ := os.Executable()
  1884. exPath := filepath.Dir(dir)
  1885. downLoadnFilePath := exPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1886. for _, v := range classifyList {
  1887. //获取指标
  1888. secNameList, err := models.GetLongzhongpriceinfoByClassifyName(v.ProductName)
  1889. if err != nil {
  1890. fmt.Println("获取数据失败,Err:" + err.Error())
  1891. return
  1892. }
  1893. if len(secNameList) <= 0 {
  1894. continue
  1895. }
  1896. sheetNew, err := xlsxFile.AddSheet(v.ProductName)
  1897. if err != nil {
  1898. fmt.Println("新增Sheet失败", err.Error())
  1899. return
  1900. }
  1901. //获取指标数据
  1902. windRow := sheetNew.AddRow()
  1903. secNameRow := sheetNew.AddRow()
  1904. frequencyRow := sheetNew.AddRow()
  1905. unitRow := sheetNew.AddRow()
  1906. lastModifyDateRow := sheetNew.AddRow()
  1907. marketRow := sheetNew.AddRow()
  1908. manufactureRow := sheetNew.AddRow()
  1909. standardRow := sheetNew.AddRow() //规格
  1910. modelNameRow := sheetNew.AddRow() //规格型号
  1911. titleRow := sheetNew.AddRow()
  1912. //获取分类下指标最大数据量
  1913. dataMax, err := models.GetLongzhongPriceDataMaxCount(v.ProductName)
  1914. if err != nil {
  1915. fmt.Println("获取指标最大数据量失败", err.Error())
  1916. return
  1917. }
  1918. for k, sv := range secNameList {
  1919. fmt.Println("line 399:", v.ProductName, sv.InfoTypeRemark, sv.AreaName)
  1920. windRow.AddCell().SetString("Wind")
  1921. secNameRow.AddCell().SetValue("分类")
  1922. frequencyRow.AddCell().SetValue("价格类型")
  1923. unitRow.AddCell().SetValue("单位")
  1924. lastModifyDateRow.AddCell().SetValue("地区")
  1925. marketRow.AddCell().SetValue("市场名称")
  1926. manufactureRow.AddCell().SetValue("厂家名称")
  1927. standardRow.AddCell().SetValue("规格")
  1928. modelNameRow.AddCell().SetValue("规格型号")
  1929. secNameRow.AddCell().SetValue(sv.InfoTypeRemark)
  1930. frequencyRow.AddCell().SetValue(sv.PriceType)
  1931. unitRow.AddCell().SetValue(sv.Unit)
  1932. lastModifyDateRow.AddCell().SetValue(sv.AreaName)
  1933. marketRow.AddCell().SetValue(sv.MarketName)
  1934. manufactureRow.AddCell().SetValue(sv.ManufactureName)
  1935. standardRow.AddCell().SetValue(sv.Standard)
  1936. modelNameRow.AddCell().SetValue(sv.ModelName)
  1937. windRow.AddCell()
  1938. windRow.AddCell()
  1939. windRow.AddCell()
  1940. windRow.AddCell()
  1941. secNameRow.AddCell()
  1942. frequencyRow.AddCell()
  1943. unitRow.AddCell()
  1944. lastModifyDateRow.AddCell()
  1945. marketRow.AddCell()
  1946. manufactureRow.AddCell()
  1947. standardRow.AddCell()
  1948. modelNameRow.AddCell()
  1949. secNameRow.AddCell()
  1950. frequencyRow.AddCell()
  1951. unitRow.AddCell()
  1952. lastModifyDateRow.AddCell()
  1953. marketRow.AddCell()
  1954. manufactureRow.AddCell()
  1955. standardRow.AddCell()
  1956. modelNameRow.AddCell()
  1957. secNameRow.AddCell()
  1958. frequencyRow.AddCell()
  1959. unitRow.AddCell()
  1960. lastModifyDateRow.AddCell()
  1961. marketRow.AddCell()
  1962. manufactureRow.AddCell()
  1963. standardRow.AddCell()
  1964. modelNameRow.AddCell()
  1965. titleRow.AddCell().SetValue("价格日期")
  1966. titleRow.AddCell().SetValue("价格")
  1967. titleRow.AddCell().SetValue("低端价格")
  1968. titleRow.AddCell().SetValue("高端价格")
  1969. titleRow.AddCell()
  1970. min := k * 5
  1971. sheetNew.SetColWidth(min, min, 15)
  1972. //获取数据
  1973. dataList, err := models.GetLongzhongPriceDataById(sv.LongzhongpriceinfoId)
  1974. if err != nil {
  1975. fmt.Println("GetLongzhongDataById Err:" + err.Error())
  1976. return
  1977. }
  1978. rowStart := 10
  1979. if len(dataList) <= 0 {
  1980. for n := 0; n < dataMax; n++ {
  1981. rowIndex := rowStart + n
  1982. row := sheetNew.Row(rowIndex)
  1983. row.AddCell()
  1984. row.AddCell()
  1985. row.AddCell()
  1986. row.AddCell()
  1987. row.AddCell()
  1988. }
  1989. } else {
  1990. endRowIndex := 0
  1991. for rk, dv := range dataList {
  1992. rowIndex := rowStart + rk
  1993. row := sheetNew.Row(rowIndex)
  1994. priceDate, _ := time.Parse(utils.FormatDate, dv.PriceDate)
  1995. row.AddCell().SetDate(priceDate)
  1996. row.AddCell().SetFloat(dv.Price)
  1997. row.AddCell().SetFloat(dv.LowPrice)
  1998. row.AddCell().SetFloat(dv.HighPrice)
  1999. row.AddCell()
  2000. endRowIndex = rowIndex
  2001. }
  2002. if len(dataList) < dataMax {
  2003. dataLen := dataMax - len(dataList)
  2004. for n := 0; n < dataLen; n++ {
  2005. rowIndex := (endRowIndex + 1) + n
  2006. row := sheetNew.Row(rowIndex)
  2007. row.AddCell()
  2008. row.AddCell()
  2009. row.AddCell()
  2010. row.AddCell()
  2011. row.AddCell()
  2012. }
  2013. }
  2014. }
  2015. }
  2016. }
  2017. err = xlsxFile.Save(downLoadnFilePath)
  2018. if err != nil {
  2019. br.Msg = "保存文件失败"
  2020. br.ErrMsg = "保存文件失败"
  2021. return
  2022. }
  2023. fileName := "隆众价格数据" + time.Now().Format("200601021504") + ".xlsx"
  2024. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  2025. defer func() {
  2026. os.Remove(downLoadnFilePath)
  2027. }()
  2028. br.Ret = 200
  2029. br.Success = true
  2030. br.Msg = "success"
  2031. }
  2032. // ExportLongzhongSurvey
  2033. // @Title 隆众调研数据下载
  2034. // @Description 隆众调研数据下载接口
  2035. // @Success 200 {object} models.ResearcherListResp
  2036. // @router /export/longzhong/survey [get]
  2037. func (this *TargetCommonController) ExportLongzhongSurvey() {
  2038. br := new(models.BaseResponse).Init()
  2039. defer func() {
  2040. this.Data["json"] = br
  2041. this.ServeJSON()
  2042. }()
  2043. classifyList, err := models.GetLzSurveyClassify()
  2044. if err != nil {
  2045. return
  2046. }
  2047. xlsxFile := xlsx.NewFile()
  2048. if err != nil {
  2049. fmt.Println("生成文件失败" + err.Error())
  2050. return
  2051. }
  2052. dir, _ := os.Executable()
  2053. exPath := filepath.Dir(dir)
  2054. downLoadnFilePath := exPath + "/static/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  2055. for _, v := range classifyList {
  2056. //获取指标
  2057. secNameList, err := models.GetLongzhongSurveyProductByClassifyName(v.ProductName)
  2058. if err != nil {
  2059. fmt.Println("获取数据失败,Err:" + err.Error())
  2060. return
  2061. }
  2062. if len(secNameList) <= 0 {
  2063. continue
  2064. }
  2065. sheetNew, err := xlsxFile.AddSheet(v.ProductName)
  2066. if err != nil {
  2067. fmt.Println("新增Sheet失败", err.Error())
  2068. return
  2069. }
  2070. //sheetNew.SetColWidth()
  2071. //获取指标数据
  2072. windRow := sheetNew.AddRow()
  2073. secNameRow := sheetNew.AddRow()
  2074. indexCodeRow := sheetNew.AddRow()
  2075. frequencyRow := sheetNew.AddRow()
  2076. unitRow := sheetNew.AddRow()
  2077. lastModifyDateRow := sheetNew.AddRow()
  2078. //获取分类下指标最大数据量
  2079. dataMax, err := data_source.GetLongzhongSurveyDataMaxCount(v.ProductName)
  2080. if err != nil {
  2081. fmt.Println("获取指标最大数据量失败", err.Error())
  2082. return
  2083. }
  2084. setRowIndex := 6
  2085. for k, sv := range secNameList {
  2086. windRow.AddCell().SetValue("隆众")
  2087. secNameRow.AddCell().SetValue("指标名称")
  2088. indexCodeRow.AddCell().SetValue("指标ID")
  2089. frequencyRow.AddCell().SetValue("频率")
  2090. unitRow.AddCell().SetValue("单位")
  2091. lastModifyDateRow.AddCell().SetValue("更新时间")
  2092. secNameRow.AddCell().SetValue(sv.SampleName + sv.BreedName + sv.Custom + sv.QuotaName)
  2093. indexCodeRow.AddCell().SetValue(sv.LzCode)
  2094. if sv.Frequency == 1 {
  2095. frequencyRow.AddCell().SetValue("日度")
  2096. } else if sv.Frequency == 2 {
  2097. frequencyRow.AddCell().SetValue("周度")
  2098. } else if sv.Frequency == 3 {
  2099. frequencyRow.AddCell().SetValue("月度")
  2100. } else if sv.Frequency == 4 {
  2101. frequencyRow.AddCell().SetValue("季度")
  2102. } else if sv.Frequency == 5 {
  2103. frequencyRow.AddCell().SetValue("年度")
  2104. } else if sv.Frequency == 99 {
  2105. frequencyRow.AddCell().SetValue("无固定频率")
  2106. }
  2107. unitRow.AddCell().SetValue(sv.UnitName)
  2108. lastModifyDateRow.AddCell().SetValue(sv.EndDate)
  2109. windRow.AddCell()
  2110. windRow.AddCell()
  2111. secNameRow.AddCell()
  2112. indexCodeRow.AddCell()
  2113. frequencyRow.AddCell()
  2114. unitRow.AddCell()
  2115. lastModifyDateRow.AddCell()
  2116. min := k * 3
  2117. sheetNew.SetColWidth(min, min, 15)
  2118. //获取数据
  2119. dataList, err := data_source.GetLongzhongSurveyDataById(sv.SurveyProductId)
  2120. if err != nil {
  2121. br.Msg = "获取数据失败"
  2122. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  2123. return
  2124. }
  2125. if len(dataList) <= 0 {
  2126. for n := 0; n < dataMax; n++ {
  2127. rowIndex := setRowIndex + n
  2128. row := sheetNew.Row(rowIndex)
  2129. row.AddCell()
  2130. row.AddCell()
  2131. row.AddCell()
  2132. }
  2133. } else {
  2134. endRowIndex := 0
  2135. for rk, dv := range dataList {
  2136. rowIndex := setRowIndex + rk
  2137. row := sheetNew.Row(rowIndex)
  2138. displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
  2139. displayDateCell := row.AddCell()
  2140. style := new(xlsx.Style)
  2141. style.ApplyAlignment = true
  2142. style.Alignment.WrapText = true
  2143. displayDateCell.SetStyle(style)
  2144. displayDateCell.SetDate(displayDate)
  2145. row.AddCell().SetFloat(dv.InputValue)
  2146. row.AddCell()
  2147. endRowIndex = rowIndex
  2148. }
  2149. if len(dataList) < dataMax {
  2150. dataLen := dataMax - len(dataList)
  2151. for n := 0; n < dataLen; n++ {
  2152. rowIndex := (endRowIndex + 1) + n
  2153. row := sheetNew.Row(rowIndex)
  2154. row.AddCell()
  2155. row.AddCell()
  2156. row.AddCell()
  2157. }
  2158. }
  2159. }
  2160. }
  2161. }
  2162. err = xlsxFile.Save(downLoadnFilePath)
  2163. if err != nil {
  2164. br.Msg = "保存文件失败"
  2165. br.ErrMsg = "保存文件失败"
  2166. return
  2167. }
  2168. fileName := "隆众调研数据" + time.Now().Format("200601021504") + ".xlsx"
  2169. this.Ctx.Output.Download(downLoadnFilePath, fileName)
  2170. defer func() {
  2171. os.Remove(downLoadnFilePath)
  2172. }()
  2173. br.Ret = 200
  2174. br.Success = true
  2175. br.Msg = "success"
  2176. }
  2177. // TargetUnitList
  2178. // @Title 指标列表
  2179. // @Description 指标列表接口
  2180. // @Success 200 {object} []string
  2181. // @router /target/unit_list [get]
  2182. func (this *TargetController) TargetUnitList() {
  2183. br := new(models.BaseResponse).Init()
  2184. defer func() {
  2185. this.Data["json"] = br
  2186. this.ServeJSON()
  2187. }()
  2188. sysUser := this.SysUser
  2189. if sysUser == nil {
  2190. br.Msg = "请登录"
  2191. br.Ret = 408
  2192. return
  2193. }
  2194. list, err := models.GetEdbUnitList()
  2195. if err != nil {
  2196. br.Msg = "获取失败"
  2197. br.ErrMsg = "获取失败,Err:" + err.Error()
  2198. return
  2199. }
  2200. edbUnitList := make([]string, 0)
  2201. for _, unit := range list {
  2202. edbUnitList = append(edbUnitList, unit.Unit)
  2203. }
  2204. br.Ret = 200
  2205. br.Success = true
  2206. br.Msg = "获取成功"
  2207. br.Data = edbUnitList
  2208. }
  2209. // TargetFrequencyList
  2210. // @Title 指标频度列表
  2211. // @Description 指标频度列表接口
  2212. // @Param ClassifyId query int true "分类"
  2213. // @Success 200 {object} []string
  2214. // @router /target/frequency_list [get]
  2215. func (this *TargetController) TargetFrequencyList() {
  2216. br := new(models.BaseResponse).Init()
  2217. defer func() {
  2218. this.Data["json"] = br
  2219. this.ServeJSON()
  2220. }()
  2221. sysUser := this.SysUser
  2222. if sysUser == nil {
  2223. br.Msg = "请登录"
  2224. br.Ret = 408
  2225. return
  2226. }
  2227. classifyId, _ := this.GetInt("ClassifyId") //分类
  2228. if classifyId <= 0 {
  2229. br.Msg = "分类ID必传"
  2230. return
  2231. }
  2232. userId := sysUser.AdminId
  2233. //超管账号可以查看分类下的所有频度数据
  2234. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2235. userId = 0
  2236. }
  2237. list, err := models.GetEdbFrequencyList(classifyId, userId)
  2238. if err != nil {
  2239. br.Msg = "获取失败"
  2240. br.ErrMsg = "获取失败,Err:" + err.Error()
  2241. return
  2242. }
  2243. edbFrequencyList := make([]string, 0)
  2244. for _, unit := range list {
  2245. edbFrequencyList = append(edbFrequencyList, unit.Frequency)
  2246. }
  2247. edbFrequencyList = sortEdbFrequency(edbFrequencyList)
  2248. br.Ret = 200
  2249. br.Success = true
  2250. br.Msg = "获取成功"
  2251. br.Data = edbFrequencyList
  2252. }
  2253. func sortEdbFrequency(frequencyList []string) (newFrequencyList []string) {
  2254. var frequencyMap1 = map[string]int{
  2255. "日度": 1,
  2256. "周度": 2,
  2257. "旬度": 3,
  2258. "月度": 4,
  2259. "季度": 5,
  2260. "半年度": 6,
  2261. "年度": 7,
  2262. }
  2263. var frequencyMap2 = map[int]string{
  2264. 1: "日度",
  2265. 2: "周度",
  2266. 3: "旬度",
  2267. 4: "月度",
  2268. 5: "季度",
  2269. 6: "半年度",
  2270. 7: "年度",
  2271. }
  2272. idList := make([]int, 0)
  2273. for _, frequency := range frequencyList {
  2274. idList = append(idList, frequencyMap1[frequency])
  2275. }
  2276. sort.Ints(idList)
  2277. for _, id := range idList {
  2278. newFrequencyList = append(newFrequencyList, frequencyMap2[id])
  2279. }
  2280. return
  2281. }
  2282. // TargetItemList
  2283. // @Title 获取研究员指标
  2284. // @Description 获取研究员指标
  2285. // @Param Frequency query string false "频度;枚举值:日度、周度、旬度、月度、季度、半年度、年度"
  2286. // @Param Keyword query string false "关键字搜索"
  2287. // @Param TradeCode query string false "指标唯一编码"
  2288. // @Param ClassifyId query int false "分类id"
  2289. // @Param EdbShowType query string true "指标展示类型:0,1,2; 0:不管有没有数据,指标都展示出来, 1:只展示有数据的指标, 2:只展示没有数据的指标"
  2290. // @Success 200 {object} models.TargetItemListResp
  2291. // @router /target/item_list [get]
  2292. func (this *TargetController) TargetItemList() {
  2293. br := new(models.BaseResponse).Init()
  2294. defer func() {
  2295. this.Data["json"] = br
  2296. this.ServeJSON()
  2297. }()
  2298. sysUser := this.SysUser
  2299. if sysUser == nil {
  2300. br.Msg = "请重新登录"
  2301. return
  2302. }
  2303. //频度
  2304. frequency := this.GetString("Frequency")
  2305. //关键字
  2306. keyword := this.GetString("Keyword")
  2307. //指标编码
  2308. tradeCode := this.GetString("TradeCode")
  2309. userId := sysUser.AdminId
  2310. //超管账号可以查看分类下的所有频度数据
  2311. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2312. userId = 0
  2313. }
  2314. //获取账户所拥有权限的分类id集合
  2315. classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  2316. if err != nil {
  2317. br.Msg = "获取分类数据失败"
  2318. return
  2319. }
  2320. classifyId, _ := this.GetInt("ClassifyId")
  2321. edbShowType, _ := this.GetInt("EdbShowType")
  2322. list, err := models.GetTargetItemList(classifyId, edbShowType, frequency, keyword, tradeCode, classifyIdStrList)
  2323. if err != nil {
  2324. br.ErrMsg = "获取失败,Err:" + err.Error()
  2325. br.Msg = "获取失败"
  2326. return
  2327. }
  2328. edbTradeCodeList := make([]string, 0)
  2329. //频度数据
  2330. frequencyMap := make(map[string]int)
  2331. for _, edb := range list {
  2332. edbTradeCodeList = append(edbTradeCodeList, edb.TradeCode)
  2333. frequencyMap[edb.Frequency] = 0
  2334. }
  2335. frequencyList := make([]string, 0)
  2336. for k := range frequencyMap {
  2337. frequencyList = append(frequencyList, k)
  2338. }
  2339. frequencyList = sortEdbFrequency(frequencyList)
  2340. if len(edbTradeCodeList) > 0 {
  2341. edbTradeCodeStr := `"` + strings.Join(edbTradeCodeList, `","`) + `"`
  2342. edbDataList, err := models.GetEdbDataListByCodes(edbTradeCodeStr)
  2343. if err != nil {
  2344. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  2345. br.Msg = "获取失败"
  2346. return
  2347. }
  2348. edbDataMap := make(map[string][]*models.Edbdata, 0)
  2349. for _, edbData := range edbDataList {
  2350. if _, ok := edbDataMap[edbData.TradeCode]; ok {
  2351. edbDataMap[edbData.TradeCode] = append(edbDataMap[edbData.TradeCode], edbData)
  2352. } else {
  2353. tmpEdbDataList := make([]*models.Edbdata, 0)
  2354. tmpEdbDataList = append(tmpEdbDataList, edbData)
  2355. edbDataMap[edbData.TradeCode] = tmpEdbDataList
  2356. }
  2357. }
  2358. //nowWeek := time.Now().Weekday().String() //当前周几
  2359. myYear := time.Now().Year() //当前年份
  2360. myMonth := time.Now().Format("01") //当前月份
  2361. //当前周的周一与周日
  2362. nowWeekFirstDay := utils.GetNowWeekMonday()
  2363. nowWeekLastDay := utils.GetNowWeekLastDay()
  2364. //当前月的一号与最后一天
  2365. nowMonthFirstDay := utils.GetNowMonthFirstDay()
  2366. nowMonthLastDay := utils.GetNowMonthLastDay()
  2367. //当前季度的第一天与最后一天
  2368. nowQuarterFirstDay := utils.GetNowQuarterFirstDay()
  2369. nowQuarterLastDay := utils.GetNowQuarterLastDay()
  2370. //当前半年的第一天与最后一天
  2371. nowHalfYearFirstDay := utils.GetNowHalfYearFirstDay()
  2372. nowHalfYearLastDay := utils.GetNowHalfYearLastDay()
  2373. // 当前年的第一天与最后一天
  2374. nowYearFirstDay := utils.GetNowYearFirstDay()
  2375. nowYearLastDay := utils.GetNowYearLastDay()
  2376. for _, edb := range list {
  2377. edb.Status = "未完成"
  2378. if dataList, ok := edbDataMap[edb.TradeCode]; ok {
  2379. edb.DataList = dataList
  2380. dataDtTime, _ := time.ParseInLocation(utils.FormatDate, dataList[0].Dt, time.Now().Location()) //最近的一条数据时间
  2381. switch edb.Frequency {
  2382. case "日度":
  2383. if time.Now().After(dataDtTime.AddDate(0, 0, 1)) {
  2384. edb.ModifyDate = time.Now().Format(utils.FormatDate)
  2385. } else {
  2386. edb.ModifyDate = dataDtTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  2387. edb.Status = "完成"
  2388. }
  2389. case "周度":
  2390. //if edb.NoticeTime != "" {
  2391. // noticeArr := strings.Split(edb.NoticeTime, " ")
  2392. // noticeWeek := noticeArr[0]
  2393. // addDay := 0
  2394. //
  2395. // if nowWeek == "Sunday" {
  2396. // if noticeWeek == "周日" {
  2397. // addDay = 0
  2398. // } else if noticeWeek == "周一" {
  2399. // addDay = 1
  2400. // } else if noticeWeek == "周二" {
  2401. // addDay = 2
  2402. // } else if noticeWeek == "周三" {
  2403. // addDay = 3
  2404. // } else if noticeWeek == "周四" {
  2405. // addDay = 4
  2406. // } else if noticeWeek == "周五" {
  2407. // addDay = 5
  2408. // } else if noticeWeek == "周六" {
  2409. // addDay = 6
  2410. // } else {
  2411. // addDay = 0
  2412. // }
  2413. // } else if nowWeek == "Monday" {
  2414. // if noticeWeek == "周日" {
  2415. // addDay = 6
  2416. // } else if noticeWeek == "周一" {
  2417. // addDay = 0
  2418. // } else if noticeWeek == "周二" {
  2419. // addDay = 1
  2420. // } else if noticeWeek == "周三" {
  2421. // addDay = 2
  2422. // } else if noticeWeek == "周四" {
  2423. // addDay = 3
  2424. // } else if noticeWeek == "周五" {
  2425. // addDay = 4
  2426. // } else if noticeWeek == "周六" {
  2427. // addDay = 5
  2428. // } else {
  2429. // addDay = 0
  2430. // }
  2431. // } else if nowWeek == "Tuesday" {
  2432. // if noticeWeek == "周日" {
  2433. // addDay = 5
  2434. // } else if noticeWeek == "周一" {
  2435. // addDay = 6
  2436. // } else if noticeWeek == "周二" {
  2437. // addDay = 0
  2438. // } else if noticeWeek == "周三" {
  2439. // addDay = 1
  2440. // } else if noticeWeek == "周四" {
  2441. // addDay = 2
  2442. // } else if noticeWeek == "周五" {
  2443. // addDay = 3
  2444. // } else if noticeWeek == "周六" {
  2445. // addDay = 4
  2446. // } else {
  2447. // addDay = 0
  2448. // }
  2449. // } else if nowWeek == "Wednesday" {
  2450. // if noticeWeek == "周日" {
  2451. // addDay = 4
  2452. // } else if noticeWeek == "周一" {
  2453. // addDay = 5
  2454. // } else if noticeWeek == "周二" {
  2455. // addDay = 6
  2456. // } else if noticeWeek == "周三" {
  2457. // addDay = 0
  2458. // } else if noticeWeek == "周四" {
  2459. // addDay = 1
  2460. // } else if noticeWeek == "周五" {
  2461. // addDay = 2
  2462. // } else if noticeWeek == "周六" {
  2463. // addDay = 3
  2464. // } else {
  2465. // addDay = 0
  2466. // }
  2467. // } else if nowWeek == "Thursday" {
  2468. // if noticeWeek == "周日" {
  2469. // addDay = 3
  2470. // } else if noticeWeek == "周一" {
  2471. // addDay = 4
  2472. // } else if noticeWeek == "周二" {
  2473. // addDay = 5
  2474. // } else if noticeWeek == "周三" {
  2475. // addDay = 6
  2476. // } else if noticeWeek == "周四" {
  2477. // addDay = 0
  2478. // } else if noticeWeek == "周五" {
  2479. // addDay = 1
  2480. // } else if noticeWeek == "周六" {
  2481. // addDay = 2
  2482. // } else {
  2483. // addDay = 0
  2484. // }
  2485. // } else if nowWeek == "Friday" {
  2486. // if noticeWeek == "周日" {
  2487. // addDay = 2
  2488. // } else if noticeWeek == "周一" {
  2489. // addDay = 3
  2490. // } else if noticeWeek == "周二" {
  2491. // addDay = 4
  2492. // } else if noticeWeek == "周三" {
  2493. // addDay = 5
  2494. // } else if noticeWeek == "周四" {
  2495. // addDay = 6
  2496. // } else if noticeWeek == "周五" {
  2497. // addDay = 0
  2498. // } else if noticeWeek == "周六" {
  2499. // addDay = 1
  2500. // } else {
  2501. // addDay = 0
  2502. // }
  2503. // } else if nowWeek == "Saturday" {
  2504. // if noticeWeek == "周日" {
  2505. // addDay = 1
  2506. // } else if noticeWeek == "周一" {
  2507. // addDay = 2
  2508. // } else if noticeWeek == "周二" {
  2509. // addDay = 3
  2510. // } else if noticeWeek == "周三" {
  2511. // addDay = 4
  2512. // } else if noticeWeek == "周四" {
  2513. // addDay = 5
  2514. // } else if noticeWeek == "周五" {
  2515. // addDay = 6
  2516. // } else if noticeWeek == "周六" {
  2517. // addDay = 0
  2518. // } else {
  2519. // addDay = 0
  2520. // }
  2521. // }
  2522. //
  2523. // modifyDate := time.Now().AddDate(0, 0, addDay)
  2524. // if modifyDate.After(dataDtTime.AddDate(0, 0, 1)) {
  2525. // edb.ModifyDate = modifyDate.Format(utils.FormatDate)
  2526. // } else {
  2527. // edb.ModifyDate = modifyDate.AddDate(0, 0, 7).Format(utils.FormatDate)
  2528. // edb.Status = "完成"
  2529. // }
  2530. //} else {
  2531. // if startWeekDay.Unix() <= dataDtTime.Unix() {
  2532. // edb.ModifyDate = endWeekDay.AddDate(0, 0, 7).Format(utils.FormatDate)
  2533. // edb.Status = "完成"
  2534. // } else {
  2535. // edb.ModifyDate = endWeekDay.Format(utils.FormatDate)
  2536. // }
  2537. //}
  2538. modifyDate := nowWeekLastDay //下次更新日期
  2539. if edb.NoticeTime != "" {
  2540. addDay := 7
  2541. noticeArr := strings.Split(edb.NoticeTime, " ")
  2542. noticeWeek := noticeArr[0]
  2543. switch noticeWeek {
  2544. case "周一":
  2545. addDay = 1
  2546. case "周二":
  2547. addDay = 2
  2548. case "周三":
  2549. addDay = 3
  2550. case "周四":
  2551. addDay = 4
  2552. case "周五":
  2553. addDay = 5
  2554. case "周六":
  2555. addDay = 6
  2556. case "周日":
  2557. addDay = 7
  2558. }
  2559. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  2560. }
  2561. if nowWeekFirstDay.Before(dataDtTime) {
  2562. edb.ModifyDate = modifyDate.AddDate(0, 0, 7).Format(utils.FormatDate)
  2563. edb.Status = "完成"
  2564. } else {
  2565. edb.ModifyDate = modifyDate.Format(utils.FormatDate)
  2566. }
  2567. case "月度":
  2568. addDay := 0
  2569. modifyDate := nowMonthLastDay //下次更新日期
  2570. if edb.NoticeTime != "" {
  2571. strArr := strings.Split(edb.NoticeTime, "日")
  2572. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  2573. if tmpErr != nil {
  2574. continue
  2575. }
  2576. addDay = tmpAddDay - 1
  2577. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  2578. }
  2579. if nowMonthFirstDay.Before(dataDtTime) {
  2580. if int(time.Now().Month()) != int(nowMonthFirstDay.Month()) {
  2581. edb.ModifyDate = nowMonthLastDay.Format(utils.FormatDate)
  2582. } else {
  2583. month := nowMonthLastDay.Month() + 1
  2584. edb.ModifyDate = time.Date(nowMonthLastDay.Year(), month, 1, 0, 0, 0, 0, modifyDate.Location()).AddDate(0, 1, -1).Format(utils.FormatDate)
  2585. }
  2586. edb.Status = "完成"
  2587. } else {
  2588. edb.ModifyDate = modifyDate.Format(utils.FormatDate)
  2589. }
  2590. case "季度":
  2591. //每季度更新数据时间
  2592. if nowQuarterFirstDay.After(dataDtTime) {
  2593. edb.ModifyDate = nowQuarterLastDay.Format(utils.FormatDate)
  2594. } else {
  2595. edb.ModifyDate = nowQuarterLastDay.AddDate(0, 3, 0).Format(utils.FormatDate)
  2596. edb.Status = "完成"
  2597. }
  2598. case "半年度":
  2599. //每半年度更新数据时间
  2600. if nowHalfYearFirstDay.After(dataDtTime) {
  2601. edb.ModifyDate = nowHalfYearLastDay.Format(utils.FormatDate)
  2602. } else {
  2603. month := nowHalfYearLastDay.Month() + 1
  2604. edb.ModifyDate = time.Date(nowMonthLastDay.Year(), month, 1, 0, 0, 0, 0, nowHalfYearLastDay.Location()).AddDate(0, 6, -1).Format(utils.FormatDate)
  2605. edb.Status = "完成"
  2606. }
  2607. case "年度":
  2608. //每年度更新数据时间
  2609. if nowYearFirstDay.After(dataDtTime) {
  2610. edb.ModifyDate = nowYearLastDay.Format(utils.FormatDate)
  2611. } else {
  2612. edb.ModifyDate = nowYearLastDay.AddDate(1, 0, 0).Format(utils.FormatDate)
  2613. edb.Status = "完成"
  2614. }
  2615. }
  2616. } else {
  2617. edb.DataList = make([]*models.Edbdata, 0)
  2618. switch edb.Frequency {
  2619. case "日度":
  2620. edb.ModifyDate = time.Now().Format(utils.FormatDate)
  2621. case "周度":
  2622. modifyDate := nowWeekLastDay //下次更新日期
  2623. if edb.NoticeTime != "" {
  2624. addDay := 7
  2625. noticeArr := strings.Split(edb.NoticeTime, " ")
  2626. noticeWeek := noticeArr[0]
  2627. switch noticeWeek {
  2628. case "周一":
  2629. addDay = 1
  2630. case "周二":
  2631. addDay = 2
  2632. case "周三":
  2633. addDay = 3
  2634. case "周四":
  2635. addDay = 4
  2636. case "周五":
  2637. addDay = 5
  2638. case "周六":
  2639. addDay = 6
  2640. case "周日":
  2641. addDay = 7
  2642. }
  2643. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  2644. }
  2645. edb.ModifyDate = modifyDate.Format(utils.FormatDate)
  2646. case "月度":
  2647. if edb.NoticeTime != "" {
  2648. var modifyDateStr string
  2649. strArr := strings.Split(edb.NoticeTime, "日")
  2650. modifyDateStr = strconv.Itoa(myYear) + "-" + myMonth + "-" + strArr[0]
  2651. edb.ModifyDate = modifyDateStr
  2652. } else {
  2653. edb.ModifyDate = nowMonthFirstDay.AddDate(0, 1, -1).Format(utils.FormatDate)
  2654. }
  2655. case "季度":
  2656. edb.ModifyDate = utils.GetNowQuarterLastDay().Format(utils.FormatDate)
  2657. case "半年度":
  2658. edb.ModifyDate = utils.GetNowHalfYearLastDay().Format(utils.FormatDate)
  2659. case "年度":
  2660. edb.ModifyDate = utils.GetNowYearLastDay().Format(utils.FormatDate)
  2661. }
  2662. }
  2663. }
  2664. }
  2665. resp := models.TargetItemListResp{
  2666. List: list,
  2667. FrequencyList: frequencyList,
  2668. }
  2669. br.Ret = 200
  2670. br.Success = true
  2671. br.Msg = "获取成功"
  2672. //br.Data = resp
  2673. br.Data = resp
  2674. }
  2675. // TargetItemNameList
  2676. // @Title 获取研究员指标
  2677. // @Description 获取研究员指标
  2678. // @Param Frequency query string false "频度;枚举值:日度、周度、月度、季度、半年度、年度"
  2679. // @Param Keyword query string false "关键字搜索"
  2680. // @Param TradeCode query string false "指标唯一编码"
  2681. // @Param ClassifyId query int false "分类id"
  2682. // @Param EdbShowType query string true "指标展示类型:0,1,2; 0:不管有没有数据,指标都展示出来, 1:只展示有数据的指标, 2:只展示没有数据的指标"
  2683. // @Success 200 {object} models.TargetItemListResp
  2684. // @router /target/item_name_list [get]
  2685. func (this *TargetController) TargetItemNameList() {
  2686. br := new(models.BaseResponse).Init()
  2687. defer func() {
  2688. this.Data["json"] = br
  2689. this.ServeJSON()
  2690. }()
  2691. sysUser := this.SysUser
  2692. if sysUser == nil {
  2693. br.Msg = "请重新登录"
  2694. return
  2695. }
  2696. //频度
  2697. frequency := this.GetString("Frequency")
  2698. //关键字
  2699. keyword := this.GetString("Keyword")
  2700. //指标编码
  2701. tradeCode := this.GetString("TradeCode")
  2702. userId := sysUser.AdminId
  2703. //超管账号可以查看分类下的所有频度数据
  2704. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2705. userId = 0
  2706. }
  2707. //获取账户所拥有权限的分类id集合
  2708. classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  2709. if err != nil {
  2710. br.Msg = "获取分类数据失败"
  2711. return
  2712. }
  2713. classifyId, _ := this.GetInt("ClassifyId")
  2714. edbShowType, _ := this.GetInt("EdbShowType")
  2715. list, err := models.GetTargetItemList(classifyId, edbShowType, frequency, keyword, tradeCode, classifyIdStrList)
  2716. if err != nil {
  2717. br.ErrMsg = "获取失败,Err:" + err.Error()
  2718. br.Msg = "获取失败"
  2719. return
  2720. }
  2721. resp := models.TargetItemListResp{
  2722. List: list,
  2723. }
  2724. br.Ret = 200
  2725. br.Success = true
  2726. br.Msg = "获取成功"
  2727. //br.Data = resp
  2728. br.Data = resp
  2729. }
  2730. // BatchDataDeleteByDate
  2731. // @Title 根据日期批量删除指标的数据
  2732. // @Description 根据日期批量删除指标的数据接口
  2733. // @Param request body models.BatchDataDeleteReq true "type json string"
  2734. // @Success 200 Ret=200 删除数据成功
  2735. // @router /data/batch_delete_by_date [post]
  2736. func (this *TargetController) BatchDataDeleteByDate() {
  2737. br := new(models.BaseResponse).Init()
  2738. defer func() {
  2739. this.Data["json"] = br
  2740. this.ServeJSON()
  2741. }()
  2742. var req models.BatchDataDeleteReq
  2743. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2744. if err != nil {
  2745. br.Msg = "参数解析异常!"
  2746. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2747. return
  2748. }
  2749. if len(req.TradeCodeList) <= 0 {
  2750. br.Msg = "至少传入一个指标编码!"
  2751. return
  2752. }
  2753. if req.CreateDate == "" {
  2754. br.Msg = "请选择日期"
  2755. return
  2756. }
  2757. TradeCodeStr := `"` + strings.Join(req.TradeCodeList, `","`) + `"`
  2758. //删除数据
  2759. err = models.BatchDeleteEdbDataByDate(TradeCodeStr, req.CreateDate, this.SysUser.AdminId)
  2760. if err != nil {
  2761. br.Msg = "删除失败!"
  2762. br.ErrMsg = "删除失败,Err:" + err.Error()
  2763. return
  2764. }
  2765. br.Ret = 200
  2766. br.Success = true
  2767. br.Msg = "删除成功"
  2768. }
  2769. // BatchDataDeleteByTradeCode
  2770. // @Title 批量删除指标的数据
  2771. // @Description 批量删除指标的数据接口
  2772. // @Param request body models.TargetDeleteReq true "type json string"
  2773. // @Success 200 Ret=200 删除数据成功
  2774. // @router /data/batch_delete [post]
  2775. func (this *TargetController) BatchDataDeleteByTradeCode() {
  2776. br := new(models.BaseResponse).Init()
  2777. defer func() {
  2778. this.Data["json"] = br
  2779. this.ServeJSON()
  2780. }()
  2781. var req models.TargetDeleteReq
  2782. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2783. if err != nil {
  2784. br.Msg = "参数解析异常!"
  2785. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2786. return
  2787. }
  2788. if req.TradeCode == "" {
  2789. br.Msg = "指标编码不可为空"
  2790. return
  2791. }
  2792. //删除数据
  2793. err = models.BatchDeleteEdbData(req.TradeCode, this.SysUser.AdminId)
  2794. if err != nil {
  2795. br.Msg = "删除失败!"
  2796. br.ErrMsg = "删除失败,Err:" + err.Error()
  2797. return
  2798. }
  2799. br.Ret = 200
  2800. br.Success = true
  2801. br.Msg = "删除成功"
  2802. }
  2803. // EdbTemplateDownload
  2804. // @Title 下载指标导入模板
  2805. // @Description 下载指标导入模板
  2806. // @router /target/template [get]
  2807. func (this *TargetCommonController) EdbTemplateDownload() {
  2808. br := new(models.BaseResponse).Init()
  2809. defer func() {
  2810. this.Data["json"] = br
  2811. this.ServeJSON()
  2812. }()
  2813. this.Ctx.Output.Download("./static/指标导入模板.xlsx", "指标导入模板.xlsx")
  2814. br.Ret = 200
  2815. br.Success = true
  2816. br.Msg = "下载成功"
  2817. }
  2818. // ImportTarget
  2819. // @Title 导入指标
  2820. // @Description 导入指标接口
  2821. // @Success 200 Ret=200 导入成功
  2822. // @router /target/import [post]
  2823. func (this *TargetController) ImportTarget() {
  2824. br := new(models.BaseResponse).Init()
  2825. defer func() {
  2826. this.Data["json"] = br
  2827. this.ServeJSON()
  2828. }()
  2829. sysUser := this.SysUser
  2830. if sysUser == nil {
  2831. br.Msg = "请重新登录"
  2832. return
  2833. }
  2834. file, _, err := this.GetFile("ImportTargetFile")
  2835. if err != nil {
  2836. br.Msg = "获取文件失败"
  2837. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  2838. return
  2839. }
  2840. path := "./static/手工指标数据导入_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  2841. defer func() {
  2842. file.Close()
  2843. os.Remove(path)
  2844. }()
  2845. err = this.SaveToFile("ImportTargetFile", path)
  2846. if err != nil {
  2847. br.Msg = "文件保存失败"
  2848. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  2849. return
  2850. }
  2851. xlFile, err := xlsx.OpenFile(path)
  2852. if err != nil {
  2853. fmt.Println(err.Error())
  2854. br.Msg = "excel打开失败"
  2855. br.ErrMsg = "excel打开失败,Err:" + err.Error()
  2856. return
  2857. }
  2858. //分类map
  2859. classifyMap := make(map[string]*models.EdbdataClassify)
  2860. //获取当前账号拥有的所有分类权限
  2861. userId := sysUser.AdminId
  2862. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2863. userId = 0
  2864. }
  2865. classifyList, err := models.GetEdbdataClassify(int64(userId))
  2866. for _, classify := range classifyList {
  2867. if classify.Child != nil {
  2868. for _, child := range classify.Child {
  2869. classifyMap[child.ClassifyName] = child
  2870. }
  2871. }
  2872. }
  2873. failDatas := make([]*models.EdbImportFail, 0)
  2874. // 遍历sheet页读取
  2875. for _, sheet := range xlFile.Sheets {
  2876. fmt.Println("sheet name: ", sheet.Name)
  2877. //遍历行读取
  2878. maxRow := sheet.MaxRow
  2879. fmt.Println("maxRow:", maxRow)
  2880. fmt.Println("maxRow")
  2881. for i := 0; i < maxRow; i++ {
  2882. if i > 1 {
  2883. row := sheet.Row(i)
  2884. cells := row.Cells
  2885. if len(cells) >= 4 {
  2886. cell1 := cells[0].Value //指标名称
  2887. secName := strings.Trim(cell1, " ")
  2888. if secName == "" { //过滤空白行
  2889. continue
  2890. }
  2891. cell2 := cells[1].Value
  2892. frequency := strings.Trim(cell2, " ") //频度
  2893. classifyName := cells[2].Value //分类
  2894. classifyName = strings.Trim(classifyName, " ")
  2895. unit := cells[3].Value //单位
  2896. unit = strings.Trim(unit, " ")
  2897. //获取分类
  2898. classify, ok := classifyMap[classifyName]
  2899. if !ok {
  2900. failItem := &models.EdbImportFail{
  2901. ClassifyName: classifyName,
  2902. Frequency: frequency,
  2903. SecName: secName,
  2904. Unit: unit,
  2905. Remark: "分类不存在",
  2906. SysUserId: strconv.Itoa(sysUser.AdminId),
  2907. }
  2908. failDatas = append(failDatas, failItem)
  2909. continue
  2910. }
  2911. tradeCode, err := models.GetMaxTradeCode()
  2912. if err != nil {
  2913. failItem := &models.EdbImportFail{
  2914. ClassifyName: classifyName,
  2915. Frequency: frequency,
  2916. SecName: secName,
  2917. Unit: unit,
  2918. Remark: "获取数据失败",
  2919. SysUserId: strconv.Itoa(sysUser.AdminId),
  2920. }
  2921. failDatas = append(failDatas, failItem)
  2922. continue
  2923. //br.Msg = "获取数据失败"
  2924. //br.ErrMsg = "获取最大交易编码失败,Err:" + err.Error()
  2925. //return
  2926. }
  2927. if tradeCode == "" {
  2928. failItem := &models.EdbImportFail{
  2929. ClassifyName: classifyName,
  2930. Frequency: frequency,
  2931. SecName: secName,
  2932. Unit: unit,
  2933. Remark: "数据异常",
  2934. SysUserId: strconv.Itoa(sysUser.AdminId),
  2935. }
  2936. failDatas = append(failDatas, failItem)
  2937. continue
  2938. }
  2939. maxTradeCode, err := utils.GetMaxTradeCode(tradeCode)
  2940. if err != nil {
  2941. failItem := &models.EdbImportFail{
  2942. ClassifyName: classifyName,
  2943. Frequency: frequency,
  2944. SecName: secName,
  2945. Unit: unit,
  2946. Remark: "数据异常",
  2947. SysUserId: strconv.Itoa(sysUser.AdminId),
  2948. }
  2949. failDatas = append(failDatas, failItem)
  2950. continue
  2951. //br.Msg = "数据异常"
  2952. //br.ErrMsg = "获取编码信息失败,Err:" + err.Error()
  2953. //return
  2954. }
  2955. if maxTradeCode == "" {
  2956. failItem := &models.EdbImportFail{
  2957. ClassifyName: classifyName,
  2958. Frequency: frequency,
  2959. SecName: secName,
  2960. Unit: unit,
  2961. Remark: "编码为空,请联系技术",
  2962. SysUserId: strconv.Itoa(sysUser.AdminId),
  2963. }
  2964. failDatas = append(failDatas, failItem)
  2965. continue
  2966. //br.Msg = "编码为空,请联系技术"
  2967. //br.ErrMsg = "编码为空,请联系技术"
  2968. //return
  2969. }
  2970. if secName == "" {
  2971. failItem := &models.EdbImportFail{
  2972. ClassifyName: classifyName,
  2973. Frequency: frequency,
  2974. SecName: secName,
  2975. Unit: unit,
  2976. Remark: "指标名称不能为空",
  2977. SysUserId: strconv.Itoa(sysUser.AdminId),
  2978. }
  2979. failDatas = append(failDatas, failItem)
  2980. continue
  2981. //br.Msg = "指标名称不能为空"
  2982. //return
  2983. }
  2984. item, err := models.GetEdbinfoBySecName(secName)
  2985. if err != nil && err.Error() != utils.ErrNoRow() {
  2986. failItem := &models.EdbImportFail{
  2987. ClassifyName: classifyName,
  2988. Frequency: frequency,
  2989. SecName: secName,
  2990. Unit: unit,
  2991. Remark: "获取信息失败",
  2992. SysUserId: strconv.Itoa(sysUser.AdminId),
  2993. }
  2994. failDatas = append(failDatas, failItem)
  2995. continue
  2996. //br.Msg = "获取信息失败!"
  2997. //br.ErrMsg = "获取信息失败,Err:" + err.Error()
  2998. //return
  2999. }
  3000. //存在该指标,那么退出当前循环,进入下一循环
  3001. if item != nil {
  3002. failItem := &models.EdbImportFail{
  3003. ClassifyName: classifyName,
  3004. Frequency: frequency,
  3005. SecName: secName,
  3006. Unit: unit,
  3007. Remark: "指标已存在",
  3008. SysUserId: strconv.Itoa(sysUser.AdminId),
  3009. }
  3010. failDatas = append(failDatas, failItem)
  3011. continue
  3012. }
  3013. err = models.AddEdbinfo(maxTradeCode, secName, unit, "手动", frequency, "", classify.ClassifyId, sysUser.AdminId)
  3014. if err != nil {
  3015. failItem := &models.EdbImportFail{
  3016. ClassifyName: classifyName,
  3017. Frequency: frequency,
  3018. SecName: secName,
  3019. Unit: unit,
  3020. Remark: "导入失败",
  3021. SysUserId: strconv.Itoa(sysUser.AdminId),
  3022. }
  3023. failDatas = append(failDatas, failItem)
  3024. continue
  3025. //br.Msg = "导入失败"
  3026. //br.ErrMsg = "导入失败,Err:" + err.Error()
  3027. //return
  3028. }
  3029. err = models.AddEdbinfoUser(maxTradeCode, sysUser.Mobile)
  3030. if err != nil {
  3031. failItem := &models.EdbImportFail{
  3032. ClassifyName: classifyName,
  3033. Frequency: frequency,
  3034. SecName: secName,
  3035. Unit: unit,
  3036. Remark: "创建账号关系失败",
  3037. SysUserId: strconv.Itoa(sysUser.AdminId),
  3038. }
  3039. failDatas = append(failDatas, failItem)
  3040. continue
  3041. //br.Msg = "导入失败"
  3042. //br.ErrMsg = "导入失败,Err:" + err.Error()
  3043. //return
  3044. }
  3045. }
  3046. }
  3047. }
  3048. }
  3049. resp := new(models.EdbdataImportResp)
  3050. if len(failDatas) > 0 {
  3051. //先删除导入失败记录
  3052. _ = models.DelEdbImportFail(sysUser.AdminId)
  3053. //错误信息字符串切片,最后作为发送邮件通知使用
  3054. failContents := make([]string, 0)
  3055. for _, v := range failDatas {
  3056. failContents = append(failContents, fmt.Sprint(v.SecName, "导入失败:", v.Remark))
  3057. err = models.AddEdbImportFail(v)
  3058. if err != nil {
  3059. go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
  3060. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入指标 新增失败记录失败:Err:"+err.Error(), utils.EmailSendToUsers)
  3061. }
  3062. }
  3063. //导入失败的话,最后统一邮件提醒就好啦,不需要每次都去提醒
  3064. go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败,Err:"+err.Error(), 3)
  3065. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入指标 存在部分数据导入失败:"+strings.Join(failContents, ";"), utils.EmailSendToUsers)
  3066. resp.Status = 1
  3067. resp.Msg = "存在部分导入失败"
  3068. } else {
  3069. resp.Status = 0
  3070. }
  3071. br.Ret = 200
  3072. br.Success = true
  3073. br.Msg = "导入成功"
  3074. br.Data = resp
  3075. }
  3076. // ImportTargetFailListDownload
  3077. // @Title 下载指标数据导入失败列表
  3078. // @Description 下载指标数据导入失败列表
  3079. // @router /import_target/failList [get]
  3080. func (this *TargetController) ImportTargetFailListDownload() {
  3081. br := new(models.BaseResponse).Init()
  3082. defer func() {
  3083. this.Data["json"] = br
  3084. this.ServeJSON()
  3085. }()
  3086. sysUser := this.SysUser
  3087. if sysUser == nil {
  3088. br.Msg = "请重新登录"
  3089. return
  3090. }
  3091. item, err := models.GetEdbImportFailList(sysUser.AdminId)
  3092. if err != nil {
  3093. br.Msg = "获取数据失败"
  3094. br.Msg = "获取数据失败,Err:" + err.Error()
  3095. return
  3096. }
  3097. xlsxFile := xlsx.NewFile()
  3098. sheet, err := xlsxFile.AddSheet("导入失败数据")
  3099. headRow := sheet.AddRow()
  3100. headRow.AddCell().SetValue("指标名称")
  3101. headRow.AddCell().SetValue("频度")
  3102. headRow.AddCell().SetValue("品种分类")
  3103. headRow.AddCell().SetValue("单位")
  3104. headRow.AddCell().SetValue("备注")
  3105. for _, v := range item {
  3106. row := sheet.AddRow()
  3107. row.AddCell().SetValue(v.SecName)
  3108. row.AddCell().SetValue(v.Frequency)
  3109. row.AddCell().SetValue(v.ClassifyName)
  3110. row.AddCell().SetValue(v.Unit)
  3111. row.AddCell().SetValue(v.Remark)
  3112. }
  3113. fileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  3114. savePath := "./static/" + fileName
  3115. err = xlsxFile.Save(savePath)
  3116. if err != nil {
  3117. br.Msg = "文件保存失败"
  3118. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  3119. return
  3120. }
  3121. defer func() {
  3122. os.Remove(savePath)
  3123. }()
  3124. this.Ctx.Output.Download(savePath, "失败列表.xlsx")
  3125. }
  3126. // ExcelDataAdd
  3127. // @Title 复制EXCEL数据保存
  3128. // @Description 复制EXCEL数据保存
  3129. // @Param request body models.ExcelStyleReq true "type json string"
  3130. // @Success 200 {object}
  3131. // @router /target/excel_style/add [post]
  3132. func (this *TargetController) ExcelDataAdd() {
  3133. br := new(models.BaseResponse).Init()
  3134. var err error
  3135. errs := make([]string, 0)
  3136. defer func() {
  3137. if len(errs) > 0 {
  3138. go alarm_msg.SendAlarmMsg("新增复制EXCEL数据 新增或修改数据失败,Err:"+strings.Join(errs, ","), 3)
  3139. }
  3140. this.Data["json"] = br
  3141. this.ServeJSON()
  3142. }()
  3143. sysUser := this.SysUser
  3144. if sysUser == nil {
  3145. br.Msg = "请重新登录"
  3146. return
  3147. }
  3148. //频度
  3149. //frequency := this.GetString("Frequency")
  3150. //
  3151. //关键字
  3152. //keyword := this.GetString("Keyword")
  3153. //指标编码
  3154. //tradeCode := this.GetString("TradeCode")
  3155. //
  3156. //userId := sysUser.AdminId
  3157. //超管账号可以查看分类下的所有频度数据
  3158. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  3159. // userId = 0
  3160. //}
  3161. //
  3162. //list, err := models.GetExcelData()
  3163. //if err != nil {
  3164. // br.ErrMsg = "获取失败,Err:" + err.Error()
  3165. // br.Msg = "获取失败"
  3166. // return
  3167. //}
  3168. var req data_manage.ExcelStyleReq
  3169. err = json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  3170. if err != nil {
  3171. br.Msg = "参数解析异常!"
  3172. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3173. return
  3174. }
  3175. //指标map
  3176. targetMap := make(map[string]*models.Edbinfo)
  3177. var cellDataList []map[string]interface{}
  3178. var dataList [][]map[string]interface{}
  3179. //var configList []map[string]interface{}
  3180. for _, item := range req.CellData {
  3181. v := item.(map[string]interface{})
  3182. cellDataList = append(cellDataList, v)
  3183. }
  3184. valueIndex := 0
  3185. var secNameList []string
  3186. var frequencyList []string
  3187. var unitList []string
  3188. var dateList []string
  3189. dateValueMap := make(map[string]map[int]string)
  3190. //取到所有数据
  3191. for i := range req.Data {
  3192. //取到一行
  3193. if i > 4 {
  3194. //先拿下日期列表
  3195. if req.Data[i] == nil || req.Data[i][0] == nil {
  3196. continue
  3197. }
  3198. //fmt.Println("i:", i)
  3199. //fmt.Println(req.Data[i])
  3200. //fmt.Println(req.Data[i][0])
  3201. dateMap := req.Data[i][0].(map[string]interface{})
  3202. _, ok := dateMap["m"]
  3203. if ok {
  3204. dateList = append(dateList, dateMap["m"].(string))
  3205. //fmt.Println("dateList:", dateList)
  3206. } else {
  3207. continue
  3208. }
  3209. }
  3210. for j, dataArr := range req.Data[i] {
  3211. //每一列
  3212. if dataArr == nil {
  3213. continue
  3214. }
  3215. dataMap := dataArr.(map[string]interface{})
  3216. if dataList == nil {
  3217. dataList = make([][]map[string]interface{}, len(req.Data))
  3218. }
  3219. if dataList[i] == nil {
  3220. dataList[i] = make([]map[string]interface{}, len(req.Data[i]))
  3221. }
  3222. dataList[i][j] = dataMap
  3223. //fmt.Println("dataMap", dataMap["m"])
  3224. v, _ := dataMap["m"]
  3225. //指标名称列表
  3226. if v == "指标名称" {
  3227. for nameIndex := 1; nameIndex < len(req.Data[i]); nameIndex++ {
  3228. if req.Data[i][nameIndex] == nil {
  3229. break
  3230. }
  3231. secNameMap := req.Data[i][nameIndex].(map[string]interface{})
  3232. secName, _ := secNameMap["m"]
  3233. if secName == nil {
  3234. continue
  3235. }
  3236. secNameList = append(secNameList, secNameMap["m"].(string))
  3237. //fmt.Println("secName:", secNameList)
  3238. }
  3239. }
  3240. //频度列表
  3241. if v == "频度" {
  3242. for nameIndex := 1; nameIndex < len(req.Data[i]); nameIndex++ {
  3243. if req.Data[i][nameIndex] == nil {
  3244. break
  3245. }
  3246. frequencyMap := req.Data[i][nameIndex].(map[string]interface{})
  3247. frequency, _ := frequencyMap["m"]
  3248. if frequency == nil {
  3249. continue
  3250. }
  3251. frequencyList = append(frequencyList, frequencyMap["m"].(string))
  3252. //fmt.Println("frequencyList:", frequencyList)
  3253. }
  3254. }
  3255. //单位列表
  3256. if v == "单位" {
  3257. for nameIndex := 1; nameIndex < len(req.Data[i]); nameIndex++ {
  3258. if req.Data[i][nameIndex] == nil {
  3259. break
  3260. }
  3261. unitMap := req.Data[i][nameIndex].(map[string]interface{})
  3262. unit, _ := unitMap["m"]
  3263. if unit == nil {
  3264. continue
  3265. }
  3266. unitList = append(unitList, unitMap["m"].(string))
  3267. //fmt.Println("unitList:", unitList)
  3268. }
  3269. }
  3270. //数据列表
  3271. //var valueList []string
  3272. valueListMap := make(map[int]string)
  3273. if i > 4 {
  3274. for index := 1; index < len(req.Data[i]); index++ {
  3275. if req.Data[i][index] == nil {
  3276. break
  3277. }
  3278. valueMap := req.Data[i][index].(map[string]interface{})
  3279. value, _ := valueMap["m"]
  3280. if value == nil {
  3281. continue
  3282. }
  3283. valueListMap[index] = valueMap["m"].(string)
  3284. // 09-27 千位分隔符时用 "m" 取字符串存数据库会把逗号当小数点,现在换用 "v" 直接取数字再转为字符串,看看会不会有问题
  3285. if ct, ok := valueMap["ct"].(map[string]interface{}); ok{
  3286. fa, _ := ct["fa"]
  3287. if fa == "#,##0.000" {
  3288. value = valueMap["v"]
  3289. floatValue := valueMap["v"].(float64)
  3290. valueListMap[index] = strconv.FormatFloat(floatValue, 'f', -1, 64)
  3291. }
  3292. }
  3293. //valueList = append(valueList, valueMap["m"].(string))
  3294. //fmt.Println("valueList:", valueList)
  3295. }
  3296. dateMap := req.Data[i][0].(map[string]interface{})
  3297. date := dateMap["m"].(string)
  3298. dateValueMap[date] = valueListMap
  3299. }
  3300. }
  3301. //firstCell := req.Data[i][0]
  3302. //if firstCell == nil {
  3303. // continue
  3304. //}
  3305. //dataMap := firstCell.(map[string]interface{})
  3306. //if dataList == nil {
  3307. // dataList = make([][]map[string]interface{}, len(req.Data[i]))
  3308. //}
  3309. //if dataList[i] == nil {
  3310. // dataList[i] = make([]map[string]interface{}, len(req.Data[i]))
  3311. //}
  3312. //dataList[i][0] = dataMap
  3313. //fmt.Println("dataMap", dataMap["m"])
  3314. //v, _ := dataMap["m"]
  3315. ////指标名称
  3316. //if v == "指标名称" {
  3317. // secNameMap := req.Data[i][1].(map[string]interface{})
  3318. // secName = secNameMap["m"].(string)
  3319. // fmt.Println("secName:", secName)
  3320. //}
  3321. ////频度
  3322. //if v == "频度" {
  3323. // frequencyMap := req.Data[i][1].(map[string]interface{})
  3324. // frequency = frequencyMap["m"].(string)
  3325. // fmt.Println("frequency:", frequency)
  3326. //}
  3327. ////单位
  3328. //if v == "单位" {
  3329. // unitMap := req.Data[i][1].(map[string]interface{})
  3330. // unit = unitMap["m"].(string)
  3331. // fmt.Println("unit:", unit)
  3332. //}
  3333. //
  3334. ////日期
  3335. //dateMap := req.Data[i][1].(map[string]interface{})
  3336. //createDate = dateMap["m"].(string)
  3337. //
  3338. ////值
  3339. //valueMap := req.Data[i][valueIndex].(map[string]interface{})
  3340. //closeVal = valueMap["m"].(string)
  3341. //新增指标
  3342. //errMsg := services.AddEdbItem(secName, unit, frequency, req.ClassifyId, sysUser)
  3343. //if errMsg == "" {
  3344. // br.Msg = "新增指标异常!"
  3345. // br.ErrMsg = errMsg
  3346. // return
  3347. //}
  3348. valueIndex++
  3349. }
  3350. //var excelId int64
  3351. //保存EXCEL样式
  3352. //{
  3353. // cellData, err := json.Marshal(cellDataList)
  3354. // if err != nil {
  3355. // br.Msg = "参数解析异常!"
  3356. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3357. // return
  3358. // }
  3359. // config, err := json.Marshal(req.Config)
  3360. // if err != nil {
  3361. // br.Msg = "参数解析异常!"
  3362. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3363. // return
  3364. // }
  3365. // datas, err := json.Marshal(dataList)
  3366. // if err != nil {
  3367. // br.Msg = "参数解析异常!"
  3368. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3369. // return
  3370. // }
  3371. // excelItem := data_manage.ExcelStyle{
  3372. // CellData: string(cellData),
  3373. // Config: string(config),
  3374. // Data: string(datas),
  3375. // }
  3376. // excelId, err = models.AddExcelData(&excelItem)
  3377. // if err != nil {
  3378. // br.ErrMsg = "获取失败,Err:" + err.Error()
  3379. // br.Msg = "获取失败"
  3380. // return
  3381. // }
  3382. //
  3383. // //判断映射表是否已经存在
  3384. // count, err := data_manage.GetExcelEdbdataMappingCount(req.ClassifyId, target.TradeCode, frequency)
  3385. // if err != nil {
  3386. // br.ErrMsg = "GetExcelEdbdataMappingCount,Err:" + err.Error()
  3387. // br.Msg = "新增映射表失败"
  3388. // return
  3389. // }
  3390. // mappingItem := data_manage.ExcelEdbdataMapping{
  3391. // ExcelId: excelId,
  3392. // ClassifyId: req.ClassifyId,
  3393. // Frequency: frequency,
  3394. // TradeCode: target.TradeCode,
  3395. // CreateTime: time.Now(),
  3396. // }
  3397. // if count == 0{
  3398. // //新增
  3399. // err := data_manage.AddExcelEdbdataMapping(&mappingItem)
  3400. // if err != nil {
  3401. // br.ErrMsg = "AddExcelEdbdataMapping,Err:" + err.Error()
  3402. // br.Msg = "新增映射表失败"
  3403. // return
  3404. // }
  3405. // }
  3406. //}
  3407. // 所有的指标数据map
  3408. edbCodeDataMap := make(map[string]map[string]string)
  3409. //操作指标,新增指标及数据等
  3410. {
  3411. for i := 0; i < len(secNameList); i++ {
  3412. //fmt.Println("len(secNameList):", len(secNameList))
  3413. secName := secNameList[i]
  3414. frequency := frequencyList[i]
  3415. //防止填了指标名称后没填单位报错
  3416. if len(unitList) < len(secNameList) {
  3417. br.Msg = "单位填写异常!"
  3418. br.ErrMsg = "单位填写异常!"
  3419. return
  3420. }
  3421. unit := unitList[i]
  3422. for j, s := range dateList {
  3423. var closeVal string
  3424. //valueList, ok := dateValueMap[s]
  3425. valueMap, ok := dateValueMap[s]
  3426. //fmt.Println("valueMap:", valueMap)
  3427. if ok {
  3428. createDate := dateList[j]
  3429. //if i < len(valueList) {
  3430. // closeVal = valueList[i]
  3431. //} else {
  3432. // continue
  3433. //}
  3434. closeVal, ok = valueMap[i+1]
  3435. target, ok2 := targetMap[secName]
  3436. if !ok2 {
  3437. tmpTarget, err := models.GetTargetBySecName(secName)
  3438. if err != nil {
  3439. //如果是找不到该指标,那么新增指标
  3440. if err.Error() == utils.ErrNoRow() {
  3441. //if frequency == "" {
  3442. // failItem := new(models.EdbdataImportFail)
  3443. // failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  3444. // failItem.ClassifyName = classifyName
  3445. // failItem.CreateDate = createDate
  3446. // failItem.SecName = secName
  3447. // failItem.Close = closeVal
  3448. // failItem.Remark = "新增指标失败,频度字段为空"
  3449. // failItem.Frequency = frequency
  3450. // failItem.Unit = unit
  3451. // failDatas = append(failDatas, failItem)
  3452. // continue
  3453. //}
  3454. //if unit == "" {
  3455. // failItem := new(models.EdbdataImportFail)
  3456. // failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  3457. // failItem.ClassifyName = classifyName
  3458. // failItem.CreateDate = createDate
  3459. // failItem.SecName = secName
  3460. // failItem.Close = closeVal
  3461. // failItem.Remark = "新增指标失败,单位字段为空"
  3462. // failItem.Frequency = frequency
  3463. // failItem.Unit = unit
  3464. // failDatas = append(failDatas, failItem)
  3465. // continue
  3466. //}
  3467. tmpErr := data.AddEdbInfo(secName, unit, frequency, "", sysUser.Mobile, req.ClassifyId, sysUser.AdminId)
  3468. if tmpErr != nil {
  3469. //fmt.Println("AddEdbInfo err:", err.Error())
  3470. utils.FileLogData.Error("AddEdbInfo err :%s", tmpErr.Error())
  3471. continue
  3472. }
  3473. tmpTarget, tmpErr := models.GetTargetBySecName(secName)
  3474. target = tmpTarget
  3475. targetMap[secName] = target
  3476. } else {
  3477. fmt.Println("导入数据 获取指标:Err:" + err.Error())
  3478. }
  3479. } else {
  3480. target = tmpTarget
  3481. targetMap[secName] = target
  3482. }
  3483. }
  3484. //判断指标数据是否已经存在
  3485. tmpDataMap, ok2 := edbCodeDataMap[target.TradeCode]
  3486. if !ok2 {
  3487. tmpDataMap = make(map[string]string)
  3488. targetDataList, tmpErr := models.GetTargetsDataList(target.TradeCode)
  3489. if tmpErr != nil {
  3490. err = tmpErr
  3491. }
  3492. for _, tmpData := range targetDataList {
  3493. tmpDataMap[tmpData.Dt] = tmpData.Close
  3494. }
  3495. edbCodeDataMap[target.TradeCode] = tmpDataMap
  3496. }
  3497. //判断数据是否已经存在
  3498. tmpVal, ok3 := tmpDataMap[createDate]
  3499. //数据已存在,进行更新操作
  3500. if ok3 {
  3501. if closeVal != "" {
  3502. // 库里面的数据
  3503. tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
  3504. if tmpErr != nil {
  3505. fmt.Println("tmpVal Parse err:", tmpErr.Error())
  3506. err = tmpErr
  3507. continue
  3508. }
  3509. // 用户填写的数据
  3510. closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
  3511. if tmpErr != nil {
  3512. fmt.Println("closeVal Parse err:", tmpErr.Error())
  3513. err = tmpErr
  3514. continue
  3515. }
  3516. if !tmpValDecimal.Equal(closeValDecimal) {
  3517. fmt.Println("更新数值")
  3518. err = models.ModifyTargetsDataByImport(target.TradeCode, createDate, closeVal)
  3519. if err != nil {
  3520. fmt.Println("ModifyTargetsDataByImport err:", err.Error())
  3521. errs = append(errs, err.Error())
  3522. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  3523. }
  3524. }
  3525. } else {
  3526. fmt.Println("删除数值")
  3527. err = models.DeleteTargetsDataByImport(target.TradeCode, createDate)
  3528. if err != nil {
  3529. fmt.Println("DeleteTargetsDataByImport err:", err.Error())
  3530. errs = append(errs, err.Error())
  3531. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  3532. }
  3533. }
  3534. if frequency != target.Frequency || unit != target.Unit || req.ClassifyId != target.ClassifyId {
  3535. fmt.Println("更新指标频度或单位")
  3536. err = models.ModifyEdbinfo(target.TradeCode, unit, frequency, req.ClassifyId)
  3537. if err != nil {
  3538. fmt.Println("EditEdbinfo err:", err.Error())
  3539. return
  3540. }
  3541. }
  3542. } else { //数据不存在,进行新增操作
  3543. if target.TradeCode != "" && createDate != "" && closeVal != "" {
  3544. fmt.Println("新增")
  3545. models.AddTargetsDataByImport(target.TradeCode, createDate, closeVal)
  3546. if err != nil {
  3547. fmt.Println("AddTargetsDataByImport err:", err.Error())
  3548. errs = append(errs, err.Error())
  3549. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 新增数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  3550. }
  3551. tmpDataMap[createDate] = closeVal
  3552. edbCodeDataMap[target.TradeCode] = tmpDataMap
  3553. }
  3554. }
  3555. //保存关联表
  3556. //{
  3557. // //判断映射表是否已经存在
  3558. // count, err := data_manage.GetExcelEdbdataMappingCount(req.ClassifyId, target.TradeCode, frequency)
  3559. // if err != nil {
  3560. // br.ErrMsg = "GetExcelEdbdataMappingCount,Err:" + err.Error()
  3561. // br.Msg = "新增映射表失败"
  3562. // return
  3563. // }
  3564. // mappingItem := data_manage.ExcelEdbdataMapping{
  3565. // ExcelId: excelId,
  3566. // ClassifyId: req.ClassifyId,
  3567. // Frequency: frequency,
  3568. // TradeCode: target.TradeCode,
  3569. // CreateTime: time.Now(),
  3570. // }
  3571. // if count == 0{
  3572. // //新增
  3573. // err := data_manage.AddExcelEdbdataMapping(&mappingItem)
  3574. // if err != nil {
  3575. // br.ErrMsg = "AddExcelEdbdataMapping,Err:" + err.Error()
  3576. // br.Msg = "新增映射表失败"
  3577. // return
  3578. // }
  3579. // }
  3580. //}
  3581. }
  3582. }
  3583. }
  3584. }
  3585. //resp := models.TargetItemListResp{
  3586. // List: list,
  3587. //}
  3588. br.Ret = 200
  3589. br.Success = true
  3590. br.Msg = "获取成功"
  3591. //br.Data = resp
  3592. //br.Data = list
  3593. }
  3594. // ExcelDataEdit
  3595. // @Title 在线Excel编辑
  3596. // @Description 在线Excel编辑
  3597. // @Param ClassifyId query string false "指标唯一编码"
  3598. // @Param TradeCode query string false "指标唯一编码"
  3599. // @Param Frequency query string true "频度"
  3600. // @Success 200 {object}
  3601. // @router /target/excel_style/edit [get]
  3602. func (this *TargetController) ExcelDataEdit() {
  3603. br := new(models.BaseResponse).Init()
  3604. defer func() {
  3605. this.Data["json"] = br
  3606. this.ServeJSON()
  3607. }()
  3608. sysUser := this.SysUser
  3609. if sysUser == nil {
  3610. br.Msg = "请重新登录"
  3611. return
  3612. }
  3613. //指标编码
  3614. //tradeCode := this.GetString("TradeCode")
  3615. classifyId, _ := this.GetInt("ClassifyId")
  3616. frequency := this.GetString("Frequency")
  3617. excelId, err := data_manage.GetExcelEdbdataMappingByTradeCode(classifyId, frequency)
  3618. if err != nil {
  3619. br.Msg = "查找关联表失败"
  3620. br.ErrMsg = "GetExcelEdbdataMappingByTradeCode,Err:" + err.Error()
  3621. return
  3622. }
  3623. item, err := data_manage.GetExcelStyleById(*excelId)
  3624. if err != nil {
  3625. br.Msg = "查找样式失败"
  3626. br.ErrMsg = "GetExcelStyleById,Err:" + err.Error()
  3627. return
  3628. }
  3629. br.Ret = 200
  3630. br.Success = true
  3631. br.Msg = "获取成功"
  3632. //br.Data = resp
  3633. br.Data = item
  3634. }