notice_setting.go 6.5 KB
package service

import (
	"strconv"

	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/noticesetting/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/noticesetting/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/noticesetting/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_basic"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
)

// 推送消息配置
type NoticeSettingService struct {
}

//NoticeSettingList 推送消息配置列表
func (noticeSettingService *NoticeSettingService) NoticeSettingList(noticeSettingListQuery *query.NoticeSettingListQuery) (int64, interface{}, error) {

	creationBasicGateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
		noticeSettingListQuery.Operator,
	)
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		noticeSettingListQuery.Operator,
	)
	var orgIds []int
	if len(noticeSettingListQuery.OrgName) > 0 {
		orgResult, err := creationUserGateway.OrgSearch(allied_creation_user.ReqOrgSearch{
			Offset:       0,
			Limit:        100,
			CompanyId:    int(noticeSettingListQuery.Operator.CompanyId),
			IsOrg:        0,
			MatchOrgName: noticeSettingListQuery.OrgName,
		})
		if err != nil {
			if err != nil {
				return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
			}
		}
		if len(orgResult.Orgs) == 0 {
			return 0, []dto.NoticeSettingItem{}, nil
		}
		for _, v := range orgResult.Orgs {
			orgIds = append(orgIds, v.OrgID)
		}
	}

	result, err := creationBasicGateway.NoticeSettingSearch(allied_creation_basic.ReqNoticeSettingSearch{
		PageIndex:        noticeSettingListQuery.PageNumber,
		PageSize:         noticeSettingListQuery.PageSize,
		CompanyId:        noticeSettingListQuery.Operator.CompanyId,
		OrgIds:           orgIds,
		ModuleActionName: noticeSettingListQuery.ModuleAction,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	dataList := []dto.NoticeSettingItem{}

	for _, v := range result.List {

		item := dto.NoticeSettingItem{
			CompanyID:        v.CompanyID,
			Content:          v.Content,
			IsPush:           v.IsPush,
			Module:           v.Module,
			ModuleAction:     v.ModuleAction,
			NoticeSettingID:  v.NoticeSettingID,
			OrgID:            v.OrgID,
			ModuleActionName: v.ModuleActionName,
			AuthFlag:         v.OrgID == int(noticeSettingListQuery.Operator.OrgId),
		}
		orgData, _ := creationUserGateway.OrgGet(allied_creation_user.ReqOrgGet{
			OrgId: v.OrgID,
		})
		if orgData != nil {
			item.OrgName = orgData.OrgName
		}
		dataList = append(dataList, item)
	}

	return result.Count, dataList, nil
}

//NoticeSettingProfile 推送消息配置需求的参数候选项
func (noticeSettingService *NoticeSettingService) NoticeSettingProfile(noticeSettingProfileQuery *query.NoticeSettingProfileQuery) (interface{}, error) {

	creationBasicGateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
		noticeSettingProfileQuery.Operator,
	)
	result, err := creationBasicGateway.NoticeSettingProfile(allied_creation_basic.ReqNoticeSettingProfile{})
	return result, err
}

// NoticeSettingUpdate 更新配置
func (noticeSettingService *NoticeSettingService) NoticeSettingUpdate(noticeSettingUpdateCommand *command.NoticeSettingUpdateCommand) (interface{}, error) {
	creationBasicGateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
		noticeSettingUpdateCommand.Operator,
	)

	noticeSettingID, _ := strconv.Atoi(noticeSettingUpdateCommand.NoticeSettingId)
	_, err := creationBasicGateway.NoticeSettingUpdate(allied_creation_basic.ReqNoticeSettingUpdate{
		NoticeSettingID: noticeSettingID,
		CompanyID:       int(noticeSettingUpdateCommand.Operator.CompanyId),
		Content:         noticeSettingUpdateCommand.Content,
		IsPush:          noticeSettingUpdateCommand.IsPush,
		Module:          noticeSettingUpdateCommand.Module,
		ModuleAction:    noticeSettingUpdateCommand.ModuleAction,
		OrgId:           int(noticeSettingUpdateCommand.Operator.OrgId),
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return noticeSettingUpdateCommand, nil
}

// NoticeSettingUpdate 添加配置
func (noticeSettingService *NoticeSettingService) NoticeSettingAdd(noticeSettingAddCommand *command.NoticeSettingAddCommand) (interface{}, error) {

	creationBasicGateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
		noticeSettingAddCommand.Operator,
	)
	result, err := creationBasicGateway.NoticeSettingAdd(allied_creation_basic.ReqNoticeSettingAdd{
		CompanyID:    int(noticeSettingAddCommand.Operator.CompanyId),
		Content:      noticeSettingAddCommand.Content,
		IsPush:       noticeSettingAddCommand.IsPush,
		Module:       noticeSettingAddCommand.Module,
		ModuleAction: noticeSettingAddCommand.ModuleAction,
		OrgId:        int(noticeSettingAddCommand.Operator.OrgId),
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	data := struct {
		NoticeSettingId int `json:"noticeSettingId,string"`
		command.NoticeSettingAddCommand
	}{
		NoticeSettingId:         result.NoticeSettingID,
		NoticeSettingAddCommand: *noticeSettingAddCommand,
	}
	return data, nil
}

// NoticeSettingGet 获取配置
func (noticeSettingService *NoticeSettingService) NoticeSettingGet(noticeSettingGetQuery *query.NoticeSettingGetQuery) (interface{}, error) {

	creationBasicGateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
		noticeSettingGetQuery.Operator,
	)

	noticeSettingID, _ := strconv.Atoi(noticeSettingGetQuery.NoticeSettingId)
	result, err := creationBasicGateway.NoticeSettingGet(allied_creation_basic.ReqNoticeSettingGet{
		NoticeSettingID: noticeSettingID,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	data := dto.NoticeSettingItem{
		CompanyID:       result.CompanyID,
		Content:         result.Content,
		IsPush:          result.IsPush,
		Module:          result.Module,
		ModuleAction:    result.ModuleAction,
		NoticeSettingID: result.NoticeSettingID,
		OrgID:           result.OrgID,
		AuthFlag:        result.OrgID == int(noticeSettingGetQuery.Operator.OrgId),
	}
	return data, nil
}

func NewNoticeSettingService(options map[string]interface{}) *NoticeSettingService {
	newNoticeSettingService := &NoticeSettingService{}
	return newNoticeSettingService
}