target.go 110 KB

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