package user

import (
	"context"
	"fmt"
	"git.i2edu.net/i2/go-zero/core/logx"
	"git.i2edu.net/i2/i2-bill-api/internal/svc"
	"git.i2edu.net/i2/i2-bill-api/internal/types"
)

type JzQrCodeLogic struct {
	logx.Logger
	ctx        context.Context
	svcCtx     *svc.ServiceContext
	UserId     int64
	SessionKey string
}

func NewJzQrCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) JzQrCodeLogic {
	return JzQrCodeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// JzQrCode 兼职人员兼职码
func (l *JzQrCodeLogic) JzQrCode() (*types.JzQrcodeResponse, error) {
	//uid, err := l.svcCtx.Wechat.GenUserQrId(svc.QrcodeJzPrefix, l.UserId, map[string]interface{}{
	//	"userId":    l.UserId,
	//	"timestamp": time.Now().Unix(),
	//	"type":      "JzQrCode",
	//})
	//fmt.Println("--uid=", uid)
	// l.svcCtx.Wechat.GetQrParams(qrcodeJzPrefix, uid, map[string]interface{}{})
	//if err != nil {
	//	return nil, err
	//}
	userId := l.svcCtx.GetUserIdByJwt(l.ctx)
	attr, err := l.svcCtx.Wechat.GenQrCode(fmt.Sprintf("%d", userId), l.svcCtx.Config.Weixin.SharePage)
	tx := l.svcCtx.DB.NewSession()
	_, err = tx.Where("user_id = ?", userId).Update(map[string]interface{}{"qr": attr.Url})
	if err != nil {
		tx.Rollback()
		return &types.JzQrcodeResponse{}, err
	}
	_, err = tx.Insert(attr)
	if err != nil {
		tx.Rollback()
		return &types.JzQrcodeResponse{}, err
	}
	return &types.JzQrcodeResponse{
		FileId: attr.Url,
	}, nil
}
