package com.docus.server.convert;

import com.docus.core.util.DateUtil;
import com.docus.core.util.ListUtils;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.dto.recovery.rcvbasic.AddRcvBasicDTO;
import com.docus.server.dto.recovery.rcvbasic.EditRcvBasicDTO;
import com.docus.server.dto.recovery.rcvbasic.MedicalInfoExcelModel;
import com.docus.server.entity.recovery.RcvBasic;
import com.docus.server.vo.recovery.rcvbasic.RcvBasicVO;
import com.docus.server.vo.recovery.rcvbasic.SearchRcvBasicVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.docus.core.util.DateUtil.PATTERN_DATETIME;

/**
 * 患者信息表 服务转换器
 *
 * @author AutoGenerator
 * @since 2023-09-13
 */
@Mapper
public interface RcvBasicConvert {

    RcvBasicConvert INSTANCE = Mappers.getMapper(RcvBasicConvert.class);

    @Mappings({})
    RcvBasic convertDO(AddRcvBasicDTO addRcvBasicDTO);

    @Mappings({})
    RcvBasic convertDO(EditRcvBasicDTO editRcvBasicDTO);

    @Mappings({})
    List<RcvBasic> convertAddDOList(List<AddRcvBasicDTO> addRcvBasicDTO);

    default List<RcvBasic> convertUpdateDOList(List<AddRcvBasicDTO> updateDataList, List<RcvBasic> rcvBasicsDO) {
        Map<Long, AddRcvBasicDTO> idToDOMap = ListUtils.toMap(updateDataList, AddRcvBasicDTO::getId);

        List<RcvBasic> basics = new ArrayList<>();

        for (RcvBasic rcvBasic : rcvBasicsDO) {
            Long id = rcvBasic.getId();
            if (idToDOMap.containsKey(id)) {
                AddRcvBasicDTO addRcvBasicDTO = idToDOMap.get(id);

                rcvBasic.setAdmissId(addRcvBasicDTO.getAdmissId());
                rcvBasic.setAdmissTimes(addRcvBasicDTO.getAdmissTimes());
                rcvBasic.setSerialNumber(addRcvBasicDTO.getSerialNumber());
                rcvBasic.setName(addRcvBasicDTO.getName());
                rcvBasic.setAttendingName(addRcvBasicDTO.getAttendingName());
                rcvBasic.setDisDate(addRcvBasicDTO.getDisDate());
                rcvBasic.setDisDept(addRcvBasicDTO.getDisDept());
                rcvBasic.setDisDeptName(addRcvBasicDTO.getDisDeptName());
                basics.add(rcvBasic);
            }
        }

        return basics;

    }


    @Mappings({})
    List<RcvBasic> convertEditDOList(List<EditRcvBasicDTO> editRcvBasicDTO);

    @Mappings({})
    RcvBasicVO convertVO(RcvBasic rcvBasic);

    @Mappings({})
    List<RcvBasicVO> convertVO(List<RcvBasic> rcvBasicList);

    @Mappings({})
    PageResult<RcvBasicVO> convertVO(PageResult<RcvBasic> pageResult);

    @Mappings({})
    PageResult<SearchRcvBasicVO> convertPageVO(PageResult<RcvBasic> pageResult);

    default List<AddRcvBasicDTO> convert(List<Object> excelData) {
        List<AddRcvBasicDTO> list = new ArrayList<>();
        MedicalInfoExcelModel medicalInfoExcelModel;
        for (Object obj : excelData) {
            // 使用的模板下载，可以强转
            medicalInfoExcelModel = (MedicalInfoExcelModel) obj;
            AddRcvBasicDTO excelModelDTO = convert(medicalInfoExcelModel);
            list.add(excelModelDTO);
        }
        return list;
    }

    default AddRcvBasicDTO convert(MedicalInfoExcelModel medicalInfoExcelModel) {
        AddRcvBasicDTO AddRcvBasicDTO = new AddRcvBasicDTO();
        BeanUtils.copyProperties(medicalInfoExcelModel, AddRcvBasicDTO);
        AddRcvBasicDTO.setDisDate(DateUtil.parse(medicalInfoExcelModel.getDisDate(), PATTERN_DATETIME));
        return AddRcvBasicDTO;
    }


}

