package com.lantone.external.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantone.common.api.CommonResult;
import com.lantone.common.util.BeanUtil;
import com.lantone.common.util.ListUtil;
import com.lantone.external.client.TranService;
import com.lantone.external.dto.GetLostBehospitalDTO;
import com.lantone.external.dto.client.AddLostBehospitalDTO;
import com.lantone.external.entity.BehospitalInfo;
import com.lantone.external.enums.IsPlacefileEnum;
import com.lantone.external.facade.base.BehospitalInfoFacade;
import com.lantone.external.vo.GetLostBehospitalVO;
import com.lantone.external.vo.client.AddLostBehospitalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

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

    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    private TranService tranService;

    public GetLostBehospitalDTO getLostBehospital(GetLostBehospitalVO getLostBehospitalVO) {
        QueryWrapper<BehospitalInfo> behospitalInfoQueryWrapper = new QueryWrapper<>();
        behospitalInfoQueryWrapper.eq("is_placefile", IsPlacefileEnum.Y.getKey());
        behospitalInfoQueryWrapper.between("behospital_date", getLostBehospitalVO.getBehospitalStartDate(), getLostBehospitalVO.getBehospitalEndDate());
        List<BehospitalInfo> behospitalInfoList = behospitalInfoFacade.list(behospitalInfoQueryWrapper);
        if (ListUtil.isEmpty(behospitalInfoList)) {
            return null;
        }

        List<AddLostBehospitalVO> addLostBehospitalVOList = BeanUtil.listCopyTo(behospitalInfoList, AddLostBehospitalVO.class);
        CommonResult<AddLostBehospitalDTO> commonResult = tranService.addLostBehospital(addLostBehospitalVOList);
        if (commonResult == null || commonResult.getData() == null) {
            return null;
        }

        GetLostBehospitalDTO getLostBehospitalDTO = new GetLostBehospitalDTO();
        getLostBehospitalDTO.setBehospitalCodes(commonResult.getData().getBehospitalCodes());
        return getLostBehospitalDTO;
    }

}