/*
*  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.PointDto;
import com.youchain.basicdata.service.dto.PointQueryCriteria;
import com.youchain.businessdata.domain.Task;
import com.youchain.businessdata.service.TaskService;
import com.youchain.businessdata.service.dto.TaskQueryCriteria;
import com.youchain.utils.BaseStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.util.StandardSessionIdGenerator;
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.List;
import javax.servlet.http.HttpServletResponse;

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

    private final TaskService taskService;

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

    @GetMapping
    @Log("查询task")
    @ApiOperation("查询task")
    @AnonymousAccess
    public ResponseEntity<Object> queryTask(TaskQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(taskService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @GetMapping(value = "/queryAsnTask")
    @Log("查询task")
    @ApiOperation("查询task")
    @AnonymousAccess
    public ResponseEntity<Object> queryAsnTask(TaskQueryCriteria criteria){
        log.info(criteria.getAsnDetailId()+"");
//        TaskQueryCriteria tq=new TaskQueryCriteria();
//        tq.setAsnDetailId(id);
        return new ResponseEntity<>(taskService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping("/queryPickTask")
    @Log("拣货确认-拣货记录")
    @ApiOperation("拣货确认-拣货记录")
    @AnonymousAccess
    public  ResponseEntity<Object> queryPickTask(TaskQueryCriteria criteria){
        return new ResponseEntity<>(taskService.queryAll(criteria),HttpStatus.OK);
    }

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

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

    @DeleteMapping
    @Log("删除task")
    @ApiOperation("删除task")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> deleteTask(@RequestBody Long[] ids) {
        taskService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}