package cn.iocoder.yudao.module.system.api.bpm;

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.module.system.api.bpm.vo.CheckInWaitApiPageReqVO;
import cn.iocoder.yudao.module.system.api.bpm.vo.CheckInWaitCreateReqVO;
import cn.iocoder.yudao.module.system.api.bpm.vo.CheckInWaitRecordRespVO;
import cn.iocoder.yudao.module.system.api.bpm.vo.CheckInWaitSupplementReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.*;
import cn.iocoder.yudao.module.system.dal.mysql.biz.BuildBedMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.BuildFloorMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.BuildMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.BuildRoomMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyCheckInWaitMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.NurseLevelMapper;
import cn.iocoder.yudao.module.system.enums.change.BusinessTypeEnum;
import cn.iocoder.yudao.module.system.service.biz.BuildService;
import com.alibaba.fastjson.JSONObject;
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 org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Validated
public class BpmCheckInWaitApiImpl implements BpmCheckInWaitApi {

    @Resource
    private ElderlyCheckInWaitMapper checkInWaitMapper;

    @Resource
    private NurseLevelMapper nurseLevelMapper;

    @Autowired
    private BuildService buildService;


    @Override
    public Long createCheckInWait(CheckInWaitCreateReqVO reqVO) {
        ElderlyCheckInWaitDO data = new ElderlyCheckInWaitDO();
        data.setElderName(reqVO.getElderName());
        data.setIdCard(reqVO.getIdCard());
        data.setRecentLivingSituation(reqVO.getRecentLivingSituation());
        data.setWalkSituationVideo(reqVO.getWalkSituationVideo());
        data.setMedicalRecordsWithinSixMonth(reqVO.getMedicalRecordsWithinSixMonth());
        data.setIdCardPhoto(reqVO.getIdCardPhoto());
        data.setRecentDischargeSummary(reqVO.getRecentDischargeSummary());
        data.setTenantId(reqVO.getTenantId());
        data.setStatus(reqVO.getStatus());
        data.setNurseLevelId(reqVO.getNurseLevelId());
        data.setRemark(reqVO.getRemark());
        data.setPhysicalExaminationReport(reqVO.getPhysicalExaminationReport());
        data.setBedId(reqVO.getBedId());
        data.setCommonMedicine(reqVO.getCommonMedicine());
        checkInWaitMapper.insert(data);
        return data.getId();
    }

    @Override
    public Integer updateInfo(CheckInWaitSupplementReqVO reqVO) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        BeanUtils.copyProperties(reqVO,updateDO);
        return checkInWaitMapper.updateById(updateDO);
    }

    @Override
    public Integer updateFirstReviewInfo(CheckInWaitSupplementReqVO reqVO) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        updateDO.setId(reqVO.getId());
        updateDO.setIsPsychosis(reqVO.getIsPsychosis());
        return checkInWaitMapper.updateById(updateDO);
    }
    @Override
    public Integer updateSecondReviewInfo(CheckInWaitSupplementReqVO reqVO) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        updateDO.setId(reqVO.getId());
        updateDO.setFirstAuditName(reqVO.getFirstAuditName());
        updateDO.setMedicalCareRemark(reqVO.getMedicalCareRemark());
        return checkInWaitMapper.updateById(updateDO);
    }

    @Override
    public Integer updateCheckInWaitStatus(Long id, Integer status) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        updateDO.setId(id);
        updateDO.setStatus(status);
        return checkInWaitMapper.updateById(updateDO);
    }

    @Override
    public Integer updateCheckInWaitStatusByBusinessId(Long businessId, Integer status) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        updateDO.setId(businessId);
        updateDO.setStatus(status);
        if(status == 11){
            updateDO.setIsPsychosis("是");
        }
        return checkInWaitMapper.updateById(updateDO);
    }

    @Override
    public Integer updateThirdReviewInfo(CheckInWaitSupplementReqVO reqVO) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        updateDO.setId(reqVO.getId());
        updateDO.setNurseLevelId(reqVO.getNurseLevelId());
        updateDO.setRemark(reqVO.getRemark());
        return checkInWaitMapper.updateById(updateDO);
    }

    @Override
    public Integer updateFourthReviewInfo(CheckInWaitSupplementReqVO reqVO) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        updateDO.setId(reqVO.getId());
        updateDO.setRemark(reqVO.getRemark());
        updateDO.setPhysicalExaminationReport(reqVO.getPhysicalExaminationReport());
        return checkInWaitMapper.updateById(updateDO);
    }

    @Override
    public Integer updateFifthReviewInfo(CheckInWaitSupplementReqVO reqVO) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        updateDO.setId(reqVO.getId());
        updateDO.setAuditName(reqVO.getAuditName());
        return checkInWaitMapper.updateById(updateDO);
    }

    @Override
    public Integer updateSixthReviewInfo(CheckInWaitSupplementReqVO reqVO) {
        ElderlyCheckInWaitDO updateDO = new ElderlyCheckInWaitDO();
        updateDO.setId(reqVO.getId());
        updateDO.setBedId(reqVO.getBedId());
        return checkInWaitMapper.updateById(updateDO);
    }

    @Override
    public JSONObject selectById(Long id) {
        ElderlyCheckInWaitDO elderlyCheckInWaitDO = checkInWaitMapper.selectById(id);
        JSONObject result = (JSONObject) JSONObject.toJSON(elderlyCheckInWaitDO);
        result.put("bedName",buildService.getFullBedNameByBedId(elderlyCheckInWaitDO.getBedId()));
        NurseLevelDO nurseLevelDO = nurseLevelMapper.selectById(elderlyCheckInWaitDO.getNurseLevelId());
        result.put("nurseLevelName", nurseLevelDO == null ? "" : nurseLevelDO.getNurseLevelName());
        return result;
    }

    @Override
    public PageResult<CheckInWaitRecordRespVO> getProcessInstanceCheckInWaitPage(CheckInWaitApiPageReqVO pageReqVO) {
        Page<CheckInWaitRecordRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        pageReqVO.setType(BusinessTypeEnum.CHECK_IN_WAIT.getValue());
        List<CheckInWaitRecordRespVO> list = checkInWaitMapper.selectCheckInWaitListPage(page, pageReqVO);
        return new PageResult<>(list, page.getTotal());
    }
}
