/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.dmReadingRoomConfig.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dm.dmReadingRoomConfig.domain.DmReadingRoomConfig;
import me.zhengjie.modules.dm.dmReadingRoomConfig.service.DmReadingRoomConfigService;
import me.zhengjie.modules.dm.dmReadingRoomConfig.service.dto.DmReadingRoomConfigDto;
import me.zhengjie.modules.dm.dmReadingRoomConfig.service.dto.DmReadingRoomConfigQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author longda
* @date 2022-02-24
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "/api/configReadRoom管理")
@RequestMapping("/api/dmReadingRoomConfig")
public class DmReadingRoomConfigController {

    private final DmReadingRoomConfigService dmReadingRoomConfigService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dmReadingRoomConfig:list')")
    public void download(HttpServletResponse response, DmReadingRoomConfigQueryCriteria criteria) throws IOException {
        dmReadingRoomConfigService.download(dmReadingRoomConfigService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询/api/configReadRoom")
    @ApiOperation("查询/api/configReadRoom")
    @PreAuthorize("@el.check('dmReadingRoomConfig:list')")
    public ResponseEntity<Object> query(DmReadingRoomConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(dmReadingRoomConfigService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增/api/configReadRoom")
    @ApiOperation("新增/api/configReadRoom")
    @PreAuthorize("@el.check('dmReadingRoomConfig:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DmReadingRoomConfig resources){
        Map<String,Object> map = new HashMap<>();
         List<DmReadingRoomConfigDto> dmReadingRoomConfigDtos = dmReadingRoomConfigService.queryAll(null);
        if (dmReadingRoomConfigDtos.size()>0 ){
            map.put("content","只能允许一个配置");
             return new ResponseEntity<>(map,HttpStatus.OK);
         }
        map.put("content","添加配置成功");
        dmReadingRoomConfigService.create(resources);
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @PutMapping
    @Log("修改/api/configReadRoom")
    @ApiOperation("修改/api/configReadRoom")
    @PreAuthorize("@el.check('dmReadingRoomConfig:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DmReadingRoomConfig resources){
        dmReadingRoomConfigService.update(resources);
        Map<String,Object> map = new HashMap<>();
        map.put("content","修改配置成功");
        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @Log("删除/api/configReadRoom")
    @ApiOperation("删除/api/configReadRoom")
    @PreAuthorize("@el.check('dmReadingRoomConfig:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        dmReadingRoomConfigService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}