/*
 *  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 com.youchain.basicdata.rest;

import com.youchain.annotation.Log;
import com.youchain.basicdata.domain.PackageCheck;
import com.youchain.basicdata.service.PackageCheckService;
import com.youchain.basicdata.service.dto.PackageCheckQueryCriteria;
import com.youchain.utils.UserUtils;
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 javax.servlet.http.HttpServletResponse;

/**
 * @author huojin
 * @website https://eladmin.vip
 * @date 2024-06-13
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "package_check管理")
@RequestMapping("/api/packageCheck")
public class PackageCheckController {

    private final PackageCheckService packageCheckService;

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

    @GetMapping
    @ApiOperation("查询package_check")
    @PreAuthorize("@el.check('packageCheck:list')")
    public ResponseEntity<Object> queryPackageCheck(PackageCheckQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(packageCheckService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增package_check")
    @ApiOperation("新增package_check")
    @PreAuthorize("@el.check('packageCheck:add')")
    public ResponseEntity<Object> createPackageCheck(@Validated @RequestBody PackageCheck resources) {
        resources.setDept(UserUtils.getDept());
        return new ResponseEntity<>(packageCheckService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改package_check")
    @ApiOperation("修改package_check")
    @PreAuthorize("@el.check('packageCheck:edit')")
    public ResponseEntity<Object> updatePackageCheck(@Validated @RequestBody PackageCheck resources) {
        packageCheckService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除package_check")
    @ApiOperation("删除package_check")
    @PreAuthorize("@el.check('packageCheck:del')")
    public ResponseEntity<Object> deletePackageCheck(@RequestBody Long[] ids) {
        packageCheckService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
