package mkt_part_time_user

import (
	"context"
	"git.i2edu.net/i2/i2-bill-api/internal/svc"
	"git.i2edu.net/i2/i2-bill-api/internal/types"
	"git.i2edu.net/i2/i2-bill-api/model"

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

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

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

func (l *PartTimeUserUpdateLogic) PartTimeUserUpdate(req types.PartTimeUserAddRequest) (*types.Response, error) {
	// todo: add your logic here and delete this line
	userId := l.svcCtx.GetUserIdByJwt(l.ctx)

	var bean = new(model.I2billMktPartTimeUser)
	_, err := l.svcCtx.DB.Where("user_id = ? and del_flag = 0", userId).Get(bean)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{Code: 500, Msg: err.Error(), Data: nil}, nil
	}
	if bean.Id == 0 {
		return &types.Response{Code: 500, Msg: "你还没有申请成为兼职人员", Data: nil}, nil
	}
	if bean.Id != 0 {
		if bean.CheckState == 54 {
			return &types.Response{Code: 500, Msg: "你的申请兼职人员的个人信息已提交，请等待审核！", Data: nil}, nil
		}
		if bean.CheckState == 57 {
			return &types.Response{Code: 500, Msg: "你已经是兼职人员：", Data: nil}, nil
		}
	}
	bean.CheckState = 54
	bean.CheckDesc = ""
	bean.CityId = req.CityId
	bean.Sex = req.Sex
	bean.Name = req.Name
	bean.MkId = req.MkId
	bean.IponeNumber = req.IponeNumber
	_, err = l.svcCtx.DB.ID(bean.Id).AllCols().Update(*bean)
	if err != nil {
		logx.Error(err.Error())
		return &types.Response{Code: 500, Msg: err.Error(), Data: nil}, nil
	}
	return &types.Response{Code: 200, Msg: "", Data: nil}, nil
}
