/*
 *  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.Item;
import com.youchain.basicdata.service.ItemService;
import com.youchain.basicdata.service.dto.ItemDto;
import com.youchain.basicdata.service.dto.ItemQueryCriteria;
import com.youchain.config.FileProperties;
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 org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

/**
 * @author houjianlan
 * @website https://eladmin.vip
 * @date 2023-08-16
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "item管理")
@Slf4j
@RequestMapping("/api/item")
public class ItemController {

    private final ItemService itemService;
    private final FileProperties properties;


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

    @GetMapping
    @ApiOperation("查询item")
    public ResponseEntity<Object> queryItem(ItemQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(itemService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping("/queryItemAll")
    @ApiOperation("查询item")
    public ResponseEntity<Object> queryItemAll(ItemQueryCriteria criteria) {
        return new ResponseEntity<>(itemService.queryAll(criteria), HttpStatus.OK);
    }


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

    @PostMapping(value = "/import_data")
    @Log("导入物料")
    @ApiOperation("导入物料")
    @PreAuthorize("@el.check('point:importPoint')")
    public ResponseEntity<Object> importItem(@RequestParam("file") MultipartFile multipartFile) {
        try {
            String result = itemService.impoertItem(multipartFile);
            return successRequest(result);
        } catch (Exception e) {
            return badRequest("导入失败：" + e.getMessage());
        }
    }


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

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

    private ResponseEntity<Object> badRequest(String message) {
        return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST);
    }

    private ResponseEntity<Object> successRequest(Object object) {
        return new ResponseEntity<>(object, HttpStatus.OK);
    }
}
