package com.miaxis.app.controller.school;

import com.miaxis.school.domain.SchoolRegion;
import com.miaxis.school.service.ISchoolRegionService;
import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.utils.poi.ExcelUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 【驾校训练场】Controller
 *
 * @author zhangbin
 * @date 2020-12-28
 */
@RestController
@RequestMapping(Constants.OPEN_PREFIX+"/school/region")
@Api(tags={"【app-驾校训练场】"})
public class SchoolRegionController extends BaseController{

    @Autowired
    private ISchoolRegionService schoolRegionService;

    /**
     * 查询驾校训练场列表
     */
    @GetMapping("/list")
    @ApiOperation("查询驾校训练场列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<SchoolRegion> list(@ModelAttribute SchoolRegion schoolRegion){
        startPage();
        List<SchoolRegion> list = schoolRegionService.queryList(schoolRegion);
        return toResponsePageInfo(list);
    }

    /**
     * 导出驾校训练场列表
     */
    @GetMapping("/export")
    @ApiOperation("导出驾校训练场列表Excel")
    public Response<String> export(@ModelAttribute SchoolRegion schoolRegion){
        List<SchoolRegion> list = schoolRegionService.queryList(schoolRegion);
        ExcelUtil<SchoolRegion> util = new ExcelUtil<>(SchoolRegion.class);
        return util.exportExcel(list, "region");
    }

    /**
     * 获取驾校训练场详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取驾校训练场详细信息")
    public Response<SchoolRegion> getInfo(
            @ApiParam(name = "id", value = "驾校训练场参数", required = true)
            @PathVariable("id") Long id
    ){
        return Response.success(schoolRegionService.getById(id));
    }

    /**
     * 新增驾校训练场
     */
    @PostMapping
    @ApiOperation("新增驾校训练场")
    public Response<Integer> add(@RequestBody SchoolRegion schoolRegion){
        return toResponse(schoolRegionService.save(schoolRegion) ? 1 : 0);
    }

    /**
     * 修改驾校训练场
     */
    @PutMapping
    @ApiOperation("修改驾校训练场")
    public Response<Integer> edit(@RequestBody SchoolRegion schoolRegion){
        return toResponse(schoolRegionService.updateById(schoolRegion) ? 1 : 0);
    }

    /**
     * 删除驾校训练场
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除驾校训练场")
    public  Response<Integer> remove(
            @ApiParam(name = "ids", value = "驾校训练场ids参数", required = true)
            @PathVariable Long[] ids
    ){
        return toResponse(schoolRegionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
