package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.EmployeeOnboarding;
import com.ruoyi.oa.domain.bo.EmployeeOnboardingBo;
import com.ruoyi.oa.domain.vo.EmployeeFilesVo;
import com.ruoyi.oa.domain.vo.EmployeeOnboardingVo;
import com.ruoyi.oa.domain.vo.FileUser;
import com.ruoyi.oa.domain.vo.UserFilesVo;
import com.ruoyi.oa.mapper.EmployeeOnboardingMapper;
import com.ruoyi.oa.service.IEmployeeFilesService;
import com.ruoyi.oa.service.IEmployeeOnboardingService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 入职管理Service业务层处理
 *
 * @author hdka
 * @date 2025-02-15
 */
@RequiredArgsConstructor
@Service
public class EmployeeOnboardingServiceImpl implements IEmployeeOnboardingService {

    private final EmployeeOnboardingMapper baseMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IEmployeeFilesService employeeFilesService;

    /**
     * 查询入职管理
     */
    @Override
    public EmployeeOnboardingVo queryById(Long onboardingId){
        EmployeeOnboardingVo employeeOnboardingVo = baseMapper.selectVoById(onboardingId);
        SysUser sysUser = userService.selectUserByIdAndNotDelFlag(employeeOnboardingVo.getUserId());
        employeeOnboardingVo.setUserName(sysUser.getUserName());
        employeeOnboardingVo.setNickName(sysUser.getNickName());
        employeeOnboardingVo.setEmail(sysUser.getEmail());
        employeeOnboardingVo.setPhonenumber(sysUser.getPhonenumber());
        employeeOnboardingVo.setBankCard(sysUser.getBankCard());
        employeeOnboardingVo.setIdCard(sysUser.getIdCard());
        employeeOnboardingVo.setDeptId(sysUser.getDeptId());
        return employeeOnboardingVo;

    }

    /**
     * 查询入职管理列表
     */
    @Override
    public TableDataInfo<EmployeeOnboardingVo> queryPageList(EmployeeOnboardingBo bo, PageQuery pageQuery) {
        QueryWrapper<EmployeeOnboarding> lqw = buildQueryWrapper(bo);
        Page<EmployeeOnboardingVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询入职管理列表
     */
    @Override
    public List<EmployeeOnboardingVo> queryList(EmployeeOnboardingBo bo) {
        QueryWrapper<EmployeeOnboarding> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<EmployeeOnboarding> buildQueryWrapper(EmployeeOnboardingBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<EmployeeOnboarding> lqw =new QueryWrapper<EmployeeOnboarding>();
        lqw.eq(bo.getUserId() != null, "eo.user_id", bo.getUserId());
        lqw.eq(bo.getJoiningDate() != null, "eo.joining_date", bo.getJoiningDate());
        lqw.eq("eo.del_flag", 0L);
        lqw.like(bo.getNickName()!=null,"su.nick_name",bo.getNickName());

        return lqw;
    }

    /**
     * 新增入职管理
     */
    @Override
    public Boolean insertByBo(EmployeeOnboardingBo bo) {
        EmployeeOnboarding add = BeanUtil.toBean(bo, EmployeeOnboarding.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setOnboardingId(add.getOnboardingId());
        }
        return flag;
    }

    /**
     * 修改入职管理
     */
    @Override
    public Boolean updateByBo(EmployeeOnboardingBo bo) {
        EmployeeOnboarding update = BeanUtil.toBean(bo, EmployeeOnboarding.class);
        validEntityBeforeSave(update);

        StringBuilder files = new StringBuilder();
        if (bo.getCheckInFile() != null) {
            files.append(bo.getCheckInFile())
                    .append(",");

        }
        if (bo.getRegisterFile() != null) {
            files.append(bo.getRegisterFile())
                    .append(",");
        }
        if (bo.getTrainFile() != null) {
            files.append(bo.getTrainFile())
                    .append(",");
        }
        UserFilesVo filesVo = new UserFilesVo();
        filesVo.setUserId(bo.getUserId());
        filesVo.setFiles(String.valueOf(files));
        employeeFilesService.insertByBo(filesVo);
        return baseMapper.updateByUserId(update);
    }

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

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

    @Override
    public EmployeeOnboardingVo queryByUserId(Long userId) {

        return baseMapper.selectVoByUserId(userId);
    }
}
