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

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.partitionbrothers.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.partitionbrothers.PartitionBrothersDO;
import cn.newfeifan.mall.module.distri.service.partitionbrothers.PartitionBrothersService;

@Tag(name = "管理后台 - 兄弟分区")
@RestController
@RequestMapping("/distri/partition-brothers")
@Validated
public class PartitionBrothersController {

    @Resource
    private PartitionBrothersService partitionBrothersService;

    @PostMapping("/create")
    @Operation(summary = "创建兄弟分区")
    @PreAuthorize("@ss.hasPermission('distri:partition-brothers:create')")
    public CommonResult<Long> createPartitionBrothers(@Valid @RequestBody PartitionBrothersSaveReqVO createReqVO) {
        return success(partitionBrothersService.createPartitionBrothers(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新兄弟分区")
    @PreAuthorize("@ss.hasPermission('distri:partition-brothers:update')")
    public CommonResult<Boolean> updatePartitionBrothers(@Valid @RequestBody PartitionBrothersSaveReqVO updateReqVO) {
        partitionBrothersService.updatePartitionBrothers(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除兄弟分区")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:partition-brothers:delete')")
    public CommonResult<Boolean> deletePartitionBrothers(@RequestParam("id") Long id) {
        partitionBrothersService.deletePartitionBrothers(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得兄弟分区")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:partition-brothers:query')")
    public CommonResult<PartitionBrothersRespVO> getPartitionBrothers(@RequestParam("id") Long id) {
        PartitionBrothersDO partitionBrothers = partitionBrothersService.getPartitionBrothers(id);
        return success(BeanUtils.toBean(partitionBrothers, PartitionBrothersRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得兄弟分区分页")
    @PreAuthorize("@ss.hasPermission('distri:partition-brothers:query')")
    public CommonResult<PageResult<PartitionBrothersRespVO>> getPartitionBrothersPage(@Valid PartitionBrothersPageReqVO pageReqVO) {
        PageResult<PartitionBrothersDO> pageResult = partitionBrothersService.getPartitionBrothersPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PartitionBrothersRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出兄弟分区 Excel")
    @PreAuthorize("@ss.hasPermission('distri:partition-brothers:export')")
    @OperateLog(type = EXPORT)
    public void exportPartitionBrothersExcel(@Valid PartitionBrothersPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PartitionBrothersDO> list = partitionBrothersService.getPartitionBrothersPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "兄弟分区.xls", "数据", PartitionBrothersRespVO.class,
                        BeanUtils.toBean(list, PartitionBrothersRespVO.class));
    }

}