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

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.information.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.information.InformationDO;
import com.jojubanking.boot.module.system.convert.information.InformationConvert;
import com.jojubanking.boot.module.system.service.information.InformationService;

@Api(tags = "管理后台 - 指南信息")
@RestController
@RequestMapping("/system/information")
@Validated
public class InformationController {

    @Resource
    private InformationService informationService;

    @PostMapping("/create")
    @ApiOperation("创建指南信息")
    @OperateLog
    @PreAuthorize("@ss.hasPermission('system:information:create')")
    public CommonResult<Integer> createInformation(@Valid @RequestBody InformationCreateReqVO createReqVO) {
        return success(informationService.createInformation(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新指南信息")
    @OperateLog
    @PreAuthorize("@ss.hasPermission('system:information:update')")
    public CommonResult<Boolean> updateInformation(@Valid @RequestBody InformationUpdateReqVO updateReqVO) {
        informationService.updateInformation(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得指南信息")
    @OperateLog
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:information:query')")
    public CommonResult<InformationRespVO> getInformation(@RequestParam("id") Integer id) {
        InformationDO information = informationService.getInformation(id);
        return success(InformationConvert.INSTANCE.convert(information));
    }

    @GetMapping("/list")
    @ApiOperation("获得指南信息列表")
    @OperateLog
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:information:query')")
    public CommonResult<List<InformationRespVO>> getInformationList(@RequestParam("ids") Collection<Integer> ids) {
        List<InformationDO> list = informationService.getInformationList(ids);
        return success(InformationConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得指南信息分页")
    @OperateLog
    @PreAuthorize("@ss.hasPermission('system:information:query')")
    public CommonResult<PageResult<InformationRespVO>> getInformationPage(@Valid InformationPageReqVO pageVO) {
        PageResult<InformationDO> pageResult = informationService.getInformationPage(pageVO);
        return success(InformationConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出指南信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:information:export')")
    @OperateLog(type = EXPORT)
    public void exportInformationExcel(@Valid InformationExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InformationDO> list = informationService.getInformationList(exportReqVO);
        // 导出 Excel
        List<InformationExcelVO> datas = InformationConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "指南信息.xls", "数据", InformationExcelVO.class, datas);
    }

}
