target.go 123 KB

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