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

import com.ruoyi.common.core.domain.BaseValidator;
import com.ruoyi.common.core.domain.ValidateResult;
import com.ruoyi.ss.device.domain.vo.DeviceVO;
import com.ruoyi.ss.timeBill.domain.TimeBillVO;
import com.ruoyi.ss.timeBill.domain.dto.TimeBillAddOrderDTO;
import com.ruoyi.ss.timeBill.service.TimeBillValidator;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author wjh
 * 2024/7/24
 */
@Service
public class TimeBillValidatorImpl extends BaseValidator implements TimeBillValidator {
    @Override
    public ValidateResult checkInfo(DeviceVO device, TimeBillAddOrderDTO dto) {
        if (device == null || dto == null) {
            return error("参数错误");
        }

        if (!Objects.equals(dto.getDeviceStartTime(), device.getStartTime())) {
            return error("当前设备起步时间已发生变化，请刷新后重试");
        }
        if (!Objects.equals(dto.getDeviceStartUnit(), device.getStartUnit())) {
            return error("当前设备起步时间已发生变化，请刷新后重试");
        }
        if (dto.getDeviceStartPrice().compareTo(device.getStartPrice()) != 0) {
            return error("当前设备起步金额已发生变化，请刷新后重试");
        }
        if (!Objects.equals(dto.getDeviceOverTime(), device.getOverTime())) {
            return error("当前设备续期时间已发生变化，请刷新后重试");
        }
        if (!Objects.equals(dto.getDeviceOverUnit(), device.getOverUnit())) {
            return error("当前设备续期时间已发生变化，请刷新后重试");
        }
        if (dto.getDeviceOverPrice().compareTo(device.getOverPrice()) != 0) {
            return error("当前设备续期金额已发生变化，请刷新后重试");
        }

        return success();
    }

    @Override
    public boolean canClose(TimeBillVO bill, Long userId) {
        return this.isBelong(bill, userId);
    }

    @Override
    public boolean isBelong(TimeBillVO bill, Long userId) {
        return bill != null && bill.getUserId().equals(userId);
    }

    @Override
    public boolean canPay(TimeBillVO bill, Long userId) {
        return this.isBelong(bill, userId);
    }
}
