package cn.newfeifan.mall.module.distri.controller.admin.source;

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.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

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

import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;

import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;

import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.newfeifan.mall.module.distri.controller.admin.source.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.source.SourceDO;
import cn.newfeifan.mall.module.distri.service.source.SourceService;

@Tag(name = "管理后台 - 分销来源")
@RestController
@RequestMapping("/distri/source")
@Validated
public class SourceController {

    @Resource
    private SourceService sourceService;

    @PostMapping("/create")
    @Operation(summary = "创建分销来源")
    @PreAuthorize("@ss.hasPermission('distri:source:create')")
    public CommonResult<Long> createSource(@Valid @RequestBody SourceSaveReqVO createReqVO) {
        return success(sourceService.createSource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销来源")
    @PreAuthorize("@ss.hasPermission('distri:source:update')")
    public CommonResult<Boolean> updateSource(@Valid @RequestBody SourceSaveReqVO updateReqVO) {
        sourceService.updateSource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销来源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:source:delete')")
    public CommonResult<Boolean> deleteSource(@RequestParam("id") Long id) {
        sourceService.deleteSource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销来源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:source:query')")
    public CommonResult<SourceRespVO> getSource(@RequestParam("id") Long id) {
        SourceDO source = sourceService.getSource(id);
        return success(BeanUtils.toBean(source, SourceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销来源分页")
    @PreAuthorize("@ss.hasPermission('distri:source:query')")
    public CommonResult<PageResult<SourceRespVO>> getSourcePage(@Valid SourcePageReqVO pageReqVO) {
        PageResult<SourceDO> pageResult = sourceService.getSourcePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SourceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销来源 Excel")
    @PreAuthorize("@ss.hasPermission('distri:source:export')")
    @OperateLog(type = EXPORT)
    public void exportSourceExcel(@Valid SourcePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SourceDO> list = sourceService.getSourcePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分销来源.xls", "数据", SourceRespVO.class,
                        BeanUtils.toBean(list, SourceRespVO.class));
    }

}