system_user_info_logic.go 5.7 KB
package user

import (
	"context"
	"fmt"
	"strconv"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/authlib"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type SystemUserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSystemUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUserInfoLogic {
	return &SystemUserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SystemUserInfoLogic) SystemUserInfo(req *types.SystemUserInfoRequest) (resp *types.SystemUserInfoResponse, err error) {
	var (
		conn      = l.svcCtx.DefaultDBConn()
		userToken = contextdata.GetUserTokenFromCtx(l.ctx)
		response  *authlib.DataUserMe
		company   *domain.Company
		companyId int64
		code      = tool.Krand(6, tool.KC_RAND_KIND_ALL)
	)
	if response, err = l.svcCtx.ApiAuthService.MeInfo(l.ctx, authlib.RequestUserMeQuery{
		Token: req.Token,
	}); err != nil {
		return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
	}

	companyId, _ = strconv.ParseInt(response.CurrentCompany.ID, 10, 64)
	resp = &types.SystemUserInfoResponse{}
	resp.UserName = response.User.NickName
	resp.UserId, _ = strconv.ParseInt(response.User.ID, 10, 64)
	resp.Avatar = response.User.Avatar
	resp.CompanyName = response.CurrentCompany.Name
	resp.CompanyId = companyId
	resp.Code = code

	if companyId != userToken.CompanyId {
		return nil, xerr.NewErrMsgErr("获取用户资料失败", fmt.Errorf("当前登录公司信息不匹配"))
	}
	company, err = l.svcCtx.CompanyRepository.FindOne(l.ctx, conn, userToken.CompanyId)
	// 新建公司
	if err == domain.ErrNotFound {
		company = &domain.Company{
			Id:   companyId,
			Name: response.CurrentCompany.Name,
			Logo: response.CurrentCompany.Logo,
			Code: code,
		}
		if company, err = l.svcCtx.CompanyRepository.Insert(l.ctx, conn, company); err != nil {
			return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
		}
		err = nil
		return
	}
	if err != nil {
		return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
	}
	resp.Code = company.Code
	// 更新公司
	if response.CurrentCompany != nil {
		var changed bool
		if response.CurrentCompany.Name != "" && response.CurrentCompany.Name != company.Name {
			company.Name = response.CurrentCompany.Name
			changed = true
		}
		if response.CurrentCompany.Logo != "" && response.CurrentCompany.Logo != company.Logo {
			company.Logo = response.CurrentCompany.Logo
			changed = true
		}
		if changed {
			if company, err = l.svcCtx.CompanyRepository.UpdateWithVersion(l.ctx, conn, company); err != nil {
				return nil, xerr.NewErrMsgErr("获取用户资料失败", err)
			}
		}
	}
	err = l.initSystemData(companyId)
	if err != nil {
		return nil, err
	}
	return
}

// 后台登录时检查/设置公司的初始数据
func (l *SystemUserInfoLogic) initSystemData(companyId int64) error {
	//  初始设置文章标签
	queryOption := domain.NewQueryOptions().WithCountOnly()
	conn := l.svcCtx.DefaultDBConn()
	cnt, _, err := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, companyId, queryOption)
	if err != nil {
		return xerr.NewErrMsgErr("初始话公司数据失败", err)
	}
	if cnt == 0 {
		articleTags := []*domain.ArticleTag{
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "紧急重要", Category: "紧急重要", Remark: "优先解决", SortBy: 1,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "不紧急不重要", Category: "紧急重要", Remark: "给别人做", SortBy: 2,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "紧急不重要", Category: "紧急重要", Remark: "有空再做", SortBy: 3,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "不紧急重要", Category: "紧急重要", Remark: "制定计划去做", SortBy: 4,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "大机会高风险", Category: "机会风险", Remark: "谨慎考虑专项讨论", SortBy: 5,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "大机会中风险", Category: "机会风险", Remark: "加大关注值得尝试", SortBy: 6,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "大机会低风险", Category: "机会风险", Remark: "全员投入抓紧落实", SortBy: 7,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "中机会高风险", Category: "机会风险", Remark: "专人跟踪成立项目", SortBy: 8,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "中机会中风险", Category: "机会风险", Remark: "讨论落实", SortBy: 9,
			},
			{
				Id: 0, CompanyId: companyId, Image: domain.Image{Url: "", Width: 0, Height: 0},
				Name: "中机会低风险", Category: "机会风险", Remark: "解决问题多手准备", SortBy: 10,
			},
		}
		err = l.svcCtx.ArticleTagRepository.CreateInBatches(l.ctx, conn, articleTags)
		if err != nil {
			return xerr.NewErrMsgErr("初始话公司数据失败", err)
		}
	}

	return nil
}