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

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.partitionson.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.partitionson.PartitionSonDO;
import cn.newfeifan.mall.module.distri.service.partitionson.PartitionSonService;

@Tag(name = "管理后台 - 后代分区")
@RestController
@RequestMapping("/distri/partition-son")
@Validated
public class PartitionSonController {

    @Resource
    private PartitionSonService partitionSonService;

    @PostMapping("/create")
    @Operation(summary = "创建后代分区")
    @PreAuthorize("@ss.hasPermission('distri:partition-son:create')")
    public CommonResult<Long> createPartitionSon(@Valid @RequestBody PartitionSonSaveReqVO createReqVO) {
        return success(partitionSonService.createPartitionSon(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新后代分区")
    @PreAuthorize("@ss.hasPermission('distri:partition-son:update')")
    public CommonResult<Boolean> updatePartitionSon(@Valid @RequestBody PartitionSonSaveReqVO updateReqVO) {
        partitionSonService.updatePartitionSon(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得后代分区")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:partition-son:query')")
    public CommonResult<PartitionSonRespVO> getPartitionSon(@RequestParam("id") Long id) {
        PartitionSonDO partitionSon = partitionSonService.getPartitionSon(id);
        return success(BeanUtils.toBean(partitionSon, PartitionSonRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得后代分区分页")
    @PreAuthorize("@ss.hasPermission('distri:partition-son:query')")
    public CommonResult<PageResult<PartitionSonRespVO>> getPartitionSonPage(@Valid PartitionSonPageReqVO pageReqVO) {
        PageResult<PartitionSonDO> pageResult = partitionSonService.getPartitionSonPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PartitionSonRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出后代分区 Excel")
    @PreAuthorize("@ss.hasPermission('distri:partition-son:export')")
    @OperateLog(type = EXPORT)
    public void exportPartitionSonExcel(@Valid PartitionSonPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PartitionSonDO> list = partitionSonService.getPartitionSonPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "后代分区.xls", "数据", PartitionSonRespVO.class,
                        BeanUtils.toBean(list, PartitionSonRespVO.class));
    }

}