package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.entity.thmz.FileUpload;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.FileTypeEnum;
import cn.hnthyy.thmz.mapper.thmz.FileUploadMapper;
import cn.hnthyy.thmz.mapper.thmz.UserMapper;
import cn.hnthyy.thmz.service.thmz.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class FileUploadServiceImpl implements FileUploadService {
    @SuppressWarnings("all")
    @Autowired
    private FileUploadMapper fileUploadMapper;
    @SuppressWarnings("all")
    @Autowired
    private UserMapper userMapper;


    @Override
    public FileUpload queryByName(String name) {
        return fileUploadMapper.selectUserByName(name);
    }

    @Override
    public FileUpload queryByid(Long id) {
        return fileUploadMapper.selectUserById(id);
    }

    @Override
    @Transactional
    public void saveUploadFile(FileUpload fileUpload) {
       fileUploadMapper.insertFileUpload(fileUpload);
       if(FileTypeEnum.PROFILE_IMAGE.code.equals(fileUpload.getFileType()) && fileUpload.getCreateUser()!=null && fileUpload.getId()!=null){
           User user = new User();
           user.setId(fileUpload.getCreateUser());
           user.setProfileImage(fileUpload.getId());
           userMapper.updateUser(user);
       }
    }
}
