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.ScoreIndicator;
import com.sundot.airport.ledger.service.IScoreIndicatorService;
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/indicator")
public class ScoreIndicatorController extends BaseController {

    @Autowired
    private IScoreIndicatorService service;

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

    /**
     * 获取指定维度下的指标树（无分页），前端树形表格用
     */
    @PreAuthorize("@ss.hasPermi('score:indicator:list')")
    @GetMapping("/tree")
    public AjaxResult tree(Long dimensionId, Integer org) {
        return AjaxResult.success(service.selectTree(dimensionId, org));
    }

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

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

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

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

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