/*
 *  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.basicdata.service.dto.BillTypeQueryCriteria;
import com.youchain.businessdata.domain.Asn;
import com.youchain.businessdata.repository.AsnRepository;
import com.youchain.businessdata.service.AsnService;
import com.youchain.businessdata.service.dto.AsnQueryCriteria;
import com.youchain.exception.handler.ApiResult;
import com.youchain.utils.CodeUtils;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.HttpStatus.BAD_REQUEST;

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

    private final AsnService asnService;
    private final AsnRepository asnRepository;
    private final CodeUtils codeUtils;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportAsn(HttpServletResponse response, AsnQueryCriteria criteria) throws Exception {
        asnService.download(asnService.queryAll(criteria), response);
    }

    @GetMapping(value = "/maxNo")
    @Log("查询最大的单号")
    @ApiOperation("查询最大单号")
    @AnonymousAccess
    public ResponseEntity<Object> maxNo(@RequestParam("code") String code) {
        return new ResponseEntity<>(codeUtils.getCode_yyMMdd(code, 3), HttpStatus.OK);
    }


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


    @GetMapping("/getIdByAsn")
    @Log("查询asn")
    @ApiOperation("查询asn")
    @AnonymousAccess
    public ResponseEntity<Object> getIdByAsn(@RequestParam("id") Long id) {
        return new ResponseEntity<>(asnService.findById(id), HttpStatus.OK);
    }

    @GetMapping("/queryAsnAll")
    @Log("查询asn")
    @ApiOperation("查询asn")
    @AnonymousAccess
    public ResponseEntity<Object> queryAsnAll(AsnQueryCriteria criteria) {
        return new ResponseEntity<>(asnService.queryAll(criteria), HttpStatus.OK);
    }

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

    @PutMapping
    @Log("修改asn")
    @ApiOperation("修改asn")
    @PreAuthorize("@el.check('super:man')")
    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('super:man')")
    public ResponseEntity<Object> deleteAsn(@RequestBody Long[] ids) {
        asnService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}