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

import java.time.Instant;
import java.util.UUID;
import com.ruoyi.web.modules.industryservice.domain.BizPasture;
import com.ruoyi.web.modules.industryservice.domain.BizYakAsset;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenApiResponse;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchPastureVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakAssetVo;

/**
 * 科研开放接口响应组装与实体脱敏转换。
 */
public final class ResearchOpenApiSupport
{
    private ResearchOpenApiSupport()
    {
    }

    public static <T> OpenApiResponse<T> success(T data)
    {
        OpenApiResponse<T> response = new OpenApiResponse<>();
        response.setCode(200);
        response.setMessage("success");
        response.setRequestId(UUID.randomUUID().toString());
        response.setData(data);
        response.setTimestamp(Instant.now().toString());
        return response;
    }

    public static <T> OpenApiResponse<T> error(int code, String message)
    {
        OpenApiResponse<T> response = new OpenApiResponse<>();
        response.setCode(code);
        response.setMessage(message);
        response.setRequestId(UUID.randomUUID().toString());
        response.setTimestamp(Instant.now().toString());
        return response;
    }

    public static ResearchPastureVo toPastureVo(BizPasture row)
    {
        if (row == null)
        {
            return null;
        }
        ResearchPastureVo vo = new ResearchPastureVo();
        vo.setPastureId(row.getId());
        vo.setPastureName(row.getPastureName());
        vo.setFarmType(row.getFarmType());
        vo.setCounty(row.getCounty());
        vo.setTown(row.getTown());
        vo.setRegionSummary(ResearchOpenApiMasking.regionSummary(row.getCounty(), row.getTown()));
        vo.setLatitude(ResearchOpenApiMasking.roundCoordinate(row.getLatitude()));
        vo.setLongitude(ResearchOpenApiMasking.roundCoordinate(row.getLongitude()));
        vo.setFloorArea(row.getFloorArea());
        vo.setScaleBreeding(row.getScaleBreeding());
        vo.setBreedSpecies(row.getBreedSpecies());
        vo.setIntroduction(row.getIntroduction());
        vo.setPersonInCharge(ResearchOpenApiMasking.maskPersonName(row.getPersonInCharge()));
        vo.setContactPhone(ResearchOpenApiMasking.maskPhone(row.getContactPhone()));
        return vo;
    }

    public static ResearchYakAssetVo toYakAssetVo(BizYakAsset row)
    {
        if (row == null)
        {
            return null;
        }
        ResearchYakAssetVo vo = new ResearchYakAssetVo();
        vo.setSubjectCode(ResearchOpenApiMasking.subjectCode(row.getId()));
        vo.setPastureName(row.getPastureName());
        vo.setEnclosureName(row.getEnclosureName());
        vo.setGender(row.getGender());
        vo.setCattleVariety(row.getCattleVariety());
        vo.setAgeMonths(row.getAgeMonths());
        vo.setBirthYear(ResearchOpenApiMasking.birthYear(row.getBirthDate()));
        vo.setBirthMonth(ResearchOpenApiMasking.birthMonth(row.getBirthDate()));
        vo.setAssetStatus(row.getAssetStatus());
        vo.setAssetStatusLabel(ResearchOpenApiMasking.assetStatusLabel(row.getAssetStatus()));
        vo.setEntryWeightKg(row.getEntryWeightKg());
        vo.setSource(row.getSource());
        vo.setBreedingMethod(row.getBreedingMethod());
        vo.setEntryCycle(row.getEntryCycle());
        vo.setRealtimeTemp(row.getRealtimeTemp());
        vo.setRealtimeSteps(row.getRealtimeSteps());
        vo.setEnvTemp(row.getEnvTemp());
        vo.setFatherSubjectCode(ResearchOpenApiMasking.pseudonymCode(row.getFatherYakNo(), "YP"));
        vo.setMotherSubjectCode(ResearchOpenApiMasking.pseudonymCode(row.getMotherYakNo(), "YM"));
        return vo;
    }
}
