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

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.biz.vo.MedicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicationSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.MedicationExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicationDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.MedicationMapper;
import cn.iocoder.yudao.module.system.enums.common.BooleanEnum;
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.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 长者用药 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class MedicationServiceImpl implements MedicationService {


    @Resource
    private BuildService buildService;

    @Resource
    private MedicationMapper medicationMapper;
    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public void createMedication(MedicationSaveReqVO saveReqVO) {
        List<MedicationDO> medications = saveReqVO.getMedications();
        for (MedicationDO medication : medications) {
            medication.setElderId(saveReqVO.getElderId());
            medication.setCreatedTime(new Date());
            medication.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
            medicationMapper.insert(medication);
        }
    }

    @Override
    public MedicationRespVO getMedication(Long elderId) {
        List<MedicationDO> medicationList = medicationMapper.selectList(new LambdaQueryWrapperX<MedicationDO>()
                .eq(MedicationDO::getElderId, elderId));

        ElderlyInfoDO elderlyInfoDO = elderlyInfoMapper.selectById(elderId);
        MedicationRespVO respVO = new MedicationRespVO();
        respVO.setElderId(elderId);
        respVO.setElderName(elderlyInfoDO.getElderName());
        respVO.setElderSex(elderlyInfoDO.getElderSex());
        respVO.setBedName(buildService.getFullBedName(elderId));
        respVO.setMedications(medicationList);
        return respVO;
    }

    @Override
    public void updateUsingMedical(MedicationDO medicationDO) {
        medicationDO.setUpdateTime(new Date());
        medicationDO.setUpdateBy(SecurityFrameworkUtils.getLoginUserNickname());
        medicationMapper.updateById(medicationDO);
    }

    @Override
    public void deleteMedication(Long id) {
        medicationMapper.deleteById(id);
    }

    @Override
    public List<MedicationExcelVO> exportMedicationExcel(MedicationPageReqVO pageReqVO) {
        PageResult<MedicationDO> pageResult = medicationMapper.selectPage(pageReqVO);
        return pageResult.getList().stream().map(e -> {
            MedicationExcelVO excelVO = new MedicationExcelVO();
            BeanUtils.copyProperties(e, excelVO);
            if (Objects.equals(e.getStatus(), BooleanEnum.TRUE.getValue())) {
                excelVO.setStatusStr("已停用");
            } else {
                excelVO.setStatusStr("已启用");
            }
            return excelVO;
        }).collect(Collectors.toList());
    }

    @Override
    public PageResult<MedicationDO> getMedicationPage(MedicationPageReqVO pageReqVO) {
        return medicationMapper.selectPage(pageReqVO);
    }
}