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

import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.timeBill.domain.TimeBill;
import com.ruoyi.ss.timeBill.domain.dto.TimeBillAddOrderDTO;
import com.ruoyi.ss.timeBill.domain.enums.TimeBillStatus;
import com.ruoyi.ss.timeBill.service.TimeBillConverter;
import com.ruoyi.ss.timeBill.service.TimeBillService;
import com.ruoyi.ss.timeBill.service.TimeBillValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author wjh
 * 2024/7/24
 */
@Service
public class TimeBillConverterImpl implements TimeBillConverter {

    @Autowired
    private TimeBillService timeBillService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private TimeBillValidator timeBillValidator;

    @Override
    public TimeBill toPo(TimeBillAddOrderDTO dto) {
        if (dto == null) {
            return null;
        }
        DeviceVO device = dto.getDevice();

        // 判断信息是否准确
        ServiceUtil.assertion(timeBillValidator.checkInfo(device, dto));

        // 拼接数据
        TimeBill po = new TimeBill();
        po.setUserId(dto.getUserId());
        po.setDeviceId(dto.getDeviceId());
        po.setStatus(TimeBillStatus.USING.getStatus());
        po.setAmount(null);
        po.setDeviceStartTime(dto.getDeviceStartTime());
        po.setDeviceStartUnit(dto.getDeviceStartUnit());
        po.setDeviceStartPrice(dto.getDeviceStartPrice());
        po.setDeviceOverTime(dto.getDeviceOverTime());
        po.setDeviceOverUnit(dto.getDeviceOverUnit());
        po.setDeviceOverPrice(dto.getDeviceOverPrice());
        po.setStartTime(LocalDateTime.now());
        po.setMchId(device.getUserId());
        po.setDeviceNo(device.getDeviceNo());
        po.setDeviceMac(device.getMac());
        return po;
    }
}
