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

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.socialstatus.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.socialstatus.SocialStatusDO;
import cn.newfeifan.mall.module.distri.service.socialstatus.SocialStatusService;

@Tag(name = "管理后台 - 分销用户身价")
@RestController
@RequestMapping("/distri/social-status")
@Validated
public class SocialStatusController {

    @Resource
    private SocialStatusService socialStatusService;

    @PostMapping("/create")
    @Operation(summary = "创建分销用户身价")
    @PreAuthorize("@ss.hasPermission('distri:social-status:create')")
    public CommonResult<Long> createSocialStatus(@Valid @RequestBody SocialStatusSaveReqVO createReqVO) {
        return success(socialStatusService.createSocialStatus(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销用户身价")
    @PreAuthorize("@ss.hasPermission('distri:social-status:update')")
    public CommonResult<Boolean> updateSocialStatus(@Valid @RequestBody SocialStatusSaveReqVO updateReqVO) {
        socialStatusService.updateSocialStatus(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销用户身价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:social-status:delete')")
    public CommonResult<Boolean> deleteSocialStatus(@RequestParam("id") Long id) {
        socialStatusService.deleteSocialStatus(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销用户身价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:social-status:query')")
    public CommonResult<SocialStatusRespVO> getSocialStatus(@RequestParam("id") Long id) {
        SocialStatusDO socialStatus = socialStatusService.getSocialStatus(id);
        return success(BeanUtils.toBean(socialStatus, SocialStatusRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销用户身价分页")
    @PreAuthorize("@ss.hasPermission('distri:social-status:query')")
    public CommonResult<PageResult<SocialStatusRespVO>> getSocialStatusPage(@Valid SocialStatusPageReqVO pageReqVO) {
        PageResult<SocialStatusDO> pageResult = socialStatusService.getSocialStatusPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SocialStatusRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销用户身价 Excel")
    @PreAuthorize("@ss.hasPermission('distri:social-status:export')")
    @OperateLog(type = EXPORT)
    public void exportSocialStatusExcel(@Valid SocialStatusPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SocialStatusDO> list = socialStatusService.getSocialStatusPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分销用户身价.xls", "数据", SocialStatusRespVO.class,
                        BeanUtils.toBean(list, SocialStatusRespVO.class));
    }

}