作者 yangfu

fix: decimal problem

... ... @@ -125,6 +125,8 @@ var (
Int SQLType = "INT"
BigInt SQLType = "BIGINT"
Float SQLType = "FLOAT"
DECIMALV2 SQLType = "DECIMALV2"
DECIMAL279 SQLType = "DECIMAL(27,9)"
Date SQLType = "DATE"
Datetime SQLType = "DATETIME"
)
... ... @@ -134,6 +136,7 @@ var SQLTypeMap = map[string]string{
Int.ToString(): "整数",
BigInt.ToString(): "整数",
Float.ToString(): "小数",
DECIMALV2.ToString(): "小数",
Date.ToString(): "日期",
Datetime.ToString(): "日期时间",
}
... ...
... ... @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/linmadan/egglib-go/utils/xtime"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
"strconv"
"strings"
)
... ... @@ -186,6 +185,16 @@ func ValueToType(value string, sqlType string) (interface{}, error) {
if err != nil {
err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
}
case DECIMALV2.ToString():
toTypeVal, err = numberString.Float64()
if err != nil {
err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
}
case DECIMAL279.ToString():
toTypeVal, err = numberString.Float64()
if err != nil {
err = fmt.Errorf("[%v]不是有效的浮点数类型", value)
}
case Date.ToString():
toTypeVal, err = xtime.Parse(value)
if err != nil {
... ... @@ -235,15 +244,16 @@ func ToFieldData(fields []*Field, data [][]string, byName bool, configs ...bool)
// RoundFieldValue 字段值精度处理
func RoundFieldValue(f *Field, v string) string {
if f.SQLType != Float.ToString() {
return v
}
fv, err := strconv.ParseFloat(v, 64)
if err != nil {
return v
}
fv = utils.Round(fv, 6)
return fmt.Sprintf("%v", fv)
//if f.SQLType != DECIMALV2.ToString() {
// return v
//}
//fv, err := strconv.ParseFloat(v, 64)
//if err != nil {
// return v
//}
//fv = utils.Round(fv, 6)
//return fmt.Sprintf("%v", fv)
}
func GripData(data []map[string]string, total int64) map[string]interface{} {
... ...
package bytelib
import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
func DomainFieldsToColumnSchemas(fields []*domain.Field) []domain.ColumnSchema {
result := make([]domain.ColumnSchema, 0)
for _, f := range fields {
res := domain.ColumnSchema{
ColumnName: f.Name,
ColumnType: f.SQLType,
}
if convertFiledSQLType(f.SQLType) {
res.ColumnType = domain.DECIMAL279.ToString()
}
result = append(result)
}
return result
}
func ToFieldSchemas(fields []*domain.Field) []FieldSchema {
result := make([]FieldSchema, 0)
for _, f := range fields {
res := FieldSchema{
FieldZhName: f.Name,
FieldEnName: f.SQLName,
FieldType: f.SQLType,
FieldDescription: f.Description,
IsAllowNull: true,
}
if convertFiledSQLType(f.SQLType) {
res.FieldType = domain.DECIMAL279.ToString()
}
result = append(result, res)
}
return result
}
func NewFieldSchema(f domain.TableField) FieldSchema {
var res = FieldSchema{
FieldZhName: f.FieldName,
FieldEnName: f.FieldSqlName,
FieldType: f.FieldSQLType,
FieldDescription: "",
IsAllowNull: true,
}
if convertFiledSQLType(f.FieldSQLType) {
res.FieldType = domain.DECIMAL279.ToString()
}
return res
}
func NewFieldSchemaFromField(f *domain.Field) FieldSchema {
var res = FieldSchema{
FieldZhName: f.Name,
FieldEnName: f.SQLName,
FieldType: f.SQLType,
FieldDescription: "",
IsAllowNull: true,
}
if convertFiledSQLType(f.SQLType) {
res.FieldType = domain.DECIMAL279.ToString()
}
return res
}
func convertFiledSQLType(sqlType string) bool {
if sqlType == domain.Float.ToString() || sqlType == domain.DECIMAL279.ToString() {
return true
}
return false
}
... ...
... ... @@ -108,18 +108,6 @@ func FieldsToColumnSchemas(fields []*domain.Field) []domain.ColumnSchema {
return result
}
func DomainFieldsToColumnSchemas(fields []*domain.Field) []domain.ColumnSchema {
result := make([]domain.ColumnSchema, 0)
for _, f := range fields {
result = append(result, domain.ColumnSchema{
ColumnName: f.Name,
ColumnType: f.SQLType, // domain.String.ToString(),
})
}
return result
}
func ToDataLoadDataTable(data DataCheckoutTables) *domain.DataLoadDataTable {
response := &domain.DataLoadDataTable{
PageNumber: data.PageNumber,
... ... @@ -181,28 +169,6 @@ func NewRequestCheckoutTablesGenerateMasterTable(param domain.ReqGenerateTable)
return request
}
func ToFieldSchemas(fields []*domain.Field) []FieldSchema {
result := make([]FieldSchema, 0)
for _, f := range fields {
result = append(result, FieldSchema{
FieldZhName: f.Name,
FieldEnName: f.SQLName,
FieldType: f.SQLType,
FieldDescription: f.Description,
IsAllowNull: true,
})
}
return result
}
func ToFieldSchemaEnNames(fields []*domain.Field) []string {
result := make([]string, 0)
for _, f := range fields {
result = append(result, f.SQLName)
}
return result
}
type (
TableAppendRequest struct {
//MasterTableId string `json:"masterTableId"`
... ... @@ -243,10 +209,6 @@ func NewTableAppendRequest(param domain.ReqAppendData) TableAppendRequest {
req.SchemaMap[param.To[i].SQLName] = columnSchemas[i]
}
}
//if len(param.From) > 0 {
// req.ColumnSchemas = DomainFieldsToColumnSchemas(param.From)
// req.FieldSchemas = ToFieldSchemas(param.To)
//}
return req
}
... ...
... ... @@ -162,7 +162,7 @@ func NewFormulaCalculate(table *domain.Table, queryComponent *domain.QueryCompon
}
if queryComponent.Aggregation != nil {
res.DatabaseTableName = queryComponent.MasterTable.SQLName
if len(queryComponent.Aggregation.ValueFields)>0 || len(queryComponent.Aggregation.RowFields)>0{
if len(queryComponent.Aggregation.ValueFields) > 0 || len(queryComponent.Aggregation.RowFields) > 0 {
res.FormulaCalculateFields = append(res.FormulaCalculateFields, &FormulaCalculateField{
DatabaseTableName: queryComponent.MasterTable.SQLName,
FieldSchema: NewFieldSchemaFromField(&domain.Field{
... ... @@ -252,28 +252,6 @@ func NewFormulaField(f domain.FieldExpr, args ...interface{}) FormulaField {
return res
}
func NewFieldSchema(f domain.TableField) FieldSchema {
var res = FieldSchema{
FieldZhName: f.FieldName,
FieldEnName: f.FieldSqlName,
FieldType: f.FieldSQLType,
FieldDescription: "",
IsAllowNull: true,
}
return res
}
func NewFieldSchemaFromField(f *domain.Field) FieldSchema {
var res = FieldSchema{
FieldZhName: f.Name,
FieldEnName: f.SQLName,
FieldType: f.SQLType,
FieldDescription: "",
IsAllowNull: true,
}
return res
}
func NewFormulaDataHandleRule(s domain.SelectExprGroup) FormulaDataHandleRule {
var res = FormulaDataHandleRule{
RuleType: 1,
... ...
... ... @@ -168,7 +168,8 @@ func (c Condition) SetWhere(params QueryOptions, q *gorm.DB) {
}
}
if c.Distinct {
q.Distinct(c.Field.SQLName)
// 需要优化
q.Distinct(c.FormatFiled(c.Field))
}
if len(c.Order) > 0 {
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 {
return f.SQLName
}
func (c Condition) FormatFiled(f *domain.Field) string {
return formatFiled(c.Field)
}
func (c Condition) CastType(sql, t string) string {
if c.params.Table != nil && c.params.Table.TableType == domain.ObjectDBTable {
return sql
}
return castType(sql, t)
}
func formatFiled(f *domain.Field) string {
if f.SQLType == domain.Float.ToString() || f.SQLType == domain.DECIMAL279.ToString() {
return castTypeAlias(f.SQLName, domain.DECIMALV2.ToString())
}
return f.SQLName
}
func castType(sql, t string) string {
return fmt.Sprintf("cast(%v as %v)", sql, t)
}
func castTypeAlias(sql, t string) string {
return fmt.Sprintf("cast(%v as %v) %v", sql, t, sql)
}
var opMap = map[string]string{
"=": "=",
">": ">",
... ... @@ -305,7 +325,7 @@ func queryWithoutLimitOffset(query *gorm.DB, params QueryOptions) {
fields = append(fields, "'' "+f.SQLName)
continue
}
fields = append(fields, f.SQLName)
fields = append(fields, formatFiled(f))
}
query.Select(strings.Join(fields, ","))
}
... ...