/*
*  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.DuplicateSubmission.DuplicateSubmission;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.basicdata.domain.BillType;
import com.youchain.basicdata.repository.BillTypeRepository;
import com.youchain.businessdata.domain.PickInstruction;
import com.youchain.businessdata.service.PickInstructionService;
import com.youchain.businessdata.service.dto.PickInstructionQueryCriteria;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.CodeUtils;
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.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author DengJiangKun
* @date 2024-02-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "pickInstruction管理")
@RequestMapping("/api/pickInstruction")
public class PickInstructionController {

    private final PickInstructionService pickInstructionService;
    private final CodeUtils codeUtil;
    private final BillTypeRepository billTypeRepository;

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

    @GetMapping
    @Log("查询pickInstruction")
    @ApiOperation("查询pickInstruction")
    @AnonymousAccess
    public ResponseEntity<Object> queryPickInstruction(PickInstructionQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(pickInstructionService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增pickInstruction")
    @ApiOperation("新增pickInstruction")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> createPickInstruction(@Validated @RequestBody PickInstruction resources){
        if (resources.getAllocationNumber() == null || resources.getAllocationNumber().isEmpty()){
            resources.setAllocationNumber(codeUtil.getCode_yyMMdd("ZS",3));
        }
        BillType billType = billTypeRepository.findByName(BaseStatus.BT_SGDD);
        resources.setOutKType(billType);
        resources.setAllocationDate(new Timestamp(new Date().getTime()));
        return new ResponseEntity<>(pickInstructionService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改pickInstruction")
    @ApiOperation("修改pickInstruction")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> updatePickInstruction(@Validated @RequestBody PickInstruction resources){
        pickInstructionService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除pickInstruction")
    @ApiOperation("删除pickInstruction")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> deletePickInstruction(@RequestBody Long[] ids) {
        pickInstructionService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @PostMapping("/generatePrepFlag")
    @Log("手工指示-生成备货操作")
    @ApiOperation("手工指示-生成备货操作")
    @PreAuthorize("@el.check('super:man')")
    @Transactional
    @DuplicateSubmission
    public ResponseEntity<Object> generatePrepFlag(@RequestBody Long[] ids) throws Exception {
        pickInstructionService.generatePrepFlag(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}