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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingInspectionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.NursingInspectionSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NursingInspectionDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.NursingInspectionMapper;
import org.springframework.beans.BeanUtils;
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 NursingInspectionServiceImpl implements NursingInspectionService {

    @Resource
    private NursingInspectionMapper nursingInspectionMapper;
    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;


    @Override
    public void createNursingInspection(NursingInspectionSaveReqVO createReqVO) {
        NursingInspectionDO nursingInspectionDO = new NursingInspectionDO();
        BeanUtils.copyProperties(createReqVO, nursingInspectionDO);
        nursingInspectionDO.setInspectionTime(LocalDateTime.now());
        ElderlyInfoDO elderlyInfoDO = elderlyInfoMapper.selectById(nursingInspectionDO.getElderId());
        nursingInspectionDO.setOrgType(elderlyInfoDO.getOrgType() == null ? null : elderlyInfoDO.getOrgType());
        nursingInspectionDO.setInspectionName(SecurityFrameworkUtils.getLoginUserNickname());
        nursingInspectionMapper.insert(nursingInspectionDO);
    }

    @Override
    public NursingInspectionDO getNursingInspection(Long id) {
        NursingInspectionDO nursingInspectionDO = nursingInspectionMapper.selectById(id);
        nursingInspectionDO.setContent("巡检时间: " + nursingInspectionDO.getInspectionTime()
                + "，" + "巡检人: " + nursingInspectionDO.getInspectionName() + "，巡检内容: " + nursingInspectionDO.getContent());
        return nursingInspectionDO;
    }

    @Override
    public PageResult<NursingInspectionDO> getNursingInspectionPage(NursingInspectionPageReqVO pageReqVO) {
        return nursingInspectionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<NursingInspectionDO> getMobileNursingInspectionList(Long nursingLogId) {
        return nursingInspectionMapper.selectList(new LambdaQueryWrapperX<NursingInspectionDO>()
                .eq(NursingInspectionDO::getNursingLogId,nursingLogId)
                .orderByDesc(NursingInspectionDO::getInspectionTime));
    }
}