package com.zksy.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.property.domain.ARoom;
import com.zksy.property.mapper.AHouseTypeMapper;
import com.zksy.property.service.ARoomService;
import com.zksy.property.mapper.ARoomMapper;
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.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
* @author Administrator
* @description 针对表【a_room(房间表)】的数据库操作Service实现
* @createDate 2025-06-26 09:44:49
*/
@Service
public class ARoomServiceImpl extends ServiceImpl<ARoomMapper, ARoom>
    implements ARoomService{
    @Autowired
    private ARoomMapper mapper;
    @Autowired
    private MinioFileStorageService minioFileStorageService;

    @Override
    public List<ARoom> getByHouseTypeId(String houseTypeId) {
        QueryWrapper<ARoom> queryWrapper = new QueryWrapper();
        queryWrapper.like(houseTypeId != null,"house_type_id",houseTypeId);
        List<ARoom> list = mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    @Transactional
    public boolean saveARoom(ARoom aRoom, MultipartFile multipartFile) {
        try {
            if (multipartFile != null) {
                String path = minioFileStorageService.uploadFile(multipartFile, "room");
                aRoom.setRoomPictureUrl(path);
            }
            return save(aRoom);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("添加失败");
        }
    }

    @Override
    @Transactional
    public boolean updateARoom(ARoom aRoom, MultipartFile multipartFile) {
        try {
            if (multipartFile != null) {
                // 删除minio文件
                minioFileStorageService.deleteFile(aRoom.getRoomPictureUrl());
                String path = minioFileStorageService.uploadFile(multipartFile, "room");
                aRoom.setRoomPictureUrl(path);
            }
            return updateById(aRoom);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("修改失败");
        }
    }

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




