package com.lantone.tran.facade;

import com.lantone.common.util.BeanUtil;
import com.lantone.common.util.DateUtil;
import com.lantone.common.util.ListUtil;
import com.lantone.tran.dto.AddLostBehospitalDTO;
import com.lantone.tran.entity.BehospitalInfo;
import com.lantone.tran.facade.base.BehospitalInfoFacade;
import com.lantone.tran.service.BehospitalInfoService;
import com.lantone.tran.vo.AddLostBehospitalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 住院病历信息入库简单示例-业务处理类
 * @author: rengb
 * @time: 2022/3/16 16:28
 */
@Component
public class BehospitalManagementFacade {

    @Autowired
    @Qualifier("behospitalInfoServiceImpl")
    private BehospitalInfoService behospitalInfoService;

    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;

    public AddLostBehospitalDTO addLostBehospital(List<AddLostBehospitalVO> addLostBehospitalVOList) {
        if (ListUtil.isEmpty(addLostBehospitalVOList)) {
            return null;
        }
        List<BehospitalInfo> behospitalInfoList = BeanUtil.listCopyTo(addLostBehospitalVOList, BehospitalInfo.class);
        Date now = DateUtil.now();
        behospitalInfoList.forEach(i -> {
            i.setGmtCreate(now);
            i.setGmtModified(now);
        });
        behospitalInfoService.saveOrUpdateBatch(behospitalInfoList);
        AddLostBehospitalDTO addLostBehospitalDTO = new AddLostBehospitalDTO();
        addLostBehospitalDTO.setBehospitalCodes(behospitalInfoList.stream().map(i -> i.getBehospitalCode()).collect(Collectors.toList()));
        return addLostBehospitalDTO;
    }

}