target.go 110 KB

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