package com.mortals.xhx.module.room.web;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.DeviceTypeEnum;
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 com.mortals.xhx.module.notice.model.NoticeQuery;
import com.mortals.xhx.module.notice.model.NoticeRoomEntity;
import com.mortals.xhx.module.notice.model.NoticeRoomQuery;
import com.mortals.xhx.module.notice.service.NoticeRoomService;
import com.mortals.xhx.module.notice.service.NoticeService;
import com.mortals.xhx.module.room.model.RoomDeviceEntity;
import com.mortals.xhx.module.room.model.RoomDeviceQuery;
import com.mortals.xhx.module.room.model.RoomQuery;
import com.mortals.xhx.module.room.service.RoomDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.room.model.RoomEntity;
import com.mortals.xhx.module.room.service.RoomService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import static com.mortals.framework.ap.SysConstains.*;

/**
 * 房间管理
 *
 * @author zxfei
 * @date 2023-05-22
 */
@RestController
@RequestMapping("room")
@Slf4j
public class RoomController extends BaseCRUDJsonBodyMappingController<RoomService, RoomEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private RoomDeviceService roomDeviceService;

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private NoticeRoomService noticeRoomService;


    public RoomController() {
        super.setModuleDesc("房间管理");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "roomType", paramService.getParamBySecondOrganize("Room", "roomType"));
        //this.addDict(model, "device", deviceService.find(new DeviceQuery()));

        Set<Long> deviceLedSet = roomDeviceService.find(new RoomDeviceQuery()).stream().filter(f -> f.getDeviceId() != null).map(item -> item.getDeviceId()).collect(Collectors.toSet());

        // Set<Long> deviceLedSet = this.service.find(new RoomQuery()).stream().filter(f -> f.getDeviceId() != null).map(item -> item.getDeviceId()).collect(Collectors.toSet());
        Map<String, String> ledMap = deviceService.find(new DeviceQuery()).stream().filter(f -> !deviceLedSet.contains(f.getId()))
                .collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getDeviceName(), (o, n) -> n));

        RoomEntity roomEntity = (RoomEntity) model.get(KEY_RESULT_ENTITY);
        if (!ObjectUtils.isEmpty(roomEntity) && !roomEntity.newEntity()) {
            ledMap = deviceService.find(new DeviceQuery()).stream()
                    .collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getDeviceName(), (o, n) -> n));

        }

        Map<String, String> noticeMap = noticeService.find(new NoticeQuery()).stream().collect(Collectors.toMap(x -> x.getId().toString(), y -> y.getTitle(), (o, n) -> n));

        this.addDict(model, "noticeId", noticeMap);

        this.addDict(model, "deviceIds", ledMap);
        super.init(model, context);
    }


    @Override
    protected void doListBefore(RoomEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);
        if (!ObjectUtils.isEmpty(query.getDeviceCode())) {
            //根据设备编码 查询房间room
            RoomDeviceQuery roomDeviceQuery = new RoomDeviceQuery();
            roomDeviceQuery.setDeviceCode(query.getDeviceCode());
            RoomDeviceEntity roomEntity = roomDeviceService.selectOne(roomDeviceQuery);
            log.info("deviceCode:{}", roomDeviceQuery.getDeviceCode());
            if (!ObjectUtils.isEmpty(roomEntity)) {
                // query.setIdList(Arrays.asList(roomEntity.getRoomId()));
                query.setId(roomEntity.getRoomId());
            } else {
                // query.setIdList(Arrays.asList(-1L));
                query.setId(-1L);
            }
            query.setDeviceCode(null);
        }
    }


    @Override
    protected int editAfter(Long id, Map<String, Object> model, RoomEntity entity, Context context) throws AppException {
        //查询通知相关

        NoticeRoomEntity noticeRoomEntity = noticeRoomService.selectOne(new NoticeRoomQuery().roomId(entity.getId()));
        if (!ObjectUtils.isEmpty(noticeRoomEntity)) {
            entity.setNoticeId(noticeRoomEntity.getNoticeId());
        }
        return super.editAfter(id, model, entity, context);
    }
}