12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/services/data"
- etaTrialService "eta/eta_api/services/eta_trial"
- "eta/eta_api/utils"
- "fmt"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/shopspring/decimal"
- "github.com/tealeg/xlsx"
- "os"
- "strconv"
- "strings"
- "time"
- )
- // EdbDetail
- // @Title 指标列表
- // @Description 指标列表
- // @Param TradeCode query string true "指标编码"
- // @Success 200 {object} models.TargetDetailResp
- // @router /target/edb/detail [get]
- func (this *TargetController) EdbDetail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- // 指标编码
- tradeCode := this.GetString("TradeCode")
- if tradeCode == `` {
- br.Msg = "请输入指标编码"
- br.IsSendEmail = false
- return
- }
- manualEdbInfo, err := models.GetTargetByTradeCode(tradeCode)
- if err != nil {
- br.Msg = "获取指标失败"
- br.ErrMsg = "获取指标失败,err:" + err.Error()
- return
- }
- // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- //获取账户所拥有权限的分类id集合
- count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.ErrMsg = "获取分类数据失败,err:" + err.Error()
- return
- }
- if count <= 0 {
- br.Msg = "无权访问"
- br.ErrMsg = "无权访问"
- br.IsSendEmail = false
- return
- }
- }
- classifyList := make([]*models.EdbdataClassify, 0)
- {
- classify, err := models.GetManualClassifyByClassifyId(manualEdbInfo.ClassifyId)
- if err != nil {
- br.Msg = "获取分类信息失败"
- br.ErrMsg = "获取分类信息失败,err:" + err.Error()
- return
- }
- parentClassify, err := models.GetManualClassifyByClassifyId(classify.ParentId)
- if err != nil {
- br.Msg = "获取分类信息失败"
- br.ErrMsg = "获取父级分类信息失败,err:" + err.Error()
- return
- }
- classifyList = []*models.EdbdataClassify{
- parentClassify, classify,
- }
- }
- // 明细数据
- {
- dataList, err := models.GetEdbDataListByCode(manualEdbInfo.TradeCode)
- if err != nil {
- br.Msg = "获取明细数据失败"
- br.ErrMsg = "获取明细数据失败,err:" + err.Error()
- return
- }
- manualEdbInfo.DataList = dataList
- }
- resp := models.TargetDetailResp{
- Detail: manualEdbInfo,
- ClassifyList: classifyList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- //br.Data = resp
- br.Data = resp
- }
- // ClassifyEdbList
- // @Title 分类指标列表
- // @Description 指标列表
- // @Param ClassifyId query string true "分类id"
- // @Success 200 {object} models.TargetDetailResp
- // @router /target/classify/edb/list [get]
- func (this *TargetController) ClassifyEdbList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.Ret = 408
- return
- }
- resp := new(models.EdbdataClassifyResp)
- classifyId, _ := this.GetInt("ClassifyId") //分类
- if classifyId <= 0 {
- br.Msg = "请传入分类"
- br.ErrMsg = "请传入分类"
- br.IsSendEmail = false
- }
- var condition string
- var pars []interface{}
- // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- //获取账户所拥有权限的分类id集合
- count, err := models.GetCountManualUserClassify(sysUser.AdminId, classifyId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.ErrMsg = "获取分类数据失败,err:" + err.Error()
- return
- }
- if count <= 0 {
- br.Msg = "无权访问"
- br.ErrMsg = "无权访问"
- br.IsSendEmail = false
- return
- }
- }
- list := make([]*models.EdbdataClassifyList, 0)
- condition += ` AND a.classify_id = ? `
- pars = append(pars, classifyId)
- tmpList, err := models.GetEdbInfoList(condition, pars, 0, 0)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- for _, v := range tmpList {
- list = append(list, &models.EdbdataClassifyList{
- ClassifyId: 0,
- ClassifyName: v.SecName,
- ParentId: v.ClassifyId,
- Child: nil,
- TradeCode: v.TradeCode,
- UniqueCode: utils.MD5(v.TradeCode),
- })
- }
- resp.List = list
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // EdbSearch
- // @Title 指标搜索
- // @Description 指标搜索
- // @Param Keyword query string false "关键字搜索"
- // @Success 200 {object} models.TargetItemListResp
- // @router /target/edb/search [get]
- func (this *TargetController) EdbSearch() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- //关键字
- keyword := this.GetString("Keyword")
- var condition string
- var pars []interface{}
- userId := sysUser.AdminId
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyList, err := models.GetEdbdataClassify(int64(userId))
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyList)
- if num > 0 {
- classifyIdList := make([]int, 0)
- for _, v := range classifyList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- }
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- if keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- }
- total, err := models.GetCountEdbInfoList(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- for _, v := range list {
- v.UniqueCode = utils.MD5(v.TradeCode)
- }
- resp := models.EdbListResp{
- List: list,
- Paging: paging.GetPaging(currentIndex, pageSize, total),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // EdbList
- // @Title 获取研究员指标
- // @Description 获取研究员指标
- // @Param Frequency query string false "频度;枚举值:日度、周度、月度、季度、半年度、年度"
- // @Param Keyword query string false "关键字搜索"
- // @Param TradeCode query string false "指标唯一编码"
- // @Param ClassifyId query int false "分类id"
- // @Param UserId query int false "用户id"
- // @Param EdbShowType query string true "指标展示类型:0,1,2; 0:不管有没有数据,指标都展示出来, 1:只展示有数据的指标, 2:只展示没有数据的指标"
- // @Success 200 {object} models.TargetItemListResp
- // @router /target/edb/list [get]
- func (this *TargetController) EdbList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- // 频度
- frequency := this.GetString("Frequency")
- //关键字
- keyword := this.GetString("Keyword")
- //关键字
- classifyId, _ := this.GetInt("ClassifyId", 0)
- //用户ID
- userId, _ := this.GetInt("UserId", 0)
- var condition string
- var pars []interface{}
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyList, err := models.GetEdbdataClassify(int64(sysUser.AdminId))
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyList)
- if num > 0 {
- classifyIdList := make([]int, 0)
- for _, v := range classifyList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- }
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- // 关键词
- if keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, keyword, 2)
- }
- if frequency != "" {
- condition += ` AND a.frequency = ?`
- pars = append(pars, frequency)
- }
- // 所属分类
- if classifyId > 0 {
- condition += ` AND a.classify_id = ? `
- pars = append(pars, classifyId)
- }
- // 所属用户
- if userId > 0 {
- condition += ` AND a.user_id = ? `
- pars = append(pars, userId)
- }
- total, err := models.GetCountEdbInfoList(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := models.EdbListResp{
- List: list,
- Paging: paging.GetPaging(currentIndex, pageSize, total),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // EditExcelData
- // @Title 根据excel的样式去编辑指标
- // @Description 根据excel的样式去编辑指标
- // @Param request body models.ManualEdbExcelStyleEditReq true "type json string"
- // @Success 200
- // @router /target/edb/excel_style/edit [post]
- func (this *TargetController) EditExcelData() {
- br := new(models.BaseResponse).Init()
- var err error
- errs := make([]string, 0)
- defer func() {
- if len(errs) > 0 {
- utils.FileLog.Info("编辑EXCEL数据 新增或修改数据失败,Err:" + strings.Join(errs, "\n"))
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- var req data_manage.ManualEdbExcelStyleEditReq
- err = json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.TradeCode == `` {
- br.Msg = "指标编码异常!"
- br.ErrMsg = "指标编码异常!"
- return
- }
- if req.Unit == `` {
- br.Msg = "单位填写异常!"
- br.ErrMsg = "单位填写异常!"
- return
- }
- if req.Frequency == `` {
- br.Msg = "频度填写异常!"
- br.ErrMsg = "频度填写异常!"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "品种填写异常!"
- br.ErrMsg = "品种填写异常!"
- return
- }
- // 获取指标信息
- manualEdbInfo, err := models.GetEdbinfoByTradeCode(req.TradeCode)
- if err != nil {
- br.Msg = `找不到该指标`
- br.Msg = `找不到该指标,ERR:` + err.Error()
- return
- }
- // 如果不是超管账号,那么得校验下当前用户是否有该指标的权限
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- //获取账户所拥有权限的分类id集合
- count, err := models.GetCountManualUserClassify(sysUser.AdminId, manualEdbInfo.ClassifyId)
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.ErrMsg = "获取分类数据失败,err:" + err.Error()
- return
- }
- if count <= 0 {
- br.Msg = "无权访问"
- br.ErrMsg = "无权访问"
- br.IsSendEmail = false
- return
- }
- }
- dateValueMap := make(map[string]string)
- //取到所有数据
- for i := range req.Data {
- // 过滤第一行
- if i == 0 {
- continue
- }
- if req.Data[i] == nil {
- continue
- }
- tmpData := req.Data[i]
- // 少于两列,说明数据是异常的,得过滤
- if len(tmpData) < 2 {
- continue
- }
- var tmpDate string
- //先拿下日期列表
- {
- dateMap := tmpData[0].(map[string]interface{})
- _, ok := dateMap["m"]
- if !ok {
- continue
- }
- tmpDate = dateMap["m"].(string)
- }
- // 数据处理
- {
- if tmpData[1] == nil {
- continue
- }
- valueMap := tmpData[1].(map[string]interface{})
- value, _ := valueMap["m"]
- if value == nil {
- continue
- }
- dateValueMap[tmpDate] = valueMap["m"].(string)
- // 09-27 千位分隔符时用 "m" 取字符串存数据库会把逗号当小数点,现在换用 "v" 直接取数字再转为字符串,看看会不会有问题
- if ct, ok := valueMap["ct"].(map[string]interface{}); ok {
- fa, _ := ct["fa"]
- if fa == "#,##0.000" {
- value = valueMap["v"]
- floatValue := valueMap["v"].(float64)
- dateValueMap[tmpDate] = strconv.FormatFloat(floatValue, 'f', -1, 64)
- }
- }
- }
- }
- //操作指标,新增指标及数据等
- {
- secName := strings.TrimSpace(req.Name)
- frequency := strings.TrimSpace(req.Frequency)
- unit := strings.TrimSpace(req.Unit)
- // 指标基础信息变更
- {
- updateCols := make([]string, 0)
- if manualEdbInfo.SecName != secName {
- updateCols = append(updateCols, "SecName")
- manualEdbInfo.SecName = secName
- }
- if manualEdbInfo.ClassifyId != req.ClassifyId {
- updateCols = append(updateCols, "ClassifyId")
- manualEdbInfo.ClassifyId = req.ClassifyId
- }
- if manualEdbInfo.Frequency != frequency {
- updateCols = append(updateCols, "Frequency")
- manualEdbInfo.Frequency = frequency
- }
- if manualEdbInfo.Unit != unit {
- updateCols = append(updateCols, "Unit")
- manualEdbInfo.Unit = unit
- }
- if len(updateCols) > 0 {
- err = manualEdbInfo.Update(updateCols)
- if err != nil {
- br.Msg = `指标保存失败`
- br.Msg = `指标保存失败,ERR:` + err.Error()
- return
- }
- }
- }
- // 当前已经存在的指标明细数据
- targetDataList, tmpErr := models.GetTargetsDataList(manualEdbInfo.TradeCode)
- if tmpErr != nil {
- err = tmpErr
- }
- existDataMap := make(map[string]string)
- deleteDataMap := make(map[string]string)
- for _, tmpData := range targetDataList {
- existDataMap[tmpData.Dt] = tmpData.Close
- deleteDataMap[tmpData.Dt] = tmpData.Dt
- }
- addDataList := make([]*models.Edbdata, 0)
- for createDate, closeVal := range dateValueMap {
- if createDate == "" || closeVal == "" {
- continue
- }
- //判断数据是否已经存在
- tmpVal, ok3 := existDataMap[createDate]
- // 不存在,那么后面进行插入
- if !ok3 {
- addDataList = append(addDataList, &models.Edbdata{
- TradeCode: manualEdbInfo.TradeCode,
- Dt: createDate,
- Close: closeVal,
- ModifyTime: time.Now(),
- })
- continue
- }
- delete(deleteDataMap, createDate)
- // 库里面的数据
- tmpValDecimal, tmpErr := decimal.NewFromString(tmpVal)
- if tmpErr != nil {
- fmt.Println("tmpVal Parse err:", tmpErr.Error())
- err = tmpErr
- continue
- }
- // 用户填写的数据
- closeValDecimal, tmpErr := decimal.NewFromString(closeVal)
- if tmpErr != nil {
- fmt.Println("closeVal Parse err:", tmpErr.Error())
- err = tmpErr
- continue
- }
- if !tmpValDecimal.Equal(closeValDecimal) {
- fmt.Println("更新数值")
- err = models.ModifyTargetsDataByImport(manualEdbInfo.TradeCode, createDate, closeVal)
- if err != nil {
- fmt.Println("ModifyTargetsDataByImport err:", err.Error())
- errs = append(errs, err.Error())
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }
- }
- // 新增指标
- if len(addDataList) > 0 {
- err = models.OnlyMultiAddEdbdata(addDataList)
- if err != nil {
- fmt.Println("MultiAddEdbdata err:", err.Error())
- errs = append(errs, err.Error())
- }
- }
- // 删除不需要的日期数据
- num := len(deleteDataMap)
- if num > 0 {
- deleteDateList := make([]string, 0)
- for date := range deleteDataMap {
- deleteDateList = append(deleteDateList, date)
- }
- err = models.DelEdbdataByCodeAndDateList(manualEdbInfo.TradeCode, deleteDateList)
- if err != nil {
- fmt.Println("DelEdbdataByCodeAndDateList err:", err.Error())
- errs = append(errs, err.Error())
- }
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- }
- // BatchEdbList
- // @Title 获取研究员指标
- // @Description 获取研究员指标
- // @Param request body models.BatchAddEdbReq true "type json string"
- // @Success 200 {object} models.TargetItemListResp
- // @router /target/edb/batch/list [post]
- func (this *TargetController) BatchEdbList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- var req models.BatchManualEdbReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- pageSize := req.PageSize
- currentIndex := req.CurrentIndex
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- req.Keyword = strings.TrimSpace(req.Keyword)
- var condition string
- var pars []interface{}
- // 是否加到指标库
- condition += ` AND a.is_join_edb = ? `
- pars = append(pars, 0)
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyList, err := models.GetEdbdataClassify(int64(sysUser.AdminId))
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyList)
- if num > 0 {
- classifyIdList := make([]int, 0)
- for _, v := range classifyList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- }
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- // 关键词
- if req.Keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
- }
- // 所属分类
- lenFrequency := len(req.FrequencyList)
- if lenFrequency > 0 {
- condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
- pars = append(pars, req.FrequencyList)
- }
- // 所属分类
- lenClassify := len(req.ClassifyIdList)
- if lenClassify > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
- pars = append(pars, req.ClassifyIdList)
- }
- // 所属用户
- lenUser := len(req.UserIdList)
- if lenUser > 0 {
- condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
- pars = append(pars, req.UserIdList)
- }
- total, err := models.GetCountEdbInfoList(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- list, err := models.GetEdbInfoList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := models.EdbListResp{
- List: list,
- Paging: paging.GetPaging(currentIndex, pageSize, total),
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // BatchAddEdbCheck
- // @Title 新增校验
- // @Description 新增校验
- // @Param request body data_manage.BatchManualEdbReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/add/check [post]
- func (this *TargetController) BatchAddEdbCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- // 最大批量添加的数量
- codeMax := 30
- var req models.BatchManualEdbReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- req.Keyword = strings.TrimSpace(req.Keyword)
- var condition string
- var pars []interface{}
- //
- condition += ` AND a.is_join_edb = 0 `
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyList, err := models.GetEdbdataClassify(int64(sysUser.AdminId))
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyList)
- if num > 0 {
- classifyIdList := make([]int, 0)
- for _, v := range classifyList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- }
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- if req.ListAll {
- // 关键词
- if req.Keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
- }
- // 所属分类
- lenFrequency := len(req.FrequencyList)
- if lenFrequency > 0 {
- condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
- pars = append(pars, req.FrequencyList)
- }
- // 所属分类
- lenClassify := len(req.ClassifyIdList)
- if lenClassify > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
- pars = append(pars, req.ClassifyIdList)
- }
- // 所属用户
- lenUser := len(req.UserIdList)
- if lenUser > 0 {
- condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
- pars = append(pars, req.UserIdList)
- }
- lenTradeList := len(req.TradeCodeList)
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- } else {
- lenTradeList := len(req.TradeCodeList)
- if lenTradeList <= 0 {
- br.Msg = "请选择指标"
- br.ErrMsg = "请选择指标"
- return
- }
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- }
- list, err := models.GetEdbInfoList(condition, pars, 0, 0)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if len(list) > codeMax {
- br.Msg = fmt.Sprintf("最多只能选择%d个指标", codeMax)
- return
- }
- br.Data = list
- br.Msg = "校验成功"
- br.Ret = 200
- br.Success = true
- }
- // NameCheck
- // @Title 重名校验
- // @Description 批量新增
- // @Param request body data_manage.AddEdbInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/add/name_check [post]
- func (this *TargetController) NameCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req []*data_manage.AddEdbInfoReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if len(req) == 0 {
- br.Msg = "请选择指标"
- return
- }
- type NameCheckResult struct {
- EdbCode string
- EdbName string
- Exist bool
- }
- indexNames := make([]string, 0)
- resp := make([]*NameCheckResult, 0)
- for _, v := range req {
- v.EdbCode = strings.TrimSpace(v.EdbCode)
- if v.EdbCode == "" {
- br.Msg = "指标ID不可为空"
- return
- }
- v.EdbName = strings.TrimSpace(v.EdbName)
- if v.EdbName == "" {
- br.Msg = "请输入指标名称"
- return
- }
- indexNames = append(indexNames, v.EdbName)
- resp = append(resp, &NameCheckResult{
- EdbCode: v.EdbCode,
- EdbName: v.EdbName,
- })
- }
- // 重名校验
- edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
- return
- }
- nameExists := make(map[string]bool)
- for _, v := range edbList {
- nameExists[v.EdbName] = true
- }
- if len(nameExists) > 0 {
- for _, v := range resp {
- v.Exist = nameExists[v.EdbName]
- }
- }
- br.Data = resp
- br.Msg = "操作成功"
- br.Ret = 200
- br.Success = true
- }
- // BatchAdd
- // @Title 批量新增
- // @Description 批量新增
- // @Param request body data_manage.AddEdbInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/add [post]
- func (this *TargetController) BatchAdd() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- // 最大批量添加的数量
- codeMax := 30
- deleteCache := true
- cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- var req []*data_manage.AddEdbInfoReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if len(req) == 0 {
- br.Msg = "请选择指标"
- return
- }
- if len(req) > codeMax {
- br.Msg = "批量添加指标数量不得超过30个"
- return
- }
- indexNames := make([]string, 0)
- for _, v := range req {
- v.EdbCode = strings.TrimSpace(v.EdbCode)
- if v.EdbCode == "" {
- br.Msg = "指标ID不可为空"
- return
- }
- v.EdbName = strings.TrimSpace(v.EdbName)
- if v.EdbName == "" {
- br.Msg = "请输入指标名称"
- return
- }
- indexNames = append(indexNames, v.EdbName)
- v.Frequency = strings.TrimSpace(v.Frequency)
- if v.Frequency == "" {
- br.Msg = "请选择频度"
- return
- }
- v.Unit = strings.TrimSpace(v.Unit)
- if v.Unit == "" {
- br.Msg = "请输入单位"
- return
- }
- if v.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- }
- // 限定同一时间最多批量新增30个指标
- for _, v := range req {
- var r data.ManualIndexSource2EdbReq
- r.EdbCode = v.EdbCode
- r.EdbName = v.EdbName
- r.Frequency = v.Frequency
- r.Unit = v.Unit
- r.ClassifyId = v.ClassifyId
- r.AdminId = sysUser.AdminId
- r.AdminRealName = sysUser.RealName
- edbInfo, e, errMsg, skip := data.ManualIndexSource2Edb(r, this.Lang)
- if e != nil {
- br.Msg = "操作失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = e.Error()
- return
- }
- if skip {
- continue
- }
- // 试用平台更新用户累计新增指标数
- if utils.BusinessCode == utils.BusinessCodeSandbox {
- go func() {
- adminItem, e := system.GetSysAdminById(sysUser.AdminId)
- if e != nil {
- tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
- utils.FileLog.Info(tips)
- return
- }
- if adminItem.DepartmentName != "ETA试用客户" {
- return
- }
- var ur etaTrialService.EtaTrialUserReq
- ur.Mobile = adminItem.Mobile
- _, _ = etaTrialService.UpdateUserIndexNum(ur)
- }()
- }
- // 新增操作日志
- {
- edbLog := new(data_manage.EdbInfoLog)
- edbLog.EdbInfoId = edbInfo.EdbInfoId
- edbLog.SourceName = edbInfo.SourceName
- edbLog.Source = edbInfo.Source
- edbLog.EdbCode = edbInfo.EdbCode
- edbLog.EdbName = edbInfo.EdbName
- edbLog.ClassifyId = edbInfo.ClassifyId
- edbLog.SysUserId = sysUser.AdminId
- edbLog.SysUserRealName = sysUser.RealName
- edbLog.CreateTime = time.Now()
- edbLog.Content = string(this.Ctx.Input.RequestBody)
- edbLog.Status = "新增指标"
- edbLog.Method = this.Ctx.Input.URI()
- go data_manage.AddEdbInfoLog(edbLog)
- }
- }
- br.Msg = "操作成功"
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- }
- // BatchDelEdbCheck
- // @Title 新增校验
- // @Description 新增校验
- // @Param request body data_manage.BatchManualEdbReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/del/check [post]
- func (this *TargetController) BatchDelEdbCheck() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req models.BatchManualEdbReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- req.Keyword = strings.TrimSpace(req.Keyword)
- var condition string
- var pars []interface{}
- // 就是为了找出已经加入指标库的指标
- condition += ` AND a.is_join_edb = ? `
- pars = append(pars, 1)
- //超管账号可以查看分类下的所有频度数据
- if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
- classifyList, err := models.GetEdbdataClassify(int64(sysUser.AdminId))
- if err != nil {
- br.Msg = "获取分类数据失败"
- br.Msg = "获取拥有的分类数据失败,Err:" + err.Error()
- return
- }
- num := len(classifyList)
- if num > 0 {
- classifyIdList := make([]int, 0)
- for _, v := range classifyList {
- classifyIdList = append(classifyIdList, v.ClassifyId)
- }
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(num) + `) `
- pars = append(pars, classifyIdList)
- }
- }
- if req.ListAll {
- // 关键词
- if req.Keyword != "" {
- condition += ` AND (a.SEC_NAME like ? or a.TRADE_CODE like ? )`
- pars = utils.GetLikeKeywordPars(pars, req.Keyword, 2)
- }
- // 所属分类
- lenFrequency := len(req.FrequencyList)
- if lenFrequency > 0 {
- condition += ` AND a.frequency in (` + utils.GetOrmInReplace(lenFrequency) + `) `
- pars = append(pars, req.FrequencyList)
- }
- // 所属分类
- lenClassify := len(req.ClassifyIdList)
- if lenClassify > 0 {
- condition += ` AND a.classify_id in (` + utils.GetOrmInReplace(lenClassify) + `) `
- pars = append(pars, req.ClassifyIdList)
- }
- // 所属用户
- lenUser := len(req.UserIdList)
- if lenUser > 0 {
- condition += ` AND a.user_id in (` + utils.GetOrmInReplace(lenUser) + `) `
- pars = append(pars, req.UserIdList)
- }
- lenTradeList := len(req.TradeCodeList)
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE not in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- } else {
- lenTradeList := len(req.TradeCodeList)
- if lenTradeList <= 0 {
- br.Msg = "请选择指标"
- br.ErrMsg = "请选择指标"
- return
- }
- // 指标
- if lenTradeList > 0 {
- condition += ` AND a.TRADE_CODE in (` + utils.GetOrmInReplace(lenTradeList) + `) `
- pars = append(pars, req.TradeCodeList)
- }
- }
- list, err := models.GetEdbInfoList(condition, pars, 0, 0)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- br.Data = list
- br.Msg = "校验成功"
- br.Ret = 200
- br.Success = true
- }
- // BatchDel
- // @Title 批量删除
- // @Description 批量新增
- // @Param request body data_manage.AddEdbInfoReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /target/edb/batch/del [post]
- func (this *TargetController) BatchDel() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- // 最大批量添加的数量
- codeMax := 30
- deleteCache := true
- cacheKey := "CACHE_EDB_INFO_BATCH_ADD_MANUAL_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- var req []*data_manage.AddEdbInfoReq
- if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + e.Error()
- return
- }
- if len(req) == 0 {
- br.Msg = "请选择指标"
- return
- }
- if len(req) > codeMax {
- br.Msg = "批量添加指标数量不得超过30个"
- return
- }
- indexNames := make([]string, 0)
- for _, v := range req {
- v.EdbCode = strings.TrimSpace(v.EdbCode)
- if v.EdbCode == "" {
- br.Msg = "指标ID不可为空"
- return
- }
- v.EdbName = strings.TrimSpace(v.EdbName)
- if v.EdbName == "" {
- br.Msg = "请输入指标名称"
- return
- }
- indexNames = append(indexNames, v.EdbName)
- v.Frequency = strings.TrimSpace(v.Frequency)
- if v.Frequency == "" {
- br.Msg = "请选择频度"
- return
- }
- v.Unit = strings.TrimSpace(v.Unit)
- if v.Unit == "" {
- br.Msg = "请输入单位"
- return
- }
- if v.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- }
- // 限定同一时间最多批量新增30个指标
- for _, v := range req {
- var r data.ManualIndexSource2EdbReq
- r.EdbCode = v.EdbCode
- r.EdbName = v.EdbName
- r.Frequency = v.Frequency
- r.Unit = v.Unit
- r.ClassifyId = v.ClassifyId
- r.AdminId = sysUser.AdminId
- r.AdminRealName = sysUser.RealName
- edbInfo, e, errMsg, skip := data.ManualIndexSource2Edb(r, this.Lang)
- if e != nil {
- br.Msg = "操作失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = e.Error()
- return
- }
- if skip {
- continue
- }
- // 试用平台更新用户累计新增指标数
- if utils.BusinessCode == utils.BusinessCodeSandbox {
- go func() {
- adminItem, e := system.GetSysAdminById(sysUser.AdminId)
- if e != nil {
- tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
- utils.FileLog.Info(tips)
- return
- }
- if adminItem.DepartmentName != "ETA试用客户" {
- return
- }
- var ur etaTrialService.EtaTrialUserReq
- ur.Mobile = adminItem.Mobile
- _, _ = etaTrialService.UpdateUserIndexNum(ur)
- }()
- }
- // 新增操作日志
- {
- edbLog := new(data_manage.EdbInfoLog)
- edbLog.EdbInfoId = edbInfo.EdbInfoId
- edbLog.SourceName = edbInfo.SourceName
- edbLog.Source = edbInfo.Source
- edbLog.EdbCode = edbInfo.EdbCode
- edbLog.EdbName = edbInfo.EdbName
- edbLog.ClassifyId = edbInfo.ClassifyId
- edbLog.SysUserId = sysUser.AdminId
- edbLog.SysUserRealName = sysUser.RealName
- edbLog.CreateTime = time.Now()
- edbLog.Content = string(this.Ctx.Input.RequestBody)
- edbLog.Status = "新增指标"
- edbLog.Method = this.Ctx.Input.URI()
- go data_manage.AddEdbInfoLog(edbLog)
- }
- }
- br.Msg = "操作成功"
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- }
- // ImportData
- // @Title 导入数据
- // @Description 导入数据
- // @Param EntryFile query file true "文件"
- // @Success 200 Ret=200 录入成功
- // @router /import/data [post]
- func (this *TargetController) ImportData() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请重新登录"
- return
- }
- file, _, err := this.GetFile("EntryFile")
- if err != nil {
- br.Msg = "获取文件失败"
- br.ErrMsg = "获取文件失败,Err:" + err.Error()
- return
- }
- path := "./static/数据导入_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- defer file.Close()
- err = this.SaveToFile("EntryFile", path)
- if err != nil {
- br.Msg = "文件保存失败"
- br.ErrMsg = "文件保存失败,Err:" + err.Error()
- return
- }
- if utils.RunMode == "debug" {
- defer os.Remove(path)
- }
- xlFile, err := xlsx.OpenFile(path)
- if err != nil {
- fmt.Println(err.Error())
- return
- }
- //超管账号可以查看分类下的所有频度数据
- userId := sysUser.AdminId
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
- userId = 0
- }
- //获取账户所拥有权限的分类id集合
- classifyNameStrList, edbDataClassifyMap, err := data.GetEdbClassifyNameListByAdminId(int64(userId))
- if err != nil {
- br.Msg = "获取分类数据失败"
- return
- }
- //导入成功数量
- successCount := 0
- //导入失败数据
- failDatas := make([]*models.EdbdataImportFail, 0)
- //指标map
- targetMap := make(map[string]*models.Edbinfo)
- defer func() {
- for _, target := range targetMap {
- //结束后,清除掉对应的缓存
- key := "import:edbinfo:data:" + target.TradeCode
- utils.Rc.Delete(key)
- //将该指标的code加入到 “手工数据导入后刷新” 缓存
- if utils.Re == nil {
- err := utils.Rc.LPush(utils.CACHE_IMPORT_MANUAL_DATA, target.TradeCode)
- if err != nil {
- fmt.Println("CACHE_IMPORT_MANUAL_DATA LPush Err:" + err.Error())
- }
- }
- }
- }()
- // 所有的指标数据map
- edbCodeDataMap := make(map[string]map[string]string)
- // 遍历sheet页读取
- for _, sheet := range xlFile.Sheets {
- fmt.Println("sheet name: ", sheet.Name)
- //遍历行读取
- maxRow := sheet.MaxRow
- fmt.Println("maxRow:", maxRow)
- fmt.Println("maxRow")
- for i := 0; i < maxRow; i++ {
- if i == 1 {
- row := sheet.Row(i)
- cells := row.Cells
- if len(cells) < 6 {
- br.ErrMsg = "导入文件异常,请下载最新导入模板文件"
- br.Msg = "导入文件异常,请下载最新导入模板文件"
- return
- }
- templateFail := false
- if cells[0].Value != "品种分类" && cells[0].Value != "Species Category" {
- templateFail = true
- }
- if cells[1].Value != "录入日期" && cells[1].Value != "Input Date" {
- templateFail = true
- }
- if cells[2].Value != "指标名称" && cells[2].Value != "Indicator Name Indicator Name" {
- templateFail = true
- }
- if cells[3].Value != "值" && cells[3].Value != "Value" {
- templateFail = true
- }
- if cells[4].Value != "频度" && cells[4].Value != "Frequency" {
- templateFail = true
- }
- if cells[5].Value != "单位" && cells[5].Value != "Unit" {
- templateFail = true
- }
- if templateFail {
- br.ErrMsg = "导入文件异常,请下载最新导入模板文件"
- br.Msg = "导入文件异常,请下载最新导入模板文件"
- return
- }
- }
- if i > 1 {
- row := sheet.Row(i)
- cells := row.Cells
- if len(cells) >= 6 {
- classifyName := cells[0].Value //分类
- if classifyName == "" { //过滤空白行
- continue
- }
- cell1 := cells[1].Value
- //createDate := utils.ConvertToFormatDay(cell1) //录入日期
- createDate := cell1
- cell2 := cells[2].Value //指标名称
- secName := utils.TrimStr(cell2)
- frequency := cells[4].Value //频度
- unit := cells[5].Value //单位
- closeVal := cells[3].Value //值
- if strings.Contains(closeVal, "#N/A") {
- continue
- }
- //校验表格中的日期格式
- if strings.Contains(createDate, "-") {
- //如果是带有 - 的普通日期格式文本
- _, timeErr := time.Parse("2006-1-2", createDate)
- if timeErr != nil {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "日期格式异常"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
- continue
- }
- } else if strings.Contains(createDate, "/") {
- //如果是带有 / 的普通日期格式文本
- createDateTime, timeErr := time.Parse("2006/1/2", createDate)
- if timeErr != nil {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "日期格式异常"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
- continue
- }
- createDate = createDateTime.Format("2006-01-02")
- } else {
- //可能是excel的日期格式
- _, tmpErr := strconv.Atoi(createDate)
- if tmpErr != nil {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "日期格式异常"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
- continue
- }
- createDate = utils.ConvertToFormatDay(createDate) //录入日期
- }
- closeValFloat, err := cells[3].Float() //值
- if err != nil {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = cells[3].Value
- failItem.Remark = "值类型异常"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- continue
- }
- newDecimal := decimal.NewFromFloat(closeValFloat)
- newDecimal.Round(4)
- closeVal = newDecimal.String()
- if strings.Contains(closeVal, "#N/A") {
- continue
- }
- //closeVal := closeValFloat.
- //判断指标,类型,等数据是否正常
- classifyName = strings.Trim(classifyName, " ")
- frequency = utils.TrimStr(frequency)
- unit = utils.TrimStr(unit)
- // 成功数量超过20个指标,那就不导入了
- if len(targetMap) >= 150 {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "导入指标数量过多"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
- continue
- }
- if !strings.Contains(strings.Join(classifyNameStrList, ","), classifyName) {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "没有该品种分类权限"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- continue
- }
- //fmt.Println(classifyName, createDate, secName, closeVal)
- //获取指标分类信息
- classify, ok := edbDataClassifyMap[classifyName]
- if !ok {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "指标分类不存在"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 获取分类:Err:"+err.Error(), utils.EmailSendToUsers)
- continue
- }
- //判断指标是否存在
- target, ok := targetMap[secName]
- if !ok {
- tmpTarget, err := models.GetTargetBySecName(secName)
- if err != nil {
- //如果是找不到该指标,那么新增指标
- if err.Error() == utils.ErrNoRow() {
- if frequency == "" {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "新增指标失败,频度字段为空"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- continue
- }
- if unit == "" {
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "新增指标失败,单位字段为空"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- continue
- }
- tmpErr := data.AddEdbInfo(secName, unit, frequency, "", sysUser.Mobile, classify.ClassifyId, sysUser.AdminId)
- if tmpErr != nil {
- fmt.Println("line 158")
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "新增指标失败"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- continue
- }
- tmpTarget, tmpErr := models.GetTargetBySecName(secName)
- target = tmpTarget
- targetMap[secName] = target
- } else {
- fmt.Println("导入数据 获取指标:Err:" + err.Error())
- }
- } else {
- target = tmpTarget
- targetMap[secName] = target
- }
- //设置10分钟缓存,不允许其他地方删除
- key := "import:edbinfo:data:" + target.TradeCode
- utils.Rc.SetNX(key, 1, time.Second*600)
- }
- if target == nil {
- fmt.Println("指标不存在")
- failItem := new(models.EdbdataImportFail)
- failItem.SysUserId = strconv.Itoa(sysUser.AdminId)
- failItem.ClassifyName = classifyName
- failItem.CreateDate = createDate
- failItem.SecName = secName
- failItem.Close = closeVal
- failItem.Remark = "指标不存在"
- failItem.Frequency = frequency
- failItem.Unit = unit
- failDatas = append(failDatas, failItem)
- continue
- }
- //更新指标信息
- updateCols := make([]string, 0)
- //更新指标分类
- if target.ClassifyId <= 0 && classify.ClassifyId > 0 {
- target.ClassifyId = classify.ClassifyId
- updateCols = append(updateCols, "ClassifyId")
- }
- if target.Frequency != frequency {
- target.Frequency = frequency
- target.NoticeTime = ""
- updateCols = append(updateCols, "Frequency", "NoticeTime")
- }
- if target.Unit != unit {
- target.Unit = unit
- updateCols = append(updateCols, "Unit")
- }
- if len(updateCols) > 0 {
- _ = target.Update(updateCols)
- }
- // 判断指标数据列表是否已经存在
- tmpDataMap, ok := edbCodeDataMap[target.TradeCode]
- if !ok {
- tmpDataMap = make(map[string]string)
- dataList, tmpErr := models.GetTargetsDataList(target.TradeCode)
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("导入数据"+target.TradeCode+" 获取指标的数据失败,Err:"+err.Error(), 3)
- }
- for _, tmpData := range dataList {
- tmpDataMap[tmpData.Dt] = tmpData.Close
- }
- edbCodeDataMap[target.TradeCode] = tmpDataMap
- }
- //判断数据是否已经存在
- tmpVal, ok := tmpDataMap[createDate]
- //数据已存在,进行更新操作
- if ok {
- if tmpVal != closeVal {
- err = models.ModifyTargetsDataByImport(target.TradeCode, createDate, closeVal)
- if err != nil {
- go alarm_msg.SendAlarmMsg("导入数据 修改数据失败,Err:"+err.Error(), 3)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 修改数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }
- } else { //数据不存在,进行新增操作
- if target.TradeCode != "" && createDate != "" && closeVal != "" {
- models.AddTargetsDataByImport(target.TradeCode, createDate, closeVal)
- if err != nil {
- go alarm_msg.SendAlarmMsg("导入数据 新增数据失败,Err:"+err.Error(), 3)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 新增数据失败:Err:"+err.Error(), utils.EmailSendToUsers)
- }
- tmpDataMap[createDate] = closeVal
- edbCodeDataMap[target.TradeCode] = tmpDataMap
- }
- }
- successCount++
- } else {
- //br.ErrMsg = "请填写导入内容"
- //br.Msg = "请填写导入内容"
- //return
- }
- }
- }
- }
- resp := models.EdbdataImportResp{
- SuccessCount: successCount,
- FailCount: len(failDatas),
- }
- fmt.Println("failDatas:", len(failDatas))
- if len(failDatas) > 0 {
- //先删除导入失败记录
- _ = models.DelEdbDataImportFail(sysUser.AdminId)
- //错误信息字符串切片,最后作为发送邮件通知使用
- failContents := make([]string, 0)
- for _, v := range failDatas {
- failContents = append(failContents, fmt.Sprint(v.SecName, "导入失败:", v.Remark))
- err = models.AddEdbdataImportFail(v)
- if err != nil {
- go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 新增失败记录失败:Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }
- //导入失败的话,最后统一邮件提醒就好啦,不需要每次都去提醒
- go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), 3)
- //go utils.SendEmail(utils.APPNAME+"失败提醒", "导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), utils.EmailSendToUsers)
- if successCount == 0 {
- resp.Status = -1
- resp.Msg = "导入失败"
- } else {
- resp.Status = 1
- resp.Msg = "存在部分导入失败"
- }
- } else {
- resp.Status = 0
- }
- br.Msg = "导入成功"
- br.Ret = 200
- br.Success = true
- br.Data = resp
- }
|