package com.zksy.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.info.domain.EEmployeeInfo;
import com.zksy.info.service.EEmployeeInfoService;
import com.zksy.info.mapper.EEmployeeInfoMapper;
import com.zksy.service.MinioFileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
* @author Administrator
* @description 针对表【e_employee_info(员工基本信息表)】的数据库操作Service实现
* @createDate 2025-06-27 11:30:27
*/
@Service
public class EEmployeeInfoServiceImpl extends ServiceImpl<EEmployeeInfoMapper, EEmployeeInfo>
    implements EEmployeeInfoService{
    @Autowired
    private MinioFileStorageService minioFileStorageService;
    @Override
    public Page<EEmployeeInfo> findByPage(long pageNum, long pageSize, String workNumber, String fullName) {
        Page<EEmployeeInfo> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<EEmployeeInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(workNumber != null,EEmployeeInfo::getWorkNumber,workNumber);
        queryWrapper.like(fullName != null,EEmployeeInfo::getFullName,fullName);
        queryWrapper.orderByDesc(EEmployeeInfo::getUpdateTime);
        Page<EEmployeeInfo> page1 = this.page(page, queryWrapper);
        return page1;
    }

    @Override
    public List<EEmployeeInfo> getEEmployeeInfoList(String workNumber, String fullName) {
        LambdaQueryWrapper<EEmployeeInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(workNumber != null,EEmployeeInfo::getWorkNumber,workNumber);
        queryWrapper.like(fullName != null,EEmployeeInfo::getFullName,fullName);
        queryWrapper.orderByDesc(EEmployeeInfo::getUpdateTime);
        List<EEmployeeInfo> list = this.list(queryWrapper);
        return list;
    }

    @Override
    @Transactional
    public boolean saveEEmployeeInfo(EEmployeeInfo eEmployeeInfo, MultipartFile multipartFile) {
        try {
            if (multipartFile != null) {
                String path = minioFileStorageService.uploadFile(multipartFile, "employeeInfo");
                eEmployeeInfo.setPhotoPath(path);
            }
            return save(eEmployeeInfo);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("保存失败");
        }
    }

    @Override
    @Transactional
    public boolean updateEEmployeeInfo(EEmployeeInfo eEmployeeInfo, MultipartFile multipartFile) {
        try {
            if (multipartFile != null) {
                // 删除minio文件
                minioFileStorageService.deleteFile(eEmployeeInfo.getPhotoPath());
                String path = minioFileStorageService.uploadFile(multipartFile, "employeeInfo");
                eEmployeeInfo.setPhotoPath(path);
            }
            return updateById(eEmployeeInfo);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("更新失败");
        }
    }

    @Override
    @Transactional
    public boolean removeBatchEEmployeeInfo(String[] ids) {
        try {
            for (String id : ids) {
                EEmployeeInfo employeeInfo = getById(id);
                if (employeeInfo != null) {
                    minioFileStorageService.deleteFile(employeeInfo.getPhotoPath());
                }
                removeById(id);
            }
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除失败");
        }
    }
}




