micro-document/internel/dao/members.go
2025-09-30 11:56:04 +08:00

333 lines
9.7 KiB
Go

package dao
import (
"errors"
"micro-document/api/members"
"micro-document/internel/model"
"micro-document/pkg/db"
"time"
"gorm.io/gorm"
)
type membersDao struct{}
var MembersDao = new(membersDao)
func (membersDao) AddMember(req *members.AddMemberReq) (err error) {
if req.Name == "" {
return errors.New("姓名不能为空")
}
err = db.DocDB.Model(&model.Members{}).Create(&model.Members{
Uuid: req.Uuid,
Name: req.Name,
Brief: req.Brief,
Introduction: req.Introduction,
Operator: req.Operator,
OperatorID: req.OperatorId,
}).Error
if err != nil {
return errors.New("新增成员失败")
}
//同步增加成员管理,委员会,董事会信息
err = db.DocDB.Model(&model.Management{}).Create(&model.Management{
Uuid: req.Uuid,
Name: req.Name,
Brief: req.Brief,
Introduction: req.Introduction,
}).Error
if err != nil {
return errors.New("新增成员管理失败")
}
err = db.DocDB.Model(&model.BoardOfDirectors{}).Create(&model.BoardOfDirectors{
Uuid: req.Uuid,
Name: req.Name,
Brief: req.Brief,
Introduction: req.Introduction,
}).Error
if err != nil {
return errors.New("新增董事会失败")
}
err = db.DocDB.Model(&model.CommitteeAppointments{}).Create(&model.CommitteeAppointments{
Uuid: req.Uuid,
Name: req.Name,
}).Error
if err != nil {
return errors.New("新增委员会失败")
}
return
}
func (membersDao) EditMember(req *members.EditMemberReq) (err error) {
if req.Name == "" {
return errors.New("姓名不能为空")
}
err = db.DocDB.Model(&model.Members{}).Where("uuid = ?", req.Uuid).Select("name", "brief", "introduction", "sort", "operator", "operator_id").Updates(&model.Members{
Name: req.Name,
Brief: req.Brief,
Introduction: req.Introduction,
Sort: req.Sort,
Operator: req.Operator,
OperatorID: req.OperatorId,
UpdatedAt: time.Now(),
}).Error
return
}
// 删除成员及其相关记录
func (membersDao) DeleteMember(req *members.DeleteMemberReq) (err error) {
return db.DocDB.Transaction(func(tx *gorm.DB) error {
// 检查所有相关记录的状态
if err := checkAllStatus(tx, req.Uuid); err != nil {
return err
}
// 删除所有相关记录
if err := deleteAllRecords(tx, req.Uuid); err != nil {
return err
}
return nil
})
}
// 检查所有记录状态
func checkAllStatus(tx *gorm.DB, uuid string) error {
tables := []struct {
model interface{}
name string
}{
{&model.Management{}, "成员管理"},
{&model.BoardOfDirectors{}, "董事会"},
{&model.CommitteeAppointments{}, "委员会"},
}
for _, table := range tables {
var count int64
err := tx.Model(table.model).Where("uuid = ? AND status = ?", uuid, 1).Count(&count).Error
if err != nil {
return errors.New("查询" + table.name + "失败")
}
if count > 0 {
return errors.New("该" + table.name + "已上架,不允许删除")
}
}
return nil
}
// 删除所有记录
func deleteAllRecords(tx *gorm.DB, uuid string) error {
tables := []struct {
model interface{}
name string
}{
{&model.Members{}, "成员"},
{&model.Management{}, "成员管理"},
{&model.BoardOfDirectors{}, "董事会"},
{&model.CommitteeAppointments{}, "委员会"},
}
for _, table := range tables {
err := tx.Model(table.model).Where("uuid = ?", uuid).Delete(table.model).Error
if err != nil {
return errors.New("删除" + table.name + "失败")
}
}
return nil
}
// 获取成员列表
func (membersDao) GetMemberList(req *members.GetMemberListReq) (data []model.Members, total int64, err error) {
dbQuery := db.DocDB.Model(&model.Members{})
if req.Filtrate != nil {
if req.Filtrate.Name != "" {
dbQuery = dbQuery.Where("name like ?", "%"+req.Filtrate.Name+"%")
}
}
//数据查询
dbQuery.Count(&total)
err = dbQuery.Scopes(db.Pagination(req.Page, req.PageSize)).Find(&data).Error
if err != nil {
return
}
return
}
// 通过uuid查询成员管理
func (membersDao) GetManagementByUuid(uuid string) (management model.Management, err error) {
management = model.Management{}
err = db.DocDB.Model(&model.Management{}).Where("uuid = ?", uuid).First(&management).Error
if err != nil {
return
}
return
}
// 通过uuid查询董事会
func (membersDao) GetBoardOfDirectorsByUuid(uuid string) (boardOfDirectors model.BoardOfDirectors, err error) {
boardOfDirectors = model.BoardOfDirectors{}
err = db.DocDB.Model(&model.BoardOfDirectors{}).Where("uuid = ?", uuid).First(&boardOfDirectors).Error
if err != nil {
return
}
return
}
// 通过uuid查询委员会
func (membersDao) GetCommitteeAppointmentsByUuid(uuid string) (committeeAppointments model.CommitteeAppointments, err error) {
committeeAppointments = model.CommitteeAppointments{}
err = db.DocDB.Model(&model.CommitteeAppointments{}).Where("uuid = ?", uuid).First(&committeeAppointments).Error
if err != nil {
return
}
return
}
// 编辑成员管理
func (membersDao) EditManagement(req *members.EditManagementReq) (err error) {
if req.Status == 0 {
return errors.New("状态不能为空")
}
if req.Introduction == "" {
return errors.New("详细介绍不能为空")
}
err = db.DocDB.Model(&model.Management{}).Where("uuid = ?", req.Uuid).Select("name", "image", "status", "brief", "introduction", "is_setting").Updates(&model.Management{
Name: req.Name,
Image: req.Image,
Status: req.Status,
Brief: req.Brief,
Introduction: req.Introduction,
IsSetting: 1,
}).Error
if err != nil {
return errors.New("编辑成员管理失败")
}
//更新主表操作人员信息和时间
err = db.DocDB.Model(&model.Members{}).Where("uuid = ?", req.Uuid).Updates(&model.Members{
Operator: req.Operator,
OperatorID: req.OperatorId,
UpdatedAt: time.Now(),
}).Error
if err != nil {
return errors.New("更新操作人员信息和时间失败")
}
return
}
// 编辑董事会
func (membersDao) EditBoardOfDirectors(req *members.EditBoardOfDirectorsReq) (err error) {
if req.Status == 0 {
return errors.New("状态不能为空")
}
if req.Introduction == "" {
return errors.New("详细介绍不能为空")
}
err = db.DocDB.Model(&model.BoardOfDirectors{}).Where("uuid = ?", req.Uuid).Select("name", "status", "brief", "introduction", "is_setting").Updates(&model.BoardOfDirectors{
Name: req.Name,
Status: req.Status,
Brief: req.Brief,
Introduction: req.Introduction,
IsSetting: 1,
}).Error
if err != nil {
return errors.New("编辑董事会失败")
}
//更新主表操作人员信息和时间
err = db.DocDB.Model(&model.Members{}).Where("uuid = ?", req.Uuid).Updates(&model.Members{
Operator: req.Operator,
OperatorID: req.OperatorId,
UpdatedAt: time.Now(),
}).Error
if err != nil {
return errors.New("更新操作人员信息和时间失败")
}
return
}
// 编辑委员会
func (membersDao) EditCommitteeAppointments(req *members.EditCommitteeAppointmentsReq) (err error) {
if req.AuditCommittee == 0 {
return errors.New("审计委员会不能为空")
}
if req.CompensationCommittee == 0 {
return errors.New("薪酬委员会不能为空")
}
if req.NominatingCommittee == 0 {
return errors.New("提名委员会不能为空")
}
if req.Status == 0 {
return errors.New("状态不能为空")
}
err = db.DocDB.Model(&model.CommitteeAppointments{}).Where("uuid = ?", req.Uuid).Updates(&model.CommitteeAppointments{
Name: req.Name,
Status: req.Status,
AuditCommittee: req.AuditCommittee,
CompensationCommittee: req.CompensationCommittee,
NominatingCommittee: req.NominatingCommittee,
IsSetting: 1,
}).Error
if err != nil {
return errors.New("编辑委员会失败")
}
//更新主表操作人员信息和时间
err = db.DocDB.Model(&model.Members{}).Where("uuid = ?", req.Uuid).Updates(&model.Members{
Operator: req.Operator,
OperatorID: req.OperatorId,
UpdatedAt: time.Now(),
}).Error
if err != nil {
return errors.New("更新操作人员信息和时间失败")
}
return
}
// =============批量查询成员管理,董事会,委员会接口==================
// 批量查询成员管理
func (membersDao) GetManagementByUuids(uuids []string) (map[string]model.Management, error) {
var managementList []model.Management
err := db.DocDB.Model(&model.Management{}).Where("uuid IN ?", uuids).Find(&managementList).Error
if err != nil {
return nil, err
}
// 转换为map
managementMap := make(map[string]model.Management)
for _, management := range managementList {
managementMap[management.Uuid] = management
}
return managementMap, nil
}
// 批量查询董事会
func (membersDao) GetBoardOfDirectorsByUuids(uuids []string) (map[string]model.BoardOfDirectors, error) {
var boardOfDirectorsList []model.BoardOfDirectors
err := db.DocDB.Model(&model.BoardOfDirectors{}).Where("uuid IN ?", uuids).Find(&boardOfDirectorsList).Error
if err != nil {
return nil, err
}
boardOfDirectorsMap := make(map[string]model.BoardOfDirectors)
for _, boardOfDirectors := range boardOfDirectorsList {
boardOfDirectorsMap[boardOfDirectors.Uuid] = boardOfDirectors
}
return boardOfDirectorsMap, nil
}
// 批量查询委员会
func (membersDao) GetCommitteeAppointmentsByUuids(uuids []string) (map[string]model.CommitteeAppointments, error) {
var committeeAppointmentsList []model.CommitteeAppointments
err := db.DocDB.Model(&model.CommitteeAppointments{}).Where("uuid IN ?", uuids).Find(&committeeAppointmentsList).Error
if err != nil {
return nil, err
}
committeeAppointmentsMap := make(map[string]model.CommitteeAppointments)
for _, committeeAppointments := range committeeAppointmentsList {
committeeAppointmentsMap[committeeAppointments.Uuid] = committeeAppointments
}
return committeeAppointmentsMap, nil
}