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.LargeCarDetail;
import com.project.zcustom.service.unit.ILargeCarDetailService;
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/carDetail")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class LargeCarDetailController extends BaseController {

    private final ILargeCarDetailService largeCarDetailService;

    @GetMapping("/list")
    public AjaxResult list(LargeCarDetail entity) {
        return AjaxResult.success("查询成功", largeCarDetailService.pagingLargeCarDetailList(entity));
    }

    @GetMapping("/listAll")
    public AjaxResult listAll(LargeCarDetail entity) {
        return AjaxResult.success("查询成功", largeCarDetailService.selectLargeCarDetailList(entity));
    }

    @Log(title = "公务用车年度增加减少", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LargeCarDetail entity) {
        List<LargeCarDetail> list = largeCarDetailService.selectLargeCarDetailList(entity);
        ExcelUtil<LargeCarDetail> util = new ExcelUtil<>(LargeCarDetail.class);
        util.exportExcel(response, list, "公务用车年度增加减少数据");
    }

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

    @PreAuthorize("@ss.hasPermi('zcustom:platPublicCarChangeDetail:add')")
    @Log(title = "公务用车年度增加减少", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody LargeCarDetail entity) {
        return toAjax(largeCarDetailService.save(entity));
    }

    @PreAuthorize("@ss.hasPermi('zcustom:platPublicCarChangeDetail:edit')")
    @Log(title = "公务用车年度增加减少", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody LargeCarDetail entity) {
        return toAjax(largeCarDetailService.updateById(entity));
    }

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

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

