package acquirer_mkt_qr

import (
	"context"
	"encoding/json"
	"fmt"
	"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.GetAcquirePermInfo(userId, l.svcCtx.Transformer, l.svcCtx.DB, l.ctx)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	if userInfo == nil || userInfo.UserId == "" {
		return &types.Response{500, "请先申请成为兼职", nil}, nil
	}
	//var ty = "part"
	//mk
	if userInfo.Role == 2 {
		bean.Role = 2
		if bean.QuaoYji == 0 && bean.CallType == 0 && bean.MaType == 0 && bean.NetworkDetailId == 0 {
			bean.QuaoYji = 1001
			bean.CallType = 1007
			bean.MaType = 1445
			bean.NetworkDetailId = 1058
			bean.ActivityId = 0
		}
		//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
		}
		if partUser.MkId == "" {
			return &types.Response{500, "请联系管理员,分配mk", nil}, nil
		}
		erpUser, err := model.GetErpUser("", partUser.MkId, l.svcCtx.Transformer, l.ctx)
		if err != nil {
			logx.Error(err.Error())
			return &types.Response{500, err.Error(), nil}, nil
		}
		if erpUser == nil || erpUser.UserId == "" {
			return &types.Response{500, "未找到分配有效的mk", nil}, nil
		}
		bean.QuaoYji = 1001
		bean.CallType = 1007
		bean.MaType = 1445
		bean.NetworkDetailId = 1058
		bean.ActivityId = 0
		bean.Role = 1
	}
	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 {
		acquirersQr.LastUpdateTime = time.Now()
		acquirersQr.LastUpdateBy = userId
		acquirersQr.SchoolId = bean.SchoolId
		acquirersQr.ActivityId = bean.ActivityId
		acquirersQr.QuaoYji = bean.QuaoYji
		acquirersQr.CallType = bean.CallType
		acquirersQr.MaType = bean.MaType
		acquirersQr.NetworkDetailId = bean.NetworkDetailId
		acquirersQr.Role = bean.Role
		_, err = l.svcCtx.DB.ID(acquirersQr.Id).Cols("school_id", "activity_id", "quao_yji", "call_type", "ma_type", "network_detail_id", "last_update_time", "last_update_by", "role").Update(acquirersQr)
		if err != nil {
			logx.Error(err.Error())
			return &types.Response{500, err.Error(), nil}, nil
		}
		acquirersQr.Qr = strings.TrimLeft(acquirersQr.Qr, "/")
		domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
		acquirersQr.Qr = domain + "/" + bean.Qr
		return &types.Response{200, "", bean}, 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("%d", userId)
	atr, err := l.svcCtx.Wechat.GenQrCode(sceneStr, l.svcCtx.Config.Weixin.SharePage)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	if atr == nil || atr.Url == "" {
		return &types.Response{500, "二维码生成失败", nil}, nil
	}
	_, err = l.svcCtx.DB.Insert(atr)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	bean.Qr = atr.Url
	_, err = l.svcCtx.DB.Insert(bean)
	bean.Qr = strings.TrimLeft(bean.Qr, "/")
	domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
	bean.Qr = domain + "/" + bean.Qr
	if err != nil {
		return &types.Response{500, err.Error(), nil}, nil
	}
	return &types.Response{200, "", bean}, nil
}
