package acquirer_student

import (
	"context"
	"encoding/json"
	"git.i2edu.net/i2/i2-bill-api/model"
	"io/ioutil"
	"net/http"

	"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 AcquirerStudentRemarkLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *AcquirerStudentRemarkLogic) AcquirerStudentRemark(r *http.Request) (*types.Response, error) {
	// todo: add your logic here and delete this line
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	userId := l.svcCtx.GetUserIdByJwt(l.ctx)
	bean := new(model.I2billAcquirerStudentXorm)
	err = json.Unmarshal(body, bean)
	if bean.Id == 0 || userId != bean.UserId {
		return &types.Response{500, "参数错误", nil}, nil
	}
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	_, err = l.svcCtx.DB.Where("id = ?", bean.Id).Cols("remark").Update(bean)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{500, err.Error(), nil}, nil
	}
	return &types.Response{200, "", bean}, nil
}
