target.go 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/models"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/models/system"
  7. "eta_gn/eta_api/services/alarm_msg"
  8. "eta_gn/eta_api/services/data"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "os"
  12. "path/filepath"
  13. "reflect"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "github.com/rdlucklib/rdluck_tools/paging"
  19. "github.com/shopspring/decimal"
  20. "github.com/tealeg/xlsx"
  21. )
  22. type TargetController struct {
  23. BaseAuthController
  24. }
  25. type TargetCommonController struct {
  26. BaseCommonController
  27. }
  28. // @router /data/list [get]
  29. func (this *TargetController) DataList() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. pageSize, _ := this.GetInt("PageSize")
  36. currentIndex, _ := this.GetInt("CurrentIndex")
  37. keyWord := this.GetString("KeyWord")
  38. startDate := this.GetString("StartDate")
  39. endDate := this.GetString("EndDate")
  40. frequency := this.GetString("Frequency")
  41. tradeCode := this.GetString("TradeCode")
  42. classifyId, _ := this.GetInt("ClassifyId") //分类
  43. var startSize int
  44. if pageSize <= 0 {
  45. pageSize = utils.PageSize20
  46. }
  47. if currentIndex <= 0 {
  48. currentIndex = 1
  49. }
  50. startSize = utils.StartIndex(currentIndex, pageSize)
  51. var condition string
  52. var pars []interface{}
  53. if keyWord != "" {
  54. condition += ` AND a.SEC_NAME LIKE ? `
  55. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  56. }
  57. if startDate != "" {
  58. condition += ` AND c.DT >= ? `
  59. pars = append(pars, startDate)
  60. }
  61. if endDate != "" {
  62. condition += ` AND c.DT <= ? `
  63. pars = append(pars, endDate)
  64. }
  65. if frequency != "" {
  66. condition += ` AND a.frequency = ? `
  67. pars = append(pars, frequency)
  68. }
  69. if classifyId > 0 {
  70. condition += ` AND a.classify_id = ? `
  71. pars = append(pars, classifyId)
  72. }
  73. if tradeCode != "" {
  74. condition += ` AND c.TRADE_CODE = ? `
  75. pars = append(pars, tradeCode)
  76. }
  77. total, err := models.GetDataListCount(condition, pars)
  78. if err != nil {
  79. br.Msg = "获取失败"
  80. br.ErrMsg = "获取失败,Err:" + err.Error()
  81. return
  82. }
  83. list, err := models.GetDataList(condition, pars, startSize, pageSize)
  84. if err != nil {
  85. br.Msg = "获取失败"
  86. br.ErrMsg = "获取失败,Err:" + err.Error()
  87. return
  88. }
  89. page := paging.GetPaging(currentIndex, pageSize, total)
  90. resp := new(models.DataListResp)
  91. resp.Paging = page
  92. if len(list) <= 0 {
  93. list = make([]*models.DataList, 0)
  94. }
  95. resp.List = list
  96. br.Ret = 200
  97. br.Success = true
  98. br.Msg = "获取成功"
  99. br.Data = resp
  100. }
  101. // @router /data/add [post]
  102. func (this *TargetController) DataAdd() {
  103. br := new(models.BaseResponse).Init()
  104. defer func() {
  105. this.Data["json"] = br
  106. this.ServeJSON()
  107. }()
  108. var req models.DataAddReq
  109. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  110. if err != nil {
  111. br.Msg = "参数解析异常!"
  112. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  113. return
  114. }
  115. if req.TradeCode == "" {
  116. br.Msg = "指标编码不可为空!"
  117. return
  118. }
  119. if req.CreateDate == "" {
  120. br.Msg = "请选择日期"
  121. return
  122. }
  123. item, err := models.GetDataInfo(req.TradeCode, req.CreateDate)
  124. if err != nil && !utils.IsErrNoRow(err) {
  125. br.Msg = "获取信息失败!"
  126. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  127. return
  128. }
  129. if item != nil {
  130. br.Msg = "该指标当前日期下,数据已录入"
  131. return
  132. }
  133. edbdata := new(models.Edbdata)
  134. edbdata.TradeCode = req.TradeCode
  135. edbdata.Dt = req.CreateDate
  136. edbdata.Close = req.Close
  137. edbdata.ModifyTime = time.Now()
  138. _, err = models.AddEdbdata(edbdata)
  139. if err != nil {
  140. br.Msg = "新增失败"
  141. br.ErrMsg = "新增失败,Err:" + err.Error()
  142. return
  143. }
  144. go func() {
  145. record := &models.EdbinfoOpRecord{
  146. TradeCode: edbdata.TradeCode,
  147. Remark: "更新数据",
  148. UserId: this.SysUser.AdminId,
  149. UserName: this.SysUser.RealName,
  150. CreateTime: time.Now(),
  151. }
  152. data.ModifyManualEdbMaxMinDate(edbdata.TradeCode)
  153. _ = record.Create()
  154. if utils.Re == nil {
  155. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, req.TradeCode)
  156. if err != nil {
  157. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  158. }
  159. }
  160. }()
  161. br.Ret = 200
  162. br.Success = true
  163. br.Msg = "新增成功"
  164. }
  165. // @router /data/edit [post]
  166. func (this *TargetController) DataEdit() {
  167. br := new(models.BaseResponse).Init()
  168. defer func() {
  169. this.Data["json"] = br
  170. this.ServeJSON()
  171. }()
  172. var req models.DataEditReq
  173. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  174. if err != nil {
  175. br.Msg = "参数解析异常!"
  176. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  177. return
  178. }
  179. if req.TradeCode == "" {
  180. br.Msg = "指标编码不可为空!"
  181. return
  182. }
  183. if req.CreateDate == "" {
  184. br.Msg = "请选择日期"
  185. return
  186. }
  187. close := ""
  188. loc := reflect.ValueOf(req.Close).Kind().String()
  189. if loc == "string" {
  190. close = req.Close.(string)
  191. } else if loc == "int" {
  192. closeInt := req.Close.(int)
  193. close = strconv.Itoa(closeInt)
  194. } else if loc == "float64" {
  195. close64 := req.Close.(float64)
  196. close = strconv.FormatFloat(close64, 'g', 30, 64)
  197. } else {
  198. close = ""
  199. }
  200. edbdata := new(models.Edbdata)
  201. edbdata.TradeCode = req.TradeCode
  202. edbdata.Dt = req.CreateDate
  203. edbdata.Close = close
  204. edbdata.ModifyTime = time.Now()
  205. if req.OldCreateDate == "" {
  206. item, err := models.GetDataInfo(req.TradeCode, req.CreateDate)
  207. if err != nil && !utils.IsErrNoRow(err) {
  208. br.Msg = "获取信息失败!"
  209. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  210. return
  211. }
  212. if item == nil { //新增
  213. _, err = models.AddEdbdata(edbdata)
  214. if err != nil {
  215. br.Msg = "新增失败"
  216. br.ErrMsg = "新增失败,Err:" + err.Error()
  217. return
  218. }
  219. } else { //修改
  220. err = models.EditEdbdata(edbdata)
  221. if err != nil {
  222. br.Msg = "编辑失败"
  223. br.ErrMsg = "编辑失败,Err:" + err.Error()
  224. return
  225. }
  226. }
  227. } else {
  228. if req.CreateDate == req.OldCreateDate { //修改
  229. err = models.EditEdbdata(edbdata)
  230. if err != nil {
  231. br.Msg = "编辑失败"
  232. br.ErrMsg = "编辑失败,Err:" + err.Error()
  233. return
  234. }
  235. } else {
  236. item, err := models.GetDataInfo(req.TradeCode, req.CreateDate)
  237. if err != nil && !utils.IsErrNoRow(err) {
  238. br.Msg = "获取信息失败!"
  239. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  240. return
  241. }
  242. if item == nil {
  243. _, err = models.AddEdbdata(edbdata)
  244. if err != nil {
  245. br.Msg = "新增失败"
  246. br.ErrMsg = "新增失败,Err:" + err.Error()
  247. return
  248. }
  249. } else {
  250. err = models.EditEdbdata(edbdata)
  251. if err != nil {
  252. br.Msg = "编辑失败"
  253. br.ErrMsg = "编辑失败,Err:" + err.Error()
  254. return
  255. }
  256. }
  257. oldItem, err := models.GetDataInfo(req.TradeCode, req.OldCreateDate)
  258. if err != nil && !utils.IsErrNoRow(err) {
  259. br.Msg = "获取信息失败!"
  260. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  261. return
  262. }
  263. if oldItem != nil {
  264. record := new(models.EdbdataDeleteRecord)
  265. record.TradeCode = oldItem.TradeCode
  266. record.Dt = oldItem.Dt
  267. record.Close = oldItem.Close
  268. record.ModifyTime = oldItem.ModifyTime
  269. record.CreateTime = time.Now()
  270. record.SysUserId = this.SysUser.AdminId
  271. _, err = models.AddEdbdataDeleteRecord(record)
  272. if err != nil {
  273. br.Msg = "编辑失败"
  274. br.ErrMsg = "新增删除记录失败,Err:" + err.Error()
  275. return
  276. }
  277. models.DeleteEdbData(req.TradeCode, req.OldCreateDate)
  278. }
  279. }
  280. }
  281. {
  282. go func() {
  283. data.ModifyManualEdbMaxMinDate(req.TradeCode)
  284. if utils.Re == nil {
  285. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, req.TradeCode)
  286. if err != nil {
  287. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  288. }
  289. }
  290. }()
  291. }
  292. br.Ret = 200
  293. br.Success = true
  294. br.Msg = "编辑成功"
  295. }
  296. // @router /data/batch_edit [post]
  297. func (this *TargetController) BatchDataEdit() {
  298. br := new(models.BaseResponse).Init()
  299. defer func() {
  300. this.Data["json"] = br
  301. this.ServeJSON()
  302. }()
  303. var req models.BatchDataEditReq
  304. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  305. if err != nil {
  306. br.Msg = "参数解析异常!"
  307. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  308. return
  309. }
  310. if req.CreateDate == req.OldCreateDate { //修改
  311. br.Msg = "开始日期与结束日期一致!"
  312. return
  313. }
  314. edbDataEditList := make([]data.EdbDataEdit, 0)
  315. for _, trade := range req.List {
  316. if trade.TradeCode == "" {
  317. br.Msg = "指标编码不可为空!"
  318. return
  319. }
  320. close := ""
  321. loc := reflect.ValueOf(trade.Close).Kind().String()
  322. if loc == "string" {
  323. close = trade.Close.(string)
  324. } else if loc == "int" {
  325. closeInt := trade.Close.(int)
  326. close = strconv.Itoa(closeInt)
  327. } else if loc == "float64" {
  328. close64 := trade.Close.(float64)
  329. close = strconv.FormatFloat(close64, 'g', 30, 64)
  330. } else {
  331. close = ""
  332. }
  333. edbDataEdit := data.EdbDataEdit{
  334. OldCreateDate: req.OldCreateDate,
  335. CreateDate: req.CreateDate,
  336. TradeCode: trade.TradeCode,
  337. Close: close,
  338. }
  339. edbDataEditList = append(edbDataEditList, edbDataEdit)
  340. }
  341. failEdbDataList, err := data.BatchEdbData(req.OldCreateDate, req.CreateDate, edbDataEditList, this.SysUser.AdminId)
  342. if err != nil {
  343. br.Msg = "批量修改失败"
  344. br.ErrMsg = "批量修改失败:" + err.Error()
  345. return
  346. }
  347. br.Ret = 200
  348. br.Success = true
  349. br.Data = failEdbDataList
  350. br.Msg = "编辑成功"
  351. }
  352. // @router /target/list [get]
  353. func (this *TargetController) TargetList() {
  354. br := new(models.BaseResponse).Init()
  355. defer func() {
  356. this.Data["json"] = br
  357. this.ServeJSON()
  358. }()
  359. sysUser := this.SysUser
  360. if sysUser == nil {
  361. br.Msg = "请登录"
  362. br.Ret = 408
  363. return
  364. }
  365. resp := new(models.TargetListResp)
  366. pageSize, _ := this.GetInt("PageSize")
  367. currentIndex, _ := this.GetInt("CurrentIndex")
  368. keyWord := this.GetString("KeyWord")
  369. startDate := this.GetString("StartDate")
  370. endDate := this.GetString("EndDate")
  371. frequency := this.GetString("Frequency")
  372. classifyId, _ := this.GetInt("ClassifyId") //分类
  373. var startSize int
  374. if pageSize <= 0 {
  375. pageSize = utils.PageSize20
  376. }
  377. if currentIndex <= 0 {
  378. currentIndex = 1
  379. }
  380. startSize = utils.StartIndex(currentIndex, pageSize)
  381. var condition string
  382. var pars []interface{}
  383. userId := sysUser.AdminId
  384. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  385. userId = 0
  386. }
  387. classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  388. if err != nil {
  389. br.Msg = "获取分类数据失败"
  390. return
  391. }
  392. total := 0
  393. list := make([]*models.EdbinfoItem, 0)
  394. if len(classifyIdStrList) > 0 {
  395. condition += ` AND a.classify_id in (` + strings.Join(classifyIdStrList, ",") + `) `
  396. if keyWord != "" {
  397. condition += ` AND (a.SEC_NAME LIKE '%` + keyWord + `%' or a.TRADE_CODE LIKE '%` + keyWord + `%' )`
  398. }
  399. if startDate != "" {
  400. startDate = startDate + " 00:00:00"
  401. condition += ` AND a.create_date >= ? `
  402. pars = append(pars, startDate)
  403. }
  404. if endDate != "" {
  405. endDate = endDate + " 23:59:59"
  406. condition += ` AND a.create_date <= ? `
  407. pars = append(pars, endDate)
  408. }
  409. if frequency != "" {
  410. condition += ` AND a.frequency = ? `
  411. pars = append(pars, frequency)
  412. }
  413. if classifyId > 0 {
  414. condition += ` AND a.classify_id = ? `
  415. pars = append(pars, classifyId)
  416. }
  417. mobile := sysUser.Mobile
  418. total, err = models.GetEdbinfoListCount(condition, pars, mobile, sysUser.RoleType)
  419. if err != nil {
  420. br.Msg = "获取失败"
  421. br.ErrMsg = "获取失败,Err:" + err.Error()
  422. return
  423. }
  424. tmpList, err := models.GetEdbinfoItemList(condition, pars, startSize, pageSize, mobile, sysUser.RoleType)
  425. if err != nil {
  426. br.Msg = "获取失败"
  427. br.ErrMsg = "获取失败,Err:" + err.Error()
  428. return
  429. }
  430. if len(tmpList) <= 0 {
  431. list = make([]*models.EdbinfoItem, 0)
  432. } else {
  433. list = tmpList
  434. }
  435. systemUserMap := make(map[int]string)
  436. for k, v := range list {
  437. userName, ok := systemUserMap[v.UserId]
  438. if ok == false {
  439. systemUser, _ := system.GetSysAdminById(v.UserId)
  440. if systemUser != nil {
  441. userName = systemUser.RealName
  442. } else {
  443. userName = `无`
  444. }
  445. systemUserMap[v.UserId] = userName
  446. }
  447. list[k].UserName = userName
  448. }
  449. }
  450. page := paging.GetPaging(currentIndex, pageSize, total)
  451. resp.Paging = page
  452. resp.List = list
  453. br.Ret = 200
  454. br.Success = true
  455. br.Msg = "获取成功"
  456. br.Data = resp
  457. }
  458. // @router /target/add [post]
  459. func (this *TargetController) TargetAdd() {
  460. br := new(models.BaseResponse).Init()
  461. defer func() {
  462. this.Data["json"] = br
  463. this.ServeJSON()
  464. }()
  465. var req models.EdbinfoAddReq
  466. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  467. if err != nil {
  468. br.Msg = "参数解析异常!"
  469. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  470. return
  471. }
  472. sysUser := this.SysUser
  473. if sysUser == nil {
  474. br.Msg = "请重新登录"
  475. return
  476. }
  477. tradeCode, err := models.GetMaxTradeCode()
  478. if err != nil {
  479. br.Msg = "获取数据失败"
  480. br.ErrMsg = "获取最大交易编码失败,Err:" + err.Error()
  481. return
  482. }
  483. maxTradeCode, err := utils.GetMaxTradeCode(tradeCode)
  484. if err != nil {
  485. br.Msg = "数据异常"
  486. br.ErrMsg = "获取编码信息失败,Err:" + err.Error()
  487. return
  488. }
  489. if maxTradeCode == "" {
  490. br.Msg = "编码为空,请联系技术"
  491. br.ErrMsg = "编码为空,请联系技术"
  492. return
  493. }
  494. if req.SecName == "" {
  495. br.Msg = "指标名称不能为空"
  496. return
  497. }
  498. item, err := models.GetEdbinfoBySecName(req.SecName)
  499. if err != nil && !utils.IsErrNoRow(err) {
  500. br.Msg = "获取信息失败!"
  501. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  502. return
  503. }
  504. if item != nil {
  505. br.Msg = "指标名称已存在"
  506. return
  507. }
  508. err = models.AddEdbinfo(maxTradeCode, req.SecName, req.Unit, "手动", req.Frequency, req.NoticeTime, req.ClassifyId, sysUser.AdminId, sysUser.RealName)
  509. if err != nil {
  510. br.Msg = "新增失败"
  511. br.ErrMsg = "新增失败,Err:" + err.Error()
  512. return
  513. }
  514. err = models.AddEdbinfoUser(maxTradeCode, sysUser.Mobile)
  515. if err != nil {
  516. br.Msg = "新增失败"
  517. br.ErrMsg = "新增失败,Err:" + err.Error()
  518. return
  519. }
  520. br.Ret = 200
  521. br.Success = true
  522. br.Msg = "新增成功"
  523. }
  524. // @router /target/edit [post]
  525. func (this *TargetController) TargetEdit() {
  526. br := new(models.BaseResponse).Init()
  527. defer func() {
  528. this.Data["json"] = br
  529. this.ServeJSON()
  530. }()
  531. var req models.EdbinfoEditReq
  532. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  533. if err != nil {
  534. br.Msg = "参数解析异常!"
  535. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  536. return
  537. }
  538. sysUser := this.SysUser
  539. if sysUser == nil {
  540. br.Msg = "请重新登录"
  541. return
  542. }
  543. if req.TradeCode == "" {
  544. br.Msg = "数据异常"
  545. br.ErrMsg = "最大编码为空"
  546. return
  547. }
  548. if req.SecName == "" {
  549. br.Msg = "指标名称不能为空"
  550. return
  551. }
  552. item, err := models.GetEdbinfoBySecName(req.SecName)
  553. if err != nil && !utils.IsErrNoRow(err) {
  554. br.Msg = "获取信息失败!"
  555. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  556. return
  557. }
  558. if item != nil {
  559. if req.TradeCode != item.TradeCode {
  560. br.Msg = "指标名称已存在"
  561. return
  562. }
  563. }
  564. err = models.EditEdbinfo(req.TradeCode, req.SecName, req.Unit, req.Frequency, req.NoticeTime, req.ClassifyId)
  565. if err != nil {
  566. br.Msg = "编辑失败"
  567. br.ErrMsg = "编辑失败,Err:" + err.Error()
  568. return
  569. }
  570. br.Ret = 200
  571. br.Success = true
  572. br.Msg = "编辑成功"
  573. }
  574. // @router /target/search [get]
  575. func (this *TargetController) TargetSearch() {
  576. br := new(models.BaseResponse).Init()
  577. defer func() {
  578. this.Data["json"] = br
  579. this.ServeJSON()
  580. }()
  581. sysUser := this.SysUser
  582. if sysUser == nil {
  583. br.Msg = "请登录"
  584. br.Ret = 408
  585. return
  586. }
  587. classifyId, _ := this.GetInt("ClassifyId")
  588. keyWord := this.GetString("KeyWord")
  589. list, err := models.SearchTargetEntry(classifyId, keyWord)
  590. if err != nil {
  591. br.Msg = "获取失败"
  592. br.ErrMsg = "获取失败,Err:" + err.Error()
  593. return
  594. }
  595. resp := new(models.SearchTargetListResp)
  596. resp.List = list
  597. br.Ret = 200
  598. br.Success = true
  599. br.Msg = "获取成功"
  600. br.Data = resp
  601. }
  602. // @router /classify [get]
  603. func (this *TargetController) ClassifyList() {
  604. br := new(models.BaseResponse).Init()
  605. defer func() {
  606. this.Data["json"] = br
  607. this.ServeJSON()
  608. }()
  609. sysUser := this.SysUser
  610. if sysUser == nil {
  611. br.Msg = "请登录"
  612. br.Ret = 408
  613. return
  614. }
  615. var wxUserId int64
  616. wxUserId = int64(sysUser.AdminId)
  617. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  618. wxUserId = 0
  619. }
  620. list, err := models.GetEdbdataClassify(wxUserId)
  621. if err != nil {
  622. br.Msg = "获取失败"
  623. br.ErrMsg = "获取失败,Err:" + err.Error()
  624. return
  625. }
  626. classifyIdStrList := make([]string, 0)
  627. for _, classifyList := range list {
  628. if classifyList.Child != nil {
  629. for _, classify := range classifyList.Child {
  630. classifyIdStrList = append(classifyIdStrList, strconv.Itoa(classify.ClassifyId))
  631. }
  632. }
  633. }
  634. if len(classifyIdStrList) > 0 {
  635. edbInfoGroupCountList, err := models.GetEdbInfoGroupCountByClassifyIds(strings.Join(classifyIdStrList, ","))
  636. if err != nil {
  637. br.Msg = "获取失败"
  638. br.ErrMsg = "获取分类下有数据的指标数量失败,Err:" + err.Error()
  639. return
  640. }
  641. edbInfoGroupCountMap := make(map[int]int)
  642. for _, edbInfoGroupCount := range edbInfoGroupCountList {
  643. edbInfoGroupCountMap[edbInfoGroupCount.ClassifyId] = edbInfoGroupCount.Count
  644. }
  645. for _, classifyList := range list {
  646. classifyList.UniqueCode = utils.MD5(fmt.Sprint(classifyList.ClassifyId))
  647. if classifyList.Child != nil {
  648. for _, classify := range classifyList.Child {
  649. if total, ok := edbInfoGroupCountMap[classify.ClassifyId]; ok {
  650. classify.EdbInfoTotal = total
  651. classify.UniqueCode = utils.MD5(fmt.Sprint(classify.ClassifyId))
  652. }
  653. }
  654. }
  655. }
  656. }
  657. resp := new(models.EdbdataClassifyResp)
  658. resp.List = list
  659. br.Ret = 200
  660. br.Success = true
  661. br.Msg = "获取成功"
  662. br.Data = resp
  663. }
  664. // @router /template [get]
  665. func (this *TargetCommonController) TemplateDownload() {
  666. br := new(models.BaseResponse).Init()
  667. defer func() {
  668. this.Data["json"] = br
  669. this.ServeJSON()
  670. }()
  671. source, _ := this.GetInt("Source")
  672. switch source {
  673. case 2:
  674. this.Ctx.Output.Download("./static/template/导入模板2.xlsx", "数据导入模板2.xlsx")
  675. default:
  676. this.Ctx.Output.Download("./static/template/导入模板1.xlsx", "数据导入模板1.xlsx")
  677. }
  678. br.Ret = 200
  679. br.Success = true
  680. br.Msg = "下载成功"
  681. }
  682. // @router /template/en [get]
  683. func (this *TargetCommonController) TemplateDownloadEn() {
  684. br := new(models.BaseResponse).Init()
  685. defer func() {
  686. this.Data["json"] = br
  687. this.ServeJSON()
  688. }()
  689. this.Ctx.Output.Download("./static/Data Import Template.xlsx", "Data Import Template.xlsx")
  690. br.Ret = 200
  691. br.Success = true
  692. br.Msg = "下载成功"
  693. }
  694. // @router /import/failList [get]
  695. func (this *TargetController) ImportFailListDownload() {
  696. br := new(models.BaseResponse).Init()
  697. defer func() {
  698. this.Data["json"] = br
  699. this.ServeJSON()
  700. }()
  701. sysUser := this.SysUser
  702. if sysUser == nil {
  703. br.Msg = "请重新登录"
  704. return
  705. }
  706. item, err := models.GetFailList(sysUser.AdminId)
  707. if err != nil {
  708. br.Msg = "获取数据失败"
  709. br.Msg = "获取数据失败,Err:" + err.Error()
  710. return
  711. }
  712. xlsxFile := xlsx.NewFile()
  713. SheetName := "导入失败数据"
  714. if this.Lang == utils.EnLangVersion {
  715. SheetName = "Import Fail List"
  716. }
  717. sheet, err := xlsxFile.AddSheet(SheetName)
  718. headRow := sheet.AddRow()
  719. headRow.AddCell().SetValue("品种/Variety")
  720. headRow.AddCell().SetValue("指标名称/Indicator Name")
  721. headRow.AddCell().SetValue("指标日期/Indicator Date")
  722. headRow.AddCell().SetValue("值/Value")
  723. headRow.AddCell().SetValue("频度/Frequency")
  724. headRow.AddCell().SetValue("单位/Unit")
  725. headRow.AddCell().SetValue("备注/Note")
  726. for _, v := range item {
  727. row := sheet.AddRow()
  728. row.AddCell().SetValue(v.ClassifyName)
  729. row.AddCell().SetValue(v.SecName)
  730. row.AddCell().SetValue(v.CreateDate)
  731. row.AddCell().SetString(v.Close)
  732. row.AddCell().SetValue(v.Frequency)
  733. row.AddCell().SetValue(v.Unit)
  734. row.AddCell().SetValue(v.Remark)
  735. }
  736. fileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  737. savePath := "./static/" + fileName
  738. err = xlsxFile.Save(savePath)
  739. if err != nil {
  740. br.Msg = "文件保存失败"
  741. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  742. return
  743. }
  744. defer func() {
  745. os.Remove(savePath)
  746. }()
  747. finalFileName := "失败列表.xlsx"
  748. if this.Lang == utils.EnLangVersion {
  749. finalFileName = "Failure List.xlsx"
  750. }
  751. this.Ctx.Output.Download(savePath, finalFileName)
  752. }
  753. // @router /export/dataList [get]
  754. func (this *TargetController) ExportDataList() {
  755. br := new(models.BaseResponse).Init()
  756. defer func() {
  757. this.Data["json"] = br
  758. this.ServeJSON()
  759. }()
  760. sysUser := this.SysUser
  761. if sysUser == nil {
  762. br.Msg = "请重新登录"
  763. return
  764. }
  765. keyWord := this.GetString("KeyWord")
  766. startDate := this.GetString("StartDate")
  767. endDate := this.GetString("EndDate")
  768. frequency := this.GetString("Frequency")
  769. classifyId, _ := this.GetInt("ClassifyId") //分类
  770. mobile := this.GetString("Mobile") //分类
  771. tradeCode := this.GetString("TradeCode") //指标唯一编码
  772. var condition string
  773. var pars []interface{}
  774. if keyWord != "" {
  775. condition += ` AND a.SEC_NAME LIKE '%` + keyWord + `%'`
  776. }
  777. if tradeCode != "" {
  778. condition += ` AND a.TRADE_CODE = ? `
  779. pars = append(pars, tradeCode)
  780. }
  781. if startDate != "" {
  782. condition += ` AND c.DT >= ? `
  783. pars = append(pars, startDate)
  784. }
  785. if endDate != "" {
  786. condition += ` AND c.DT <= ? `
  787. pars = append(pars, endDate)
  788. }
  789. if frequency != "" {
  790. condition += ` AND a.frequency = ? `
  791. pars = append(pars, frequency)
  792. }
  793. permissionUserId := sysUser.AdminId
  794. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  795. permissionUserId = 0
  796. }
  797. childClassifyIdList := make([]int, 0)
  798. if classifyId > 0 {
  799. classifyList, err := models.GetEdbdataClassify(int64(permissionUserId))
  800. if err != nil {
  801. return
  802. }
  803. var isParent bool
  804. for _, v := range classifyList {
  805. if v.ClassifyId == classifyId {
  806. isParent = true
  807. childClassifyIdList = append(childClassifyIdList, v.ClassifyId)
  808. if v.Child != nil && len(v.Child) > 0 {
  809. for _, vv := range v.Child {
  810. childClassifyIdList = append(childClassifyIdList, vv.ClassifyId)
  811. }
  812. }
  813. break
  814. }
  815. }
  816. if !isParent {
  817. childClassifyIdList = append(childClassifyIdList, classifyId)
  818. }
  819. }
  820. num := len(childClassifyIdList)
  821. condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
  822. pars = append(pars, childClassifyIdList)
  823. if mobile != "" {
  824. condition += ` AND d.mobile=? `
  825. pars = append(pars, mobile)
  826. }
  827. userId := sysUser.AdminId
  828. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  829. userId = 0
  830. }
  831. classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  832. if err != nil {
  833. br.Msg = "获取分类数据失败"
  834. return
  835. }
  836. dir, _ := os.Executable()
  837. exPath := filepath.Dir(dir)
  838. downloadFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  839. xlsxFile := xlsx.NewFile()
  840. if err != nil {
  841. br.Msg = "生成文件失败"
  842. br.ErrMsg = "生成文件失败"
  843. return
  844. }
  845. var frequencies []string
  846. if len(childClassifyIdList) > 0 {
  847. frequencies, err = models.GetEdbDataFrequencyByClassifyIdList(childClassifyIdList)
  848. if err != nil {
  849. br.Msg = "查询频度失败"
  850. br.ErrMsg = "查询频度失败"
  851. return
  852. }
  853. } else if tradeCode != `` {
  854. manualEdb, err := models.GetEdbinfoByTradeCode(tradeCode)
  855. if err != nil {
  856. br.Msg = "查询指标信息失败"
  857. br.ErrMsg = "查询指标信息失败,err:" + err.Error()
  858. return
  859. }
  860. frequencies = append(frequencies, manualEdb.Frequency)
  861. } else if keyWord != `` {
  862. frequencies, err = models.GetEdbDataFrequencyByKeyord(keyWord)
  863. if err != nil {
  864. br.Msg = "查询频度失败"
  865. br.ErrMsg = "查询频度失败"
  866. return
  867. }
  868. } else {
  869. frequencies = append(frequencies, `日度`, `周度`, `月度`, `季度`, `半年度`, `年度`)
  870. }
  871. fileName := `手工数据`
  872. if frequencies == nil {
  873. sheet, err := xlsxFile.AddSheet("无数据")
  874. if err != nil {
  875. br.Msg = "新增Sheet失败"
  876. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  877. return
  878. }
  879. rowSecName := sheet.AddRow()
  880. celSecName := rowSecName.AddCell()
  881. celSecName.SetValue("")
  882. }
  883. for _, frequency := range frequencies {
  884. secNameList := make([]*models.EdbdataExportList, 0)
  885. if len(classifyIdStrList) > 0 {
  886. newCondition := condition
  887. newCondition += ` AND a.classify_id in (` + strings.Join(classifyIdStrList, ",") + `) `
  888. newCondition += ` AND a.frequency = ? `
  889. pars := append(pars, frequency)
  890. tmpSecNameList, err := models.GetEdbdataSecName(newCondition, pars)
  891. if err != nil {
  892. br.Msg = "获取数据失败"
  893. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  894. return
  895. }
  896. secNameList = tmpSecNameList
  897. }
  898. var sheet *xlsx.Sheet
  899. if len(secNameList) > 0 {
  900. sheet, err = xlsxFile.AddSheet(frequency)
  901. if err != nil {
  902. br.Msg = "新增Sheet失败"
  903. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  904. return
  905. }
  906. fileName = secNameList[0].ClassifyName
  907. } else {
  908. continue
  909. }
  910. rowSecName := sheet.AddRow()
  911. celSecName := rowSecName.AddCell()
  912. celSecName.SetValue("指标名称")
  913. rowFrequency := sheet.AddRow()
  914. celFrequency := rowFrequency.AddCell()
  915. celFrequency.SetValue("频率")
  916. rowUnit := sheet.AddRow()
  917. celUnit := rowUnit.AddCell()
  918. celUnit.SetValue("单位")
  919. rowModifyDate := sheet.AddRow()
  920. rowModifyCell := rowModifyDate.AddCell()
  921. rowModifyCell.SetValue("更新时间")
  922. dataMap := make(map[string]map[string]*models.EdbdataItem)
  923. var tradeCodeList []string
  924. for _, v := range secNameList {
  925. cellSenName := rowSecName.AddCell()
  926. cellSenName.SetValue(v.SecName)
  927. celFrequency := rowFrequency.AddCell()
  928. celFrequency.SetValue(v.Frequency)
  929. celUnit := rowUnit.AddCell()
  930. celUnit.SetValue(v.Unit)
  931. rowModifyCell := rowModifyDate.AddCell()
  932. rowModifyCell.SetValue(v.Dt)
  933. tradeCodeList = append(tradeCodeList, v.TradeCode)
  934. items, err := models.GetEdbdataAllByTradeCode(v.TradeCode)
  935. if err != nil && !utils.IsErrNoRow(err) {
  936. br.ErrMsg = "GetEdbdataAllByTradeCode,Err:" + err.Error()
  937. br.Msg = "获取数据失败"
  938. return
  939. }
  940. for _, item := range items {
  941. if dataMap[item.TradeCode] == nil {
  942. dataMap[item.TradeCode] = make(map[string]*models.EdbdataItem)
  943. }
  944. dataMap[item.TradeCode][item.Dt] = item
  945. fmt.Println(item.TradeCode, item.Dt)
  946. }
  947. }
  948. tradeCodeStr := strings.Join(tradeCodeList, "','")
  949. tradeCodeStr = "'" + tradeCodeStr + "'"
  950. dateList, err := models.GetEdbdataList(tradeCodeStr)
  951. if err != nil {
  952. br.Msg = "获取数据失败"
  953. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  954. return
  955. }
  956. for _, n := range dateList {
  957. rowData := sheet.AddRow()
  958. celDate := rowData.AddCell()
  959. celDate.SetValue(n.Dt)
  960. for _, m := range secNameList {
  961. celData := rowData.AddCell()
  962. fmt.Println(m.TradeCode)
  963. fmt.Println(n.Dt)
  964. if dataMap[m.TradeCode][n.Dt] != nil {
  965. celData.SetValue(dataMap[m.TradeCode][n.Dt].Close)
  966. }
  967. }
  968. }
  969. }
  970. err = xlsxFile.Save(downloadFilePath)
  971. if err != nil {
  972. sheet, err := xlsxFile.AddSheet("无数据")
  973. if err != nil {
  974. br.Msg = "新增Sheet失败"
  975. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  976. return
  977. }
  978. rowSecName := sheet.AddRow()
  979. celSecName := rowSecName.AddCell()
  980. celSecName.SetValue("")
  981. err = xlsxFile.Save(downloadFilePath)
  982. if err != nil {
  983. br.Msg = "保存文件失败"
  984. br.ErrMsg = "保存文件失败"
  985. return
  986. }
  987. }
  988. defer func() {
  989. os.Remove(downloadFilePath)
  990. }()
  991. fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
  992. this.Ctx.Output.Download(downloadFilePath, fileName)
  993. br.Ret = 200
  994. br.Success = true
  995. br.Msg = "success"
  996. }
  997. // @router /dataList [get]
  998. func (this *TargetCommonController) DataList() {
  999. br := new(models.BaseResponse).Init()
  1000. defer func() {
  1001. this.Data["json"] = br
  1002. this.ServeJSON()
  1003. }()
  1004. br.Msg = "成功"
  1005. br.Ret = 200
  1006. }
  1007. // @router /data/delete [post]
  1008. func (this *TargetController) DataDelete() {
  1009. br := new(models.BaseResponse).Init()
  1010. defer func() {
  1011. this.Data["json"] = br
  1012. this.ServeJSON()
  1013. }()
  1014. var req models.DataDeleteReq
  1015. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1016. if err != nil {
  1017. br.Msg = "参数解析异常!"
  1018. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1019. return
  1020. }
  1021. sysUser := this.SysUser
  1022. if sysUser == nil {
  1023. br.Msg = "请重新登录"
  1024. return
  1025. }
  1026. if req.TradeCode == "" {
  1027. br.Msg = "指标编码不可为空"
  1028. br.ErrMsg = "指标编码不可为空"
  1029. return
  1030. }
  1031. if req.CreateDate == "" {
  1032. br.Msg = "数据录入日期不可为空"
  1033. return
  1034. }
  1035. item, err := models.GetDataInfo(req.TradeCode, req.CreateDate)
  1036. if err != nil && !utils.IsErrNoRow(err) {
  1037. br.Msg = "获取信息失败!"
  1038. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  1039. return
  1040. }
  1041. if item == nil {
  1042. br.Msg = "数据不存在"
  1043. return
  1044. }
  1045. err = models.DataDelete(req.TradeCode, req.CreateDate, item.Close, item.ModifyTime, sysUser.AdminId)
  1046. if err != nil {
  1047. br.Msg = "删除失败"
  1048. br.ErrMsg = "删除失败,Err:" + err.Error()
  1049. return
  1050. }
  1051. br.Ret = 200
  1052. br.Success = true
  1053. br.Msg = "删除成功"
  1054. }
  1055. // @router /data/check [get]
  1056. func (this *TargetController) DataCheck() {
  1057. br := new(models.BaseResponse).Init()
  1058. defer func() {
  1059. this.Data["json"] = br
  1060. this.ServeJSON()
  1061. }()
  1062. tradeCode := this.GetString("TradeCode")
  1063. createDate := this.GetString("CreateDate")
  1064. sysUser := this.SysUser
  1065. if sysUser == nil {
  1066. br.Msg = "请重新登录"
  1067. return
  1068. }
  1069. if tradeCode == "" {
  1070. br.Msg = "指标编码不可为空"
  1071. return
  1072. }
  1073. if createDate == "" {
  1074. br.Msg = "请选择日期"
  1075. return
  1076. }
  1077. resp := new(models.DataCheckResp)
  1078. item, err := models.GetDataInfo(tradeCode, createDate)
  1079. if err != nil && !utils.IsErrNoRow(err) {
  1080. br.Msg = "获取数据失败"
  1081. br.Msg = "获取数据失败,Err:" + err.Error()
  1082. return
  1083. }
  1084. if item != nil {
  1085. resp.Status = 1
  1086. resp.Close = item.Close
  1087. } else {
  1088. resp.Status = 0
  1089. }
  1090. br.Ret = 200
  1091. br.Success = true
  1092. br.Msg = "检测成功"
  1093. br.Data = resp
  1094. }
  1095. // @router /target/check [get]
  1096. func (this *TargetController) TargetCheck() {
  1097. br := new(models.BaseResponse).Init()
  1098. defer func() {
  1099. this.Data["json"] = br
  1100. this.ServeJSON()
  1101. }()
  1102. tradeCode := this.GetString("TradeCode")
  1103. sysUser := this.SysUser
  1104. if sysUser == nil {
  1105. br.Msg = "请重新登录"
  1106. return
  1107. }
  1108. if tradeCode == "" {
  1109. br.Msg = "指标编码不可为空"
  1110. return
  1111. }
  1112. resp := new(models.DataCheckResp)
  1113. count, err := data_manage.GetEdbInfoCount(utils.DATA_SOURCE_MANUAL, tradeCode)
  1114. if err != nil && !utils.IsErrNoRow(err) {
  1115. br.Msg = "获取数据失败"
  1116. br.Msg = "获取数据失败,Err:" + err.Error()
  1117. return
  1118. }
  1119. if count > 0 {
  1120. resp.Status = 1
  1121. } else {
  1122. resp.Status = 0
  1123. }
  1124. br.Ret = 200
  1125. br.Success = true
  1126. br.Msg = "指标不存在关联数据,可直接删除"
  1127. br.Data = resp
  1128. }
  1129. // @router /target/delete [post]
  1130. func (this *TargetController) TargetDelete() {
  1131. br := new(models.BaseResponse).Init()
  1132. defer func() {
  1133. this.Data["json"] = br
  1134. this.ServeJSON()
  1135. }()
  1136. var req models.TargetDeleteReq
  1137. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1138. if err != nil {
  1139. br.Msg = "参数解析异常!"
  1140. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1141. return
  1142. }
  1143. if req.TradeCode == "" {
  1144. br.Msg = "指标编码不可为空"
  1145. return
  1146. }
  1147. key := "import:edbinfo:data:" + req.TradeCode
  1148. if utils.Rc.IsExist(key) {
  1149. br.Msg = "该指标正在导入数据中,请稍后操作"
  1150. return
  1151. }
  1152. err = models.TargetDelete(req.TradeCode)
  1153. if err != nil {
  1154. br.Msg = "删除失败"
  1155. br.Msg = "删除失败,Err:" + err.Error()
  1156. return
  1157. }
  1158. br.Ret = 200
  1159. br.Success = true
  1160. br.IsAddLog = true
  1161. br.Msg = "删除成功"
  1162. }
  1163. // @router /getResearcher [get]
  1164. func (this *TargetController) GetResearcher() {
  1165. br := new(models.BaseResponse).Init()
  1166. defer func() {
  1167. this.Data["json"] = br
  1168. this.ServeJSON()
  1169. }()
  1170. sysUser := this.SysUser
  1171. if sysUser == nil {
  1172. br.Msg = "请重新登录"
  1173. return
  1174. }
  1175. var err error
  1176. list := make([]*models.Researcher, 0)
  1177. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN ||
  1178. sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN ||
  1179. sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  1180. list, err = models.GetResearcherEntry()
  1181. if err != nil {
  1182. br.Msg = "获取数据失败"
  1183. br.Msg = "获取数据失败,Err:" + err.Error()
  1184. return
  1185. }
  1186. } else if sysUser.Role == "hggroup" {
  1187. mobileStr := "'15888577277','15888856487','15158848535','13651627324' "
  1188. list, err = models.GetResearcherEntryByMobile(mobileStr)
  1189. if err != nil {
  1190. br.Msg = "获取数据失败"
  1191. br.Msg = "获取数据失败,Err:" + err.Error()
  1192. return
  1193. }
  1194. } else if sysUser.AdminName == "dfma" {
  1195. list, err = models.GetResearcherEntry()
  1196. if err != nil {
  1197. br.Msg = "获取数据失败"
  1198. br.Msg = "获取数据失败,Err:" + err.Error()
  1199. return
  1200. }
  1201. }
  1202. resp := new(models.ResearcherListResp)
  1203. resp.List = list
  1204. br.Ret = 200
  1205. br.Success = true
  1206. br.Msg = "获取成功"
  1207. br.Data = resp
  1208. }
  1209. // @router /getTargetItems [get]
  1210. func (this *TargetController) GetTargetItems() {
  1211. br := new(models.BaseResponse).Init()
  1212. defer func() {
  1213. this.Data["json"] = br
  1214. this.ServeJSON()
  1215. }()
  1216. sysUser := this.SysUser
  1217. if sysUser == nil {
  1218. br.Msg = "请重新登录"
  1219. return
  1220. }
  1221. mobile := this.GetString("Mobile")
  1222. if mobile == "" {
  1223. mobile = sysUser.Mobile
  1224. }
  1225. classifyId, _ := this.GetInt("ClassifyId")
  1226. items, err := models.GetTargetItems(mobile, classifyId)
  1227. if err != nil {
  1228. br.ErrMsg = "获取失败,Err:" + err.Error()
  1229. br.Msg = "获取失败"
  1230. return
  1231. }
  1232. resp := new(models.TargetItemsResp)
  1233. resp.List = items
  1234. br.Ret = 200
  1235. br.Success = true
  1236. br.Msg = "获取成功"
  1237. br.Data = resp
  1238. }
  1239. // @router /target/unit_list [get]
  1240. func (this *TargetController) TargetUnitList() {
  1241. br := new(models.BaseResponse).Init()
  1242. defer func() {
  1243. this.Data["json"] = br
  1244. this.ServeJSON()
  1245. }()
  1246. sysUser := this.SysUser
  1247. if sysUser == nil {
  1248. br.Msg = "请登录"
  1249. br.Ret = 408
  1250. return
  1251. }
  1252. list, err := models.GetEdbUnitList()
  1253. if err != nil {
  1254. br.Msg = "获取失败"
  1255. br.ErrMsg = "获取失败,Err:" + err.Error()
  1256. return
  1257. }
  1258. edbUnitList := make([]string, 0)
  1259. for _, unit := range list {
  1260. edbUnitList = append(edbUnitList, unit.Unit)
  1261. }
  1262. br.Ret = 200
  1263. br.Success = true
  1264. br.Msg = "获取成功"
  1265. br.Data = edbUnitList
  1266. }
  1267. // @router /target/frequency_list [get]
  1268. func (this *TargetController) TargetFrequencyList() {
  1269. br := new(models.BaseResponse).Init()
  1270. defer func() {
  1271. this.Data["json"] = br
  1272. this.ServeJSON()
  1273. }()
  1274. sysUser := this.SysUser
  1275. if sysUser == nil {
  1276. br.Msg = "请登录"
  1277. br.Ret = 408
  1278. return
  1279. }
  1280. classifyId, _ := this.GetInt("ClassifyId") //分类
  1281. if classifyId <= 0 {
  1282. br.Msg = "分类ID必传"
  1283. return
  1284. }
  1285. userId := sysUser.AdminId
  1286. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  1287. userId = 0
  1288. }
  1289. list, err := models.GetEdbFrequencyList(classifyId, userId)
  1290. if err != nil {
  1291. br.Msg = "获取失败"
  1292. br.ErrMsg = "获取失败,Err:" + err.Error()
  1293. return
  1294. }
  1295. edbFrequencyList := make([]string, 0)
  1296. for _, unit := range list {
  1297. edbFrequencyList = append(edbFrequencyList, unit.Frequency)
  1298. }
  1299. edbFrequencyList = sortEdbFrequency(edbFrequencyList)
  1300. br.Ret = 200
  1301. br.Success = true
  1302. br.Msg = "获取成功"
  1303. br.Data = edbFrequencyList
  1304. }
  1305. func sortEdbFrequency(frequencyList []string) (newFrequencyList []string) {
  1306. var frequencyMap1 = map[string]int{
  1307. "日度": 1,
  1308. "周度": 2,
  1309. "旬度": 3,
  1310. "月度": 4,
  1311. "季度": 5,
  1312. "半年度": 6,
  1313. "年度": 7,
  1314. }
  1315. var frequencyMap2 = map[int]string{
  1316. 1: "日度",
  1317. 2: "周度",
  1318. 3: "旬度",
  1319. 4: "月度",
  1320. 5: "季度",
  1321. 6: "半年度",
  1322. 7: "年度",
  1323. }
  1324. idList := make([]int, 0)
  1325. for _, frequency := range frequencyList {
  1326. idList = append(idList, frequencyMap1[frequency])
  1327. }
  1328. sort.Ints(idList)
  1329. for _, id := range idList {
  1330. newFrequencyList = append(newFrequencyList, frequencyMap2[id])
  1331. }
  1332. return
  1333. }
  1334. // @router /target/item_list [get]
  1335. func (this *TargetController) TargetItemList() {
  1336. br := new(models.BaseResponse).Init()
  1337. defer func() {
  1338. this.Data["json"] = br
  1339. this.ServeJSON()
  1340. }()
  1341. sysUser := this.SysUser
  1342. if sysUser == nil {
  1343. br.Msg = "请重新登录"
  1344. return
  1345. }
  1346. frequency := this.GetString("Frequency")
  1347. keyword := this.GetString("Keyword")
  1348. tradeCode := this.GetString("TradeCode")
  1349. userId := sysUser.AdminId
  1350. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  1351. userId = 0
  1352. }
  1353. classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  1354. if err != nil {
  1355. br.Msg = "获取分类数据失败"
  1356. return
  1357. }
  1358. classifyId, _ := this.GetInt("ClassifyId")
  1359. edbShowType, _ := this.GetInt("EdbShowType")
  1360. list, err := models.GetTargetItemList(classifyId, edbShowType, frequency, keyword, tradeCode, classifyIdStrList)
  1361. if err != nil {
  1362. br.ErrMsg = "获取失败,Err:" + err.Error()
  1363. br.Msg = "获取失败"
  1364. return
  1365. }
  1366. edbTradeCodeList := make([]string, 0)
  1367. frequencyMap := make(map[string]int)
  1368. for _, edb := range list {
  1369. edbTradeCodeList = append(edbTradeCodeList, edb.TradeCode)
  1370. frequencyMap[edb.Frequency] = 0
  1371. }
  1372. frequencyList := make([]string, 0)
  1373. for k := range frequencyMap {
  1374. frequencyList = append(frequencyList, k)
  1375. }
  1376. frequencyList = sortEdbFrequency(frequencyList)
  1377. if len(edbTradeCodeList) > 0 {
  1378. edbTradeCodeStr := `"` + strings.Join(edbTradeCodeList, `","`) + `"`
  1379. edbDataList, err := models.GetEdbDataListByCodes(edbTradeCodeStr)
  1380. if err != nil {
  1381. br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
  1382. br.Msg = "获取失败"
  1383. return
  1384. }
  1385. edbDataMap := make(map[string][]*models.Edbdata, 0)
  1386. for _, edbData := range edbDataList {
  1387. if _, ok := edbDataMap[edbData.TradeCode]; ok {
  1388. edbDataMap[edbData.TradeCode] = append(edbDataMap[edbData.TradeCode], edbData)
  1389. } else {
  1390. tmpEdbDataList := make([]*models.Edbdata, 0)
  1391. tmpEdbDataList = append(tmpEdbDataList, edbData)
  1392. edbDataMap[edbData.TradeCode] = tmpEdbDataList
  1393. }
  1394. }
  1395. myYear := time.Now().Year() //当前年份
  1396. myMonth := time.Now().Format("01") //当前月份
  1397. nowWeekFirstDay := utils.GetNowWeekMonday()
  1398. nowWeekLastDay := utils.GetNowWeekLastDay()
  1399. nowMonthFirstDay := utils.GetNowMonthFirstDay()
  1400. nowMonthLastDay := utils.GetNowMonthLastDay()
  1401. nowQuarterFirstDay := utils.GetNowQuarterFirstDay()
  1402. nowQuarterLastDay := utils.GetNowQuarterLastDay()
  1403. nowHalfYearFirstDay := utils.GetNowHalfYearFirstDay()
  1404. nowHalfYearLastDay := utils.GetNowHalfYearLastDay()
  1405. nowYearFirstDay := utils.GetNowYearFirstDay()
  1406. nowYearLastDay := utils.GetNowYearLastDay()
  1407. for _, edb := range list {
  1408. edb.Status = "未完成"
  1409. if dataList, ok := edbDataMap[edb.TradeCode]; ok {
  1410. edb.DataList = dataList
  1411. dataDtTime, _ := time.ParseInLocation(utils.FormatDate, dataList[0].Dt, time.Now().Location()) //最近的一条数据时间
  1412. switch edb.Frequency {
  1413. case "日度":
  1414. if time.Now().After(dataDtTime.AddDate(0, 0, 1)) {
  1415. edb.ModifyDate = time.Now().Format(utils.FormatDate)
  1416. } else {
  1417. edb.ModifyDate = dataDtTime.AddDate(0, 0, 1).Format(utils.FormatDate)
  1418. edb.Status = "完成"
  1419. }
  1420. case "周度":
  1421. modifyDate := nowWeekLastDay //下次更新日期
  1422. if edb.NoticeTime != "" {
  1423. addDay := 7
  1424. noticeArr := strings.Split(edb.NoticeTime, " ")
  1425. noticeWeek := noticeArr[0]
  1426. switch noticeWeek {
  1427. case "周一":
  1428. addDay = 1
  1429. case "周二":
  1430. addDay = 2
  1431. case "周三":
  1432. addDay = 3
  1433. case "周四":
  1434. addDay = 4
  1435. case "周五":
  1436. addDay = 5
  1437. case "周六":
  1438. addDay = 6
  1439. case "周日":
  1440. addDay = 7
  1441. }
  1442. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  1443. }
  1444. if nowWeekFirstDay.Before(dataDtTime) {
  1445. edb.ModifyDate = modifyDate.AddDate(0, 0, 7).Format(utils.FormatDate)
  1446. edb.Status = "完成"
  1447. } else {
  1448. edb.ModifyDate = modifyDate.Format(utils.FormatDate)
  1449. }
  1450. case "月度":
  1451. addDay := 0
  1452. modifyDate := nowMonthLastDay //下次更新日期
  1453. if edb.NoticeTime != "" {
  1454. strArr := strings.Split(edb.NoticeTime, "日")
  1455. tmpAddDay, tmpErr := strconv.Atoi(strArr[0])
  1456. if tmpErr != nil {
  1457. continue
  1458. }
  1459. addDay = tmpAddDay - 1
  1460. modifyDate = nowMonthFirstDay.AddDate(0, 0, addDay)
  1461. }
  1462. if nowMonthFirstDay.Before(dataDtTime) {
  1463. if int(time.Now().Month()) != int(nowMonthFirstDay.Month()) {
  1464. edb.ModifyDate = nowMonthLastDay.Format(utils.FormatDate)
  1465. } else {
  1466. month := nowMonthLastDay.Month() + 1
  1467. edb.ModifyDate = time.Date(nowMonthLastDay.Year(), month, 1, 0, 0, 0, 0, modifyDate.Location()).AddDate(0, 1, -1).Format(utils.FormatDate)
  1468. }
  1469. edb.Status = "完成"
  1470. } else {
  1471. edb.ModifyDate = modifyDate.Format(utils.FormatDate)
  1472. }
  1473. case "季度":
  1474. if nowQuarterFirstDay.After(dataDtTime) {
  1475. edb.ModifyDate = nowQuarterLastDay.Format(utils.FormatDate)
  1476. } else {
  1477. edb.ModifyDate = nowQuarterLastDay.AddDate(0, 3, 0).Format(utils.FormatDate)
  1478. edb.Status = "完成"
  1479. }
  1480. case "半年度":
  1481. if nowHalfYearFirstDay.After(dataDtTime) {
  1482. edb.ModifyDate = nowHalfYearLastDay.Format(utils.FormatDate)
  1483. } else {
  1484. month := nowHalfYearLastDay.Month() + 1
  1485. edb.ModifyDate = time.Date(nowMonthLastDay.Year(), month, 1, 0, 0, 0, 0, nowHalfYearLastDay.Location()).AddDate(0, 6, -1).Format(utils.FormatDate)
  1486. edb.Status = "完成"
  1487. }
  1488. case "年度":
  1489. if nowYearFirstDay.After(dataDtTime) {
  1490. edb.ModifyDate = nowYearLastDay.Format(utils.FormatDate)
  1491. } else {
  1492. edb.ModifyDate = nowYearLastDay.AddDate(1, 0, 0).Format(utils.FormatDate)
  1493. edb.Status = "完成"
  1494. }
  1495. }
  1496. } else {
  1497. edb.DataList = make([]*models.Edbdata, 0)
  1498. switch edb.Frequency {
  1499. case "日度":
  1500. edb.ModifyDate = time.Now().Format(utils.FormatDate)
  1501. case "周度":
  1502. modifyDate := nowWeekLastDay //下次更新日期
  1503. if edb.NoticeTime != "" {
  1504. addDay := 7
  1505. noticeArr := strings.Split(edb.NoticeTime, " ")
  1506. noticeWeek := noticeArr[0]
  1507. switch noticeWeek {
  1508. case "周一":
  1509. addDay = 1
  1510. case "周二":
  1511. addDay = 2
  1512. case "周三":
  1513. addDay = 3
  1514. case "周四":
  1515. addDay = 4
  1516. case "周五":
  1517. addDay = 5
  1518. case "周六":
  1519. addDay = 6
  1520. case "周日":
  1521. addDay = 7
  1522. }
  1523. modifyDate = modifyDate.AddDate(0, 0, addDay-7)
  1524. }
  1525. edb.ModifyDate = modifyDate.Format(utils.FormatDate)
  1526. case "月度":
  1527. if edb.NoticeTime != "" {
  1528. var modifyDateStr string
  1529. strArr := strings.Split(edb.NoticeTime, "日")
  1530. modifyDateStr = strconv.Itoa(myYear) + "-" + myMonth + "-" + strArr[0]
  1531. edb.ModifyDate = modifyDateStr
  1532. } else {
  1533. edb.ModifyDate = nowMonthFirstDay.AddDate(0, 1, -1).Format(utils.FormatDate)
  1534. }
  1535. case "季度":
  1536. edb.ModifyDate = utils.GetNowQuarterLastDay().Format(utils.FormatDate)
  1537. case "半年度":
  1538. edb.ModifyDate = utils.GetNowHalfYearLastDay().Format(utils.FormatDate)
  1539. case "年度":
  1540. edb.ModifyDate = utils.GetNowYearLastDay().Format(utils.FormatDate)
  1541. }
  1542. }
  1543. }
  1544. }
  1545. resp := models.TargetItemListResp{
  1546. List: list,
  1547. FrequencyList: frequencyList,
  1548. }
  1549. br.Ret = 200
  1550. br.Success = true
  1551. br.Msg = "获取成功"
  1552. br.Data = resp
  1553. }
  1554. // @router /target/item_name_list [get]
  1555. func (this *TargetController) TargetItemNameList() {
  1556. br := new(models.BaseResponse).Init()
  1557. defer func() {
  1558. this.Data["json"] = br
  1559. this.ServeJSON()
  1560. }()
  1561. sysUser := this.SysUser
  1562. if sysUser == nil {
  1563. br.Msg = "请重新登录"
  1564. return
  1565. }
  1566. frequency := this.GetString("Frequency")
  1567. keyword := this.GetString("Keyword")
  1568. tradeCode := this.GetString("TradeCode")
  1569. userId := sysUser.AdminId
  1570. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  1571. userId = 0
  1572. }
  1573. classifyIdStrList, err := data.GetEdbClassifyListByAdminId(int64(userId))
  1574. if err != nil {
  1575. br.Msg = "获取分类数据失败"
  1576. return
  1577. }
  1578. classifyId, _ := this.GetInt("ClassifyId")
  1579. edbShowType, _ := this.GetInt("EdbShowType")
  1580. list, err := models.GetTargetItemList(classifyId, edbShowType, frequency, keyword, tradeCode, classifyIdStrList)
  1581. if err != nil {
  1582. br.ErrMsg = "获取失败,Err:" + err.Error()
  1583. br.Msg = "获取失败"
  1584. return
  1585. }
  1586. resp := models.TargetItemListResp{
  1587. List: list,
  1588. }
  1589. br.Ret = 200
  1590. br.Success = true
  1591. br.Msg = "获取成功"
  1592. br.Data = resp
  1593. }
  1594. // @router /data/batch_delete_by_date [post]
  1595. func (this *TargetController) BatchDataDeleteByDate() {
  1596. br := new(models.BaseResponse).Init()
  1597. defer func() {
  1598. this.Data["json"] = br
  1599. this.ServeJSON()
  1600. }()
  1601. var req models.BatchDataDeleteReq
  1602. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1603. if err != nil {
  1604. br.Msg = "参数解析异常!"
  1605. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1606. return
  1607. }
  1608. if len(req.TradeCodeList) <= 0 {
  1609. br.Msg = "至少传入一个指标编码!"
  1610. return
  1611. }
  1612. if req.CreateDate == "" {
  1613. br.Msg = "请选择日期"
  1614. return
  1615. }
  1616. TradeCodeStr := `"` + strings.Join(req.TradeCodeList, `","`) + `"`
  1617. err = models.BatchDeleteEdbDataByDate(TradeCodeStr, req.CreateDate, this.SysUser.AdminId)
  1618. if err != nil {
  1619. br.Msg = "删除失败!"
  1620. br.ErrMsg = "删除失败,Err:" + err.Error()
  1621. return
  1622. }
  1623. br.Ret = 200
  1624. br.Success = true
  1625. br.Msg = "删除成功"
  1626. }
  1627. // @router /data/batch_delete [post]
  1628. func (this *TargetController) BatchDataDeleteByTradeCode() {
  1629. br := new(models.BaseResponse).Init()
  1630. defer func() {
  1631. this.Data["json"] = br
  1632. this.ServeJSON()
  1633. }()
  1634. var req models.TargetDeleteReq
  1635. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1636. if err != nil {
  1637. br.Msg = "参数解析异常!"
  1638. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1639. return
  1640. }
  1641. if req.TradeCode == "" {
  1642. br.Msg = "指标编码不可为空"
  1643. return
  1644. }
  1645. err = models.BatchDeleteEdbData(req.TradeCode, this.SysUser.AdminId)
  1646. if err != nil {
  1647. br.Msg = "删除失败!"
  1648. br.ErrMsg = "删除失败,Err:" + err.Error()
  1649. return
  1650. }
  1651. br.Ret = 200
  1652. br.Success = true
  1653. br.Msg = "删除成功"
  1654. }
  1655. // @router /target/template [get]
  1656. func (this *TargetCommonController) EdbTemplateDownload() {
  1657. br := new(models.BaseResponse).Init()
  1658. defer func() {
  1659. this.Data["json"] = br
  1660. this.ServeJSON()
  1661. }()
  1662. this.Ctx.Output.Download("./static/指标导入模板.xlsx", "指标导入模板.xlsx")
  1663. br.Ret = 200
  1664. br.Success = true
  1665. br.Msg = "下载成功"
  1666. }
  1667. // @router /target/import [post]
  1668. func (this *TargetController) ImportTarget() {
  1669. br := new(models.BaseResponse).Init()
  1670. defer func() {
  1671. this.Data["json"] = br
  1672. this.ServeJSON()
  1673. }()
  1674. sysUser := this.SysUser
  1675. if sysUser == nil {
  1676. br.Msg = "请重新登录"
  1677. return
  1678. }
  1679. file, _, err := this.GetFile("ImportTargetFile")
  1680. if err != nil {
  1681. br.Msg = "获取文件失败"
  1682. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  1683. return
  1684. }
  1685. path := "./static/手工指标数据导入_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1686. defer func() {
  1687. file.Close()
  1688. os.Remove(path)
  1689. }()
  1690. err = this.SaveToFile("ImportTargetFile", path)
  1691. if err != nil {
  1692. br.Msg = "文件保存失败"
  1693. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  1694. return
  1695. }
  1696. xlFile, err := xlsx.OpenFile(path)
  1697. if err != nil {
  1698. fmt.Println(err.Error())
  1699. br.Msg = "excel打开失败"
  1700. br.ErrMsg = "excel打开失败,Err:" + err.Error()
  1701. return
  1702. }
  1703. classifyMap := make(map[string]*models.EdbdataClassify)
  1704. userId := sysUser.AdminId
  1705. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  1706. userId = 0
  1707. }
  1708. classifyList, err := models.GetEdbdataClassify(int64(userId))
  1709. for _, classify := range classifyList {
  1710. if classify.Child != nil {
  1711. for _, child := range classify.Child {
  1712. classifyMap[child.ClassifyName] = child
  1713. }
  1714. }
  1715. }
  1716. failDatas := make([]*models.EdbImportFail, 0)
  1717. for _, sheet := range xlFile.Sheets {
  1718. fmt.Println("sheet name: ", sheet.Name)
  1719. maxRow := sheet.MaxRow
  1720. fmt.Println("maxRow:", maxRow)
  1721. fmt.Println("maxRow")
  1722. for i := 0; i < maxRow; i++ {
  1723. if i > 1 {
  1724. row := sheet.Row(i)
  1725. cells := row.Cells
  1726. if len(cells) >= 4 {
  1727. cell1 := cells[0].Value //指标名称
  1728. secName := strings.Trim(cell1, " ")
  1729. if secName == "" { //过滤空白行
  1730. continue
  1731. }
  1732. cell2 := cells[1].Value
  1733. frequency := strings.Trim(cell2, " ") //频度
  1734. classifyName := cells[2].Value //分类
  1735. classifyName = strings.Trim(classifyName, " ")
  1736. unit := cells[3].Value //单位
  1737. unit = strings.Trim(unit, " ")
  1738. classify, ok := classifyMap[classifyName]
  1739. if !ok {
  1740. failItem := &models.EdbImportFail{
  1741. ClassifyName: classifyName,
  1742. Frequency: frequency,
  1743. SecName: secName,
  1744. Unit: unit,
  1745. Remark: "分类不存在",
  1746. SysUserId: strconv.Itoa(sysUser.AdminId),
  1747. }
  1748. failDatas = append(failDatas, failItem)
  1749. continue
  1750. }
  1751. tradeCode, err := models.GetMaxTradeCode()
  1752. if err != nil {
  1753. failItem := &models.EdbImportFail{
  1754. ClassifyName: classifyName,
  1755. Frequency: frequency,
  1756. SecName: secName,
  1757. Unit: unit,
  1758. Remark: "获取数据失败",
  1759. SysUserId: strconv.Itoa(sysUser.AdminId),
  1760. }
  1761. failDatas = append(failDatas, failItem)
  1762. continue
  1763. }
  1764. if tradeCode == "" {
  1765. failItem := &models.EdbImportFail{
  1766. ClassifyName: classifyName,
  1767. Frequency: frequency,
  1768. SecName: secName,
  1769. Unit: unit,
  1770. Remark: "数据异常",
  1771. SysUserId: strconv.Itoa(sysUser.AdminId),
  1772. }
  1773. failDatas = append(failDatas, failItem)
  1774. continue
  1775. }
  1776. maxTradeCode, err := utils.GetMaxTradeCode(tradeCode)
  1777. if err != nil {
  1778. failItem := &models.EdbImportFail{
  1779. ClassifyName: classifyName,
  1780. Frequency: frequency,
  1781. SecName: secName,
  1782. Unit: unit,
  1783. Remark: "数据异常",
  1784. SysUserId: strconv.Itoa(sysUser.AdminId),
  1785. }
  1786. failDatas = append(failDatas, failItem)
  1787. continue
  1788. }
  1789. if maxTradeCode == "" {
  1790. failItem := &models.EdbImportFail{
  1791. ClassifyName: classifyName,
  1792. Frequency: frequency,
  1793. SecName: secName,
  1794. Unit: unit,
  1795. Remark: "编码为空,请联系技术",
  1796. SysUserId: strconv.Itoa(sysUser.AdminId),
  1797. }
  1798. failDatas = append(failDatas, failItem)
  1799. continue
  1800. }
  1801. if secName == "" {
  1802. failItem := &models.EdbImportFail{
  1803. ClassifyName: classifyName,
  1804. Frequency: frequency,
  1805. SecName: secName,
  1806. Unit: unit,
  1807. Remark: "指标名称不能为空",
  1808. SysUserId: strconv.Itoa(sysUser.AdminId),
  1809. }
  1810. failDatas = append(failDatas, failItem)
  1811. continue
  1812. }
  1813. item, err := models.GetEdbinfoBySecName(secName)
  1814. if err != nil && !utils.IsErrNoRow(err) {
  1815. failItem := &models.EdbImportFail{
  1816. ClassifyName: classifyName,
  1817. Frequency: frequency,
  1818. SecName: secName,
  1819. Unit: unit,
  1820. Remark: "获取信息失败",
  1821. SysUserId: strconv.Itoa(sysUser.AdminId),
  1822. }
  1823. failDatas = append(failDatas, failItem)
  1824. continue
  1825. }
  1826. if item != nil {
  1827. failItem := &models.EdbImportFail{
  1828. ClassifyName: classifyName,
  1829. Frequency: frequency,
  1830. SecName: secName,
  1831. Unit: unit,
  1832. Remark: "指标已存在",
  1833. SysUserId: strconv.Itoa(sysUser.AdminId),
  1834. }
  1835. failDatas = append(failDatas, failItem)
  1836. continue
  1837. }
  1838. err = models.AddEdbinfo(maxTradeCode, secName, unit, "手动", frequency, "", classify.ClassifyId, sysUser.AdminId, sysUser.RealName)
  1839. if err != nil {
  1840. failItem := &models.EdbImportFail{
  1841. ClassifyName: classifyName,
  1842. Frequency: frequency,
  1843. SecName: secName,
  1844. Unit: unit,
  1845. Remark: "导入失败",
  1846. SysUserId: strconv.Itoa(sysUser.AdminId),
  1847. }
  1848. failDatas = append(failDatas, failItem)
  1849. continue
  1850. }
  1851. err = models.AddEdbinfoUser(maxTradeCode, sysUser.Mobile)
  1852. if err != nil {
  1853. failItem := &models.EdbImportFail{
  1854. ClassifyName: classifyName,
  1855. Frequency: frequency,
  1856. SecName: secName,
  1857. Unit: unit,
  1858. Remark: "创建账号关系失败",
  1859. SysUserId: strconv.Itoa(sysUser.AdminId),
  1860. }
  1861. failDatas = append(failDatas, failItem)
  1862. continue
  1863. }
  1864. }
  1865. }
  1866. }
  1867. }
  1868. resp := new(models.EdbdataImportResp)
  1869. if len(failDatas) > 0 {
  1870. _ = models.DelEdbImportFail(sysUser.AdminId)
  1871. failContents := make([]string, 0)
  1872. for _, v := range failDatas {
  1873. failContents = append(failContents, fmt.Sprint(v.SecName, "导入失败:", v.Remark))
  1874. err = models.AddEdbImportFail(v)
  1875. if err != nil {
  1876. go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
  1877. }
  1878. }
  1879. go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败,Err:"+err.Error(), 3)
  1880. resp.Status = 1
  1881. resp.Msg = "存在部分导入失败"
  1882. } else {
  1883. resp.Status = 0
  1884. }
  1885. br.Ret = 200
  1886. br.Success = true
  1887. br.Msg = "导入成功"
  1888. br.Data = resp
  1889. }
  1890. // @router /import_target/failList [get]
  1891. func (this *TargetController) ImportTargetFailListDownload() {
  1892. br := new(models.BaseResponse).Init()
  1893. defer func() {
  1894. this.Data["json"] = br
  1895. this.ServeJSON()
  1896. }()
  1897. sysUser := this.SysUser
  1898. if sysUser == nil {
  1899. br.Msg = "请重新登录"
  1900. return
  1901. }
  1902. item, err := models.GetEdbImportFailList(sysUser.AdminId)
  1903. if err != nil {
  1904. br.Msg = "获取数据失败"
  1905. br.Msg = "获取数据失败,Err:" + err.Error()
  1906. return
  1907. }
  1908. xlsxFile := xlsx.NewFile()
  1909. sheet, err := xlsxFile.AddSheet("导入失败数据")
  1910. headRow := sheet.AddRow()
  1911. headRow.AddCell().SetValue("指标名称")
  1912. headRow.AddCell().SetValue("频度")
  1913. headRow.AddCell().SetValue("品种分类")
  1914. headRow.AddCell().SetValue("单位")
  1915. headRow.AddCell().SetValue("备注")
  1916. for _, v := range item {
  1917. row := sheet.AddRow()
  1918. row.AddCell().SetValue(v.SecName)
  1919. row.AddCell().SetValue(v.Frequency)
  1920. row.AddCell().SetValue(v.ClassifyName)
  1921. row.AddCell().SetValue(v.Unit)
  1922. row.AddCell().SetValue(v.Remark)
  1923. }
  1924. fileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1925. savePath := "./static/" + fileName
  1926. err = xlsxFile.Save(savePath)
  1927. if err != nil {
  1928. br.Msg = "文件保存失败"
  1929. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  1930. return
  1931. }
  1932. defer func() {
  1933. os.Remove(savePath)
  1934. }()
  1935. this.Ctx.Output.Download(savePath, "失败列表.xlsx")
  1936. }
  1937. // @router /target/excel_style/add [post]
  1938. func (this *TargetController) ExcelDataAdd() {
  1939. br := new(models.BaseResponse).Init()
  1940. var err error
  1941. errs := make([]string, 0)
  1942. defer func() {
  1943. if len(errs) > 0 {
  1944. go alarm_msg.SendAlarmMsg("新增复制EXCEL数据 新增或修改数据失败,Err:"+strings.Join(errs, ","), 3)
  1945. }
  1946. this.Data["json"] = br
  1947. this.ServeJSON()
  1948. }()
  1949. sysUser := this.SysUser
  1950. if sysUser == nil {
  1951. br.Msg = "请重新登录"
  1952. return
  1953. }
  1954. var req data_manage.ExcelStyleReq
  1955. err = json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1956. if err != nil {
  1957. br.Msg = "参数解析异常!"
  1958. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1959. return
  1960. }
  1961. targetMap := make(map[string]*models.Edbinfo)
  1962. var cellDataList []map[string]interface{}
  1963. var dataList [][]map[string]interface{}
  1964. for _, item := range req.CellData {
  1965. v := item.(map[string]interface{})
  1966. cellDataList = append(cellDataList, v)
  1967. }
  1968. valueIndex := 0
  1969. var secNameList []string
  1970. var frequencyList []string
  1971. var unitList []string
  1972. var dateList []string
  1973. dateValueMap := make(map[string]map[int]string)
  1974. for i := range req.Data {
  1975. if i > 4 {
  1976. if req.Data[i] == nil || req.Data[i][0] == nil {
  1977. continue
  1978. }
  1979. dateMap := req.Data[i][0].(map[string]interface{})
  1980. _, ok := dateMap["m"]
  1981. if ok {
  1982. dateList = append(dateList, dateMap["m"].(string))
  1983. } else {
  1984. continue
  1985. }
  1986. }
  1987. for j, dataArr := range req.Data[i] {
  1988. if dataArr == nil {
  1989. continue
  1990. }
  1991. dataMap := dataArr.(map[string]interface{})
  1992. if dataList == nil {
  1993. dataList = make([][]map[string]interface{}, len(req.Data))
  1994. }
  1995. if dataList[i] == nil {
  1996. dataList[i] = make([]map[string]interface{}, len(req.Data[i]))
  1997. }
  1998. dataList[i][j] = dataMap
  1999. v, _ := dataMap["m"]
  2000. if v == "指标名称" {
  2001. for nameIndex := 1; nameIndex < len(req.Data[i]); nameIndex++ {
  2002. if req.Data[i][nameIndex] == nil {
  2003. break
  2004. }
  2005. secNameMap := req.Data[i][nameIndex].(map[string]interface{})
  2006. secName, _ := secNameMap["m"]
  2007. if secName == nil {
  2008. continue
  2009. }
  2010. secNameList = append(secNameList, secNameMap["m"].(string))
  2011. }
  2012. }
  2013. if v == "频度" {
  2014. for nameIndex := 1; nameIndex < len(req.Data[i]); nameIndex++ {
  2015. if req.Data[i][nameIndex] == nil {
  2016. break
  2017. }
  2018. frequencyMap := req.Data[i][nameIndex].(map[string]interface{})
  2019. frequency, _ := frequencyMap["m"]
  2020. if frequency == nil {
  2021. continue
  2022. }
  2023. frequencyList = append(frequencyList, frequencyMap["m"].(string))
  2024. }
  2025. }
  2026. if v == "单位" {
  2027. for nameIndex := 1; nameIndex < len(req.Data[i]); nameIndex++ {
  2028. if req.Data[i][nameIndex] == nil {
  2029. break
  2030. }
  2031. unitMap := req.Data[i][nameIndex].(map[string]interface{})
  2032. unit, _ := unitMap["m"]
  2033. if unit == nil {
  2034. continue
  2035. }
  2036. unitList = append(unitList, unitMap["m"].(string))
  2037. }
  2038. }
  2039. valueListMap := make(map[int]string)
  2040. if i > 4 {
  2041. for index := 1; index < len(req.Data[i]); index++ {
  2042. if req.Data[i][index] == nil {
  2043. continue
  2044. }
  2045. valueMap := req.Data[i][index].(map[string]interface{})
  2046. value, _ := valueMap["m"]
  2047. if value == nil {
  2048. continue
  2049. }
  2050. valueListMap[index] = valueMap["m"].(string)
  2051. if ct, ok := valueMap["ct"].(map[string]interface{}); ok {
  2052. fa, _ := ct["fa"]
  2053. if fa == "#,##0.000" {
  2054. value = valueMap["v"]
  2055. floatValue := valueMap["v"].(float64)
  2056. valueListMap[index] = strconv.FormatFloat(floatValue, 'f', -1, 64)
  2057. }
  2058. }
  2059. }
  2060. dateMap := req.Data[i][0].(map[string]interface{})
  2061. date := dateMap["m"].(string)
  2062. dateValueMap[date] = valueListMap
  2063. }
  2064. }
  2065. valueIndex++
  2066. }
  2067. edbCodeDataMap := make(map[string]map[string]string)
  2068. addEdbTradeMap := make(map[string]bool)
  2069. updateEdbTradeMap := make(map[string]bool)
  2070. updateDataTradeMap := make(map[string]bool)
  2071. defer func() {
  2072. go func(addEdbTradeMap, updateEdbTradeMap, updateDataTradeMap map[string]bool) {
  2073. addRecordList := make([]*models.EdbinfoOpRecord, 0)
  2074. for tradeCode := range addEdbTradeMap {
  2075. addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
  2076. TradeCode: tradeCode,
  2077. Remark: "创建指标",
  2078. UserId: sysUser.AdminId,
  2079. UserName: sysUser.RealName,
  2080. CreateTime: time.Now(),
  2081. })
  2082. }
  2083. for tradeCode := range updateEdbTradeMap {
  2084. addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
  2085. TradeCode: tradeCode,
  2086. Remark: "编辑指标",
  2087. UserId: sysUser.AdminId,
  2088. UserName: sysUser.RealName,
  2089. CreateTime: time.Now(),
  2090. })
  2091. }
  2092. for tradeCode := range updateDataTradeMap {
  2093. addRecordList = append(addRecordList, &models.EdbinfoOpRecord{
  2094. TradeCode: tradeCode,
  2095. Remark: "更新数据",
  2096. UserId: sysUser.AdminId,
  2097. UserName: sysUser.RealName,
  2098. CreateTime: time.Now(),
  2099. })
  2100. }
  2101. if len(addRecordList) > 0 {
  2102. obj := models.EdbinfoOpRecord{}
  2103. _ = obj.MulCreate(addRecordList)
  2104. }
  2105. if utils.Re == nil {
  2106. for tradeCode := range updateDataTradeMap {
  2107. data.ModifyManualEdbMaxMinDate(tradeCode)
  2108. err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, tradeCode)
  2109. if err != nil {
  2110. fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
  2111. }
  2112. }
  2113. }
  2114. }(addEdbTradeMap, updateEdbTradeMap, updateDataTradeMap)
  2115. }()
  2116. userId := sysUser.AdminId
  2117. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  2118. userId = 0
  2119. }
  2120. classifyIdList, err := data.GetUserManualClassifyIdList(userId)
  2121. if err != nil {
  2122. br.Msg = "获取分类数据失败"
  2123. br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
  2124. return
  2125. }
  2126. if !utils.InArrayByInt(classifyIdList, req.ClassifyId) {
  2127. br.Msg = "无权访问"
  2128. br.ErrMsg = "无权访问"
  2129. br.IsSendEmail = false
  2130. return
  2131. }
  2132. {
  2133. for i := 0; i < len(secNameList); i++ {
  2134. secName := secNameList[i]
  2135. frequency := frequencyList[i]
  2136. if len(unitList) < len(secNameList) {
  2137. br.Msg = "单位填写异常!"
  2138. br.ErrMsg = "单位填写异常!"
  2139. return
  2140. }
  2141. unit := unitList[i]
  2142. for j, s := range dateList {
  2143. var closeVal string
  2144. valueMap, ok := dateValueMap[s]
  2145. if ok {
  2146. createDate := dateList[j]
  2147. closeVal, ok = valueMap[i+1]
  2148. target, ok2 := targetMap[secName]
  2149. if !ok2 {
  2150. tmpTarget, err := models.GetTargetBySecName(secName)
  2151. if err != nil {
  2152. if utils.IsErrNoRow(err) {
  2153. tmpErr := data.AddEdbInfo(secName, unit, frequency, "", sysUser.Mobile, req.ClassifyId, sysUser.AdminId, sysUser.RealName)
  2154. if tmpErr != nil {
  2155. utils.FileLogData.Error("AddEdbInfo err :%s", tmpErr.Error())
  2156. continue
  2157. }
  2158. tmpTarget, tmpErr := models.GetTargetBySecName(secName)
  2159. target = tmpTarget
  2160. targetMap[secName] = target
  2161. addEdbTradeMap[target.TradeCode] = true
  2162. } else {
  2163. fmt.Println("导入数据 获取指标:Err:" + err.Error())
  2164. }
  2165. } else {
  2166. target = tmpTarget
  2167. targetMap[secName] = target
  2168. }
  2169. }
  2170. if !utils.InArrayByInt(classifyIdList, target.ClassifyId) {
  2171. continue
  2172. }
  2173. if frequency != target.Frequency || unit != target.Unit || req.ClassifyId != target.ClassifyId {
  2174. fmt.Println("更新指标频度或单位")
  2175. err = models.ModifyEdbinfo(target.TradeCode, unit, frequency, req.ClassifyId)
  2176. if err != nil {
  2177. fmt.Println("EditEdbinfo err:", err.Error())
  2178. return
  2179. }
  2180. if _, isAdd := addEdbTradeMap[target.TradeCode]; !isAdd {
  2181. updateEdbTradeMap[target.TradeCode] = true
  2182. }
  2183. }
  2184. tmpDataMap, ok2 := edbCodeDataMap[target.TradeCode]
  2185. if !ok2 {
  2186. tmpDataMap = make(map[string]string)
  2187. targetDataList, tmpErr := models.GetTargetsDataList(target.TradeCode)
  2188. if tmpErr != nil {
  2189. err = tmpErr
  2190. }
  2191. for _, tmpData := range targetDataList {
  2192. tmpDataMap[tmpData.Dt] = tmpData.Close
  2193. }
  2194. edbCodeDataMap[target.TradeCode] = tmpDataMap
  2195. }
  2196. tmpVal, ok3 := tmpDataMap[createDate]
  2197. if ok3 {
  2198. if closeVal != "" {
  2199. tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
  2200. if tmpErr != nil {
  2201. fmt.Println("tmpVal Parse err:", tmpErr.Error())
  2202. err = tmpErr
  2203. continue
  2204. }
  2205. closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
  2206. if tmpErr != nil {
  2207. fmt.Println("closeVal Parse err:", tmpErr.Error())
  2208. err = tmpErr
  2209. continue
  2210. }
  2211. if !tmpValDecimal.Equal(closeValDecimal) {
  2212. fmt.Println("更新数值")
  2213. err = models.ModifyTargetsDataByImport(target.TradeCode, createDate, closeVal)
  2214. if err != nil {
  2215. fmt.Println("ModifyTargetsDataByImport err:", err.Error())
  2216. errs = append(errs, err.Error())
  2217. }
  2218. updateDataTradeMap[target.TradeCode] = true
  2219. }
  2220. } else {
  2221. fmt.Println("删除数值")
  2222. err = models.DeleteTargetsDataByImport(target.TradeCode, createDate)
  2223. if err != nil {
  2224. fmt.Println("DeleteTargetsDataByImport err:", err.Error())
  2225. errs = append(errs, err.Error())
  2226. }
  2227. updateDataTradeMap[target.TradeCode] = true
  2228. }
  2229. } else { //数据不存在,进行新增操作
  2230. if target.TradeCode != "" && createDate != "" && closeVal != "" {
  2231. fmt.Println("新增")
  2232. models.AddTargetsDataByImport(target.TradeCode, createDate, closeVal)
  2233. if err != nil {
  2234. fmt.Println("AddTargetsDataByImport err:", err.Error())
  2235. errs = append(errs, err.Error())
  2236. }
  2237. tmpDataMap[createDate] = closeVal
  2238. edbCodeDataMap[target.TradeCode] = tmpDataMap
  2239. updateDataTradeMap[target.TradeCode] = true
  2240. }
  2241. }
  2242. }
  2243. }
  2244. }
  2245. }
  2246. br.Ret = 200
  2247. br.Success = true
  2248. br.Msg = "获取成功"
  2249. }
  2250. // @router /target/excel_style/edit [get]
  2251. func (this *TargetController) ExcelDataEdit() {
  2252. br := new(models.BaseResponse).Init()
  2253. defer func() {
  2254. this.Data["json"] = br
  2255. this.ServeJSON()
  2256. }()
  2257. sysUser := this.SysUser
  2258. if sysUser == nil {
  2259. br.Msg = "请重新登录"
  2260. return
  2261. }
  2262. classifyId, _ := this.GetInt("ClassifyId")
  2263. frequency := this.GetString("Frequency")
  2264. excelId, err := data_manage.GetExcelEdbdataMappingByTradeCode(classifyId, frequency)
  2265. if err != nil {
  2266. br.Msg = "查找关联表失败"
  2267. br.ErrMsg = "GetExcelEdbdataMappingByTradeCode,Err:" + err.Error()
  2268. return
  2269. }
  2270. item, err := data_manage.GetExcelStyleById(*excelId)
  2271. if err != nil {
  2272. br.Msg = "查找样式失败"
  2273. br.ErrMsg = "GetExcelStyleById,Err:" + err.Error()
  2274. return
  2275. }
  2276. br.Ret = 200
  2277. br.Success = true
  2278. br.Msg = "获取成功"
  2279. br.Data = item
  2280. }
  2281. //// @router /import/data [post]