/*
*  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.store.rest;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Pageable;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.store.domain.Store;
import me.zhengjie.modules.store.service.StoreService;
import me.zhengjie.modules.store.service.dto.StoreDto;
import me.zhengjie.modules.store.service.dto.StoreQueryCriteria;
import me.zhengjie.utils.PageResult;

/**
 * @website https://eladmin.vip
 * @author Bu
 * @date 2023-09-13
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "/api/stores管理")
@RequestMapping("/api/store")
public class StoreController {

    private final StoreService storeService;

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

    @GetMapping
    @Log("查询/api/stores")
    @ApiOperation("查询/api/stores")
    @PreAuthorize("@el.check('store:list')")
    public ResponseEntity<PageResult<StoreDto>> queryStore(StoreQueryCriteria criteria, Pageable pageable) {
        criteria.setPidIsNull(true);
        return new ResponseEntity<>(storeService.queryAll(criteria, pageable),
                HttpStatus.OK);
    }

    @GetMapping("/lazy")
    @ApiOperation("返回子菜单")
    @PreAuthorize("@el.check('store:list')")
    public ResponseEntity<List<StoreDto>> queryChildren(@RequestParam Integer pid) {
        return new ResponseEntity<>(storeService.getStores(pid), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增/api/stores")
    @ApiOperation("新增/api/stores")
    @PreAuthorize("@el.check('store:add')")
    public ResponseEntity<Object> createStore(@Validated @RequestBody Store resources) {
        storeService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改/api/stores")
    @ApiOperation("修改/api/stores")
    @PreAuthorize("@el.check('store:edit')")
    public ResponseEntity<Object> updateStore(@Validated @RequestBody Store resources) {
        storeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除/api/stores")
    @ApiOperation("删除/api/stores")
    @PreAuthorize("@el.check('store:del')")
    public ResponseEntity<Object> deleteStore(@RequestBody Integer[] ids) {
        storeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}