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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ElderlyHealthRecordPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyHealthRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyHealthRecordMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
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.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 ElderlyHealthRecordServiceImpl implements ElderlyHealthRecordService {

    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;
    @Autowired
    private ElderlyHealthRecordMapper elderlyHealthRecordMapper;
    @Resource
    private BuildService buildService;
    @Resource
    private TenantMapper tenantMapper;



    @Override
    public void add(ElderlyHealthRecordDO elderlyHealthRecordDO) {
        elderlyHealthRecordDO.setCreateDate(LocalDateTime.now());
        elderlyHealthRecordMapper.insert(elderlyHealthRecordDO);
    }

    @Override
    public ElderlyHealthRecordDO getElderHealthRecordByElderId(Long elderId) {
        ElderlyInfoDO elderlyInfoDO = elderlyInfoMapper.selectById(elderId);
        elderlyInfoDO.setBedName(buildService.getFullBedName(elderId));
        ElderlyHealthRecordDO elderlyHealthRecordDO = elderlyHealthRecordMapper.selectOne(new LambdaQueryWrapper<ElderlyHealthRecordDO>()
                .eq(ElderlyHealthRecordDO::getElderId, elderId));

        if (elderlyHealthRecordDO == null){
            elderlyHealthRecordDO = new ElderlyHealthRecordDO();
        }
        elderlyHealthRecordDO.setElder(elderlyInfoDO);
        return elderlyHealthRecordDO;
    }

    @Override
    public void delete(Long elderlyHealthRecordId) {
        elderlyHealthRecordMapper.deleteById(elderlyHealthRecordId);
    }

    @Override
    public PageResult<ElderlyHealthRecordDO> queryPage(ElderlyHealthRecordPageReqVO pageReqVO) {
        Page<ElderlyHealthRecordDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ElderlyHealthRecordDO>  list = elderlyHealthRecordMapper.queryPage(page,pageReqVO);
        for (ElderlyHealthRecordDO elderlyHealthRecordDO : list) {
            TenantDO tenantDO = tenantMapper.selectById(elderlyHealthRecordDO.getElder().getTenantId());
            if (tenantDO != null){
                elderlyHealthRecordDO.getElder().setTenantName(tenantDO.getName());
            }
        }
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public void edit(ElderlyHealthRecordDO elderlyHealthRecordDO) {
        elderlyHealthRecordMapper.updateById(elderlyHealthRecordDO);
    }


}
