package com.ruoyi.web.modules.hub.support;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.hub.domain.BizDataSendRecord;

/**
 * 数据发送记录入参校验。
 */
public final class DataSendRecordValidation
{
    private static final String DATE_PATTERN = "yyyy-MM-dd";

    private DataSendRecordValidation()
    {
    }

    public static void trimFieldsForSave(BizDataSendRecord row)
    {
        if (row == null)
        {
            return;
        }
        if (row.getSendContent() != null)
        {
            row.setSendContent(row.getSendContent().trim());
        }
        if (row.getReceiverName() != null)
        {
            row.setReceiverName(row.getReceiverName().trim());
        }
        if (row.getReceiverAddress() != null)
        {
            row.setReceiverAddress(row.getReceiverAddress().trim());
        }
        if (row.getRemark() != null)
        {
            String remark = row.getRemark().trim();
            row.setRemark(remark.isEmpty() ? null : remark);
        }
    }

    public static void validateSendTime(Date sendTime)
    {
        if (sendTime == null)
        {
            throw new ServiceException("发送时间不能为空");
        }
    }

    public static void validateSendContent(String sendContent)
    {
        if (StringUtils.isEmpty(sendContent))
        {
            throw new ServiceException("请填写发送内容");
        }
        if (sendContent.length() > DataSendRecordRules.MAX_SEND_CONTENT_LEN)
        {
            throw new ServiceException("发送内容不能超过2000个字符");
        }
    }

    public static void validateReceiverName(String receiverName)
    {
        if (StringUtils.isEmpty(receiverName)
                || receiverName.length() < DataSendRecordRules.MIN_RECEIVER_NAME_LEN
                || receiverName.length() > DataSendRecordRules.MAX_RECEIVER_NAME_LEN)
        {
            throw new ServiceException("接收方名称须为1～128个字符");
        }
    }

    public static void validateReceiverAddress(String receiverAddress)
    {
        if (StringUtils.isEmpty(receiverAddress)
                || receiverAddress.length() < DataSendRecordRules.MIN_RECEIVER_ADDRESS_LEN
                || receiverAddress.length() > DataSendRecordRules.MAX_RECEIVER_ADDRESS_LEN)
        {
            throw new ServiceException("接收方地址须为1～512个字符");
        }
    }

    public static void validateRemark(String remark)
    {
        if (remark != null && remark.length() > DataSendRecordRules.MAX_REMARK_LEN)
        {
            throw new ServiceException("备注不能超过500个字符");
        }
    }

    public static void validateRowForSave(BizDataSendRecord row)
    {
        trimFieldsForSave(row);
        validateSendTime(row.getSendTime());
        validateSendContent(row.getSendContent());
        validateReceiverName(row.getReceiverName());
        validateReceiverAddress(row.getReceiverAddress());
        validateRemark(row.getRemark());
    }

    public static void validateListDateRange(String startDate, String endDate)
    {
        boolean hasStart = StringUtils.isNotEmpty(startDate);
        boolean hasEnd = StringUtils.isNotEmpty(endDate);
        if (!hasStart && !hasEnd)
        {
            return;
        }
        Date start = hasStart ? parseQueryDate(startDate, "发送日期开始") : null;
        Date end = hasEnd ? parseQueryDate(endDate, "发送日期结束") : null;
        if (start != null && end != null && start.after(end))
        {
            throw new ServiceException("发送日期开始不能晚于结束");
        }
    }

    private static Date parseQueryDate(String text, String label)
    {
        try
        {
            return new SimpleDateFormat(DATE_PATTERN).parse(text.trim());
        }
        catch (ParseException ex)
        {
            throw new ServiceException(label + "格式须为 yyyy-MM-dd");
        }
    }
}
