package digital.laboratory.platform.imr.convert;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import digital.laboratory.platform.imr.dto.DrugUsageRecordDTO;
import digital.laboratory.platform.imr.entity.DrugUsageRecord;
import digital.laboratory.platform.imr.vo.DrugUsageRecordVO;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 毒品检材的领用归还记录表相关 实体类转换
 */
public class DrugUsageRecordConvert {

    public static DrugUsageRecord dtoToEntity(DrugUsageRecordDTO dto) {
        if (dto == null) return null;

        DrugUsageRecord drugUsageRecord = new DrugUsageRecord();
        drugUsageRecord.setDrugId(dto.getDrugIds().stream().collect(Collectors.joining(",")));
        drugUsageRecord.setUsageDate(dto.getUsageDate());
        drugUsageRecord.setUsagePurpose(dto.getUsagePurpose());
        return drugUsageRecord;
    }

    public static DrugUsageRecordVO entityToVO(DrugUsageRecord entity) {
        if (entity == null) return null;

        DrugUsageRecordVO drugUsageRecordVO = new DrugUsageRecordVO();
        drugUsageRecordVO.setId(entity.getId());
        drugUsageRecordVO.setDrugIds(StrUtil.split(entity.getDrugId(), StrUtil.COMMA));
        drugUsageRecordVO.setUsageName(entity.getUsageName());
        drugUsageRecordVO.setUsageDate(entity.getUsageDate());
        drugUsageRecordVO.setUsagePurpose(entity.getUsagePurpose());
        drugUsageRecordVO.setApplicant(entity.getApplicant());
        drugUsageRecordVO.setApplicantName(entity.getApplicantName());
        return drugUsageRecordVO;
    }

    public static List<DrugUsageRecordVO> entityToVOList(List<DrugUsageRecord> entityList) {
        if (CollUtil.isEmpty(entityList)) return Collections.emptyList();
        return entityList.stream().map(entity -> entityToVO(entity)).collect(Collectors.toList());
    }
}
