package cn.iocoder.yudao.module.system.service.biz;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.RecordsPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.AttachmentsDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicalRecordsDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.AttachmentsMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.MedicalRecordsMapper;
import cn.iocoder.yudao.module.system.enums.AttachmentsConstants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;



import java.time.LocalDateTime;
import java.util.List;




/**
 * 医疗记录主 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class MedicalRecordsServiceImpl implements MedicalRecordsService {

    @Resource
    private MedicalRecordsMapper recordsMapper;
    @Resource
    private AttachmentsMapper attachmentsMapper;
    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;
    @Resource
    private BuildService buildService;

    @Override
    public void createRecords(MedicalRecordsDO createReqVO) {
        // 插入
        recordsMapper.insert(createReqVO);

        List<AttachmentsDO> attachments = createReqVO.getAttachments();
        if (CollectionUtil.isNotEmpty(attachments)) {
            for (AttachmentsDO attachment : attachments) {
                attachment.setBusinessType(AttachmentsConstants.MEDICAL);
                attachment.setBusinessId(createReqVO.getRecordId());
                attachment.setTenantId(TenantContextHolder.getTenantId());
                attachment.setUploadTime(LocalDateTime.now());
                attachmentsMapper.insert(attachment);
            }
        }
    }

    @Override
    public void updateRecords(MedicalRecordsDO updateReqVO) {
        // 更新
        recordsMapper.updateById(updateReqVO);

        attachmentsMapper.delete(new LambdaQueryWrapperX<AttachmentsDO>()
                .eq(AttachmentsDO::getBusinessType, AttachmentsConstants.MEDICAL)
                .eq(AttachmentsDO::getBusinessId, updateReqVO.getRecordId()));

        List<AttachmentsDO> attachments = updateReqVO.getAttachments();
        if (CollectionUtil.isNotEmpty(attachments)) {
            for (AttachmentsDO attachment : attachments) {
                attachment.setBusinessType(AttachmentsConstants.MEDICAL);
                attachment.setBusinessId(updateReqVO.getRecordId());
                attachment.setTenantId(TenantContextHolder.getTenantId());
                attachment.setUploadTime(LocalDateTime.now());
                attachmentsMapper.insert(attachment);
            }
        }
    }

    @Override
    public void deleteRecords(Integer id) {
        // 删除
        recordsMapper.deleteById(id);
    }

    @Override
    public MedicalRecordsDO getRecords(Integer id) {
        MedicalRecordsDO medicalRecordsDO = recordsMapper.selectById(id);

        ElderlyInfoDO elderlyInfoDO = elderlyInfoMapper.selectById(medicalRecordsDO.getElderId());
        medicalRecordsDO.setElderlyInfo(elderlyInfoDO);
        medicalRecordsDO.setBedInfo(buildService.getFullBedName(Long.valueOf(medicalRecordsDO.getElderId())));

        List<AttachmentsDO> attachments = attachmentsMapper.selectList(new LambdaQueryWrapperX<AttachmentsDO>()
                .eq(AttachmentsDO::getBusinessType, AttachmentsConstants.MEDICAL)
                .eq(AttachmentsDO::getBusinessId, medicalRecordsDO.getRecordId()));
        medicalRecordsDO.setAttachments(attachments);

        return medicalRecordsDO;

    }

    @Override
    public PageResult<MedicalRecordsDO> getRecordsPage(RecordsPageReqVO pageReqVO) {
        PageResult<MedicalRecordsDO> pageResult = recordsMapper.selectPage(pageReqVO);
        for (MedicalRecordsDO medicalRecordsDO : pageResult.getList()) {
            medicalRecordsDO.setBedInfo(buildService.getFullBedName(Long.valueOf(medicalRecordsDO.getElderId())));
        }
        return pageResult;
    }

}