/*
*  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.Log;
import com.youchain.businessdata.domain.CountPlan;
import com.youchain.businessdata.service.CountPlanService;
import com.youchain.businessdata.service.dto.CountPlanQueryCriteria;
import com.youchain.utils.BizStatus;
import com.youchain.utils.CodeUtils;
import com.youchain.utils.SecurityUtils;
import com.youchain.utils.UserUtils;
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 javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author houjianlan
* @date 2023-10-19
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "count_plan管理")
@RequestMapping("/api/countPlan")
public class CountPlanController {

    private final CountPlanService countPlanService;

    private final CodeUtils codeUtils;

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

    @GetMapping
    @ApiOperation("查询count_plan")
    public ResponseEntity<Object> queryCountPlan(CountPlanQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(countPlanService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增count_plan")
    @ApiOperation("新增count_plan")
    @PreAuthorize("@el.check('countPlan:add')")
    public ResponseEntity<Object> createCountPlan(@Validated @RequestBody CountPlan resources){
        resources.setCode(codeUtils.getCode_yyMMdd("CP",3));
        resources.setDept(UserUtils.getDept());
        resources.setStatus(BizStatus.OPEN);
        return new ResponseEntity<>(countPlanService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改count_plan")
    @ApiOperation("修改count_plan")
    @PreAuthorize("@el.check('countPlan:edit')")
    public ResponseEntity<Object> updateCountPlan(@Validated @RequestBody CountPlan resources){
        countPlanService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

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