target.go 111 KB

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