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

/**
 * @Author: jinyd
 * @CreateTime: 2024/8/19  11:42
 * @Description:
 * @Version: 4.0
 */


import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.*;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyChangeRecordMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.BuildBedMapper;
import cn.iocoder.yudao.module.system.enums.common.BooleanEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.BED_CHANGE_RECORD_NOT_EXISTS;

/**
 * 床位变更记录 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class NurseChangeRecordServiceImpl implements ElderlyChangeRecordService {

    @Resource
    private ElderlyChangeRecordMapper bedChangeRecordMapper;

    @Autowired
    private BuildService buildService;

    @Autowired
    private BuildBedMapper buildBedMapper;

    @Override
    public Long createBedChangeRecord(BedChangeRecordSaveReqVO createReqVO) {
        // 插入
        ElderlyChangeRecordDO bedChangeRecord = BeanUtils.toBean(createReqVO, ElderlyChangeRecordDO.class);
        bedChangeRecordMapper.insert(bedChangeRecord);
        // 返回
        return bedChangeRecord.getId();
    }

    @Override
    public void updateBedChangeRecord(BedChangeRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateBedChangeRecordExists(updateReqVO.getId());
        // 更新
        ElderlyChangeRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyChangeRecordDO.class);
        bedChangeRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteBedChangeRecord(Long id) {
        // 校验存在
        validateBedChangeRecordExists(id);
        // 删除
        bedChangeRecordMapper.deleteById(id);
    }

    private void validateBedChangeRecordExists(Long id) {
        if (bedChangeRecordMapper.selectById(id) == null) {
            throw exception(BED_CHANGE_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ElderlyChangeRecordDO getBedChangeRecord(Long id) {
        return bedChangeRecordMapper.selectById(id);
    }

    @Override
    public PageResult<ElderlyChangeRecordDO> getBedChangeRecordPage(BedChangeRecordPageReqVO pageReqVO) {
        return bedChangeRecordMapper.selectPage(pageReqVO);
    }

    @Override
    @TenantIgnore
    public BedChangeRecordRespVO detail(Long id, Integer status,Boolean isDetail) {
        Integer currentFlag  = 0;
        if (null != status && status == 4) {
            Integer count = bedChangeRecordMapper.checkBpmBusinessFormById(id);
            if (null == count || count == 0) {
                currentFlag = 1;
            }
        }
        BedChangeRecordRespVO detail = isDetail ? bedChangeRecordMapper.getBedDetail(id, currentFlag) : bedChangeRecordMapper.createBedChangeGetDetail(id, currentFlag);
        if (null != detail) {
            detail.setBedName(buildService.getFullBedName(detail.getElderlyId()));
        }
        return detail;
    }


    @Override
    public PageResult<BedChangeElderlyRespVO> findElderlyPage(ElderlyInfoPageReqVO pageVO) {
        Page<ElderlyInfoDO> page = new Page<>(pageVO.getPageNo(), pageVO.getPageSize());
        List<BedChangeElderlyRespVO> list = null;
        if (null == pageVO.getIdentification() || 0 == pageVO.getIdentification()) {
            list = bedChangeRecordMapper.selectTransferElderlyPage(page, pageVO);
        } else {
            list = bedChangeRecordMapper.selectElderlyPage(page, pageVO);
        }
        if (null != list && list.size() > 0) {
            list.forEach(bedChangeElderlyRespVO -> {
                bedChangeElderlyRespVO.setBedName(buildService.getFullBedName(bedChangeElderlyRespVO.getId()));
                if (null != pageVO.getIdentification() && 1 == pageVO.getIdentification()) {
                    bedChangeElderlyRespVO.setOriginalId(String.valueOf(bedChangeElderlyRespVO.getBedId()));
                }
            });
        }

        return new PageResult<>(list, page.getTotal());
    }

    @Override
    @TenantIgnore
    public Boolean checkPrivateRoom(Long bedId) {
        BuildBedDO buildBedDO = buildBedMapper.selectById(bedId);
        if (null == buildBedDO) {
            return Boolean.TRUE;
        }
        List<BuildBedDO> buildBedDOList = buildBedMapper.selectList(new LambdaQueryWrapperX<BuildBedDO>()
                .eq(BuildBedDO::getRoomId, buildBedDO.getRoomId()));
        if (null != buildBedDOList && buildBedDOList.size() > 1) {
            boolean b = buildBedDOList.stream().anyMatch(BuildBedDO -> buildBedDO.getStatus() == 1);
            if (b) {
                return Boolean.FALSE;
            }
            List<Long> idList = buildBedDOList.stream().map(BuildBedDO::getId).collect(Collectors.toList());
            List<ElderlyChangeRecordDO> list = bedChangeRecordMapper.selectList(new LambdaQueryWrapperX<ElderlyChangeRecordDO>()
                    .in(ElderlyChangeRecordDO::getExpectId, idList)
                    .eq(ElderlyChangeRecordDO::getCurrentFlag, 0)
                    .eq(ElderlyChangeRecordDO::getChangeType, 0));
            if (null != list && list.size() > 0) {
                return Boolean.FALSE;
            }
        }


        return Boolean.TRUE;
    }

    @Override
    @TenantIgnore
    public List<NurseChangeyEvaluationRespVO> getEvaluationByElderId(Long elderId) {
        return bedChangeRecordMapper.getEvaluationByElderId(elderId);
    }

    @Override
    @TenantIgnore
    public NurseChangeDetailRespVO getNurseDetail(Long id,Boolean isDetail) {
        NurseChangeDetailRespVO detail = isDetail ? bedChangeRecordMapper.getNurseDetail(id) : bedChangeRecordMapper.createNurseChangeGetDetail(id);
        detail.setBedName(buildService.getFullBedName(detail.getElderlyId()));
        return detail;
    }

    @Override
    @TenantIgnore
    public CateringChangeDetailRespVO getCateringDetail(Long id,Boolean isDetail) {
        CateringChangeDetailRespVO detail = isDetail ? bedChangeRecordMapper.getCateringDetail(id) : bedChangeRecordMapper.createCateringChangeGetDetail(id);
        detail.setBedName(buildService.getFullBedName(detail.getElderlyId()));
        return detail;
    }
}
