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

import java.util.Date;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.industryservice.domain.BizThreeAssetsProblemReport;

/**
 * 三资具体问题上报校验：村反推乡镇、等级枚举、描述长度。
 */
public final class ThreeAssetsProblemReportValidation
{
    private ThreeAssetsProblemReportValidation()
    {
    }

    public static void validateRowForSave(BizThreeAssetsProblemReport row, TownDeptSupport townDeptSupport)
    {
        if (row == null)
        {
            throw new ServiceException("参数不能为空");
        }
        if (row.getVillageDeptId() == null)
        {
            throw new ServiceException("所属村不能为空");
        }
        SysDept village = townDeptSupport.requireReportVillage(row.getVillageDeptId());
        if (village == null)
        {
            throw new ServiceException("所属村不合法");
        }
        SysDept town = townDeptSupport.requireReportTown(village.getParentId());
        if (town == null)
        {
            throw new ServiceException("所属村的上级乡镇不合法");
        }
        row.setVillageName(village.getDeptName());
        row.setTownDeptId(town.getDeptId());
        row.setTownName(town.getDeptName());

        if (row.getReportTime() == null)
        {
            row.setReportTime(new Date());
        }
        if (StringUtils.isEmpty(row.getProblemLevel()))
        {
            throw new ServiceException("问题等级不能为空");
        }
        if (!ThreeAssetsProblemReportRules.isValidLevel(row.getProblemLevel()))
        {
            throw new ServiceException("问题等级不合法");
        }
        String detail = StringUtils.trim(row.getProblemDetail());
        if (StringUtils.isEmpty(detail))
        {
            throw new ServiceException("问题详细描述不能为空");
        }
        if (detail.length() > ThreeAssetsProblemReportRules.MAX_DETAIL_LEN)
        {
            throw new ServiceException("问题详细描述不能超过"
                    + ThreeAssetsProblemReportRules.MAX_DETAIL_LEN + "字");
        }
        row.setProblemDetail(detail);

        String handle = StringUtils.trim(row.getHandleResult());
        if (StringUtils.isEmpty(handle))
        {
            row.setHandleResult(null);
        }
        else
        {
            if (handle.length() > ThreeAssetsProblemReportRules.MAX_HANDLE_RESULT_LEN)
            {
                throw new ServiceException("处理结果不能超过"
                        + ThreeAssetsProblemReportRules.MAX_HANDLE_RESULT_LEN + "字");
            }
            row.setHandleResult(handle);
        }
        if (StringUtils.isNotEmpty(row.getRemark()) && row.getRemark().length() > 500)
        {
            throw new ServiceException("备注不能超过500字");
        }
    }
}
