package cn.newfeifan.mall.module.system.controller.admin.config;

import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;
import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import cn.newfeifan.mall.module.system.controller.admin.config.vo.ConfigPageReqVO;
import cn.newfeifan.mall.module.system.controller.admin.config.vo.ConfigRespVO;
import cn.newfeifan.mall.module.system.controller.admin.config.vo.ConfigSaveReqVO;
import cn.newfeifan.mall.module.system.dal.dataobject.config.SystemConfigDO;
import cn.newfeifan.mall.module.system.service.config.SystemConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 系统配置")
@RestController
@RequestMapping("/system/config")
@Validated
public class SystemConfigController {

    @Resource
    private SystemConfigService configService;

    @PostMapping("/create")
    @Operation(summary = "创建系统配置")
    @PreAuthorize("@ss.hasPermission('system:config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody ConfigSaveReqVO createReqVO) {
        return success(configService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统配置")
    @PreAuthorize("@ss.hasPermission('system:config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody ConfigSaveReqVO updateReqVO) {
        configService.updateConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除系统配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:config:delete')")
    public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
        configService.deleteConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:config:query')")
    public CommonResult<ConfigRespVO> getConfig(@RequestParam("id") Long id) {
        SystemConfigDO config = configService.getConfig(id);
        return success(BeanUtils.toBean(config, ConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得系统配置分页")
    @PreAuthorize("@ss.hasPermission('system:config:query')")
    public CommonResult<PageResult<ConfigRespVO>> getConfigPage(@Valid ConfigPageReqVO pageReqVO) {
        PageResult<SystemConfigDO> pageResult = configService.getConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出系统配置 Excel")
    @PreAuthorize("@ss.hasPermission('system:config:export')")
    @OperateLog(type = EXPORT)
    public void exportConfigExcel(@Valid ConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SystemConfigDO> list = configService.getConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "系统配置.xls", "数据", ConfigRespVO.class,
                        BeanUtils.toBean(list, ConfigRespVO.class));
    }

}
