package cn.newfeifan.mall.module.distri.controller.app.sharelinkregister;

import cn.newfeifan.mall.module.distri.controller.app.sharelinkregister.vo.AppShareLinkRegisterPageReqVO;
import cn.newfeifan.mall.module.distri.controller.app.sharelinkregister.vo.AppShareLinkRegisterRespVO;
import cn.newfeifan.mall.module.distri.controller.app.sharelinkregister.vo.AppShareLinkRegisterSaveReqVO;
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.*;
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.dal.dataobject.sharelinkregister.ShareLinkRegisterDO;
import cn.newfeifan.mall.module.distri.service.sharelinkregister.ShareLinkRegisterService;

@Tag(name = "用户 APP - 通过分享链接，新注册用户")
@RestController
@RequestMapping("/distri/share-link-register")
@Validated
public class AppShareLinkRegisterController {

    @Resource
    private ShareLinkRegisterService shareLinkRegisterService;

    @PostMapping("/create")
    @Operation(summary = "创建通过分享链接，新注册用户")
    @PreAuthorize("@ss.hasPermission('distri:share-link-register:create')")
    public CommonResult<Long> createShareLinkRegister(@Valid @RequestBody AppShareLinkRegisterSaveReqVO createReqVO) {
        return success(shareLinkRegisterService.createShareLinkRegister(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通过分享链接，新注册用户")
    @PreAuthorize("@ss.hasPermission('distri:share-link-register:update')")
    public CommonResult<Boolean> updateShareLinkRegister(@Valid @RequestBody AppShareLinkRegisterSaveReqVO updateReqVO) {
        shareLinkRegisterService.updateShareLinkRegister(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通过分享链接，新注册用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('distri:share-link-register:delete')")
    public CommonResult<Boolean> deleteShareLinkRegister(@RequestParam("id") Long id) {
        shareLinkRegisterService.deleteShareLinkRegister(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通过分享链接，新注册用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distri:share-link-register:query')")
    public CommonResult<AppShareLinkRegisterRespVO> getShareLinkRegister(@RequestParam("id") Long id) {
        ShareLinkRegisterDO shareLinkRegister = shareLinkRegisterService.getShareLinkRegister(id);
        return success(BeanUtils.toBean(shareLinkRegister, AppShareLinkRegisterRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得通过分享链接，新注册用户分页")
    @PreAuthorize("@ss.hasPermission('distri:share-link-register:query')")
    public CommonResult<PageResult<AppShareLinkRegisterRespVO>> getShareLinkRegisterPage(@Valid AppShareLinkRegisterPageReqVO pageReqVO) {
        PageResult<ShareLinkRegisterDO> pageResult = shareLinkRegisterService.getShareLinkRegisterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppShareLinkRegisterRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通过分享链接，新注册用户 Excel")
    @PreAuthorize("@ss.hasPermission('distri:share-link-register:export')")
    @OperateLog(type = EXPORT)
    public void exportShareLinkRegisterExcel(@Valid AppShareLinkRegisterPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShareLinkRegisterDO> list = shareLinkRegisterService.getShareLinkRegisterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "通过分享链接，新注册用户.xls", "数据", AppShareLinkRegisterRespVO.class,
                        BeanUtils.toBean(list, AppShareLinkRegisterRespVO.class));
    }

}