package com.jeeplus.human.enrollment.enrollmentRegistration.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentSocialSecurityCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentWageCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentSocialSecurityCardMapper;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-10-16 10:17
 */
@Service
@Transactional
public class EnrollmentSocialSecurityCardService extends ServiceImpl<EnrollmentSocialSecurityCardMapper, EnrollmentSocialSecurityCard> {

    @Resource
    private EnrollmentSocialSecurityCardMapper securityCardMapper;

    public void deleteByRegistrationId(String id){
        QueryWrapper<EnrollmentSocialSecurityCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enrollment_registration_id",id);
        securityCardMapper.delete(queryWrapper);
    }

    public EnrollmentSocialSecurityCard findById(String id) throws Exception {
        // 查询基础信息表
        EnrollmentSocialSecurityCard info = securityCardMapper.getById(id);
        return info;
    }

    public void updateStatusById(EnrollmentSocialSecurityCard dto) {
        securityCardMapper.updateStatusById(dto.getId(), dto.getType());
    }

    public EnrollmentSocialSecurityCard saveSecurityCard(EnrollmentSocialSecurityCard registration) throws Exception {
        if (StringUtils.isNotEmpty(registration.getId())){

            EnrollmentSocialSecurityCard report = securityCardMapper.getById(registration.getId());
            if (report != null){
                return update(registration);
            }
        }
        return add(registration);
    }
    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public EnrollmentSocialSecurityCard add(EnrollmentSocialSecurityCard reportData) throws Exception{

        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        EnrollmentSocialSecurityCard report = new EnrollmentSocialSecurityCard();
        BeanUtils.copyProperties(reportData, report);
        report.setCreateById(userDTO.getId());
        report.setCreateTime(new Date());
        report.setUpdateTime(new Date());
        report.setUpdateById(userDTO.getId());
        securityCardMapper.insert(report);

        return report;
    }
    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public EnrollmentSocialSecurityCard update(EnrollmentSocialSecurityCard reportData) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        EnrollmentSocialSecurityCard report = new EnrollmentSocialSecurityCard();
        BeanUtils.copyProperties(reportData, report);
        report.setUpdateById(userDTO.getId());
        report.setUpdateTime(new Date());
        securityCardMapper.updateById(report);

        return report;
    }
}
