/*
*  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 me.zhengjie.ykt.rest;

import lombok.Getter;
import me.zhengjie.annotation.Log;
import me.zhengjie.ykt.domain.DmPlateNumber;
import me.zhengjie.ykt.service.DmPlateNumberService;
import me.zhengjie.ykt.service.dto.DmPlateNumberQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.data.repository.query.Param;
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 javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author zhirong
* @date 2021-11-10
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "plate_number管理")
@RequestMapping(value = "/api/dmPlateNumber")
public class DmPlateNumberController {

    private final DmPlateNumberService dmPlateNumberService;

//    @Log("多表查询plate_number")
//    @ApiModelProperty("多表查询plate_number")
//    @GetMapping
////    @PreAuthorize("@el.check('dmPlateNumber:list')")
//    public ResponseEntity<Object> getDmPlateNumberAll(Pageable pageable) {
//       return new ResponseEntity<>(dmPlateNumberService.getAllPlateNumber(pageable),HttpStatus.OK);
//    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @RequestMapping(value = "/download")
    @PreAuthorize("@el.check('dmPlateNumber:list')")
    public void download(HttpServletResponse response, DmPlateNumberQueryCriteria criteria) throws IOException {
//        System.out.println(dmPlateNumberService.queryAll(criteria)+"--------------------------------------------");
        dmPlateNumberService.download(dmPlateNumberService.queryAll(criteria), response);
    }

    @Log("查询plate_number")
    @ApiOperation("查询plate_number")
    @PreAuthorize("@el.check('dmPlateNumber:list')")
    @GetMapping
    public ResponseEntity<Object> query(DmPlateNumberQueryCriteria criteria, Pageable pageable){
//        System.out.println(dmPlateNumberService.queryAll(criteria,pageable)+"========================================");
        return new ResponseEntity<>(dmPlateNumberService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增plate_number")
    @ApiOperation("新增plate_number")
    @PreAuthorize("@el.check('dmPlateNumber:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DmPlateNumber resources){
        dmPlateNumberService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

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

    @PutMapping(value = "/updateEnabled")
    @Log("修改状态")
    @ApiOperation("修改状态")
    public ResponseEntity<Object> updateEnabled(@Validated @RequestBody String resources){
        dmPlateNumberService.updateEnabled(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

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

    /* 搜索 */
    @RequestMapping(value = "/findAllBysousuo")
    public ResponseEntity<Object> findAllBysousuo(DmPlateNumber resources){
        return new ResponseEntity<>(dmPlateNumberService.findAllBysousuo(resources),HttpStatus.OK);
    }

    /* 根据一卡通卡号查询 */
    @RequestMapping(value = "/findDmPlateNumberByCardNumber")
    public ResponseEntity<Object> findDmPlateNumberByCardNumber(@Param(value = "card_number") Long cardNumber){
        return new ResponseEntity<>(dmPlateNumberService.findDmPlateNumberByCardNumber(cardNumber),HttpStatus.OK);
    }

    @Log("查询车牌审批")
    @ApiOperation("查询车牌审批")
    @PreAuthorize("@el.check('dmPlateNumber:list')")
    @GetMapping(value = "/queryPlateUmberApprove")
    public ResponseEntity<Object> c(){
        return new ResponseEntity<>(dmPlateNumberService.queryApprove(),HttpStatus.OK);
    }

    @Log("修改车牌审批")
    @ApiOperation("修改车牌审批")
    @PreAuthorize("@el.check('dmPlateNumber:edit')")
    @GetMapping(value = "/updatePlateUmberApprove")
    public ResponseEntity<Object> updatePlateUmberApprove(String isApprove){
        dmPlateNumberService.updateApprove(isApprove);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}