package com.mortals.xhx.module.room.service.impl;

import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.room.dao.RoomDao;
import com.mortals.xhx.module.room.model.RoomEntity;
import com.mortals.xhx.module.room.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.room.model.RoomDeviceEntity;
import com.mortals.xhx.module.room.model.RoomDeviceQuery;
import com.mortals.xhx.module.room.service.RoomDeviceService;
import org.springframework.util.ObjectUtils;

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

import lombok.extern.slf4j.Slf4j;

/**
 * RoomService
 * 房间管理 service实现
 *
 * @author zxfei
 * @date 2023-05-29
 */
@Service("roomService")
@Slf4j
public class RoomServiceImpl extends AbstractCRUDServiceImpl<RoomDao, RoomEntity, Long> implements RoomService {
    @Autowired
    private RoomDeviceService roomDeviceService;
    @Autowired
    private DeviceService deviceService;


    @Override
    protected void saveAfter(RoomEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getRoomDeviceList())) {

            entity.getRoomDeviceList().stream().peek(item -> {
                item.setRoomId(entity.getId());
                item.setRoomName(entity.getRoomName());
                DeviceEntity deviceCache = deviceService.getCache(item.getDeviceId().toString());
                item.setDeviceId(item.getDeviceId());
                item.setDeviceCode(deviceCache == null ? "" : deviceCache.getDeviceCode());
                item.setDeviceName(deviceCache == null ? "" : deviceCache.getDeviceName());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
            }).count();
            roomDeviceService.save(entity.getRoomDeviceList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(RoomEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getRoomDeviceList())) {

            List<Long> deviceIdList = entity.getRoomDeviceList().stream().map(i -> i.getDeviceId()).collect(Collectors.toList());
            RoomDeviceQuery roomDeviceQuery = new RoomDeviceQuery();
            roomDeviceQuery.setDeviceIdList(deviceIdList);

            Long[] roomDeviceIds = roomDeviceService.find(roomDeviceQuery).stream().map(RoomDeviceEntity::getId).toArray(Long[]::new);
            roomDeviceService.remove(roomDeviceIds, context);


            roomDeviceIds = roomDeviceService.find(new RoomDeviceQuery().roomId(entity.getId())).stream().map(RoomDeviceEntity::getId).toArray(Long[]::new);
            roomDeviceService.remove(roomDeviceIds, context);
            entity.getRoomDeviceList().stream().peek(item -> {
                item.setRoomId(entity.getId());
                item.setRoomName(entity.getRoomName());
                DeviceEntity deviceCache = deviceService.getCache(item.getDeviceId().toString());
                item.setDeviceId(item.getDeviceId());
                item.setDeviceCode(deviceCache == null ? "" : deviceCache.getDeviceCode());
                item.setDeviceName(deviceCache == null ? "" : deviceCache.getDeviceName());
                item.setCreateUserId(this.getContextUserId(context));
                item.setCreateTime(new Date());
                item.setUpdateUserId(this.getContextUserId(context));
                item.setUpdateTime(new Date());
            }).count();
            roomDeviceService.save(entity.getRoomDeviceList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        List<RoomDeviceEntity> roomDevicelist = roomDeviceService.find(new RoomDeviceQuery().roomIdList(Arrays.asList(ids)));
        roomDeviceService.removeList(roomDevicelist, context);
        super.removeAfter(ids, context, result);
    }
}