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.LargeCarRank;
import com.project.zcustom.service.unit.ILargeCarRankService;
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;
import java.util.Map;

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

    private final ILargeCarRankService largeCarRankService;

    @GetMapping("/list")
    public AjaxResult list(LargeCarRank entity) {
        return AjaxResult.success("查询成功", largeCarRankService.pagingLargeCarRankList(entity));
    }

    @GetMapping("/listAll")
    public AjaxResult listAll(LargeCarRank entity) {
        return AjaxResult.success("查询成功", largeCarRankService.selectLargeCarRankList(entity));
    }

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

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

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

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

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

    /**
     * 上传json
     * */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestBody Map<String, Object> obj) {
        return toAjax(largeCarRankService.upload(obj));
    }
}
