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.module.system.controller.admin.biz.vo.ElderlyMedicalRecordPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyMedicalRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyMedicalRecordMapper;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ElderlyMedicalRecordServiceImpl implements ElderlyMedicalRecordService{

    @Autowired
    private ElderlyMedicalRecordMapper mapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;
    @Resource
    private BuildService buildService;
    @Resource
    private TenantMapper tenantMapper;

    @Override
    public void save(ElderlyMedicalRecordDO elderlyMedicalRecord) {
        elderlyMedicalRecord.setCreateDate(LocalDateTime.now());
        this.mapper.insert(elderlyMedicalRecord);
    }

    @Override
    public PageResult<ElderlyMedicalRecordDO> queryPage(ElderlyMedicalRecordPageReqVO pageReqVO) {
        Page<ElderlyMedicalRecordDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ElderlyMedicalRecordDO> list = mapper.queryPage(page,pageReqVO);
        for (ElderlyMedicalRecordDO elderlyMedicalRecordDO : list) {
            TenantDO tenantDO = tenantMapper.selectById(elderlyMedicalRecordDO.getTenantId());
            elderlyMedicalRecordDO.setTenantName(tenantDO == null ? null : tenantDO.getName());
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public ElderlyMedicalRecordDO getDetailById(Long id) {
        ElderlyMedicalRecordDO medicalRecord = this.mapper.selectById(id);
        if (medicalRecord != null && StringUtils.isNotBlank(medicalRecord.getElderId())) {
            ElderlyInfoDO elderlyInfo = elderlyInfoMapper.selectById(Long.valueOf(medicalRecord.getElderId()));
            elderlyInfo.setBedName(buildService.getFullBedName(elderlyInfo.getId()));
            medicalRecord.setElder(elderlyInfo);
        }
        return medicalRecord;
    }

    @Override
    public ElderlyMedicalRecordDO getLastRecord(Long elderId) {
        List<ElderlyMedicalRecordDO> elderlyMedicalRecords = this.mapper.selectList(new LambdaQueryWrapper<ElderlyMedicalRecordDO>()
                .eq(ElderlyMedicalRecordDO::getElderId, elderId).orderByDesc(ElderlyMedicalRecordDO::getCreateDate));
        if (CollectionUtil.isNotEmpty(elderlyMedicalRecords)) {
            return elderlyMedicalRecords.stream().findFirst().get();
        }
        return null;
    }

    @Override
    public void delete(Long id) {
        this.mapper.deleteById(id);
    }

    @Override
    public void edit(ElderlyMedicalRecordDO elderlyMedicalRecord) {
        this.mapper.updateById(elderlyMedicalRecord);
    }
}
