package com.mortals.xhx.module.room.web;
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.room.model.RoomQuery;
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")
public class RoomController extends BaseCRUDJsonBodyMappingController<RoomService,RoomEntity,Long> {

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


    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 = 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)) {
            if (roomEntity.getDeviceId() != null) {
                //关联led大屏设备 排除已经存在的设备
                ledMap.put(roomEntity.getDeviceId().toString(), roomEntity.getDeviceName());
            }
        }
        this.addDict(model, "deviceId", ledMap);
        super.init(model, context);
    }


}