/*
 *  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.Log;
import com.youchain.basicdata.domain.*;
import com.youchain.basicdata.service.StockService;
import com.youchain.businessdata.domain.*;
import com.youchain.businessdata.service.*;
import com.youchain.businessdata.service.dto.AsnDetailQueryCriteria;
import com.youchain.modules.quartz.utils.TimeNumberUtils;
import com.youchain.utils.*;
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 java.beans.Transient;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;


/**
 * @author hjl
 * @website https://eladmin.vip
 * @date 2023-08-14
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "asn_detail管理")
@Slf4j
@RequestMapping("/api/asnDetail")
public class AsnDetailController {

    private final AsnDetailService asnDetailService;

    private final StockService stockService;

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

    @GetMapping
    @ApiOperation("查询入库明细")
    public ResponseEntity<Object> queryAsnDetail(AsnDetailQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(asnDetailService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增入库明细")
    @ApiOperation("新增入库明细")
    @PreAuthorize("@el.check('asnDetail:add')")
    @Transient
    public ResponseEntity<Object> createAsnDetail(@Validated @RequestBody AsnDetail asnDetail) {
        asnDetail.setStatus(BizStatus.OPEN);
        asnDetail.setDept(UserUtils.getDept());
        asnDetail.setPo(TimeNumberUtils.getRKCode());
        asnDetailService.create(asnDetail);
        Stock stock = asnDetail.getStock();
        stock.setStatus(BaseStatus.USED);
        stockService.update(stock);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @PutMapping
    @Log("修改asn_detail")
    @ApiOperation("修改asn_detail")
    @PreAuthorize("@el.check('asnDetail:edit')")
    public ResponseEntity<Object> updateAsnDetail(@Validated @RequestBody AsnDetail resources) {
        asnDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

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