package acquirer_mkt_qr

import (
	"context"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"git.i2edu.net/i2/i2-bill-api/internal/utils"
	"git.i2edu.net/i2/i2-bill-api/model"
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"git.i2edu.net/i2/i2-bill-api/internal/svc"
	"git.i2edu.net/i2/i2-bill-api/internal/types"

	"git.i2edu.net/i2/go-zero/core/logx"
)

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

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

func (l *AcquirerMktQrUpdateLogic) AcquirerMktQrUpdate(r *http.Request) (*types.Response, error) {
	// todo: add your logic here and delete this line
	userId := l.svcCtx.GetUserIdByJwt(l.ctx)
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	bean := new(model.I2billAcquirerMktQrXorm)
	err = json.Unmarshal(body, bean)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	userInfo, err := model.GetI2bilUserInfo(userId, l.svcCtx.DB)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	var ty = "part"
	//mk
	if userInfo.ErpId != "" {
		erpId, err := model.GetErpUser("", userInfo.ErpId, l.svcCtx.Transformer)
		if err != nil {
			logx.Error(err.Error())
			return &types.Response{500, err.Error(), nil}, nil
		}
		if erpId == "" {
			return &types.Response{500, "未找到mk用户", nil}, nil
		}
		ty = "mk"
	} else {
		//兼职
		partUser, err := model.GetPartTimeXormByUserId(userId, l.svcCtx.DB)
		if err != nil {
			logx.Error(err.Error())
			return &types.Response{500, err.Error(), nil}, nil
		}
		if partUser.Id == 0 {
			return &types.Response{500, "请先申请成为兼职", nil}, nil
		}
		erpId, err := model.GetErpUser("", partUser.MkId, l.svcCtx.Transformer)
		if err != nil {
			logx.Error(err.Error())
			return &types.Response{500, err.Error(), nil}, nil
		}
		if erpId == "" {
			return &types.Response{500, "未找到mk用户", nil}, nil
		}
		bean.QudaoId = 1058
		bean.ActivityId = 0
	}
	acquirersQr := new(model.I2billAcquirerMktQrXorm)
	_, err = l.svcCtx.DB.Where("user_id = ? and del_flag = 0", userId).Get(acquirersQr)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	if acquirersQr.Id != 0 {
		bean.LastUpdateTime = time.Now()
		bean.LastUpdateBy = userId
		_, err = l.svcCtx.DB.ID(acquirersQr.Id).AllCols().Update(bean)
		if err != nil {
			logx.Error(err.Error())
			return &types.Response{500, err.Error(), nil}, nil
		}
		return &types.Response{200, "", nil}, nil
	}
	bean.LastUpdateBy = userId
	bean.LastUpdateTime = time.Now()
	bean.CreateBy = userId
	bean.CreateTime = bean.LastUpdateTime
	bean.DelFlag = 0
	bean.UserId = userId
	//生成二维码
	scene, _ := utils.AesEncrypt([]byte(fmt.Sprintf("%d", userId)), []byte(l.svcCtx.Config.AesSecret))
	sceneStr := fmt.Sprintf("sign=%s&type=%s", base64.StdEncoding.EncodeToString(scene), ty)
	uri, err := l.svcCtx.Wechat.GenQrCode(sceneStr, "/pages/code/code")
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	bean.Qr = uri
	_, err = l.svcCtx.DB.Insert(bean)
	bean.Qr = strings.TrimLeft(bean.Qr, "/")
	domain := strings.TrimRight(l.svcCtx.Config.Domain, "/")
	bean.Qr = domain + "/" + bean.Qr
	if err != nil {
		return &types.Response{500, err.Error(), nil}, nil
	}
	return &types.Response{200, "", bean}, nil
}
