package com.jojubanking.boot.module.system.controller.admin.label;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.label.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.label.LabelDO;
import com.jojubanking.boot.module.system.convert.label.LabelConvert;
import com.jojubanking.boot.module.system.service.label.LabelService;

@Api(tags = "管理后台 - 信息")
@RestController
@RequestMapping("/system/label")
@Validated
public class LabelController {

    @Resource
    private LabelService labelService;

    @PostMapping("/create")
    @ApiOperation("创建信息")
    @PreAuthorize("@ss.hasPermission('system:label:create')")
    public CommonResult<Integer> createLabel(@Valid @RequestBody LabelCreateReqVO createReqVO) {
        return success(labelService.createLabel(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新信息")
    @PreAuthorize("@ss.hasPermission('system:label:update')")
    public CommonResult<Boolean> updateLabel(@Valid @RequestBody LabelUpdateReqVO updateReqVO) {
        labelService.updateLabel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:label:delete')")
    public CommonResult<Boolean> deleteLabel(@RequestParam("id") Integer id) {
        labelService.deleteLabel(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:label:query')")
    public CommonResult<LabelRespVO> getLabel(@RequestParam("id") Integer id) {
        LabelDO label = labelService.getLabel(id);
        return success(LabelConvert.INSTANCE.convert(label));
    }

    @GetMapping("/list")
    @ApiOperation("获得信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:label:query')")
    public CommonResult<List<LabelRespVO>> getLabelList(@RequestParam("ids") Collection<Integer> ids) {
        List<LabelDO> list = labelService.getLabelList(ids);
        return success(LabelConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得信息分页")
    @PreAuthorize("@ss.hasPermission('system:label:query')")
    public CommonResult<PageResult<LabelRespVO>> getLabelPage(@Valid LabelPageReqVO pageVO) {
        PageResult<LabelDO> pageResult = labelService.getLabelPage(pageVO);
        return success(LabelConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:label:export')")
    @OperateLog(type = EXPORT)
    public void exportLabelExcel(@Valid LabelExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LabelDO> list = labelService.getLabelList(exportReqVO);
        // 导出 Excel
        List<LabelExcelVO> datas = LabelConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "信息.xls", "数据", LabelExcelVO.class, datas);
    }

}
