|
|
@@ -0,0 +1,111 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "math"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+
|
|
|
+ jsoniter "github.com/json-iterator/go"
|
|
|
+)
|
|
|
+
|
|
|
+func TransformType2(responseData string) (string, error) {
|
|
|
+ // 解析入参
|
|
|
+ type inputData struct {
|
|
|
+ Data struct {
|
|
|
+ Today float64 `json:"Today"`
|
|
|
+ Change float64 `json:"Change"`
|
|
|
+ DsToday float64 `json:"DsToday"`
|
|
|
+ DsChange float64 `json:"DsChange"`
|
|
|
+ Items []string `json:"Items"`
|
|
|
+ } `json:"data"`
|
|
|
+ }
|
|
|
+ var input inputData
|
|
|
+ if err := jsoniter.Unmarshal([]byte(responseData), &input); err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理内层数据
|
|
|
+ inner := struct {
|
|
|
+ Today float64 `json:"today"`
|
|
|
+ Change string `json:"change"`
|
|
|
+ Items string `json:"items"`
|
|
|
+ DsChange string `json:"dsChange"`
|
|
|
+ DsToday float64 `json:"dsToday"`
|
|
|
+ }{Today: input.Data.Today, DsToday: input.Data.DsToday}
|
|
|
+
|
|
|
+ // 处理百分比逻辑
|
|
|
+ switch {
|
|
|
+ case input.Data.Change > 0:
|
|
|
+ inner.Change = fmt.Sprintf("增加%.0f%%", input.Data.Change)
|
|
|
+ case input.Data.Change == 0:
|
|
|
+ inner.Change = "无变化"
|
|
|
+ default:
|
|
|
+ inner.Change = fmt.Sprintf("减少%.0f%%", -input.Data.Change)
|
|
|
+ }
|
|
|
+ switch {
|
|
|
+ case input.Data.DsChange > 0:
|
|
|
+ inner.DsChange = fmt.Sprintf("增加%.0f%%", input.Data.DsChange)
|
|
|
+ case input.Data.DsChange == 0:
|
|
|
+ inner.DsChange = "无变化"
|
|
|
+ default:
|
|
|
+ inner.DsChange = fmt.Sprintf("减少%.0f%%", -input.Data.DsChange)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 拼接Items
|
|
|
+ if len(input.Data.Items) > 0 {
|
|
|
+ inner.Items = strings.Join(input.Data.Items, ",")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 正确的外层包裹
|
|
|
+ outer := struct {
|
|
|
+ Data interface{} `json:"data"`
|
|
|
+ }{Data: inner}
|
|
|
+ resultJSON, err := jsoniter.Marshal(outer)
|
|
|
+ return string(resultJSON), err
|
|
|
+}
|
|
|
+
|
|
|
+func TransformType4(arg1 string) (string, error) {
|
|
|
+ // 定义入参结构体(匹配真实格式)
|
|
|
+ type inputData struct {
|
|
|
+ Data struct {
|
|
|
+ FeedFlow float64 `json:"feed_flow"`
|
|
|
+ WaterQuantity string `json:"water_quantity"`
|
|
|
+ Level string `json:"level"`
|
|
|
+ PlantFeedFlow float64 `json:"plant_feed_flow"`
|
|
|
+ } `json:"data"`
|
|
|
+ }
|
|
|
+
|
|
|
+ var input inputData
|
|
|
+ if err := jsoniter.Unmarshal([]byte(arg1), &input); err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理内层数据
|
|
|
+ inner := struct {
|
|
|
+ FeedFlow float64 `json:"feed_flow"`
|
|
|
+ WaterQuantity string `json:"water_quantity"`
|
|
|
+ Level string `json:"level"`
|
|
|
+ PlantFeedFlow float64 `json:"plant_feed_flow"`
|
|
|
+ }{
|
|
|
+ FeedFlow: input.Data.FeedFlow,
|
|
|
+ WaterQuantity: input.Data.WaterQuantity,
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理level(字符串转数字+容错)
|
|
|
+ levelNum, _ := strconv.Atoi(input.Data.Level)
|
|
|
+ inner.Level = "一般"
|
|
|
+ if levelNum != 0 {
|
|
|
+ inner.Level = "已达标"
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保留2位小数
|
|
|
+ inner.PlantFeedFlow = math.Round(input.Data.PlantFeedFlow*100) / 100
|
|
|
+
|
|
|
+ // 外层包裹data
|
|
|
+ outer := struct {
|
|
|
+ Data interface{} `json:"data"`
|
|
|
+ }{Data: inner}
|
|
|
+ resultJSON, err := jsoniter.Marshal(outer)
|
|
|
+ return string(resultJSON), err
|
|
|
+}
|