package com.izouma.wenlvju.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.izouma.wenlvju.domain.ExamRoom;
import com.izouma.wenlvju.dto.ExamRoomDTO;
import com.izouma.wenlvju.dto.PageQuery;
import com.izouma.wenlvju.exception.BusinessException;
import com.izouma.wenlvju.repo.ExamRoomRepo;
import com.izouma.wenlvju.service.ExamRoomService;
import com.izouma.wenlvju.utils.ObjUtils;
import com.izouma.wenlvju.utils.excel.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/examRoom")
@AllArgsConstructor
public class ExamRoomController extends BaseController {
    private final ExamRoomService examRoomService;
    private final ExamRoomRepo    examRoomRepo;


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public ExamRoom save(@RequestBody ExamRoom record) {
        String deviceSerial = record.getDeviceSerial();
        String validateCode = record.getValidateCode();
        if (record.getId() != null) {
            ExamRoom orig = examRoomRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
            if (StrUtil.isNotBlank(deviceSerial) && StrUtil.isNotBlank(validateCode) && !deviceSerial.equals(orig.getDeviceSerial())) {
                // 添加设备
                examRoomService.addDevice(deviceSerial, validateCode);
            }
            ObjUtils.merge(orig, record);
            return examRoomRepo.save(orig);
        }
        if (StrUtil.isNotBlank(deviceSerial) && StrUtil.isNotBlank(validateCode)) {
            // 添加设备
            examRoomService.addDevice(deviceSerial, validateCode);
        }
        return examRoomRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<ExamRoom> all(@RequestBody PageQuery pageQuery) {
        Page<ExamRoom> all = examRoomService.all(pageQuery);
        if (CollUtil.isEmpty(all)) {
            return all;
        }
        Map<String, Integer> device = examRoomService.showDevice();
        return all.map(examRoom -> {
            examRoom.setStatus(device.get(examRoom.getDeviceSerial()));
            return examRoom;
        });
    }

    @GetMapping("/get/{id}")
    public ExamRoom get(@PathVariable Long id) {
        return examRoomRepo.findById(id).orElseThrow(new BusinessException("无记录"));
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
//        ExamRoom room = examRoomRepo.findById(id).orElseThrow(new BusinessException("无考场"));
//        // 删除设备
//        examRoomService.delDevice(room.getDeviceSerial());
        examRoomRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<ExamRoom> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }

    @PostMapping("/byToday")
    @ApiOperation("今日所有备案的考场信息")
    public List<ExamRoomDTO> byToday(@RequestBody PageQuery pageQuery) {
        Map<String, Object> query = pageQuery.getQuery();
        Integer status = Convert.convert(Integer.class, query.get("status"));
        query.remove("status");
        return examRoomService.byToday(pageQuery, status);
    }
}

