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

import cn.hutool.core.collection.CollectionUtil;
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.RoundRecordPageReqVO;
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.WardRoundRecordDO;
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.WardRoundRecordMapper;
import cn.iocoder.yudao.module.system.enums.AttachmentsConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

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


/**
 * 查房记录 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class WardRoundRecordServiceImpl implements WardRoundRecordService {

    @Resource
    private WardRoundRecordMapper roundRecordMapper;
    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;
    @Resource
    private AttachmentsMapper attachmentsMapper;
    @Resource
    private BuildService buildService;

    @Override
    public void createRoundRecord(WardRoundRecordDO wardRoundRecordDO) {
        // 插入
        roundRecordMapper.insert(wardRoundRecordDO);

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

    @Override
    public void updateRoundRecord(WardRoundRecordDO wardRoundRecordDO) {
        // 更新
        roundRecordMapper.updateById(wardRoundRecordDO);

        attachmentsMapper.delete(new LambdaQueryWrapperX<AttachmentsDO>()
                .eq(AttachmentsDO::getBusinessType, AttachmentsConstants.WARD_ROUND)
                .eq(AttachmentsDO::getBusinessId, wardRoundRecordDO.getId()));

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

    @Override
    public void deleteRoundRecord(Integer id) {
        // 删除
        roundRecordMapper.deleteById(id);

        attachmentsMapper.delete(new LambdaQueryWrapperX<AttachmentsDO>()
                .eq(AttachmentsDO::getBusinessType, AttachmentsConstants.WARD_ROUND)
                .eq(AttachmentsDO::getBusinessId, id));
    }

    @Override
    public WardRoundRecordDO getRoundRecord(Integer id) {
        WardRoundRecordDO wardRoundRecordDO = roundRecordMapper.selectById(id);

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

        List<AttachmentsDO> attachments = attachmentsMapper.selectList(new LambdaQueryWrapperX<AttachmentsDO>()
                .eq(AttachmentsDO::getBusinessType, AttachmentsConstants.WARD_ROUND)
                .eq(AttachmentsDO::getBusinessId, wardRoundRecordDO.getId()));
        wardRoundRecordDO.setAttachments(attachments);

        return wardRoundRecordDO;
    }

    @Override
    public PageResult<WardRoundRecordDO> getRoundRecordPage(RoundRecordPageReqVO pageReqVO) {
        PageResult<WardRoundRecordDO> pageResult = roundRecordMapper.selectPage(pageReqVO);
        for (WardRoundRecordDO wardRoundRecordDO : pageResult.getList()) {
            wardRoundRecordDO.setBedInfo(buildService.getFullBedName(Long.valueOf(wardRoundRecordDO.getElderId())));
        }
        return pageResult;
    }

}