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.LargeWarnDetail;
import com.project.zcustom.service.unit.ILargeWarnDetailService;
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/warnDetail")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class LargeWarnDetailController extends BaseController {

    private final ILargeWarnDetailService largeWarnDetailService;

    @GetMapping("/list")
    public AjaxResult list(LargeWarnDetail entity) {
        return AjaxResult.success("查询成功", largeWarnDetailService.pagingLargeWarnDetailList(entity));
    }

    @GetMapping("/listAll")
    public AjaxResult listAll(LargeWarnDetail entity) {
        return AjaxResult.success("查询成功", largeWarnDetailService.selectLargeWarnDetailList(entity));
    }

    @Log(title = "公务用车预警明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LargeWarnDetail entity) {
        List<LargeWarnDetail> list = largeWarnDetailService.selectLargeWarnDetailList(entity);
        ExcelUtil<LargeWarnDetail> util = new ExcelUtil<>(LargeWarnDetail.class);
        util.exportExcel(response, list, "公务用车预警明细数据");
    }

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

    @PreAuthorize("@ss.hasPermi('zcustom:platPublicCarWarnDetail:add')")
    @Log(title = "公务用车预警明细", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody LargeWarnDetail entity) {
        return toAjax(largeWarnDetailService.save(entity));
    }

    @PreAuthorize("@ss.hasPermi('zcustom:platPublicCarWarnDetail:edit')")
    @Log(title = "公务用车预警明细", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody LargeWarnDetail entity) {
        return toAjax(largeWarnDetailService.updateById(entity));
    }

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

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