github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/gen_method/examples/customer_g7__generated.go (about)

     1  package examples
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/go-sql-driver/mysql"
    10  	"github.com/sirupsen/logrus"
    11  
    12  	"golib/gorm"
    13  
    14  	"github.com/johnnyeven/libtools/courier/enumeration"
    15  	"github.com/johnnyeven/libtools/duration"
    16  	"github.com/johnnyeven/libtools/mysql/dberr"
    17  	"github.com/johnnyeven/libtools/timelib"
    18  )
    19  
    20  type CustomerG7List []CustomerG7
    21  
    22  func init() {
    23  	DBTable.Register(&CustomerG7{})
    24  }
    25  
    26  func (cg CustomerG7) TableName() string {
    27  	table_name := "t_customer_g7"
    28  	if DBTable.Name == "" {
    29  		return table_name
    30  	}
    31  	return DBTable.Name + "." + table_name
    32  }
    33  
    34  func (cgl *CustomerG7List) BatchFetchByCreateTimeList(db *gorm.DB, createTimeList []timelib.MySQLTimestamp) error {
    35  	defer duration.PrintDuration(map[string]interface{}{
    36  		"request": "[DB]CustomerG7.BatchFetchByCreateTimeList",
    37  	})()
    38  
    39  	if len(createTimeList) == 0 {
    40  		return nil
    41  	}
    42  
    43  	err := db.Table(CustomerG7{}.TableName()).Where("F_create_time in (?) and F_enabled = ?", createTimeList, enumeration.BOOL__TRUE).Find(cgl).Error
    44  	if err != nil {
    45  		logrus.Errorf("%s", err.Error())
    46  		return dberr.RecordFetchFailedError
    47  	} else {
    48  		return nil
    49  	}
    50  }
    51  
    52  func (cgl *CustomerG7List) BatchFetchByCustomerIDList(db *gorm.DB, customerIDList []uint64) error {
    53  	defer duration.PrintDuration(map[string]interface{}{
    54  		"request": "[DB]CustomerG7.BatchFetchByCustomerIDList",
    55  	})()
    56  
    57  	if len(customerIDList) == 0 {
    58  		return nil
    59  	}
    60  
    61  	err := db.Table(CustomerG7{}.TableName()).Where("F_customer_id in (?) and F_enabled = ?", customerIDList, enumeration.BOOL__TRUE).Find(cgl).Error
    62  	if err != nil {
    63  		logrus.Errorf("%s", err.Error())
    64  		return dberr.RecordFetchFailedError
    65  	} else {
    66  		return nil
    67  	}
    68  }
    69  
    70  func (cgl *CustomerG7List) BatchFetchByG7sUserIDList(db *gorm.DB, g7sUserIDList []string) error {
    71  	defer duration.PrintDuration(map[string]interface{}{
    72  		"request": "[DB]CustomerG7.BatchFetchByG7sUserIDList",
    73  	})()
    74  
    75  	if len(g7sUserIDList) == 0 {
    76  		return nil
    77  	}
    78  
    79  	err := db.Table(CustomerG7{}.TableName()).Where("F_g7s_user_id in (?) and F_enabled = ?", g7sUserIDList, enumeration.BOOL__TRUE).Find(cgl).Error
    80  	if err != nil {
    81  		logrus.Errorf("%s", err.Error())
    82  		return dberr.RecordFetchFailedError
    83  	} else {
    84  		return nil
    85  	}
    86  }
    87  
    88  func (cgl *CustomerG7List) BatchFetchByUpdateTimeList(db *gorm.DB, updateTimeList []timelib.MySQLTimestamp) error {
    89  	defer duration.PrintDuration(map[string]interface{}{
    90  		"request": "[DB]CustomerG7.BatchFetchByUpdateTimeList",
    91  	})()
    92  
    93  	if len(updateTimeList) == 0 {
    94  		return nil
    95  	}
    96  
    97  	err := db.Table(CustomerG7{}.TableName()).Where("F_update_time in (?) and F_enabled = ?", updateTimeList, enumeration.BOOL__TRUE).Find(cgl).Error
    98  	if err != nil {
    99  		logrus.Errorf("%s", err.Error())
   100  		return dberr.RecordFetchFailedError
   101  	} else {
   102  		return nil
   103  	}
   104  }
   105  
   106  func (cg *CustomerG7) Create(db *gorm.DB) error {
   107  	defer duration.PrintDuration(map[string]interface{}{
   108  		"request": "[DB]CustomerG7.Create",
   109  	})()
   110  
   111  	if time.Time(cg.CreateTime).IsZero() {
   112  		cg.CreateTime = timelib.MySQLTimestamp(time.Now())
   113  	}
   114  
   115  	if time.Time(cg.UpdateTime).IsZero() {
   116  		cg.UpdateTime = timelib.MySQLTimestamp(time.Now())
   117  	}
   118  
   119  	cg.Enabled = enumeration.Bool(enumeration.BOOL__TRUE)
   120  	err := db.Table(cg.TableName()).Create(cg).Error
   121  	if err != nil {
   122  		if mysql_err, ok := err.(*mysql.MySQLError); !ok {
   123  			logrus.Errorf("%s", err.Error())
   124  			return dberr.RecordCreateFailedError
   125  		} else if mysql_err.Number != dberr.DuplicateEntryErrNumber {
   126  			logrus.Errorf("%s", err.Error())
   127  			return dberr.RecordCreateFailedError
   128  		} else {
   129  			return dberr.RecordConflictError
   130  		}
   131  	} else {
   132  		return nil
   133  	}
   134  }
   135  
   136  type CustomerG7DBFieldData struct {
   137  	CustomerID string
   138  	G7sOrgCode string
   139  	G7sUserID  string
   140  	CreateTime string
   141  	UpdateTime string
   142  	Enabled    string
   143  }
   144  
   145  // FetchNoneUniqueIndexFields without Enabled and CreateTime field.
   146  func (cgdbfd *CustomerG7DBFieldData) FetchNoneUniqueIndexFields() []string {
   147  	return []string{
   148  		"F_update_time",
   149  	}
   150  }
   151  
   152  func (cg CustomerG7) DBField() *CustomerG7DBFieldData {
   153  	return &CustomerG7DBFieldData{
   154  		CustomerID: "F_customer_id",
   155  		G7sOrgCode: "F_g7s_org_code",
   156  		G7sUserID:  "F_g7s_user_id",
   157  		CreateTime: "F_create_time",
   158  		UpdateTime: "F_update_time",
   159  		Enabled:    "F_enabled",
   160  	}
   161  }
   162  
   163  var CustomerG7StructFieldAndDBFieldRelate = map[string]string{
   164  	"CustomerID": "F_customer_id",
   165  	"G7sOrgCode": "F_g7s_org_code",
   166  	"G7sUserID":  "F_g7s_user_id",
   167  	"CreateTime": "F_create_time",
   168  	"UpdateTime": "F_update_time",
   169  	"Enabled":    "F_enabled",
   170  }
   171  
   172  var CustomerG7DBFieldAndStructFieldRelate = map[string]string{
   173  	"F_customer_id":  "CustomerID",
   174  	"F_g7s_org_code": "G7sOrgCode",
   175  	"F_g7s_user_id":  "G7sUserID",
   176  	"F_create_time":  "CreateTime",
   177  	"F_update_time":  "UpdateTime",
   178  	"F_enabled":      "Enabled",
   179  }
   180  
   181  // CreateOnDuplicateWithUpdateFields only update the no unique index field, it return error if updateFields contain unique index field.
   182  // It doesn't update the Enabled and CreateTime field.
   183  func (cg *CustomerG7) CreateOnDuplicateWithUpdateFields(db *gorm.DB, updateFields []string) error {
   184  	defer duration.PrintDuration(map[string]interface{}{
   185  		"request": "[DB]CustomerG7.CreateOnDuplicateWithUpdateFields",
   186  	})()
   187  	if len(updateFields) == 0 {
   188  		return fmt.Errorf("Must have update fields.")
   189  	}
   190  
   191  	noUniqueIndexFields := (&CustomerG7DBFieldData{}).FetchNoneUniqueIndexFields()
   192  	if len(noUniqueIndexFields) == 0 {
   193  		return fmt.Errorf("There are no unique fields.")
   194  	}
   195  
   196  	var noUniqueIndexFieldsMap = make(map[string]string)
   197  	for _, field := range noUniqueIndexFields {
   198  		noUniqueIndexFieldsMap[field] = ""
   199  	}
   200  
   201  	var updateFieldsMap = make(map[string]string)
   202  	for _, field := range updateFields {
   203  		// have unique field in updateFields
   204  		if _, ok := noUniqueIndexFieldsMap[field]; !ok {
   205  			return fmt.Errorf("Field[%s] is unique index or wrong field or Enable field", CustomerG7DBFieldAndStructFieldRelate[field])
   206  		}
   207  		updateFieldsMap[field] = ""
   208  	}
   209  
   210  	if time.Time(cg.CreateTime).IsZero() {
   211  		cg.CreateTime = timelib.MySQLTimestamp(time.Now())
   212  	}
   213  
   214  	if time.Time(cg.UpdateTime).IsZero() {
   215  		cg.UpdateTime = timelib.MySQLTimestamp(time.Now())
   216  	}
   217  
   218  	cg.Enabled = enumeration.Bool(enumeration.BOOL__TRUE)
   219  
   220  	structType := reflect.TypeOf(cg).Elem()
   221  	if structType.Kind() != reflect.Struct {
   222  		return fmt.Errorf("Instance not struct type.")
   223  	}
   224  	structVal := reflect.ValueOf(cg).Elem()
   225  
   226  	var param_list []interface{}
   227  	var str_list = []string{"insert into"}
   228  	var insertFieldsStr = cg.TableName() + "("
   229  	var placeHolder = "values("
   230  	for i := 0; i < structType.NumField(); i++ {
   231  		if i == 0 {
   232  			insertFieldsStr += CustomerG7StructFieldAndDBFieldRelate[structType.Field(i).Name]
   233  			placeHolder += fmt.Sprintf("%s", "?")
   234  		} else {
   235  			insertFieldsStr += fmt.Sprintf(",%s", CustomerG7StructFieldAndDBFieldRelate[structType.Field(i).Name])
   236  			placeHolder += fmt.Sprintf("%s", ", ?")
   237  		}
   238  		param_list = append(param_list, structVal.Field(i).Interface())
   239  	}
   240  	insertFieldsStr += ")"
   241  	placeHolder += ")"
   242  	str_list = append(str_list, []string{insertFieldsStr, placeHolder, "on duplicate key update"}...)
   243  
   244  	var updateStr []string
   245  	for i := 0; i < structType.NumField(); i++ {
   246  		if dbField, ok := CustomerG7StructFieldAndDBFieldRelate[structType.Field(i).Name]; !ok {
   247  			return fmt.Errorf("Wrong field of struct, may be changed field but not regenerate code.")
   248  		} else {
   249  			if _, ok := updateFieldsMap[dbField]; ok {
   250  				updateStr = append(updateStr, fmt.Sprintf("%s = ?", dbField))
   251  				param_list = append(param_list, structVal.Field(i).Interface())
   252  			}
   253  		}
   254  	}
   255  	str_list = append(str_list, strings.Join(updateStr, ","))
   256  	sql := strings.Join(str_list, " ")
   257  	err := db.Exec(sql, param_list...).Error
   258  	if err != nil {
   259  		logrus.Errorf("%s", err.Error())
   260  		return dberr.RecordCreateFailedError
   261  	}
   262  
   263  	return nil
   264  }
   265  
   266  func (cg *CustomerG7) DeleteByCustomerIDAndG7sOrgCodeAndG7sUserID(db *gorm.DB) error {
   267  	defer duration.PrintDuration(map[string]interface{}{
   268  		"request": "[DB]CustomerG7.DeleteByCustomerIDAndG7sOrgCodeAndG7sUserID",
   269  	})()
   270  
   271  	err := db.Table(cg.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Delete(cg).Error
   272  	if err != nil {
   273  		logrus.Errorf("%s", err.Error())
   274  		return dberr.RecordDeleteFailedError
   275  	} else {
   276  		return nil
   277  	}
   278  }
   279  
   280  func (cg *CustomerG7) DeleteByG7sUserIDAndCustomerID(db *gorm.DB) error {
   281  	defer duration.PrintDuration(map[string]interface{}{
   282  		"request": "[DB]CustomerG7.DeleteByG7sUserIDAndCustomerID",
   283  	})()
   284  
   285  	err := db.Table(cg.TableName()).Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Delete(cg).Error
   286  	if err != nil {
   287  		logrus.Errorf("%s", err.Error())
   288  		return dberr.RecordDeleteFailedError
   289  	} else {
   290  		return nil
   291  	}
   292  }
   293  
   294  func (cgl *CustomerG7List) FetchByCreateTime(db *gorm.DB, createTime timelib.MySQLTimestamp) error {
   295  	defer duration.PrintDuration(map[string]interface{}{
   296  		"request": "[DB]CustomerG7.FetchByCreateTime",
   297  	})()
   298  
   299  	err := db.Table(CustomerG7{}.TableName()).Where("F_create_time = ? and F_enabled = ?", createTime, enumeration.BOOL__TRUE).Find(cgl).Error
   300  	if err == nil {
   301  		return nil
   302  	} else {
   303  		logrus.Errorf("%s", err.Error())
   304  		return dberr.RecordFetchFailedError
   305  	}
   306  }
   307  
   308  func (cgl *CustomerG7List) FetchByCustomerID(db *gorm.DB, customerID uint64) error {
   309  	defer duration.PrintDuration(map[string]interface{}{
   310  		"request": "[DB]CustomerG7.FetchByCustomerID",
   311  	})()
   312  
   313  	err := db.Table(CustomerG7{}.TableName()).Where("F_customer_id = ? and F_enabled = ?", customerID, enumeration.BOOL__TRUE).Find(cgl).Error
   314  	if err == nil {
   315  		return nil
   316  	} else {
   317  		logrus.Errorf("%s", err.Error())
   318  		return dberr.RecordFetchFailedError
   319  	}
   320  }
   321  
   322  func (cgl *CustomerG7List) FetchByCustomerIDAndG7sOrgCode(db *gorm.DB, customerID uint64, g7sOrgCode string) error {
   323  	defer duration.PrintDuration(map[string]interface{}{
   324  		"request": "[DB]CustomerG7.FetchByCustomerIDAndG7sOrgCode",
   325  	})()
   326  
   327  	err := db.Table(CustomerG7{}.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_enabled = ?", customerID, g7sOrgCode, enumeration.BOOL__TRUE).Find(cgl).Error
   328  	if err == nil {
   329  		return nil
   330  	} else {
   331  		logrus.Errorf("%s", err.Error())
   332  		return dberr.RecordFetchFailedError
   333  	}
   334  }
   335  
   336  func (cg *CustomerG7) FetchByCustomerIDAndG7sOrgCodeAndG7sUserID(db *gorm.DB) error {
   337  	defer duration.PrintDuration(map[string]interface{}{
   338  		"request": "[DB]CustomerG7.FetchByCustomerIDAndG7sOrgCodeAndG7sUserID",
   339  	})()
   340  
   341  	err := db.Table(cg.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Find(cg).Error
   342  	if err == nil {
   343  		return nil
   344  	} else {
   345  		if err == gorm.RecordNotFound {
   346  			return dberr.RecordNotFoundError
   347  		} else {
   348  			logrus.Errorf("%s", err.Error())
   349  			return dberr.RecordFetchFailedError
   350  		}
   351  	}
   352  }
   353  
   354  func (cg *CustomerG7) FetchByCustomerIDAndG7sOrgCodeAndG7sUserIDForUpdate(db *gorm.DB) error {
   355  	defer duration.PrintDuration(map[string]interface{}{
   356  		"request": "[DB]CustomerG7.FetchByCustomerIDAndG7sOrgCodeAndG7sUserIDForUpdate",
   357  	})()
   358  
   359  	err := db.Table(cg.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Set("gorm:query_option", "FOR UPDATE").Find(cg).Error
   360  	if err == nil {
   361  		return nil
   362  	} else {
   363  		if err == gorm.RecordNotFound {
   364  			return dberr.RecordNotFoundError
   365  		} else {
   366  			logrus.Errorf("%s", err.Error())
   367  			return dberr.RecordFetchFailedError
   368  		}
   369  	}
   370  }
   371  
   372  func (cgl *CustomerG7List) FetchByG7sUserID(db *gorm.DB, g7sUserID string) error {
   373  	defer duration.PrintDuration(map[string]interface{}{
   374  		"request": "[DB]CustomerG7.FetchByG7sUserID",
   375  	})()
   376  
   377  	err := db.Table(CustomerG7{}.TableName()).Where("F_g7s_user_id = ? and F_enabled = ?", g7sUserID, enumeration.BOOL__TRUE).Find(cgl).Error
   378  	if err == nil {
   379  		return nil
   380  	} else {
   381  		logrus.Errorf("%s", err.Error())
   382  		return dberr.RecordFetchFailedError
   383  	}
   384  }
   385  
   386  func (cg *CustomerG7) FetchByG7sUserIDAndCustomerID(db *gorm.DB) error {
   387  	defer duration.PrintDuration(map[string]interface{}{
   388  		"request": "[DB]CustomerG7.FetchByG7sUserIDAndCustomerID",
   389  	})()
   390  
   391  	err := db.Table(cg.TableName()).Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Find(cg).Error
   392  	if err == nil {
   393  		return nil
   394  	} else {
   395  		if err == gorm.RecordNotFound {
   396  			return dberr.RecordNotFoundError
   397  		} else {
   398  			logrus.Errorf("%s", err.Error())
   399  			return dberr.RecordFetchFailedError
   400  		}
   401  	}
   402  }
   403  
   404  func (cg *CustomerG7) FetchByG7sUserIDAndCustomerIDForUpdate(db *gorm.DB) error {
   405  	defer duration.PrintDuration(map[string]interface{}{
   406  		"request": "[DB]CustomerG7.FetchByG7sUserIDAndCustomerIDForUpdate",
   407  	})()
   408  
   409  	err := db.Table(cg.TableName()).Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Set("gorm:query_option", "FOR UPDATE").Find(cg).Error
   410  	if err == nil {
   411  		return nil
   412  	} else {
   413  		if err == gorm.RecordNotFound {
   414  			return dberr.RecordNotFoundError
   415  		} else {
   416  			logrus.Errorf("%s", err.Error())
   417  			return dberr.RecordFetchFailedError
   418  		}
   419  	}
   420  }
   421  
   422  func (cgl *CustomerG7List) FetchByUpdateTime(db *gorm.DB, updateTime timelib.MySQLTimestamp) error {
   423  	defer duration.PrintDuration(map[string]interface{}{
   424  		"request": "[DB]CustomerG7.FetchByUpdateTime",
   425  	})()
   426  
   427  	err := db.Table(CustomerG7{}.TableName()).Where("F_update_time = ? and F_enabled = ?", updateTime, enumeration.BOOL__TRUE).Find(cgl).Error
   428  	if err == nil {
   429  		return nil
   430  	} else {
   431  		logrus.Errorf("%s", err.Error())
   432  		return dberr.RecordFetchFailedError
   433  	}
   434  }
   435  
   436  func (cgl *CustomerG7List) FetchList(db *gorm.DB, size, offset int32, query ...map[string]interface{}) (int32, error) {
   437  	defer duration.PrintDuration(map[string]interface{}{
   438  		"request": "[DB]CustomerG7.FetchList",
   439  	})()
   440  
   441  	var count int32
   442  	if len(query) == 0 {
   443  		query = append(query, map[string]interface{}{"F_enabled": enumeration.BOOL__TRUE})
   444  	} else {
   445  		if _, ok := query[0]["F_enabled"]; !ok {
   446  			query[0]["F_enabled"] = enumeration.BOOL__TRUE
   447  		}
   448  	}
   449  
   450  	if size <= 0 {
   451  		size = -1
   452  		offset = -1
   453  	}
   454  	var err error
   455  
   456  	err = db.Table(CustomerG7{}.TableName()).Where(query[0]).Count(&count).Limit(size).Offset(offset).Order("F_create_time desc").Find(cgl).Error
   457  
   458  	if err != nil {
   459  		logrus.Errorf("%s", err.Error())
   460  		return 0, dberr.RecordFetchFailedError
   461  	} else {
   462  		return int32(count), nil
   463  	}
   464  }
   465  
   466  func (cg *CustomerG7) SoftDeleteByCustomerIDAndG7sOrgCodeAndG7sUserID(db *gorm.DB) error {
   467  	defer duration.PrintDuration(map[string]interface{}{
   468  		"request": "[DB]CustomerG7.SoftDeleteByCustomerIDAndG7sOrgCodeAndG7sUserID",
   469  	})()
   470  
   471  	var updateMap = map[string]interface{}{}
   472  	updateMap["F_enabled"] = enumeration.BOOL__FALSE
   473  
   474  	if time.Time(cg.UpdateTime).IsZero() {
   475  		cg.UpdateTime = timelib.MySQLTimestamp(time.Now())
   476  	}
   477  
   478  	err := db.Table(cg.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Updates(updateMap).Error
   479  	if err != nil {
   480  		if mysql_err, ok := err.(*mysql.MySQLError); !ok {
   481  			logrus.Errorf("%s", err.Error())
   482  			return dberr.RecordDeleteFailedError
   483  		} else if mysql_err.Number != dberr.DuplicateEntryErrNumber {
   484  			logrus.Errorf("%s", err.Error())
   485  			return dberr.RecordDeleteFailedError
   486  		} else {
   487  			logrus.Warningf("%s", err.Error())
   488  			// 物理删除被软删除的数据
   489  			delErr := db.Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Delete(&CustomerG7{}).Error
   490  			if delErr != nil {
   491  				logrus.Errorf("%s", delErr.Error())
   492  				return dberr.RecordDeleteFailedError
   493  			}
   494  
   495  			return nil
   496  		}
   497  	} else {
   498  		return nil
   499  	}
   500  }
   501  
   502  func (cg *CustomerG7) SoftDeleteByG7sUserIDAndCustomerID(db *gorm.DB) error {
   503  	defer duration.PrintDuration(map[string]interface{}{
   504  		"request": "[DB]CustomerG7.SoftDeleteByG7sUserIDAndCustomerID",
   505  	})()
   506  
   507  	var updateMap = map[string]interface{}{}
   508  	updateMap["F_enabled"] = enumeration.BOOL__FALSE
   509  
   510  	if time.Time(cg.UpdateTime).IsZero() {
   511  		cg.UpdateTime = timelib.MySQLTimestamp(time.Now())
   512  	}
   513  
   514  	err := db.Table(cg.TableName()).Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Updates(updateMap).Error
   515  	if err != nil {
   516  		if mysql_err, ok := err.(*mysql.MySQLError); !ok {
   517  			logrus.Errorf("%s", err.Error())
   518  			return dberr.RecordDeleteFailedError
   519  		} else if mysql_err.Number != dberr.DuplicateEntryErrNumber {
   520  			logrus.Errorf("%s", err.Error())
   521  			return dberr.RecordDeleteFailedError
   522  		} else {
   523  			logrus.Warningf("%s", err.Error())
   524  			// 物理删除被软删除的数据
   525  			delErr := db.Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Delete(&CustomerG7{}).Error
   526  			if delErr != nil {
   527  				logrus.Errorf("%s", delErr.Error())
   528  				return dberr.RecordDeleteFailedError
   529  			}
   530  
   531  			return nil
   532  		}
   533  	} else {
   534  		return nil
   535  	}
   536  }
   537  
   538  func (cg *CustomerG7) UpdateByCustomerIDAndG7sOrgCodeAndG7sUserIDWithMap(db *gorm.DB, updateMap map[string]interface{}) error {
   539  	defer duration.PrintDuration(map[string]interface{}{
   540  		"request": "[DB]CustomerG7.UpdateByCustomerIDAndG7sOrgCodeAndG7sUserIDWithMap",
   541  	})()
   542  
   543  	if _, ok := updateMap["F_update_time"]; !ok {
   544  		updateMap["F_update_time"] = timelib.MySQLTimestamp(time.Now())
   545  
   546  	}
   547  	dbRet := db.Table(cg.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Updates(updateMap)
   548  	err := dbRet.Error
   549  	if err != nil {
   550  		if mysql_err, ok := err.(*mysql.MySQLError); !ok {
   551  			logrus.Errorf("%s", err.Error())
   552  			return dberr.RecordUpdateFailedError
   553  		} else if mysql_err.Number != dberr.DuplicateEntryErrNumber {
   554  			logrus.Errorf("%s", err.Error())
   555  			return dberr.RecordUpdateFailedError
   556  		} else {
   557  			return dberr.RecordConflictError
   558  		}
   559  	} else {
   560  		if dbRet.RowsAffected == 0 {
   561  			findErr := db.Table(cg.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Find(&CustomerG7{}).Error
   562  			if findErr == gorm.RecordNotFound {
   563  				return dberr.RecordNotFoundError
   564  			} else if findErr != nil {
   565  				return dberr.RecordUpdateFailedError
   566  			}
   567  			//存在有效数据记录,返回成功
   568  			return nil
   569  		} else {
   570  			return nil
   571  		}
   572  	}
   573  }
   574  
   575  func (cg *CustomerG7) UpdateByCustomerIDAndG7sOrgCodeAndG7sUserIDWithStruct(db *gorm.DB) error {
   576  	defer duration.PrintDuration(map[string]interface{}{
   577  		"request": "[DB]CustomerG7.UpdateByCustomerIDAndG7sOrgCodeAndG7sUserIDWithStruct",
   578  	})()
   579  
   580  	if time.Time(cg.UpdateTime).IsZero() {
   581  		cg.UpdateTime = timelib.MySQLTimestamp(time.Now())
   582  	}
   583  
   584  	dbRet := db.Table(cg.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Updates(cg)
   585  	err := dbRet.Error
   586  	if err != nil {
   587  		if mysql_err, ok := err.(*mysql.MySQLError); !ok {
   588  			logrus.Errorf("%s", err.Error())
   589  			return dberr.RecordUpdateFailedError
   590  		} else if mysql_err.Number != dberr.DuplicateEntryErrNumber {
   591  			logrus.Errorf("%s", err.Error())
   592  			return dberr.RecordUpdateFailedError
   593  		} else {
   594  			return dberr.RecordConflictError
   595  		}
   596  	} else {
   597  		if dbRet.RowsAffected == 0 {
   598  			findErr := db.Table(cg.TableName()).Where("F_customer_id = ? and F_g7s_org_code = ? and F_g7s_user_id = ? and F_enabled = ?", cg.CustomerID, cg.G7sOrgCode, cg.G7sUserID, enumeration.BOOL__TRUE).Find(&CustomerG7{}).Error
   599  			if findErr == gorm.RecordNotFound {
   600  				return dberr.RecordNotFoundError
   601  			} else if findErr != nil {
   602  				return dberr.RecordUpdateFailedError
   603  			}
   604  			//存在有效数据记录,返回成功
   605  			return nil
   606  		} else {
   607  			return nil
   608  		}
   609  	}
   610  }
   611  
   612  func (cg *CustomerG7) UpdateByG7sUserIDAndCustomerIDWithMap(db *gorm.DB, updateMap map[string]interface{}) error {
   613  	defer duration.PrintDuration(map[string]interface{}{
   614  		"request": "[DB]CustomerG7.UpdateByG7sUserIDAndCustomerIDWithMap",
   615  	})()
   616  
   617  	if _, ok := updateMap["F_update_time"]; !ok {
   618  		updateMap["F_update_time"] = timelib.MySQLTimestamp(time.Now())
   619  
   620  	}
   621  	dbRet := db.Table(cg.TableName()).Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Updates(updateMap)
   622  	err := dbRet.Error
   623  	if err != nil {
   624  		if mysql_err, ok := err.(*mysql.MySQLError); !ok {
   625  			logrus.Errorf("%s", err.Error())
   626  			return dberr.RecordUpdateFailedError
   627  		} else if mysql_err.Number != dberr.DuplicateEntryErrNumber {
   628  			logrus.Errorf("%s", err.Error())
   629  			return dberr.RecordUpdateFailedError
   630  		} else {
   631  			return dberr.RecordConflictError
   632  		}
   633  	} else {
   634  		if dbRet.RowsAffected == 0 {
   635  			findErr := db.Table(cg.TableName()).Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Find(&CustomerG7{}).Error
   636  			if findErr == gorm.RecordNotFound {
   637  				return dberr.RecordNotFoundError
   638  			} else if findErr != nil {
   639  				return dberr.RecordUpdateFailedError
   640  			}
   641  			//存在有效数据记录,返回成功
   642  			return nil
   643  		} else {
   644  			return nil
   645  		}
   646  	}
   647  }
   648  
   649  func (cg *CustomerG7) UpdateByG7sUserIDAndCustomerIDWithStruct(db *gorm.DB) error {
   650  	defer duration.PrintDuration(map[string]interface{}{
   651  		"request": "[DB]CustomerG7.UpdateByG7sUserIDAndCustomerIDWithStruct",
   652  	})()
   653  
   654  	if time.Time(cg.UpdateTime).IsZero() {
   655  		cg.UpdateTime = timelib.MySQLTimestamp(time.Now())
   656  	}
   657  
   658  	dbRet := db.Table(cg.TableName()).Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Updates(cg)
   659  	err := dbRet.Error
   660  	if err != nil {
   661  		if mysql_err, ok := err.(*mysql.MySQLError); !ok {
   662  			logrus.Errorf("%s", err.Error())
   663  			return dberr.RecordUpdateFailedError
   664  		} else if mysql_err.Number != dberr.DuplicateEntryErrNumber {
   665  			logrus.Errorf("%s", err.Error())
   666  			return dberr.RecordUpdateFailedError
   667  		} else {
   668  			return dberr.RecordConflictError
   669  		}
   670  	} else {
   671  		if dbRet.RowsAffected == 0 {
   672  			findErr := db.Table(cg.TableName()).Where("F_g7s_user_id = ? and F_customer_id = ? and F_enabled = ?", cg.G7sUserID, cg.CustomerID, enumeration.BOOL__TRUE).Find(&CustomerG7{}).Error
   673  			if findErr == gorm.RecordNotFound {
   674  				return dberr.RecordNotFoundError
   675  			} else if findErr != nil {
   676  				return dberr.RecordUpdateFailedError
   677  			}
   678  			//存在有效数据记录,返回成功
   679  			return nil
   680  		} else {
   681  			return nil
   682  		}
   683  	}
   684  }