package com.sundot.airport.web.controller.ledger;

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

import com.sundot.airport.common.annotation.Log;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.page.TableDataInfo;
import com.sundot.airport.common.enums.BusinessType;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.common.utils.poi.ExcelUtil;
import com.sundot.airport.ledger.domain.LedgerRewardPenalty;
import com.sundot.airport.ledger.service.ILedgerRewardPenaltyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 部门奖惩记录Controller（页面录入，支持增删改查）
 */
@RestController
@RequestMapping("/ledger/rewardPenalty")
public class LedgerRewardPenaltyController extends BaseController {

    @Autowired
    private ILedgerRewardPenaltyService service;

    @PreAuthorize("@ss.hasPermi('ledger:rewardPenalty:list')")
    @GetMapping("/list")
    public TableDataInfo list(LedgerRewardPenalty query) {
        startPage();
        List<LedgerRewardPenalty> list = service.selectList(query);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ledger:rewardPenalty:export')")
    @Log(title = "部门奖惩记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LedgerRewardPenalty query) {
        List<LedgerRewardPenalty> list = service.selectList(query);
        ExcelUtil<LedgerRewardPenalty> util = new ExcelUtil<>(LedgerRewardPenalty.class);
        util.exportExcel(response, list, "部门奖惩记录");
    }

    @PreAuthorize("@ss.hasPermi('ledger:rewardPenalty:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(service.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('ledger:rewardPenalty:add')")
    @Log(title = "部门奖惩记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LedgerRewardPenalty entity) {
        entity.setCreateBy(getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return toAjax(service.save(entity));
    }

    @PreAuthorize("@ss.hasPermi('ledger:rewardPenalty:edit')")
    @Log(title = "部门奖惩记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody LedgerRewardPenalty entity) {
        entity.setUpdateBy(getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return toAjax(service.updateById(entity));
    }

    @PreAuthorize("@ss.hasPermi('ledger:rewardPenalty:remove')")
    @Log(title = "部门奖惩记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            LedgerRewardPenalty entity = new LedgerRewardPenalty();
            entity.setId(id);
            service.removeById(id);
        }
        return AjaxResult.success();
    }
}
