package controllers import ( "encoding/json" "eta/eta_api/models" "eta/eta_api/models/data_manage" "eta/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "github.com/shopspring/decimal" "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 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) //关键字 keyword := this.GetString("Keyword") //关键字 classifyId, _ := this.GetInt("ClassifyId", 0) 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) } // 所属分类 if classifyId > 0 { condition += ` AND a.classify_id = ? ` pars = append(pars, classifyId) } 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 = "保存成功" }