package com.jeeplus.human.depart.registration.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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.human.depart.registration.domain.DepartRegistration;
import com.jeeplus.human.depart.registration.mapper.DepartMapper;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentKeyCard;
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-25 9:50
 */
@Service
@Transactional
public class DepartService extends ServiceImpl<DepartMapper, DepartRegistration> {

    @Resource
    private DepartMapper mapper;

    @Resource
    private IFlowableApi flowTaskService;

    /**
     * 离职申请信息
     * @param page
     * @param projectReportData
     * @return
     * @throws Exception
     */
    public IPage<DepartRegistration> findList(Page<DepartRegistration> page, DepartRegistration projectReportData) throws Exception{
        QueryWrapper<DepartRegistration> queryWrapper = QueryWrapperGenerator.buildQueryCondition (projectReportData,DepartRegistration.class);
        queryWrapper.eq("a.del_flag","0");
        if (StringUtils.isNotBlank(projectReportData.getDepartment())) {
            queryWrapper.eq("a.department",projectReportData.getDepartment());
        }
        if (StringUtils.isNotBlank(projectReportData.getMobile())) {
            queryWrapper.like("su.mobile",projectReportData.getMobile());
        }
        if (StringUtils.isNotBlank(projectReportData.getName())) {
            queryWrapper.like("a.name",projectReportData.getName());
        }
        IPage<DepartRegistration> list = mapper.findList(page, queryWrapper);
        list.getRecords().forEach(item -> {
            // 入库 修改申请
            if (StringUtils.isNotBlank(item.getTaskId()) && StringUtils.isNotBlank(item.getType())) {
                if ("2".equals(item.getType())) { // “审核中”的数据要获取数据审核人
                    item.setAuditUserIds(flowTaskService.getTaskAuditUsers(item.getTaskId()));  // 获取数据审核人
                }
            }
            //查询是否已办理完离职，离职证明文件
            if("5".equals(item.getRecoveryType())){
                //查询离职文件url

            }

        });
        return list;

    }

    public DepartRegistration saveRegistration(DepartRegistration registration) throws Exception {
        if (StringUtils.isNotEmpty(registration.getId())){

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

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

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

        return report;
    }

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

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

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

        mapper.insert(report);

        return report;
    }

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

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

    public String remove(String id) {
        mapper.deleteById(id);
        return "操作成功";
    }

    public Boolean selectComplete() {
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));
        QueryWrapper<DepartRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_by_id",userDTO.getId());
        queryWrapper.eq("type","5");
        queryWrapper.eq("del_flag","0");
        Integer integer = mapper.selectCount(queryWrapper);
        if (integer == 0) {
            return false;
        }else {
            return true;
        }
    }

    public DepartRegistration getByHandoverId(String handoverId) {
        // 查询基础信息表
        DepartRegistration info = mapper.getByHandoverId(handoverId);
        return info;
    }
}
