package work_experience

import (
	"context"
	"time"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"
	"wechat-api/internal/utils/dberrorhandler"

	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/zeromicro/go-zero/core/logx"
)

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

func NewCreateWorkExperienceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateWorkExperienceLogic {
	return &CreateWorkExperienceLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateWorkExperienceLogic) CreateWorkExperience(req *types.WorkExperienceInfo) (*types.BaseMsgResp, error) {
	var startDate, endDate *time.Time
	*startDate, _ = time.Parse("2006-01-02 15:04:05", *req.StartDateStr)
	if req.EndDateStr != nil && *req.EndDateStr != "" {
		*endDate, _ = time.Parse("2006-01-02 15:04:05", *req.EndDateStr)
	}

	_, err := l.svcCtx.DB.WorkExperience.Create().
		SetNotNilEmployeeID(req.EmployeeId).
		SetNotNilStartDate(startDate).
		SetNotNilEndDate(endDate).
		SetNotNilCompany(req.Company).
		SetNotNilExperience(req.Experience).
		Save(l.ctx)

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	return &types.BaseMsgResp{Msg: errormsg.CreateSuccess}, nil
}
