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.vo.FileUser;
import com.ruoyi.oa.domain.vo.UserFilesVo;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.EmployeeFilesBo;
import com.ruoyi.oa.domain.vo.EmployeeFilesVo;
import com.ruoyi.oa.domain.EmployeeFiles;
import com.ruoyi.oa.mapper.EmployeeFilesMapper;
import com.ruoyi.oa.service.IEmployeeFilesService;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 文件档案管理Service业务层处理
 *
 * @author hdka
 * @date 2025-03-08
 */
@RequiredArgsConstructor
@Service
public class EmployeeFilesServiceImpl implements IEmployeeFilesService {

    private final EmployeeFilesMapper baseMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询文件档案管理
     */
    @Override
    public EmployeeFilesVo queryById(Long fileId){
        return baseMapper.selectVoById(fileId);
    }

    /**
     * 查询文件档案管理列表
     */
    @Override
    public TableDataInfo<FileUser> queryPageList(EmployeeFilesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysUser> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.like(bo.getNickName() != null, SysUser::getNickName, bo.getNickName());
        Page<FileUser> result = baseMapper.selectFileUserVoPage(pageQuery.build(), userLambdaQueryWrapper);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文件档案管理列表
     */
    @Override
    public List<EmployeeFilesVo> queryList(EmployeeFilesBo bo) {
        LambdaQueryWrapper<EmployeeFiles> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmployeeFiles> buildQueryWrapper(EmployeeFilesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmployeeFiles> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, EmployeeFiles::getUserId, bo.getUserId());
        lqw.eq(bo.getFileType() != null, EmployeeFiles::getFileType, bo.getFileType());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), EmployeeFiles::getFileName, bo.getFileName());
        lqw.eq(StringUtils.isNotBlank(bo.getFilePath()), EmployeeFiles::getFilePath, bo.getFilePath());
        lqw.eq(bo.getUploadTime() != null, EmployeeFiles::getUploadTime, bo.getUploadTime());
        return lqw;
    }

    /**
     * 新增文件档案管理
     */
    @Override
    public Boolean insertByBo(UserFilesVo userFilesVo) {
        // 删除该用户所有的文件，重新填入
//        baseMapper.deleteByUserId(userFilesVo.getUserId());
        List<String> fileUrls = new ArrayList<>();
        if (userFilesVo.getUserId() != null){
            fileUrls = Arrays.stream( userFilesVo.getFiles().split(","))
                    .map(String::valueOf)
                    .collect(Collectors.toList());
        }

        fileUrls.forEach(file -> {
            EmployeeFiles employeeFiles = new EmployeeFiles();
            // 此处的filePath已经改为ossId
            employeeFiles.setFilePath(file);
            employeeFiles.setUserId(userFilesVo.getUserId());
            baseMapper.insert(employeeFiles);
        });
        return true;
    }

    /**
     * 修改文件档案管理
     */
    @Override
    public Boolean updateByBo(EmployeeFilesBo bo) {
        EmployeeFiles update = BeanUtil.toBean(bo, EmployeeFiles.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public UserFilesVo queryUserFilesVoByUserId(Long userId) {
        UserFilesVo userFilesVo = new UserFilesVo();
        SysUser sysUser = userService.selectUserById(userId);
        userFilesVo.setUserId(sysUser.getUserId());
        userFilesVo.setNickName(sysUser.getNickName());
        userFilesVo.setPhonenumber(sysUser.getPhonenumber());
        EmployeeFilesBo employeeFilesBo = new EmployeeFilesBo();
        employeeFilesBo.setUserId(userId);
        LambdaQueryWrapper<EmployeeFiles> employeeFilesLambdaQueryWrapper = buildQueryWrapper(employeeFilesBo);
        List<EmployeeFilesVo> list = baseMapper.selectVoListPlus(employeeFilesLambdaQueryWrapper);
        userFilesVo.setFileList(list);
        return userFilesVo;
    }
}
