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.CareRecordsPageReqVO;
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.MedicalCareRecordsDO;
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.MedicalCareRecordsMapper;
import cn.iocoder.yudao.module.system.enums.AttachmentsConstants;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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 MedicalCareRecordsServiceImpl implements MedicalCareRecordsService {

    @Resource
    private MedicalCareRecordsMapper careRecordsMapper;
    @Resource
    private AttachmentsMapper attachmentsMapper;
    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;
    @Resource
    private BuildService buildService;

    @Override
    public void createCareRecords(MedicalCareRecordsDO createReqVO) {
        // 插入
        careRecordsMapper.insert(createReqVO);

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

    @Override
    public void updateCareRecords(MedicalCareRecordsDO updateReqVO) {
        // 更新
        careRecordsMapper.updateById(updateReqVO);

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

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

    }

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


    @Override
    public MedicalCareRecordsDO getCareRecords(Integer id) {
        MedicalCareRecordsDO medicalCareRecordsDO = careRecordsMapper.selectById(id);

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

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

        return medicalCareRecordsDO;
    }

    @Override
    public PageResult<MedicalCareRecordsDO> getCareRecordsPage(CareRecordsPageReqVO pageReqVO) {
        PageResult<MedicalCareRecordsDO> pageResult = careRecordsMapper.selectPage(pageReqVO);
        for (MedicalCareRecordsDO medicalCareRecordsDO : pageResult.getList()) {
            medicalCareRecordsDO.setBedInfo(buildService.getFullBedName(Long.valueOf(medicalCareRecordsDO.getElderId())));
        }
        return pageResult;
    }

}