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.controller.admin.biz.vo.TrainingRecordsPageReqVO;
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.TrainingRecordsDO;
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.TrainingRecordsMapper;
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.util.object.BeanUtils;


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




/**
 * 康复训练记录 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class TrainingRecordsServiceImpl implements TrainingRecordsService {

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

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

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

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


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

        List<AttachmentsDO> attachments = updateReqVO.getAttachments();
        if (CollectionUtil.isNotEmpty(attachments)) {
            for (AttachmentsDO attachment : attachments) {
                attachment.setBusinessType(AttachmentsConstants.TRAINING);
                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 TrainingRecordsDO getRecords(Integer id) {
        TrainingRecordsDO trainingRecordsDO = recordsMapper.selectById(id);
        ElderlyInfoDO elderlyInfoDO = elderlyInfoMapper.selectById(trainingRecordsDO.getElderId());
        trainingRecordsDO.setElderlyInfo(elderlyInfoDO);
        trainingRecordsDO.setBedInfo(buildService.getFullBedName(Long.valueOf(trainingRecordsDO.getElderId())));

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

        return trainingRecordsDO;
    }

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

}