package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.oa.domain.EmployeeFiles;
import com.ruoyi.oa.domain.vo.EmployeeFilesVo;
import com.ruoyi.oa.mapper.EmployeeFilesMapper;
import com.ruoyi.system.service.ISysUserService;
import liquibase.pro.packaged.A;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.EmployeeOffboardingBo;
import com.ruoyi.oa.domain.vo.EmployeeOffboardingVo;
import com.ruoyi.oa.domain.EmployeeOffboarding;
import com.ruoyi.oa.mapper.EmployeeOffboardingMapper;
import com.ruoyi.oa.service.IEmployeeOffboardingService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 离职管理Service业务层处理
 *
 * @author hdka
 * @date 2025-02-16
 */
@RequiredArgsConstructor
@Service
public class EmployeeOffboardingServiceImpl implements IEmployeeOffboardingService {

    private final EmployeeOffboardingMapper baseMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private EmployeeFilesMapper filesMapper;

    /**
     * 查询离职管理
     */
    @Override
    public EmployeeOffboardingVo queryById(Long offboardingId){
        EmployeeOffboardingVo employeeOffboardingVo = baseMapper.selectVoById(offboardingId);
        SysUser sysUser = userService.selectUserByIdAndNotDelFlag(employeeOffboardingVo.getUserId());
        employeeOffboardingVo.setNickName(sysUser.getUserName());
        return employeeOffboardingVo;
    }

    /**
     * 查询离职管理列表
     */
    @Override
    public TableDataInfo<EmployeeOffboardingVo> queryPageList(EmployeeOffboardingBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmployeeOffboarding> lqw = buildQueryWrapper(bo);
        Page<EmployeeOffboardingVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(item -> {
            SysUser sysUser = userService.selectUserByIdAndNotDelFlag(item.getUserId());
            item.setNickName(sysUser.getNickName());
            LambdaQueryWrapper<EmployeeFiles> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(EmployeeFiles::getUserId, sysUser.getUserId());
            List<EmployeeFilesVo> employeeFiles = filesMapper.selectVoList(queryWrapper);
            item.setFileList(employeeFiles);
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询离职管理列表
     */
    @Override
    public List<EmployeeOffboardingVo> queryList(EmployeeOffboardingBo bo) {
        LambdaQueryWrapper<EmployeeOffboarding> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmployeeOffboarding> buildQueryWrapper(EmployeeOffboardingBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmployeeOffboarding> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EmployeeOffboarding::getUserId, bo.getUserId());
        lqw.eq(bo.getResignationDate() != null, EmployeeOffboarding::getResignationDate, bo.getResignationDate());
        lqw.eq(bo.getExitInterview() != null, EmployeeOffboarding::getExitInterview, bo.getExitInterview());
        lqw.eq(bo.getHandoverCompleted() != null, EmployeeOffboarding::getHandoverCompleted, bo.getHandoverCompleted());
        lqw.eq(StringUtils.isNotBlank(bo.getExitReason()), EmployeeOffboarding::getExitReason, bo.getExitReason());
        lqw.eq(bo.getFinalSalary() != null, EmployeeOffboarding::getFinalSalary, bo.getFinalSalary());
        lqw.eq(bo.getResignationApplicationSubmitted() != null, EmployeeOffboarding::getResignationApplicationSubmitted, bo.getResignationApplicationSubmitted());
        lqw.eq(bo.getDataBackupCompleted() != null, EmployeeOffboarding::getDataBackupCompleted, bo.getDataBackupCompleted());
        lqw.eq(bo.getHandoverArranged() != null, EmployeeOffboarding::getHandoverArranged, bo.getHandoverArranged());
        lqw.eq(bo.getSalarySettled() != null, EmployeeOffboarding::getSalarySettled, bo.getSalarySettled());
        lqw.eq(bo.getBenefitsSettled() != null, EmployeeOffboarding::getBenefitsSettled, bo.getBenefitsSettled());
        lqw.eq(bo.getExitCertificateIssued() != null, EmployeeOffboarding::getExitCertificateIssued, bo.getExitCertificateIssued());
        lqw.eq(bo.getOffboardingConfirmed() != null, EmployeeOffboarding::getOffboardingConfirmed, bo.getOffboardingConfirmed());
        return lqw;
    }

    /**
     * 新增离职管理
     */
    @Override
    public Boolean insertByBo(EmployeeOffboardingBo bo) {
        EmployeeOffboarding add = BeanUtil.toBean(bo, EmployeeOffboarding.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOffboardingId(add.getOffboardingId());
        }
        return flag;
    }

    /**
     * 修改离职管理
     */
    @Override
    public Boolean updateByBo(EmployeeOffboardingBo bo) {
        EmployeeOffboarding update = BeanUtil.toBean(bo, EmployeeOffboarding.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmployeeOffboarding entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除离职管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
