package com.org.web.room.service;


import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.org.system.enums.YesOrNoState;
import com.org.system.service.SysDataDictionaryItemService;
import com.org.utils.SecurityUtil;
import com.org.utils.exception.CustomException;
import com.org.web.hospital.domain.BcHospitalRecord;
import com.org.web.hospital.service.BcHospitalRecordService;
import com.org.web.room.domain.BcRoom;
import com.org.web.room.domain.BcRoomBed;
import com.org.web.room.mapper.BcRoomMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
public class BcRoomService extends ServiceImpl<BcRoomMapper, BcRoom> {

    @Autowired
    private BcRoomBedService bcRoomBedService;

    @Autowired
    SysDataDictionaryItemService sysDataDictionaryItemService;

    @Autowired
    BcHospitalRecordService hospitalRecordService;

    /**
     * 新增或编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean executeSaveOrUpdate(BcRoom bcRoom) {
        log.info("BcRoomService - executeSaveOrUpdate bcRoom:{}", bcRoom);

        if (ObjectUtil.isEmpty(bcRoom.getId())) {
            bcRoom.setCreateBy(SecurityUtil.getUserId());
            bcRoom.setCreateTime(new Date());
            bcRoom.setIsDelete(YesOrNoState.NO.getState());
        } else {
            bcRoom.setUpdateBy(SecurityUtil.getUserId());
            bcRoom.setUpdateTime(new Date());
        }

        List<BcRoomBed> resultList = bcRoom.getRoomBedList().stream()
                .filter(person -> Validator.isNotEmpty(person.getName()) )// 这里是过滤条件
                .collect(Collectors.toList());
        bcRoom.setBedNumber(resultList.size());

        saveOrUpdate(bcRoom);

        // 插入床位子表数据
        bcRoomBedService.executeSaveOrUpdate(bcRoom.getId(), resultList);

        return true;
    }

    /**
     * 详情
     */
    public BcRoom get(Long id) {
        BcRoom bcRoom = getById(id);

        // 查询床位数据
        List<BcRoomBed> roomBedList = bcRoomBedService.listByRoomId(id);
        // 为供养房 查询入住信息
        if (bcRoom.getRoomType() == 1) {
            for (BcRoomBed roomBed : roomBedList) {
                BcHospitalRecord recordByBedId = hospitalRecordService.getRecordByBedId(roomBed.getId());
                if (null != recordByBedId) {
                    roomBed.setPersonId(recordByBedId.getPersonId());
                    roomBed.setPersonName(recordByBedId.getPersonName());
                }
            }

        }

        bcRoom.setRoomBedList(roomBedList);

        return bcRoom;
    }

    /**
     * 列表查询
     */
    public List<BcRoom> queryList(BcRoom bcRoom) {
        List<BcRoom> list = baseMapper.queryList(bcRoom);
        for (BcRoom room : list) {
            //获取 房间类别
            room.setRoomTypeName(sysDataDictionaryItemService.findDictonaryBydictonaryCode("0010", room.getRoomType()));
        }
        return list;
    }

    /**
     * 删除
     */
    public boolean del(Long id) {
        log.info("BcRoomService - del id:{}", id);

        BcRoom bcRoom = getById(id);
        if (ObjectUtil.isEmpty(bcRoom)) {
            throw new CustomException("删除记录不存在");
        }
        bcRoom.setIsDelete(YesOrNoState.YES.getState());
        bcRoom.setDeleteBy(SecurityUtil.getUserId());
        bcRoom.setDeleteTime(new Date());

        bcRoomBedService.delByRoomId(id);

        return updateById(bcRoom);
    }

}
