package com.ruoyi.doubledefense.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.constant.DeletedEnum;
import com.ruoyi.doubledefense.domain.DangerManage;
import com.ruoyi.doubledefense.dto.DeviceDTO;
import com.ruoyi.doubledefense.dto.DeviceStatisticsPCDTO;
import com.ruoyi.doubledefense.dto.res.DeviceResultDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.doubledefense.mapper.DeviceMapper;
import com.ruoyi.doubledefense.domain.Device;
import com.ruoyi.doubledefense.service.IDeviceService;
import org.springframework.util.CollectionUtils;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@Service
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public DeviceResultDTO selectDeviceById(String id) {
        DeviceDTO deviceDTO = new DeviceDTO();
        deviceDTO.setId(id);
        List<DeviceResultDTO> deviceResultDTOS = selectDeviceList(deviceDTO);
        if (CollectionUtils.isEmpty(deviceResultDTOS)) {
            return null;
        }
        return deviceResultDTOS.get(0);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param deviceDTO 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DeviceResultDTO> selectDeviceList(DeviceDTO deviceDTO) {
        return deviceMapper.selectDeviceList(deviceDTO);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param deviceDTO 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDevice(DeviceDTO deviceDTO) {
        Device device = new Device();
        BeanUtils.copyProperties(deviceDTO, device);
        device.setId(UUID.randomUUID().toString());
        device.setCreateDefault();
        device.setDeviceCode(System.currentTimeMillis() + "");
        return deviceMapper.insert(device);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param deviceDTO 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDevice(DeviceDTO deviceDTO) {
        Device device = new Device();
        BeanUtils.copyProperties(deviceDTO, device);
        device.setUpdateDefault();
        int updateById = deviceMapper.updateById(device);

        // 清空过期时间
        if (device.getExpirationDate() == null) {
            LambdaUpdateWrapper<Device> updateWrapper = new LambdaUpdateWrapper();
            updateWrapper.set(Device::getExpirationDate, null);
            updateWrapper.eq(Device::getId, device.getId());
            deviceMapper.update(null, updateWrapper);
        }
        return updateById;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByIds(String[] ids) {
        Device device = new Device();
        device.setIsDel(DeletedEnum.INVALID.getValue());
        device.setUpdateDefault();
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        return deviceMapper.update(device, queryWrapper);
    }

    /**
     * PC首页-设备管理统计
     *
     * @param companyCode
     * @return
     */
    @Override
    public DeviceStatisticsPCDTO deviceStatistics(String companyCode) {
        DeviceStatisticsPCDTO data = deviceMapper.deviceStatistics(companyCode);
        if (data == null)
            data = new DeviceStatisticsPCDTO();
        return data;
    }
}
