You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.3 KiB
88 lines
2.3 KiB
package split
|
|
|
|
import (
|
|
"base/app/model"
|
|
"errors"
|
|
"fmt"
|
|
"git.oa00.com/go/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var ContactLogic = &contactLogic{}
|
|
|
|
type contactLogic struct {
|
|
}
|
|
|
|
type ContactItem struct {
|
|
Id uint `json:"id"`
|
|
Name string `json:"name"`
|
|
Phone string `json:"phone"`
|
|
Gender uint `json:"gender"`
|
|
RoleName string `json:"roleName"`
|
|
RoleId uint `json:"roleId"`
|
|
}
|
|
|
|
type ContactSearch struct {
|
|
Phone string
|
|
}
|
|
|
|
// All @Title 获取常用联系人
|
|
func (c *contactLogic) All(brokerId uint, search ContactSearch) (list []ContactItem, err error) {
|
|
var contacts []model.Broker
|
|
where := mysql.Db
|
|
if search.Phone != "" {
|
|
where = where.Where("phone like ?", fmt.Sprintf("%%%s%%", search.Phone))
|
|
} else {
|
|
where = where.Where("id in (?)", mysql.Db.Model(&model.BrokerClosely{}).Where("broker_id = ?", brokerId).Select("closely_broker_id"))
|
|
}
|
|
if mysql.Db.Where(where).Preload("Staff.Role").Find(&contacts).Error != nil {
|
|
err = errors.New("用户错误")
|
|
}
|
|
for _, contact := range contacts {
|
|
list = append(list, ContactItem{
|
|
Id: contact.Id,
|
|
Name: contact.Nickname,
|
|
Phone: contact.Phone,
|
|
Gender: contact.Gender,
|
|
RoleName: contact.Staff.Role.Name,
|
|
RoleId: contact.Staff.RoleId,
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
// Update @Title 更新常用联系人状态
|
|
func (c *contactLogic) Update(brokerId uint, closelyBrokerIds []uint) (err error) {
|
|
var contacts []model.BrokerClosely
|
|
if mysql.Db.Where("broker_id = ?", brokerId).Where("closely_broker_id in ?", closelyBrokerIds).Find(&contacts).RowsAffected != int64(len(closelyBrokerIds)) {
|
|
|
|
var unCloseBrokers []model.BrokerClosely
|
|
var mCloseIds = map[uint]uint{}
|
|
for _, contact := range contacts {
|
|
mCloseIds[contact.CloselyBrokerId] = 0
|
|
}
|
|
for _, id := range closelyBrokerIds {
|
|
if _, ok := mCloseIds[id]; !ok {
|
|
unCloseBrokers = append(unCloseBrokers, model.BrokerClosely{
|
|
BrokerId: brokerId,
|
|
CloselyBrokerId: id,
|
|
})
|
|
}
|
|
}
|
|
|
|
return mysql.Db.Transaction(func(tx *gorm.DB) error {
|
|
if tx.Create(&unCloseBrokers).RowsAffected != int64(len(unCloseBrokers)) {
|
|
return errors.New("更新失败")
|
|
}
|
|
if tx.Delete(&contacts).RowsAffected != int64(len(contacts)) {
|
|
return errors.New("更新失败")
|
|
}
|
|
return nil
|
|
})
|
|
|
|
} else {
|
|
mysql.Db.Delete(&contacts)
|
|
}
|
|
return
|
|
}
|