fix
wangpengfei
2023-07-21 81b6e4fb5859e273ba54d04e65739733eb7efeee
fix

fix the bug that the zero value does not change when updating
3个文件已修改
53 ■■■■■ 已修改文件
model/contacts.go 26 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/contact.go 22 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/serviceFeeManage.go 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/contacts.go
@@ -40,9 +40,12 @@
    return "contacts"
}
func NewContactSearch() *ContactSearch {
func NewContactSearch(db *gorm.DB) *ContactSearch {
    if db == nil {
        db = mysqlx.GetDB()
    }
    return &ContactSearch{
        Orm: mysqlx.GetDB(),
        Orm: db,
    }
}
@@ -69,7 +72,24 @@
func (slf *ContactSearch) Update(record *Contact) error {
    var db = slf.build()
    return db.Updates(record).Error
    m := map[string]interface{}{
        "name":        record.Name,
        "number":      record.Number,
        "client_id":   record.ClientId,
        "position":    record.Position,
        "phone":       record.Phone,
        "member_id":   record.MemberId,
        "is_first":    record.IsFirst,
        "wechat":      record.Wechat,
        "birthday":    record.Birthday,
        "email":       record.Email,
        "desc":        record.Desc,
        "country_id":  record.CountryId,
        "province_id": record.ProvinceId,
        "city_id":     record.CityId,
        "region_id":   record.RegionId,
    }
    return db.Updates(m).Error
}
func (slf *ContactSearch) Delete() error {
service/contact.go
@@ -3,6 +3,7 @@
import (
    "aps_crm/model"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/mysqlx"
    "gorm.io/gorm"
)
@@ -15,15 +16,14 @@
        return code
    }
    tx := model.NewContactSearch().Orm.Begin()
    tx := mysqlx.GetDB().Begin()
    // check isFirst
    errCode := setFirstContact(tx, contact)
    if errCode != ecode.OK {
        return errCode
    }
    err := model.NewContactSearch().Create(contact)
    err := model.NewContactSearch(tx).Create(contact)
    if err != nil {
        tx.Rollback()
        return ecode.ContactExist
@@ -35,13 +35,13 @@
func (ContactService) DeleteContact(id int) int {
    // check contact exist
    _, err := model.NewContactSearch().SetId(id).First()
    _, err := model.NewContactSearch(nil).SetId(id).First()
    if err != nil {
        return ecode.ContactNotExist
    }
    // delete contact
    err = model.NewContactSearch().SetId(id).Delete()
    err = model.NewContactSearch(nil).SetId(id).Delete()
    if err != nil {
        return ecode.ContactDeleteErr
    }
@@ -50,7 +50,7 @@
func (ContactService) GetContactList() ([]*model.ContactDetail, int) {
    // get contact list
    contacts, err := model.NewContactSearch().FindAll()
    contacts, err := model.NewContactSearch(nil).FindAll()
    if err != nil {
        return nil, ecode.ContactListErr
    }
@@ -59,7 +59,7 @@
func (ContactService) UpdateContact(contact *model.Contact) int {
    // check contact exist
    _, err := model.NewContactSearch().SetId(contact.Id).First()
    _, err := model.NewContactSearch(nil).SetId(contact.Id).First()
    if err != nil {
        return ecode.ContactNotExist
    }
@@ -69,14 +69,14 @@
        return code
    }
    tx := model.NewContactSearch().Orm.Begin()
    tx := mysqlx.GetDB().Begin()
    code = setFirstContact(tx, contact)
    if code != ecode.OK {
        return code
    }
    // update contact
    err = model.NewContactSearch().SetId(contact.Id).Update(contact)
    err = model.NewContactSearch(tx).SetId(contact.Id).Update(contact)
    if err != nil {
        tx.Rollback()
        return ecode.ContactUpdateErr
@@ -100,7 +100,7 @@
func setFirstContact(tx *gorm.DB, contact *model.Contact) int {
    if contact.IsFirst && contact.ClientId != 0 {
        err := model.NewContactSearch().SetClientId(contact.ClientId).UpdateByMap(map[string]interface{}{
        err := model.NewContactSearch(tx).SetClientId(contact.ClientId).UpdateByMap(map[string]interface{}{
            "is_first": false,
        })
        if err != nil {
@@ -114,7 +114,7 @@
// CheckContactExist check contact exist
func CheckContactExist(id int) int {
    tmp, err := model.NewContactSearch().SetId(id).First()
    tmp, err := model.NewContactSearch(nil).SetId(id).First()
    if err != nil {
        return ecode.ContactNotExist
    }
service/serviceFeeManage.go
@@ -3,13 +3,14 @@
import (
    "aps_crm/model"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/mysqlx"
)
type FeeManageService struct{}
func (FeeManageService) AddServiceFeeManage(serviceFeeManage *model.ServiceFeeManage) int {
    tx := model.NewContactSearch().Orm.Begin()
    tx := mysqlx.GetDB().Begin()
    err := model.NewClientSearch(tx).Create(serviceFeeManage.Client)
    if err != nil {
@@ -58,7 +59,7 @@
        return ecode.ServiceFeeManageNotExist
    }
    tx := model.NewContactSearch().Orm.Begin()
    tx := mysqlx.GetDB().Begin()
    err = model.NewServiceFeeManageSearch(tx).SetId(serviceFeeManage.Id).Update(serviceFeeManage)
    if err != nil {