package com.project.zcustom.controller.backManager.officialCar;

import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.enums.BusinessType;
import com.project.common.utils.poi.ExcelUtil;
import com.project.zcustom.domain.addional.LargeUnitRank;
import com.project.zcustom.service.unit.ILargeUnitRankService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/zcustom/unitRank")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class LargeUnitRankController extends BaseController {

    private final ILargeUnitRankService largeUnitRankService;

    @GetMapping("/getList")
    public AjaxResult getList() {
        return AjaxResult.success("查询成功", largeUnitRankService.getList());
    }

    @GetMapping("/list")
    public AjaxResult list(LargeUnitRank entity) {
        return AjaxResult.success("查询成功", largeUnitRankService.pagingLargeUnitRankList(entity));
    }

    @GetMapping("/listAll")
    public AjaxResult listAll(LargeUnitRank entity) {
        return AjaxResult.success("查询成功", largeUnitRankService.selectLargeUnitRankList(entity));
    }

    @Log(title = "公务用车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LargeUnitRank entity) {
        List<LargeUnitRank> list = largeUnitRankService.selectLargeUnitRankList(entity);
        ExcelUtil<LargeUnitRank> util = new ExcelUtil<>(LargeUnitRank.class);
        util.exportExcel(response, list, "公务用车数据");
    }

    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", largeUnitRankService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('zcustom:platPublicCarRankUnit:add')")
    @Log(title = "公务用车", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody LargeUnitRank entity) {
        return toAjax(largeUnitRankService.save(entity));
    }

    @PreAuthorize("@ss.hasPermi('zcustom:platPublicCarRankUnit:edit')")
    @Log(title = "公务用车", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody LargeUnitRank entity) {
        return toAjax(largeUnitRankService.updateById(entity));
    }

    @PreAuthorize("@ss.hasPermi('zcustom:platPublicCarRankUnit:remove')")
    @Log(title = "公务用车", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(largeUnitRankService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
