package logic

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

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

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

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

// 参标操作记录
func (l *ParticipateRecordsLogic) ParticipateRecords(in *bxcore.ParticipateRecordsReq) (*bxcore.ParticipateRecordsRes, error) {
	// 验证权限
	result := &bxcore.ParticipateRecordsRes{}
	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 := participateService.GetBidRecords(projectId, in.Page, in.PageSize)
	return &bxcore.ParticipateRecordsRes{
		Data: data,
	}, nil
}
