作者 yangfu

fix: decimal problem

@@ -121,21 +121,24 @@ var ( @@ -121,21 +121,24 @@ var (
121 ) 121 )
122 122
123 var ( 123 var (
124 - String SQLType = "STRING"  
125 - Int SQLType = "INT"  
126 - BigInt SQLType = "BIGINT"  
127 - Float SQLType = "FLOAT"  
128 - Date SQLType = "DATE"  
129 - Datetime SQLType = "DATETIME" 124 + String SQLType = "STRING"
  125 + Int SQLType = "INT"
  126 + BigInt SQLType = "BIGINT"
  127 + Float SQLType = "FLOAT"
  128 + DECIMALV2 SQLType = "DECIMALV2"
  129 + DECIMAL279 SQLType = "DECIMAL(27,9)"
  130 + Date SQLType = "DATE"
  131 + Datetime SQLType = "DATETIME"
130 ) 132 )
131 133
132 var SQLTypeMap = map[string]string{ 134 var SQLTypeMap = map[string]string{
133 - String.ToString(): "文本",  
134 - Int.ToString(): "整数",  
135 - BigInt.ToString(): "整数",  
136 - Float.ToString(): "小数",  
137 - Date.ToString(): "日期",  
138 - Datetime.ToString(): "日期时间", 135 + String.ToString(): "文本",
  136 + Int.ToString(): "整数",
  137 + BigInt.ToString(): "整数",
  138 + Float.ToString(): "小数",
  139 + DECIMALV2.ToString(): "小数",
  140 + Date.ToString(): "日期",
  141 + Datetime.ToString(): "日期时间",
139 } 142 }
140 143
141 type FileType string 144 type FileType string
@@ -4,7 +4,6 @@ import ( @@ -4,7 +4,6 @@ import (
4 "fmt" 4 "fmt"
5 "github.com/linmadan/egglib-go/utils/xtime" 5 "github.com/linmadan/egglib-go/utils/xtime"
6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils" 6 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
7 - "strconv"  
8 "strings" 7 "strings"
9 ) 8 )
10 9
@@ -186,6 +185,16 @@ func ValueToType(value string, sqlType string) (interface{}, error) { @@ -186,6 +185,16 @@ func ValueToType(value string, sqlType string) (interface{}, error) {
186 if err != nil { 185 if err != nil {
187 err = fmt.Errorf("[%v]不是有效的浮点数类型", value) 186 err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
188 } 187 }
  188 + case DECIMALV2.ToString():
  189 + toTypeVal, err = numberString.Float64()
  190 + if err != nil {
  191 + err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
  192 + }
  193 + case DECIMAL279.ToString():
  194 + toTypeVal, err = numberString.Float64()
  195 + if err != nil {
  196 + err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
  197 + }
189 case Date.ToString(): 198 case Date.ToString():
190 toTypeVal, err = xtime.Parse(value) 199 toTypeVal, err = xtime.Parse(value)
191 if err != nil { 200 if err != nil {
@@ -235,15 +244,16 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool) @@ -235,15 +244,16 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool)
235 244
236 // RoundFieldValue 字段值精度处理 245 // RoundFieldValue 字段值精度处理
237 func RoundFieldValue(f *Field, v string) string { 246 func RoundFieldValue(f *Field, v string) string {
238 - if f.SQLType != Float.ToString() {  
239 - return v  
240 - }  
241 - fv, err := strconv.ParseFloat(v, 64)  
242 - if err != nil {  
243 - return v  
244 - }  
245 - fv = utils.Round(fv, 6)  
246 - return fmt.Sprintf("%v", fv) 247 + return v
  248 + //if f.SQLType != DECIMALV2.ToString() {
  249 + // return v
  250 + //}
  251 + //fv, err := strconv.ParseFloat(v, 64)
  252 + //if err != nil {
  253 + // return v
  254 + //}
  255 + //fv = utils.Round(fv, 6)
  256 + //return fmt.Sprintf("%v", fv)
247 } 257 }
248 258
249 func GripData(data []map[string]string, total int64) map[string]interface{} { 259 func GripData(data []map[string]string, total int64) map[string]interface{} {
  1 +package bytelib
  2 +
  3 +import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  4 +
  5 +func DomainFieldsToColumnSchemas(fields []*domain.Field) []domain.ColumnSchema {
  6 + result := make([]domain.ColumnSchema, 0)
  7 + for _, f := range fields {
  8 + res := domain.ColumnSchema{
  9 + ColumnName: f.Name,
  10 + ColumnType: f.SQLType,
  11 + }
  12 + if convertFiledSQLType(f.SQLType) {
  13 + res.ColumnType = domain.DECIMAL279.ToString()
  14 + }
  15 + result = append(result)
  16 + }
  17 + return result
  18 +}
  19 +
  20 +func ToFieldSchemas(fields []*domain.Field) []FieldSchema {
  21 + result := make([]FieldSchema, 0)
  22 + for _, f := range fields {
  23 + res := FieldSchema{
  24 + FieldZhName: f.Name,
  25 + FieldEnName: f.SQLName,
  26 + FieldType: f.SQLType,
  27 + FieldDescription: f.Description,
  28 + IsAllowNull: true,
  29 + }
  30 + if convertFiledSQLType(f.SQLType) {
  31 + res.FieldType = domain.DECIMAL279.ToString()
  32 + }
  33 + result = append(result, res)
  34 + }
  35 + return result
  36 +}
  37 +
  38 +func NewFieldSchema(f domain.TableField) FieldSchema {
  39 + var res = FieldSchema{
  40 + FieldZhName: f.FieldName,
  41 + FieldEnName: f.FieldSqlName,
  42 + FieldType: f.FieldSQLType,
  43 + FieldDescription: "",
  44 + IsAllowNull: true,
  45 + }
  46 + if convertFiledSQLType(f.FieldSQLType) {
  47 + res.FieldType = domain.DECIMAL279.ToString()
  48 + }
  49 + return res
  50 +}
  51 +
  52 +func NewFieldSchemaFromField(f *domain.Field) FieldSchema {
  53 + var res = FieldSchema{
  54 + FieldZhName: f.Name,
  55 + FieldEnName: f.SQLName,
  56 + FieldType: f.SQLType,
  57 + FieldDescription: "",
  58 + IsAllowNull: true,
  59 + }
  60 + if convertFiledSQLType(f.SQLType) {
  61 + res.FieldType = domain.DECIMAL279.ToString()
  62 + }
  63 + return res
  64 +}
  65 +
  66 +func convertFiledSQLType(sqlType string) bool {
  67 + if sqlType == domain.Float.ToString() || sqlType == domain.DECIMAL279.ToString() {
  68 + return true
  69 + }
  70 + return false
  71 +}
@@ -108,18 +108,6 @@ func FieldsToColumnSchemas(fields []*domain.Field) []domain.ColumnSchema { @@ -108,18 +108,6 @@ func FieldsToColumnSchemas(fields []*domain.Field) []domain.ColumnSchema {
108 return result 108 return result
109 } 109 }
110 110
111 -func DomainFieldsToColumnSchemas(fields []*domain.Field) []domain.ColumnSchema {  
112 - result := make([]domain.ColumnSchema, 0)  
113 -  
114 - for _, f := range fields {  
115 - result = append(result, domain.ColumnSchema{  
116 - ColumnName: f.Name,  
117 - ColumnType: f.SQLType, // domain.String.ToString(),  
118 - })  
119 - }  
120 - return result  
121 -}  
122 -  
123 func ToDataLoadDataTable(data DataCheckoutTables) *domain.DataLoadDataTable { 111 func ToDataLoadDataTable(data DataCheckoutTables) *domain.DataLoadDataTable {
124 response := &domain.DataLoadDataTable{ 112 response := &domain.DataLoadDataTable{
125 PageNumber: data.PageNumber, 113 PageNumber: data.PageNumber,
@@ -181,28 +169,6 @@ func NewRequestCheckoutTablesGenerateMasterTable(param domain.ReqGenerateTable) @@ -181,28 +169,6 @@ func NewRequestCheckoutTablesGenerateMasterTable(param domain.ReqGenerateTable)
181 return request 169 return request
182 } 170 }
183 171
184 -func ToFieldSchemas(fields []*domain.Field) []FieldSchema {  
185 - result := make([]FieldSchema, 0)  
186 - for _, f := range fields {  
187 - result = append(result, FieldSchema{  
188 - FieldZhName: f.Name,  
189 - FieldEnName: f.SQLName,  
190 - FieldType: f.SQLType,  
191 - FieldDescription: f.Description,  
192 - IsAllowNull: true,  
193 - })  
194 - }  
195 - return result  
196 -}  
197 -  
198 -func ToFieldSchemaEnNames(fields []*domain.Field) []string {  
199 - result := make([]string, 0)  
200 - for _, f := range fields {  
201 - result = append(result, f.SQLName)  
202 - }  
203 - return result  
204 -}  
205 -  
206 type ( 172 type (
207 TableAppendRequest struct { 173 TableAppendRequest struct {
208 //MasterTableId string `json:"masterTableId"` 174 //MasterTableId string `json:"masterTableId"`
@@ -243,10 +209,6 @@ func NewTableAppendRequest(param domain.ReqAppendData) TableAppendRequest { @@ -243,10 +209,6 @@ func NewTableAppendRequest(param domain.ReqAppendData) TableAppendRequest {
243 req.SchemaMap[param.To[i].SQLName] = columnSchemas[i] 209 req.SchemaMap[param.To[i].SQLName] = columnSchemas[i]
244 } 210 }
245 } 211 }
246 - //if len(param.From) > 0 {  
247 - // req.ColumnSchemas = DomainFieldsToColumnSchemas(param.From)  
248 - // req.FieldSchemas = ToFieldSchemas(param.To)  
249 - //}  
250 return req 212 return req
251 } 213 }
252 214
@@ -162,7 +162,7 @@ func NewFormulaCalculate(table *domain.Table, queryComponent *domain.QueryCompon @@ -162,7 +162,7 @@ func NewFormulaCalculate(table *domain.Table, queryComponent *domain.QueryCompon
162 } 162 }
163 if queryComponent.Aggregation != nil { 163 if queryComponent.Aggregation != nil {
164 res.DatabaseTableName = queryComponent.MasterTable.SQLName 164 res.DatabaseTableName = queryComponent.MasterTable.SQLName
165 - if len(queryComponent.Aggregation.ValueFields)>0 || len(queryComponent.Aggregation.RowFields)>0{ 165 + if len(queryComponent.Aggregation.ValueFields) > 0 || len(queryComponent.Aggregation.RowFields) > 0 {
166 res.FormulaCalculateFields = append(res.FormulaCalculateFields, &FormulaCalculateField{ 166 res.FormulaCalculateFields = append(res.FormulaCalculateFields, &FormulaCalculateField{
167 DatabaseTableName: queryComponent.MasterTable.SQLName, 167 DatabaseTableName: queryComponent.MasterTable.SQLName,
168 FieldSchema: NewFieldSchemaFromField(&domain.Field{ 168 FieldSchema: NewFieldSchemaFromField(&domain.Field{
@@ -252,28 +252,6 @@ func NewFormulaField(f domain.FieldExpr, args ...interface{}) FormulaField { @@ -252,28 +252,6 @@ func NewFormulaField(f domain.FieldExpr, args ...interface{}) FormulaField {
252 return res 252 return res
253 } 253 }
254 254
255 -func NewFieldSchema(f domain.TableField) FieldSchema {  
256 - var res = FieldSchema{  
257 - FieldZhName: f.FieldName,  
258 - FieldEnName: f.FieldSqlName,  
259 - FieldType: f.FieldSQLType,  
260 - FieldDescription: "",  
261 - IsAllowNull: true,  
262 - }  
263 - return res  
264 -}  
265 -  
266 -func NewFieldSchemaFromField(f *domain.Field) FieldSchema {  
267 - var res = FieldSchema{  
268 - FieldZhName: f.Name,  
269 - FieldEnName: f.SQLName,  
270 - FieldType: f.SQLType,  
271 - FieldDescription: "",  
272 - IsAllowNull: true,  
273 - }  
274 - return res  
275 -}  
276 -  
277 func NewFormulaDataHandleRule(s domain.SelectExprGroup) FormulaDataHandleRule { 255 func NewFormulaDataHandleRule(s domain.SelectExprGroup) FormulaDataHandleRule {
278 var res = FormulaDataHandleRule{ 256 var res = FormulaDataHandleRule{
279 RuleType: 1, 257 RuleType: 1,
@@ -168,7 +168,8 @@ func (c Condition) SetWhere(params QueryOptions, q *gorm.DB) { @@ -168,7 +168,8 @@ func (c Condition) SetWhere(params QueryOptions, q *gorm.DB) {
168 } 168 }
169 } 169 }
170 if c.Distinct { 170 if c.Distinct {
171 - q.Distinct(c.Field.SQLName) 171 + // 需要优化
  172 + q.Distinct(c.FormatFiled(c.Field))
172 } 173 }
173 if len(c.Order) > 0 { 174 if len(c.Order) > 0 {
174 q.Order(fmt.Sprintf("%v %v", c.Field.SQLName, c.Order)) 175 q.Order(fmt.Sprintf("%v %v", c.Field.SQLName, c.Order))
@@ -189,13 +190,32 @@ func (c Condition) FormatIfNull(params QueryOptions, f *domain.Field) string { @@ -189,13 +190,32 @@ func (c Condition) FormatIfNull(params QueryOptions, f *domain.Field) string {
189 return f.SQLName 190 return f.SQLName
190 } 191 }
191 192
  193 +func (c Condition) FormatFiled(f *domain.Field) string {
  194 + return formatFiled(c.Field)
  195 +}
  196 +
192 func (c Condition) CastType(sql, t string) string { 197 func (c Condition) CastType(sql, t string) string {
193 if c.params.Table != nil && c.params.Table.TableType == domain.ObjectDBTable { 198 if c.params.Table != nil && c.params.Table.TableType == domain.ObjectDBTable {
194 return sql 199 return sql
195 } 200 }
  201 + return castType(sql, t)
  202 +}
  203 +
  204 +func formatFiled(f *domain.Field) string {
  205 + if f.SQLType == domain.Float.ToString() || f.SQLType == domain.DECIMAL279.ToString() {
  206 + return castTypeAlias(f.SQLName, domain.DECIMALV2.ToString())
  207 + }
  208 + return f.SQLName
  209 +}
  210 +
  211 +func castType(sql, t string) string {
196 return fmt.Sprintf("cast(%v as %v)", sql, t) 212 return fmt.Sprintf("cast(%v as %v)", sql, t)
197 } 213 }
198 214
  215 +func castTypeAlias(sql, t string) string {
  216 + return fmt.Sprintf("cast(%v as %v) %v", sql, t, sql)
  217 +}
  218 +
199 var opMap = map[string]string{ 219 var opMap = map[string]string{
200 "=": "=", 220 "=": "=",
201 ">": ">", 221 ">": ">",
@@ -305,7 +325,7 @@ func queryWithoutLimitOffset(query *gorm.DB, params QueryOptions) { @@ -305,7 +325,7 @@ func queryWithoutLimitOffset(query *gorm.DB, params QueryOptions) {
305 fields = append(fields, "'' "+f.SQLName) 325 fields = append(fields, "'' "+f.SQLName)
306 continue 326 continue
307 } 327 }
308 - fields = append(fields, f.SQLName) 328 + fields = append(fields, formatFiled(f))
309 } 329 }
310 query.Select(strings.Join(fields, ",")) 330 query.Select(strings.Join(fields, ","))
311 } 331 }