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

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.businessdata.domain.Asn;
import com.youchain.businessdata.service.AsnService;
import com.youchain.businessdata.service.dto.AsnQueryCriteria;
import com.youchain.exception.handler.ApiResult;
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;

/**
* @website https://eladmin.vip
* @author hjl
* @date 2024-02-29
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "asn管理")
@RequestMapping("/api/asn")
public class AsnController {

    private final AsnService asnService;

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

    @GetMapping
    @Log("查询asn")
    @ApiOperation("查询asn")
    @PreAuthorize("@el.check('asn:list')")
    public ResponseEntity<Object> queryAsn(AsnQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(asnService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增asn")
    @ApiOperation("新增asn")
    @PreAuthorize("@el.check('asn:add')")
    public ResponseEntity<Object> createAsn(@Validated @RequestBody Asn resources){
        return new ResponseEntity<>(asnService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改asn")
    @ApiOperation("修改asn")
    @PreAuthorize("@el.check('asn:edit')")
    public ResponseEntity<Object> updateAsn(@Validated @RequestBody Asn resources){
        asnService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除asn")
    @ApiOperation("删除asn")
    @PreAuthorize("@el.check('asn:del')")
    public ResponseEntity<Object> deleteAsn(@RequestBody Long[] ids) {
        asnService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @PostMapping("/getAsnList")
    @Log("加载出库单下拉框")
    @ApiOperation("加载出库单下拉框")
    @PreAuthorize("@el.check('asn:del')")
    public ResponseEntity<Object> getAsnList() {
        return new ResponseEntity<>(asnService.getAsnList(),HttpStatus.OK);
    }
    @PostMapping("/startAsn")
    @Log("开始收货")
    @AnonymousAccess
    public ResponseEntity<Object> startAsn(@RequestBody String id) {
        ApiResult apiResult = asnService.startAsn(id);
        return new ResponseEntity<>(ApiResult.success(apiResult.getMessage(),apiResult.getData()), HttpStatus.valueOf(apiResult.getStatus()));
    }
}