package com.jeeplus.human.practice.register.service;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.SecurityUtils;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.human.depart.handover.domain.HandoverRecovery;
import com.jeeplus.human.practice.register.domain.Registration;
import com.jeeplus.human.practice.register.domain.RegistrationBank;
import com.jeeplus.human.practice.register.mapper.RegistrationBankMapper;
import com.jeeplus.sys.feign.IOfficeApi;
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.TenantDTO;
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.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-09-13 14:11
 */
@Service
@Transactional
public class RegistrationBankService extends ServiceImpl<RegistrationBankMapper, RegistrationBank> {

    @Resource
    private RegistrationBankMapper mapper;

    public void deleteByRegistrationId(String id){
        QueryWrapper<RegistrationBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("register_id",id);
        mapper.delete(queryWrapper);
    }

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

    public void updateStatusById(RegistrationBank dto) {
        mapper.updateStatusById(dto.getId(), dto.getType());

    }

    public RegistrationBank saveRegistration(RegistrationBank accounts) throws Exception {
        if (StringUtils.isNotEmpty(accounts.getId())){

            RegistrationBank report = mapper.getById(accounts.getId());
            if (report != null){
                return update(accounts);
            }
        }
        return add(accounts);
    }

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public RegistrationBank add(RegistrationBank reportData) throws Exception{

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

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

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

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

        return report;
    }

}
