/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.dispatch.service.impl;

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.tofly.dispatch.dto.FacilitiesQuery;
import com.tofly.dispatch.entity.EmergencyFacilities;
import com.tofly.dispatch.entity.EmergencyStaff;
import com.tofly.dispatch.entity.Facilities;
import com.tofly.dispatch.service.EmergencyFacilitiesService;
import com.tofly.dispatch.service.FacilitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.dispatch.mapper.FacilitiesMapper;

import java.util.Arrays;
import java.util.List;

/**
 * 应急设施
 *
 * @author admin
 * @date Wed Nov 09 00:00:00 CST 2022
 */
@Service("facilitiesService")
public class FacilitiesServiceImpl extends ServiceImpl<FacilitiesMapper, Facilities> implements FacilitiesService {
@Autowired
private EmergencyFacilitiesService emergencyFacilitiesService;
    @Override
    public boolean deleteByIds(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        QueryWrapper<EmergencyFacilities> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("FACILITIES_ID", list);

        Integer count_check = emergencyFacilitiesService.count(queryWrapper);
        if (count_check > 0) {
            throw new RuntimeException("应急设施在应急预案中使用，不能删除");
        }
        return this.removeByIds(list);
    }

    @Override
    public Page<Facilities> getPageData(Page page, FacilitiesQuery query) {
        if (query.getStartTime() != null && query.getStartTime() != null) {
            if (query.getStartTime().getTime() > query.getEndTime().getTime()) {
                throw new RuntimeException("开始时间不能大于结束时间");
            }
        }

        return this.baseMapper.getPageData(page, query);
    }

    @Override
    public List<Facilities> getListByEmergencyId(Long emergencyId) {
      return this.baseMapper.getListByEmergencyId(emergencyId);
    }
}
