target.go 107 KB

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