package com.usky.meeting.controller.web;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.meeting.domain.MeetingRoom;
import com.usky.meeting.repository.MeetingDeviceRepository;
import com.usky.meeting.repository.MeetingRoomRepository;
import com.usky.meeting.service.MeetingRoomService;
import com.usky.meeting.service.dto.DmMeetingRoomDto;
import com.usky.meeting.service.vo.MeetingRoomRequestVO;
import com.usky.meeting.service.vo.MeetingRoomReservationVO;
import com.usky.transfer.RemoteTransferService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会议室表 前端控制器
 * </p>
 *
 * @author zyj
 * @since 2024-03-08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/meetingRoom")
public class MeetingRoomController {
    @Autowired
    private MeetingRoomService meetingRoomService;
    @Autowired
    private RemoteTransferService remoteTransferService;
    @Autowired
    private MeetingRoomRepository dmMeetingRoomRepository;

    /**
     * 下发门禁开门
     */
    @GetMapping("/{productCode}/{deviceId}/control")
    public ApiResult<Map<String,Object>> control(@PathVariable("productCode") String productCode,
                                                 @PathVariable("deviceId") String deviceId,
                                                 @RequestParam(value = "commandStr") String commandStr,
                                                 @RequestParam(value = "domain",required = false) String domain,
                                                 @RequestParam(value = "userId",required = false) Long userId,
                                                 @RequestParam(value = "userName",required = false) String userName){
        Integer tenantId;
        long commandUserId;
        String commandUserName;
        if(StringUtils.isNotBlank(domain)){
            tenantId = dmMeetingRoomRepository.getTenantIdByDomain(domain);
            commandUserId = userId;
            commandUserName = userName;
        }else{
            tenantId = SecurityUtils.getTenantId();
            commandUserId = SecurityUtils.getUserId();
            commandUserName = SecurityUtils.getUsername();
        }
        return ApiResult.success(remoteTransferService.deviceControl(productCode,deviceId,commandStr,tenantId,commandUserId,commandUserName));
    }

    /**
     * 首页统计
     * @param year
     * @return
     */
    @GetMapping("FirstPageStatistic")
    public ApiResult<Map<String,Object>> FirstPageStatistic(@RequestParam(value = "year") String year){
        return ApiResult.success(meetingRoomService.FirstPageStatistic(year));

    }

    /**
     * 会议室列表
     * @param requestVO
     * @return
     */
    @PostMapping("dmMeetingRoomList")
    public ApiResult<CommonPage<MeetingRoom>> dmMeetingRoomList(@RequestBody MeetingRoomRequestVO requestVO){
        return ApiResult.success(meetingRoomService.dmMeetingRoomList(requestVO));

    }

    /**
     * 会议室下拉框信息
     * @return
     */
    @GetMapping("MeetingRoomList")
    public ApiResult<List<MeetingRoom>> MeetingRoomList(@RequestParam(value = "domain",required = false) String domain){
        return ApiResult.success(meetingRoomService.MeetingRoomList(domain));
    }

    /**
     * 新增
     * @param meetingRoom
     * @return
     */
    @Log(title = "新增-会议室", businessType = BusinessType.INSERT)
    @PostMapping
    public ApiResult<Void> add(@RequestBody MeetingRoom meetingRoom){
        meetingRoomService.add(meetingRoom);
        return ApiResult.success();
    }

    /**
     * 修改
     * @param meetingRoom
     * @return
     */
    @Log(title = "修改-会议室", businessType = BusinessType.UPDATE)
    @PutMapping
    public ApiResult<Void> edit(@RequestBody MeetingRoom meetingRoom){
        meetingRoomService.edit(meetingRoom);
        return ApiResult.success();
    }

    /**
     * 删除
     * @param roomId
     * @return
     */
    @Log(title = "删除-会议室", businessType = BusinessType.DELETE)
    @DeleteMapping
    public ApiResult<Void> remove(@RequestParam Long roomId){
        meetingRoomService.remove(roomId);
        return ApiResult.success();
    }

    /**
     * 会议人员权限审核
     * @param meetingId
     * @param userId
     * @param userName
     * @return
     */
    @GetMapping("attendee")
    public ApiResult<Map<String,Object>> attendee(@RequestParam(value = "meetingId") Long meetingId,
                                                  @RequestParam(value = "userId") Long userId,
                                                  @RequestParam(value = "userName") String userName,
                                                  @RequestParam(value = "deviceCode",required = false) String deviceCode){
        return ApiResult.success(meetingRoomService.attendee(meetingId,userId,userName,deviceCode));
    }

    /**
     * 会议室预约情况列表
     * @param reservationVO
     * @return
     */
    @PostMapping("getMeetingRoomReservationList")
    public ApiResult<List<DmMeetingRoomDto>> getMeetingRoomReservationList(@RequestBody MeetingRoomReservationVO reservationVO)  throws ParseException {
        return ApiResult.success(meetingRoomService.getMeetingRoomReservationList(reservationVO));

    }

    /**
     * 会议室详情
     * @param roomId
     * @return
     */
    @GetMapping("meetingRoomDetails")
    public ApiResult<MeetingRoom> meetingRoomDetails(@RequestParam Long roomId){
        return ApiResult.success(meetingRoomService.meetingRoomDetails(roomId));
    }
}

















