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

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.ScoreDimension;
import com.sundot.airport.ledger.service.IScoreDimensionService;
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.*;

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

/**
 * 评分维度管理
 */
@RestController
@RequestMapping("/score/dimension")
public class ScoreDimensionController extends BaseController {

    @Autowired
    private IScoreDimensionService service;

    @PreAuthorize("@ss.hasPermi('score:dimension:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScoreDimension query) {
        startPage();
        return getDataTable(service.selectList(query));
    }

    /** 不分页列表，用于前端下拉选择 */
    @PreAuthorize("@ss.hasPermi('score:dimension:list')")
    @GetMapping("/all")
    public AjaxResult all(ScoreDimension query) {
        return AjaxResult.success(service.selectList(query));
    }

    @PreAuthorize("@ss.hasPermi('score:dimension:export')")
    @Log(title = "评分维度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScoreDimension query) {
        List<ScoreDimension> list = service.selectList(query);
        new ExcelUtil<>(ScoreDimension.class).exportExcel(response, list, "评分维度");
    }

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

    @PreAuthorize("@ss.hasPermi('score:dimension:add')")
    @Log(title = "评分维度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ScoreDimension entity) {
        entity.setCreateBy(getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return toAjax(service.save(entity));
    }

    @PreAuthorize("@ss.hasPermi('score:dimension:edit')")
    @Log(title = "评分维度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ScoreDimension entity) {
        entity.setUpdateBy(getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return toAjax(service.updateById(entity));
    }

    @PreAuthorize("@ss.hasPermi('score:dimension:remove')")
    @Log(title = "评分维度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) service.removeById(id);
        return AjaxResult.success();
    }
}
