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

import cn.newfeifan.mall.framework.security.core.annotations.PreAuthenticated;
import cn.newfeifan.mall.module.distri.controller.app.sharelink.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
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.sharelink.ShareLinkDO;
import cn.newfeifan.mall.module.distri.service.sharelink.ShareLinkService;

@Tag(name = "用户 APP - 分享链接")
@RestController
@RequestMapping("/distri/share-link")
@Validated
public class AppShareLinkController {

    @Resource
    private ShareLinkService shareLinkService;

    @PostMapping("/create")
    @Operation(summary = "创建分享链接")
    @PreAuthenticated
    public CommonResult<Long> createShareLink(@Valid @RequestBody AppShareLinkSaveReqVO createReqVO) {
        return success(shareLinkService.createShareLink(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分享链接")
    @PreAuthenticated
    public CommonResult<Boolean> updateShareLink(@Valid @RequestBody AppShareLinkSaveReqVO updateReqVO) {
        shareLinkService.updateShareLink(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分享链接")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthenticated
    public CommonResult<Boolean> deleteShareLink(@RequestParam("id") Long id) {
        shareLinkService.deleteShareLink(id);
        return success(true);
    }

    @GetMapping("/getShareLink")
    @Operation(summary = "获得分享链接")
    @PreAuthenticated
    public CommonResult<AppShareLinkResultVO> getShareLink(AppShareLinkReqVO appShareLinkReqVO) {
        String id = shareLinkService.getShareLink(appShareLinkReqVO);
        return success(AppShareLinkResultVO.builder()
                .linkId(id).build()
        );
    }

    @GetMapping("/page")
    @Operation(summary = "获得分享链接分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppShareLinkRespVO>> getShareLinkPage(@Valid AppShareLinkPageReqVO pageReqVO) {
        PageResult<ShareLinkDO> pageResult = shareLinkService.getShareLinkPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppShareLinkRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分享链接 Excel")
    @OperateLog(type = EXPORT)
    public void exportShareLinkExcel(@Valid AppShareLinkPageReqVO pageReqVO,
                                     HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShareLinkDO> list = shareLinkService.getShareLinkPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分享链接.xls", "数据", AppShareLinkRespVO.class,
                BeanUtils.toBean(list, AppShareLinkRespVO.class));
    }

    @GetMapping("/getObjectIdByLinkId")
    @Operation(summary = "通过链接编号获取对应的类型编号")
    public CommonResult<Long> getObjectIdByLinkId(String linkId) {
        return success(shareLinkService.getObjectIdByLinkId(Long.parseLong(linkId,16)));
    }

}