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

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.farming.domain.BizTechServicePersonnel;
import com.ruoyi.web.modules.farming.domain.dto.TechServicePersonnelSaveRequest;
import com.ruoyi.web.modules.farming.domain.vo.TechServicePersonnelVo;

/**
 * 科技服务人员填报校验、派生与对象转换。
 */
public final class TechServicePersonnelValidation
{
    private TechServicePersonnelValidation()
    {
    }

    public static void validateForSave(TechServicePersonnelSaveRequest request)
    {
        if (request == null)
        {
            throw new ServiceException("参数不能为空");
        }
        validateNonNegative("科技特派员男性人数", request.getEnvoyMaleCount());
        validateNonNegative("科技特派员女性人数", request.getEnvoyFemaleCount());
        validateNonNegative("科技特派员党员人数", request.getEnvoyPartyMemberCount());
        validateNonNegative("农业事业单位初级人数", request.getAgriJuniorCount());
        validateNonNegative("农业事业单位员级人数", request.getAgriClerkCount());
        validateNonNegative("农业事业单位中级人数", request.getAgriIntermediateCount());
        validateNonNegative("农业事业单位副高人数", request.getAgriAssociateSeniorCount());
        validateNonNegative("农业事业单位管理级人数", request.getAgriManagementCount());
    }

    public static void applyDerived(BizTechServicePersonnel row)
    {
        if (row == null)
        {
            return;
        }
        row.setEnvoyTotalCount(TechServicePersonnelRules.sumNullable(row.getEnvoyMaleCount(), row.getEnvoyFemaleCount()));
        row.setAgriTotalCount(TechServicePersonnelRules.sumNullable(
                row.getAgriJuniorCount(),
                row.getAgriClerkCount(),
                row.getAgriIntermediateCount(),
                row.getAgriAssociateSeniorCount(),
                row.getAgriManagementCount()));
    }

    public static BizTechServicePersonnel toEntity(TechServicePersonnelSaveRequest request)
    {
        BizTechServicePersonnel row = new BizTechServicePersonnel();
        row.setId(TechServicePersonnelRules.SINGLETON_ID);
        row.setEnvoyMaleCount(request.getEnvoyMaleCount());
        row.setEnvoyFemaleCount(request.getEnvoyFemaleCount());
        row.setEnvoyPartyMemberCount(request.getEnvoyPartyMemberCount());
        row.setAgriJuniorCount(request.getAgriJuniorCount());
        row.setAgriClerkCount(request.getAgriClerkCount());
        row.setAgriIntermediateCount(request.getAgriIntermediateCount());
        row.setAgriAssociateSeniorCount(request.getAgriAssociateSeniorCount());
        row.setAgriManagementCount(request.getAgriManagementCount());
        applyDerived(row);
        return row;
    }

    public static TechServicePersonnelVo toVo(BizTechServicePersonnel row)
    {
        if (row == null)
        {
            return null;
        }
        TechServicePersonnelVo vo = new TechServicePersonnelVo();
        vo.setEnvoyMaleCount(row.getEnvoyMaleCount());
        vo.setEnvoyFemaleCount(row.getEnvoyFemaleCount());
        vo.setEnvoyPartyMemberCount(row.getEnvoyPartyMemberCount());
        vo.setEnvoyTotalCount(row.getEnvoyTotalCount());
        vo.setAgriJuniorCount(row.getAgriJuniorCount());
        vo.setAgriClerkCount(row.getAgriClerkCount());
        vo.setAgriIntermediateCount(row.getAgriIntermediateCount());
        vo.setAgriAssociateSeniorCount(row.getAgriAssociateSeniorCount());
        vo.setAgriManagementCount(row.getAgriManagementCount());
        vo.setAgriTotalCount(row.getAgriTotalCount());
        vo.setUpdateBy(row.getUpdateBy());
        vo.setUpdateTime(row.getUpdateTime() != null ? row.getUpdateTime() : row.getCreateTime());
        return vo;
    }

    private static void validateNonNegative(String fieldName, Integer value)
    {
        if (!TechServicePersonnelRules.isNonNegative(value))
        {
            throw new ServiceException("【" + fieldName + "】不能为负数");
        }
    }
}
