package com.zd.smartlock.controller;

import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.smartlock.domain.SlSubjectRelation;
import com.zd.smartlock.domain.api.SlLockOnline;
import com.zd.smartlock.domain.api.SlRoom;
import com.zd.smartlock.domain.vo.SlSubjectRelationVo;
import com.zd.smartlock.service.ISlSubjectRelationService;
import com.zd.smartlock.utils.SmartlockUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 实验室关联Controller
 *
 * @author xxf
 * @date 2022-09-03
 */
@RestController
@Api(tags = "【实验室关联】")
@RequestMapping("/subjectRelation")
public class SlSubjectRelationController extends BaseController {
    @Autowired
    private ISlSubjectRelationService slSubjectRelationService;

    /**
     * 查询实验室关联列表
     */
    @PreAuthorize(hasPermi = "laboratory:subjectRelation:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询实验室关联列表")
    public TableDataInfo<SlSubjectRelationVo> list(SlSubjectRelationVo slSubjectRelation) {
        startPage();
        List<SlSubjectRelationVo> list = slSubjectRelationService.selectSlSubjectRelationList(slSubjectRelation);
        return getDataTable(list);
    }

    /**
     * 查询实验室关联列表-无权限
     */
    @GetMapping("/listSub")
    @ApiOperation(value = "查询实验室关联列表")
    public TableDataInfo<SlSubjectRelationVo> listSub(SlSubjectRelationVo slSubjectRelation) {
        startPage();
        List<SlSubjectRelationVo> list = slSubjectRelationService.selectSlSubjectRelationList(slSubjectRelation);
        return getDataTable(list);
    }

    /**
     * 导出实验室关联列表
     */
    @ApiOperation(value = "导出实验室关联列表")
    @PreAuthorize(hasPermi = "laboratory:subjectRelation:export")
    @Log(title = "实验室关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlSubjectRelationVo slSubjectRelation) throws IOException {
        List<SlSubjectRelationVo> list = slSubjectRelationService.selectSlSubjectRelationList(slSubjectRelation);
        ExcelUtil<SlSubjectRelationVo> util = new ExcelUtil<SlSubjectRelationVo>(SlSubjectRelationVo.class);
        util.exportExcel(response, list, "实验室关联数据");
    }

    /**
     * 获取实验室关联详细信息
     */
    @ApiOperation(value = "获取实验室关联详细信息")
    @PreAuthorize(hasPermi = "laboratory:subjectRelation:query")
    @GetMapping(value = "/{id}")
    public ResultData<SlSubjectRelation> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(slSubjectRelationService.selectSlSubjectRelationById(id));
    }

    /**
     * 新增实验室关联
     */
    @ApiOperation(value = "新增实验室关联")
    @PreAuthorize(hasPermi = "laboratory:subjectRelation:add")
    @Log(title = "实验室关联", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody SlSubjectRelationVo vo) {
        /*`subjectId`  '实验室id',
        `subjectName` '实验室名称',
        `lockRoomId` '房间id', =roomid
        `lockCode`  '门锁编号', = roomcode2
        `roomLocation`  '房间位置 =roomlocation',
        `roomName`   '房间名称' =roomname,
        */
        return ResultData.result(slSubjectRelationService.insertSlSubjectRelation(vo));
    }

    /**
     * 新增实验室关联-无权限
     */
    @ApiOperation(value = "新增实验室关联")
    @Log(title = "实验室关联", businessType = BusinessType.INSERT)
    @PostMapping("/addSub")
    public ResultData addSub(@RequestBody SlSubjectRelationVo vo) {

        return ResultData.result(slSubjectRelationService.insertSlSubjectRelation(vo));
    }

    /**
     * 修改实验室关联
     */
    @ApiOperation(value = "修改实验室关联")
    @PreAuthorize(hasPermi = "laboratory:subjectRelation:edit")
    @Log(title = "实验室关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody SlSubjectRelation slSubjectRelation) {
        return ResultData.result(slSubjectRelationService.updateSlSubjectRelation(slSubjectRelation));
    }

    /**
     * 删除实验室关联
     */
    @ApiOperation(value = "删除实验室关联")
    @Log(title = "实验室关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeSub/{ids}")
    public ResultData removeSub(@PathVariable Long[] ids) {
        return ResultData.result(slSubjectRelationService.deleteSlSubjectRelationByIds(ids));
    }

    /**
     * 删除实验室关联
     */
    @ApiOperation(value = "删除实验室关联")
    @PreAuthorize(hasPermi = "laboratory:subjectRelation:remove")
    @Log(title = "实验室关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids) {
        return ResultData.result(slSubjectRelationService.deleteSlSubjectRelationByIds(ids));
    }

    /**
     * 查询智能锁房间列表
     */
    @GetMapping("/listLockRoom")
    @ApiOperation(value = "查询实验室关联列表")
    public TableDataInfo<SlRoom> listLockRoom(SlRoom slRoom) {
        List<SlRoom> list = SmartlockUtil.getRoom( slRoom);

        /*List<SlRoom> list = new ArrayList<>();
        slRoom.setRoomname("房间1");
        slRoom.setRoomcode2("MS001");
        slRoom.setRoomlocation("1-1-110");
        slRoom.setRoomid(220);
        list.add(slRoom);

        SlRoom slRoom1 = new SlRoom();
        slRoom1.setRoomname("房间2");
        slRoom1.setRoomcode2("MS002");
        slRoom1.setRoomlocation("1-1-112");
        slRoom1.setRoomid(330);
        list.add(slRoom1);*/
        return getDataTable(list);
    }

    /**
     * 查询实验室门锁状态
     */
    @GetMapping("/lockListDetails")
    @ApiOperation(value = "查询实验室门锁状态")
    public TableDataInfo<SlLockOnline> lockListDetails(Long subjectId) {
        //List<SlRoom> list = SmartlockUtil.getRoom( slRoom);
        //查询实验室下绑定的门锁
        SlSubjectRelationVo subjectRelation = new SlSubjectRelationVo();
        subjectRelation.setSubjectId(subjectId);
        List<SlSubjectRelationVo> listrelation = slSubjectRelationService.selectSlSubjectRelationList(subjectRelation);

        List<SlLockOnline> onlineList = new ArrayList<>();
        for (SlSubjectRelation rl:listrelation) {
            SlLockOnline lockOnline = SmartlockUtil.getRoomDetails(rl.getLockRoomId());
            if(lockOnline!=null){
                onlineList.add(lockOnline);
            }
        }
        return getDataTable(onlineList);
    }
}
