package com.zksy.property.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.property.domain.ADeviceInfo;
import com.zksy.property.domain.AMaintenanceRecords;
import com.zksy.property.mapper.ADeviceInfoMapper;
import com.zksy.property.service.ADeviceInfoService;
import com.zksy.property.service.AMaintenanceRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author Administrator
* @description 针对表【a_device_info(设备信息表)】的数据库操作Service实现
* @createDate 2025-07-14 11:59:44
*/
@Service
public class ADeviceInfoServiceImpl extends ServiceImpl<ADeviceInfoMapper, ADeviceInfo>
    implements ADeviceInfoService{
    @Autowired
    private AMaintenanceRecordsService aMaintenanceRecordsService;

    @Override
    public Page<ADeviceInfo> findByPage(long pageNum, long pageSize, String deviceName, String deviceType, String deviceNumber) {
        Page<ADeviceInfo> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<ADeviceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(deviceName != null,ADeviceInfo::getDeviceName,deviceName);
        queryWrapper.like(deviceType != null,ADeviceInfo::getDeviceType,deviceType);
        queryWrapper.like(deviceNumber != null,ADeviceInfo::getDeviceNumber,deviceNumber);
        Page<ADeviceInfo> page1 = this.page(page, queryWrapper);
        return page1;
    }

    @Override
    public List<ADeviceInfo> getADeviceInfoList(String deviceName, String deviceType, String deviceNumber) {
        LambdaQueryWrapper<ADeviceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(deviceName != null,ADeviceInfo::getDeviceName,deviceName);
        queryWrapper.like(deviceType != null,ADeviceInfo::getDeviceType,deviceType);
        queryWrapper.like(deviceNumber != null,ADeviceInfo::getDeviceNumber,deviceNumber);
        List<ADeviceInfo> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<ADeviceInfo> getBySimplifiedHouseId(String simplifiedHouseId) {
        LambdaQueryWrapper<ADeviceInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ADeviceInfo::getSimplifiedHouseId,simplifiedHouseId);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional
    public boolean removeBatchId(String[] ids) {
        for (String id : ids) {
            //维修
            List<AMaintenanceRecords> maintenanceRecords = aMaintenanceRecordsService.getByDeviceId(id);
            if(maintenanceRecords != null && maintenanceRecords.size() > 0){
                //删除维修记录
                aMaintenanceRecordsService.removeBatchByIds(List.of(maintenanceRecords.stream().map(AMaintenanceRecords::getId).toArray(String[]::new)));
            }
            ADeviceInfo deviceInfo = this.getById(id);
            if(deviceInfo != null){
                this.removeById(id);
            }
            return true;
        }
        return false;
    }
}




