package com.jeeplus.pubmodules.meetingRoom.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.pubmodules.holiday.utils.EasyPoiUtil;
import com.jeeplus.pubmodules.meetingRoom.domain.MeetingRoom;
import com.jeeplus.pubmodules.meetingRoom.service.MeetingRoomService;
import com.jeeplus.pubmodules.meetingRoom.service.dto.MeetingRoomDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Api(tags ="会议室预约")
@RestController
@RequestMapping(value = "/meetingRoom")
public class MeetingRoomController {

    @Autowired
    private MeetingRoomService meetingRoomService;

    @Resource
    private IFlowableApi flowTaskService;


    /**
     * 会议室列表
     */
    @ApiOperation(value = "会议室列表")
    @GetMapping(value = "/list")
    public ResponseEntity<IPage<MeetingRoom>> findPageList(MeetingRoom meetingRoom, Page<MeetingRoom> page) throws Exception {
       IPage<MeetingRoom> iPage= meetingRoomService.findPageList(meetingRoom,page);
       iPage.getRecords().stream().forEach(i ->{
           if (StringUtils.isNotBlank(i.getTaskId()) && StringUtils.isNotBlank(i.getStatus())) {
               if ("2".equals(i.getStatus())) { // “审核中”的数据要获取数据审核人
                   i.setAuditUserIds(flowTaskService.getTaskAuditUsers(i.getTaskId()));  // 获取数据审核人
               }
           }
       });
       return ResponseEntity.ok(iPage);
    }


    /**
     * 会议室添加/修改
     */
    @ApiOperation(value = "会议室添加/修改")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody MeetingRoomDTO meetingRoomDTO) throws ParseException {
        String id=meetingRoomService.saveForm(meetingRoomDTO);
        return ResponseUtil.newInstance().add("businessTable","meeting_room").add("businessId",id).ok("操作成功");
    }

    /**
     * 根据id查询会议室信息
     */
    @ApiOperation(value = "根据id查询会议室信息")
    @GetMapping(value = "/findById")
    public ResponseEntity<MeetingRoomDTO> findById(@RequestParam String id){
        MeetingRoomDTO roomDTO=meetingRoomService.findById(id);
        return ResponseEntity.ok(roomDTO);
    }

    /**
     * 根据id修改状态status
     * @param
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "/updateStatusById")
    public void updateStatusById(@RequestBody MeetingRoomDTO meetingRoomDTO) {
        meetingRoomService.updateStatusById(meetingRoomDTO.getId(),meetingRoomDTO.getStatus());
    }

    /**
     * 根据会议开始时间查询最近结束的会议时间
     */
    @ApiOperation(value = "根据会议开始时间查询最近结束的会议时间")
    @GetMapping(value = "/checkTime")
    public Map<String, String> checkTime(@RequestParam String startDate,@RequestParam String meetNumber) throws ParseException {
        Map<String, String> map=meetingRoomService.checkTime(startDate,meetNumber);
        return map;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除")
    @GetMapping(value = "/deleteById")
    public ResponseEntity<String> deleteById(@RequestParam String id){
        String s = meetingRoomService.deleteById(id);
        return ResponseEntity.ok(s);
    }

    @ApiLog(value = "导出会议室预约数据", type = LogTypeEnum.EXPORT)
    @GetMapping("export")
    @ApiOperation(value = "导出会议室预约数据")
    public void exportFile(MeetingRoom meetingRoom, Page <MeetingRoom> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<MeetingRoom> result = new ArrayList<>();
        if ( ExportMode.current.equals ( options.getMode() ) ) {
            result = meetingRoomService.findPageList(meetingRoom,page).getRecords();
        } else if (ExportMode.selected.equals ( options.getMode() )) {
            result = meetingRoomService.findPageList(meetingRoom,page).getRecords().stream ( ).filter ( item ->
                    options.getSelectIds ( ).contains ( item.getId ( ) )
            ).collect ( Collectors.toList ( ) );
        } else {
            page.setSize (-1);
            page.setCurrent (0);
            result = meetingRoomService.findPageList(meetingRoom,page).getRecords();
        }
        EasyPoiUtil.exportExcel ( result, sheetName,  sheetName, MeetingRoom.class, fileName, response );

    }







}
