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.SecurityUtils;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentRegistration;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentSocialSecurityCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentWageCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentWageCardMapper;
import com.jeeplus.sys.feign.IRoleApi;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.OfficeDTO;
import com.jeeplus.sys.service.dto.RoleDTO;
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.*;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-10-11 15:11
 */
@Service
@Transactional
public class EnrollmentWageCardService extends ServiceImpl<EnrollmentWageCardMapper, EnrollmentWageCard> {

    @Resource
    private EnrollmentWageCardMapper wageCardMapper;

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

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

    public void updateStatusById(EnrollmentWageCard dto) {
        wageCardMapper.updateStatusById(dto.getId(), dto.getType());
    }

    public EnrollmentWageCard saveWageCard(EnrollmentWageCard registration) throws Exception {
        if (StringUtils.isNotEmpty(registration.getId())){

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

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

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

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

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

        return report;
    }
}
