/*
 * 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.StaffQuery;
import com.tofly.dispatch.entity.DispatchExpertR;
import com.tofly.dispatch.entity.EmergencyStaff;
import com.tofly.dispatch.entity.Staff;
import com.tofly.dispatch.service.EmergencyStaffService;
import com.tofly.dispatch.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.dispatch.mapper.StaffMapper;

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

/**
 * 应急人员
 *
 * @author admin
 * @date Wed Nov 09 00:00:00 CST 2022
 */
@Service("staffService")
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
@Autowired
private EmergencyStaffService emergencyStaffService;


    @Override
    public boolean deleteByIds(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        QueryWrapper<EmergencyStaff> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("STAFF_ID", list);

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

    @Override
    public Page<Staff> getPageData(Page page, StaffQuery 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<Staff> getListByEmergencyId(Long emergencyId) {
       return this.baseMapper.getListByEmergencyId(emergencyId);
    }


}
