package logic

import (
	"app.yhyue.com/moapp/jybase/common"
	"context"
	"jyBXCore/rpc/model/es"
	"jyBXCore/rpc/service"
	"jyBXCore/rpc/util"

	"jyBXCore/rpc/internal/svc"
	"jyBXCore/rpc/type/bxcore"

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

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

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

// 获取投标状态信息
func (l *ParticipateContentLogic) ParticipateContent(in *bxcore.ParticipateContentReq) (*bxcore.ParticipateContentRes, error) {
	// 验证权限
	result := &bxcore.ParticipateContentRes{}
	b, entRoleId := util.IsAllowedParticipate(in.AppId, in.MgoUserId, in.NewUserId, in.AccountId, in.EntAccountId, in.EntId, in.EntUserId, in.PositionId, in.PositionType)
	if !b {
		result.ErrMsg = "暂无权限"
		return result, nil
	}
	participateService := service.NewParticipateBid(in.EntId, in.EntUserId, in.PositionType, in.PositionId)
	participateService.EntRoleId = entRoleId
	// 信息id解密
	infoList, _ := service.DecodeId(in.Sid)
	if len(infoList) == 0 {
		result.ErrCode = -1
		result.ErrMsg = "信息id无效"
		return result, nil
	}
	// 根据标讯id 查询项目信息
	projectInfos := es.GetProjectByInfoId(infoList)
	if projectInfos == nil || len(*projectInfos) == 0 {
		return result, nil
	}
	// 验证身份
	projectId := common.ObjToString((*projectInfos)[0]["_id"])
	if !participateService.CheckBidPower(projectId, false) {
		result.ErrMsg = "没有查看权限"
		return result, nil
	}
	//  查询
	data, err := participateService.GetLastBidStatus(projectId)
	if err == nil {
		formatData := participateService.ParticipateContentFormat(data)
		return &bxcore.ParticipateContentRes{
			Data: &formatData,
		}, nil
	}
	return &bxcore.ParticipateContentRes{
		ErrCode: 0,
		ErrMsg:  err.Error(),
	}, nil
}
