package cn.com.ty.lift.ud.userresume.service.impl;

import cn.com.ty.lift.ud.recruitmentInfos.mapper.entity.RecruitmentDeliveryEntity;
import cn.com.ty.lift.ud.recruitmentInfos.service.IRecruitmentDeliveryService;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.userInfo.service.IUserInfoService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.userresume.mapper.UserResumeMapper;
import cn.com.ty.lift.ud.userresume.mapper.entity.UserResume;
import cn.com.ty.lift.ud.userresume.service.IUserResumeService;

import java.util.List;

/**
 * 用户简历信息 -ServiceImpl
 *
 * @author Xson
 * @date 2020-04-24
 */
@Service
@Transactional
public class UserResumeServiceImpl extends ServiceImpl<UserResumeMapper, UserResume> implements IUserResumeService {
    @Autowired
    private IRecruitmentDeliveryService recruitmentDeliveryService;
    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public RestResponse<UserResume> applyJob(UserResume entity) {
        UserResume userResume = this.getOne(new QueryWrapper<UserResume>().eq("user_id", entity.getUserId()));
        if (userResume != null){
            entity.setId(userResume.getId());
            this.updateById(entity);
        } else{
            this.save(entity);
        }


        RecruitmentDeliveryEntity rde = new RecruitmentDeliveryEntity();
        rde.setDataId(entity.getInfoId());
        rde.setResumeId(entity.getId());
        rde.setUserId(entity.getUserId());
        recruitmentDeliveryService.save(rde);

        return RestResponse.success(null, "申请成功");
    }


    @Override
    public RestResponse getResumeByUserId(Long userId) {
        if (userId == null){
            RestResponse.fail("用户id不能为空");
        }
        UserInfoEntity userInfo = userInfoService.getById(userId);
        if (userInfo == null){
            RestResponse.fail("当前用户不存在");
        }
        UserResume userResume = this.getOne(new QueryWrapper<UserResume>().eq("user_id", userId));

        return RestResponse.success(userResume);
    }
}
