/*
 *  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 cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.service.AgvTaskService;
import com.youchain.businessdata.service.dto.AgvTaskDto;
import com.youchain.businessdata.service.dto.AgvTaskQueryCriteria;
import com.youchain.exception.handler.ApiResult;
import com.youchain.utils.StringUtils;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @author baobinglin
 * @website https://eladmin.vip
 * @date 2023-08-18
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "agvTask管理")
@RequestMapping("/api/agvTask")
public class AgvTaskController {

    private final AgvTaskService agvTaskService;

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

    @GetMapping
    @ApiOperation("查询agvTask")
    public ResponseEntity<Object> queryAgvTask(AgvTaskQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(agvTaskService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "/queryAgvTaskInfo")
    @AnonymousAccess
    public ResponseEntity<Object> queryAgvTaskInfo(AgvTaskQueryCriteria criteria) {
        List<AgvTaskDto> agvTaskDtoList = agvTaskService.queryAll(criteria);
        JSONArray array = new JSONArray();
        for (AgvTaskDto agvTaskDto : agvTaskDtoList) {
            List<String> array2 = new ArrayList<>();
            array2.add(agvTaskDto.getId().toString());
            array2.add(agvTaskDto.getStockCode());
            array2.add(DateUtil.formatDateTime(agvTaskDto.getCreateTime()));
            array.add(array2);
        }
        return new ResponseEntity<>(ApiResult.success(HttpStatus.OK.value(), "", array), HttpStatus.OK);
    }

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

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

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

}
