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

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.partitioncrash.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.partitioncrash.PartitionCrashDO;
import cn.newfeifan.mall.module.distri.service.partitioncrash.PartitionCrashService;

@Tag(name = "管理后台 - 分区碰撞")
@RestController
@RequestMapping("/distri/partition-crash")
@Validated
public class PartitionCrashController {

    @Resource
    private PartitionCrashService partitionCrashService;


    // 查询某一个用户昨天的后代产生的订单

    @PostMapping("/create")
    @Operation(summary = "创建分区碰撞")
    @PreAuthorize("@ss.hasPermission('distri:partition-crash:create')")
    public CommonResult<Long> createPartitionCrash(@Valid @RequestBody PartitionCrashSaveReqVO createReqVO) {
        return success(partitionCrashService.createPartitionCrash(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分区碰撞")
    @PreAuthorize("@ss.hasPermission('distri:partition-crash:update')")
    public CommonResult<Boolean> updatePartitionCrash(@Valid @RequestBody PartitionCrashSaveReqVO updateReqVO) {
        partitionCrashService.updatePartitionCrash(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得分区碰撞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:partition-crash:query')")
    public CommonResult<PartitionCrashRespVO> getPartitionCrash(@RequestParam("id") Long id) {
        PartitionCrashDO partitionCrash = partitionCrashService.getPartitionCrash(id);
        return success(BeanUtils.toBean(partitionCrash, PartitionCrashRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分区碰撞分页")
    @PreAuthorize("@ss.hasPermission('distri:partition-crash:query')")
    public CommonResult<PageResult<PartitionCrashRespVO>> getPartitionCrashPage(@Valid PartitionCrashPageReqVO pageReqVO) {
        PageResult<PartitionCrashDO> pageResult = partitionCrashService.getPartitionCrashPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PartitionCrashRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分区碰撞 Excel")
    @PreAuthorize("@ss.hasPermission('distri:partition-crash:export')")
    @OperateLog(type = EXPORT)
    public void exportPartitionCrashExcel(@Valid PartitionCrashPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PartitionCrashDO> list = partitionCrashService.getPartitionCrashPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分区碰撞.xls", "数据", PartitionCrashRespVO.class,
                        BeanUtils.toBean(list, PartitionCrashRespVO.class));
    }

}