package com.zksy.property.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.property.domain.AHouseType;
import com.zksy.property.domain.ARoom;
import com.zksy.property.mapper.AHouseTypeMapper;
import com.zksy.property.service.AHouseTypeService;
import com.zksy.property.service.ARoomService;
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 针对表【a_house_type(户型表)】的数据库操作Service实现
 * @createDate 2025-06-24 16:24:28
 */
@Service
public class AHouseTypeServiceImpl extends ServiceImpl<AHouseTypeMapper, AHouseType>
        implements AHouseTypeService {

    @Autowired
    private AHouseTypeMapper mapper;
    @Autowired
    private MinioFileStorageService minioFileStorageService;
    @Autowired
    private ARoomService roomService;

    @Override
    public List<AHouseType> getHouseTypeList(String accountNumber) {
        LambdaQueryWrapper<AHouseType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(accountNumber != null, AHouseType::getAccountNumber, accountNumber);
        lambdaQueryWrapper.orderByDesc(AHouseType::getUpdateTime);
        List<AHouseType> list = mapper.selectList(lambdaQueryWrapper);
        return list;
    }

    @Override
    public Page<AHouseType> findByPage(long pageNum, long pageSize, String accountNumber) {
        Page<AHouseType> page = new Page<>(pageNum, pageSize);
        QueryWrapper<AHouseType> queryWrapper = new QueryWrapper();
        queryWrapper.like(accountNumber != null, "account_number", accountNumber);
        queryWrapper.orderByDesc("update_time");
        Page<AHouseType> page1 = mapper.selectPage(page, queryWrapper);
        return page1;
    }

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

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

    @Override
    @Transactional
    public boolean removeBatchHouseType(String[] ids) {
        try {
            for (String id : ids) {
                List<ARoom> houseTypeIds = roomService.getByHouseTypeId(id);
                if (CollUtil.isNotEmpty(houseTypeIds)) {
                    throw new RuntimeException("请先删除该户型下的所有房间");
                }
                AHouseType houseType = getById(id);
                if (houseType != null) {
                    minioFileStorageService.deleteFile(houseType.getAccountUrl());
                }
                removeById(id);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public List<AHouseType> getByRoomType(Integer roomType) {
        LambdaQueryWrapper<AHouseType> aHouseTypeLambdaQueryWrapper = new LambdaQueryWrapper<AHouseType>()
                .eq(AHouseType::getRoomType, roomType);
        return mapper.selectList(aHouseTypeLambdaQueryWrapper);
    }
}




