package com.ruoyi.ss.abnormal.service.impl;

import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.abnormal.domain.Abnormal;
import com.ruoyi.ss.abnormal.domain.dto.AbnormalSubmitDTO;
import com.ruoyi.ss.abnormal.domain.enums.AbnormalStatus;
import com.ruoyi.ss.abnormal.service.AbnormalConverter;
import com.ruoyi.ss.abnormal.service.AbnormalService;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wjh
 * 2024/7/23
 */
@Service
public class AbnormalConverterImpl implements AbnormalConverter {

    @Autowired
    private AbnormalService abnormalService;

    @Autowired
    private DeviceService deviceService;

    @Override
    public Abnormal toPo(AbnormalSubmitDTO dto) {
        if (dto == null) {
            return null;
        }
        DeviceVO device = deviceService.selectByDeviceNo(dto.getDeviceNo());
        ServiceUtil.assertion(device == null, "设备不存在");

        Abnormal po = new Abnormal();
        po.setUserId(dto.getUserId());
        po.setDeviceId(device.getDeviceId());
        po.setContent(dto.getContent());
        po.setName(dto.getName());
        po.setMobile(dto.getMobile());
        po.setAddress(dto.getAddress());
        po.setStatus(AbnormalStatus.UNREAD.getStatus());
        po.setCreateBy(dto.getCreateBy());
        return po;
    }
}
