target.go 111 KB

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