target.go 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848
  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. //valueList = append(valueList, valueMap["m"].(string))
  3285. //fmt.Println("valueList:", valueList)
  3286. }
  3287. dateMap := req.Data[i][0].(map[string]interface{})
  3288. date := dateMap["m"].(string)
  3289. dateValueMap[date] = valueListMap
  3290. }
  3291. }
  3292. //firstCell := req.Data[i][0]
  3293. //if firstCell == nil {
  3294. // continue
  3295. //}
  3296. //dataMap := firstCell.(map[string]interface{})
  3297. //if dataList == nil {
  3298. // dataList = make([][]map[string]interface{}, len(req.Data[i]))
  3299. //}
  3300. //if dataList[i] == nil {
  3301. // dataList[i] = make([]map[string]interface{}, len(req.Data[i]))
  3302. //}
  3303. //dataList[i][0] = dataMap
  3304. //fmt.Println("dataMap", dataMap["m"])
  3305. //v, _ := dataMap["m"]
  3306. ////指标名称
  3307. //if v == "指标名称" {
  3308. // secNameMap := req.Data[i][1].(map[string]interface{})
  3309. // secName = secNameMap["m"].(string)
  3310. // fmt.Println("secName:", secName)
  3311. //}
  3312. ////频度
  3313. //if v == "频度" {
  3314. // frequencyMap := req.Data[i][1].(map[string]interface{})
  3315. // frequency = frequencyMap["m"].(string)
  3316. // fmt.Println("frequency:", frequency)
  3317. //}
  3318. ////单位
  3319. //if v == "单位" {
  3320. // unitMap := req.Data[i][1].(map[string]interface{})
  3321. // unit = unitMap["m"].(string)
  3322. // fmt.Println("unit:", unit)
  3323. //}
  3324. //
  3325. ////日期
  3326. //dateMap := req.Data[i][1].(map[string]interface{})
  3327. //createDate = dateMap["m"].(string)
  3328. //
  3329. ////值
  3330. //valueMap := req.Data[i][valueIndex].(map[string]interface{})
  3331. //closeVal = valueMap["m"].(string)
  3332. //新增指标
  3333. //errMsg := services.AddEdbItem(secName, unit, frequency, req.ClassifyId, sysUser)
  3334. //if errMsg == "" {
  3335. // br.Msg = "新增指标异常!"
  3336. // br.ErrMsg = errMsg
  3337. // return
  3338. //}
  3339. valueIndex++
  3340. }
  3341. //var excelId int64
  3342. //保存EXCEL样式
  3343. //{
  3344. // cellData, err := json.Marshal(cellDataList)
  3345. // if err != nil {
  3346. // br.Msg = "参数解析异常!"
  3347. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3348. // return
  3349. // }
  3350. // config, err := json.Marshal(req.Config)
  3351. // if err != nil {
  3352. // br.Msg = "参数解析异常!"
  3353. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3354. // return
  3355. // }
  3356. // datas, err := json.Marshal(dataList)
  3357. // if err != nil {
  3358. // br.Msg = "参数解析异常!"
  3359. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3360. // return
  3361. // }
  3362. // excelItem := data_manage.ExcelStyle{
  3363. // CellData: string(cellData),
  3364. // Config: string(config),
  3365. // Data: string(datas),
  3366. // }
  3367. // excelId, err = models.AddExcelData(&excelItem)
  3368. // if err != nil {
  3369. // br.ErrMsg = "获取失败,Err:" + err.Error()
  3370. // br.Msg = "获取失败"
  3371. // return
  3372. // }
  3373. //
  3374. // //判断映射表是否已经存在
  3375. // count, err := data_manage.GetExcelEdbdataMappingCount(req.ClassifyId, target.TradeCode, frequency)
  3376. // if err != nil {
  3377. // br.ErrMsg = "GetExcelEdbdataMappingCount,Err:" + err.Error()
  3378. // br.Msg = "新增映射表失败"
  3379. // return
  3380. // }
  3381. // mappingItem := data_manage.ExcelEdbdataMapping{
  3382. // ExcelId: excelId,
  3383. // ClassifyId: req.ClassifyId,
  3384. // Frequency: frequency,
  3385. // TradeCode: target.TradeCode,
  3386. // CreateTime: time.Now(),
  3387. // }
  3388. // if count == 0{
  3389. // //新增
  3390. // err := data_manage.AddExcelEdbdataMapping(&mappingItem)
  3391. // if err != nil {
  3392. // br.ErrMsg = "AddExcelEdbdataMapping,Err:" + err.Error()
  3393. // br.Msg = "新增映射表失败"
  3394. // return
  3395. // }
  3396. // }
  3397. //}
  3398. // 所有的指标数据map
  3399. edbCodeDataMap := make(map[string]map[string]string)
  3400. //操作指标,新增指标及数据等
  3401. {
  3402. for i := 0; i < len(secNameList); i++ {
  3403. //fmt.Println("len(secNameList):", len(secNameList))
  3404. secName := secNameList[i]
  3405. frequency := frequencyList[i]
  3406. //防止填了指标名称后没填单位报错
  3407. if len(unitList) < len(secNameList) {
  3408. br.Msg = "单位填写异常!"
  3409. br.ErrMsg = "单位填写异常!"
  3410. return
  3411. }
  3412. unit := unitList[i]
  3413. for j, s := range dateList {
  3414. var closeVal string
  3415. //valueList, ok := dateValueMap[s]
  3416. valueMap, ok := dateValueMap[s]
  3417. //fmt.Println("valueMap:", valueMap)
  3418. if ok {
  3419. createDate := dateList[j]
  3420. //if i < len(valueList) {
  3421. // closeVal = valueList[i]
  3422. //} else {
  3423. // continue
  3424. //}
  3425. closeVal, ok = valueMap[i+1]
  3426. target, ok2 := targetMap[secName]
  3427. if !ok2 {
  3428. tmpTarget, err := models.GetTargetBySecName(secName)
  3429. if err != nil {
  3430. //如果是找不到该指标,那么新增指标
  3431. if err.Error() == utils.ErrNoRow() {
  3432. //if frequency == "" {
  3433. // failItem := new(models.EdbdataImportFail)
  3434. // failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  3435. // failItem.ClassifyName = classifyName
  3436. // failItem.CreateDate = createDate
  3437. // failItem.SecName = secName
  3438. // failItem.Close = closeVal
  3439. // failItem.Remark = "新增指标失败,频度字段为空"
  3440. // failItem.Frequency = frequency
  3441. // failItem.Unit = unit
  3442. // failDatas = append(failDatas, failItem)
  3443. // continue
  3444. //}
  3445. //if unit == "" {
  3446. // failItem := new(models.EdbdataImportFail)
  3447. // failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
  3448. // failItem.ClassifyName = classifyName
  3449. // failItem.CreateDate = createDate
  3450. // failItem.SecName = secName
  3451. // failItem.Close = closeVal
  3452. // failItem.Remark = "新增指标失败,单位字段为空"
  3453. // failItem.Frequency = frequency
  3454. // failItem.Unit = unit
  3455. // failDatas = append(failDatas, failItem)
  3456. // continue
  3457. //}
  3458. tmpErr := data.AddEdbInfo(secName, unit, frequency, "", sysUser.Mobile, req.ClassifyId, sysUser.AdminId)
  3459. if tmpErr != nil {
  3460. //fmt.Println("AddEdbInfo err:", err.Error())
  3461. utils.FileLogData.Error("AddEdbInfo err :%s", tmpErr.Error())
  3462. continue
  3463. }
  3464. tmpTarget, tmpErr := models.GetTargetBySecName(secName)
  3465. target = tmpTarget
  3466. targetMap[secName] = target
  3467. } else {
  3468. fmt.Println("导入数据 获取指标:Err:" + err.Error())
  3469. }
  3470. } else {
  3471. target = tmpTarget
  3472. targetMap[secName] = target
  3473. }
  3474. }
  3475. //判断指标数据是否已经存在
  3476. tmpDataMap, ok2 := edbCodeDataMap[target.TradeCode]
  3477. if !ok2 {
  3478. tmpDataMap = make(map[string]string)
  3479. targetDataList, tmpErr := models.GetTargetsDataList(target.TradeCode)
  3480. if tmpErr != nil {
  3481. err = tmpErr
  3482. }
  3483. for _, tmpData := range targetDataList {
  3484. tmpDataMap[tmpData.Dt] = tmpData.Close
  3485. }
  3486. edbCodeDataMap[target.TradeCode] = tmpDataMap
  3487. }
  3488. //判断数据是否已经存在
  3489. tmpVal, ok3 := tmpDataMap[createDate]
  3490. //数据已存在,进行更新操作
  3491. if ok3 {
  3492. if closeVal != "" {
  3493. // 库里面的数据
  3494. tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
  3495. if tmpErr != nil {
  3496. fmt.Println("tmpVal Parse err:", tmpErr.Error())
  3497. err = tmpErr
  3498. continue
  3499. }
  3500. // 用户填写的数据
  3501. closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
  3502. if tmpErr != nil {
  3503. fmt.Println("closeVal Parse err:", tmpErr.Error())
  3504. err = tmpErr
  3505. continue
  3506. }
  3507. if !tmpValDecimal.Equal(closeValDecimal) {
  3508. fmt.Println("更新数值")
  3509. err = models.ModifyTargetsDataByImport(target.TradeCode, createDate, closeVal)
  3510. if err != nil {
  3511. fmt.Println("ModifyTargetsDataByImport err:", err.Error())
  3512. errs = append(errs, err.Error())
  3513. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  3514. }
  3515. }
  3516. } else {
  3517. fmt.Println("删除数值")
  3518. err = models.DeleteTargetsDataByImport(target.TradeCode, createDate)
  3519. if err != nil {
  3520. fmt.Println("DeleteTargetsDataByImport err:", err.Error())
  3521. errs = append(errs, err.Error())
  3522. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  3523. }
  3524. }
  3525. if frequency != target.Frequency || unit != target.Unit || req.ClassifyId != target.ClassifyId {
  3526. fmt.Println("更新指标频度或单位")
  3527. err = models.ModifyEdbinfo(target.TradeCode, unit, frequency, req.ClassifyId)
  3528. if err != nil {
  3529. fmt.Println("EditEdbinfo err:", err.Error())
  3530. return
  3531. }
  3532. }
  3533. } else { //数据不存在,进行新增操作
  3534. if target.TradeCode != "" && createDate != "" && closeVal != "" {
  3535. fmt.Println("新增")
  3536. models.AddTargetsDataByImport(target.TradeCode, createDate, closeVal)
  3537. if err != nil {
  3538. fmt.Println("AddTargetsDataByImport err:", err.Error())
  3539. errs = append(errs, err.Error())
  3540. //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 新增数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
  3541. }
  3542. tmpDataMap[createDate] = closeVal
  3543. edbCodeDataMap[target.TradeCode] = tmpDataMap
  3544. }
  3545. }
  3546. //保存关联表
  3547. //{
  3548. // //判断映射表是否已经存在
  3549. // count, err := data_manage.GetExcelEdbdataMappingCount(req.ClassifyId, target.TradeCode, frequency)
  3550. // if err != nil {
  3551. // br.ErrMsg = "GetExcelEdbdataMappingCount,Err:" + err.Error()
  3552. // br.Msg = "新增映射表失败"
  3553. // return
  3554. // }
  3555. // mappingItem := data_manage.ExcelEdbdataMapping{
  3556. // ExcelId: excelId,
  3557. // ClassifyId: req.ClassifyId,
  3558. // Frequency: frequency,
  3559. // TradeCode: target.TradeCode,
  3560. // CreateTime: time.Now(),
  3561. // }
  3562. // if count == 0{
  3563. // //新增
  3564. // err := data_manage.AddExcelEdbdataMapping(&mappingItem)
  3565. // if err != nil {
  3566. // br.ErrMsg = "AddExcelEdbdataMapping,Err:" + err.Error()
  3567. // br.Msg = "新增映射表失败"
  3568. // return
  3569. // }
  3570. // }
  3571. //}
  3572. }
  3573. }
  3574. }
  3575. }
  3576. //resp := models.TargetItemListResp{
  3577. // List: list,
  3578. //}
  3579. br.Ret = 200
  3580. br.Success = true
  3581. br.Msg = "获取成功"
  3582. //br.Data = resp
  3583. //br.Data = list
  3584. }
  3585. // ExcelDataEdit
  3586. // @Title 在线Excel编辑
  3587. // @Description 在线Excel编辑
  3588. // @Param ClassifyId query string false "指标唯一编码"
  3589. // @Param TradeCode query string false "指标唯一编码"
  3590. // @Param Frequency query string true "频度"
  3591. // @Success 200 {object}
  3592. // @router /target/excel_style/edit [get]
  3593. func (this *TargetController) ExcelDataEdit() {
  3594. br := new(models.BaseResponse).Init()
  3595. defer func() {
  3596. this.Data["json"] = br
  3597. this.ServeJSON()
  3598. }()
  3599. sysUser := this.SysUser
  3600. if sysUser == nil {
  3601. br.Msg = "请重新登录"
  3602. return
  3603. }
  3604. //指标编码
  3605. //tradeCode := this.GetString("TradeCode")
  3606. classifyId, _ := this.GetInt("ClassifyId")
  3607. frequency := this.GetString("Frequency")
  3608. excelId, err := data_manage.GetExcelEdbdataMappingByTradeCode(classifyId, frequency)
  3609. if err != nil {
  3610. br.Msg = "查找关联表失败"
  3611. br.ErrMsg = "GetExcelEdbdataMappingByTradeCode,Err:" + err.Error()
  3612. return
  3613. }
  3614. item, err := data_manage.GetExcelStyleById(*excelId)
  3615. if err != nil {
  3616. br.Msg = "查找样式失败"
  3617. br.ErrMsg = "GetExcelStyleById,Err:" + err.Error()
  3618. return
  3619. }
  3620. br.Ret = 200
  3621. br.Success = true
  3622. br.Msg = "获取成功"
  3623. //br.Data = resp
  3624. br.Data = item
  3625. }