
package com.tmerclub.cloud.biz.controller;

import cn.hutool.core.util.IdUtil;
import com.tmerclub.cloud.biz.config.WxConfig;
import com.tmerclub.cloud.biz.model.QrcodeTicket;
import com.tmerclub.cloud.biz.service.QrcodeTicketService;
import com.tmerclub.cloud.biz.vo.QrcodeTicketVO;
import com.tmerclub.cloud.common.constant.QRCodeType;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.Json;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author cl
 * @date 2021-08-17 16:00:13
 */

@RestController
@RequestMapping("/ua/qrcodeTicket")
@Tag(name = "二维码数据")
public class QrcodeTicketController {

    @Autowired
    private QrcodeTicketService qrcodeTicketService;
    @Autowired
    private WxConfig wxConfig;

    /**
     * 小程序二维码
     */
    @GetMapping("/smallQrcode")
    @Operation(summary = "小程序二维码获取", description = "获取小程序二维码，返回二维码图片流，小程序跳到二维码的页面之后，" +
            "需要根据获取的scene请求获取线上保存的content，为什么要这么麻烦，以为scene的内容有限，只能在数据库保存")
    @Parameters({
            @Parameter(name = "content", description = "要保存的内容", required = true),
            @Parameter(name = "type", description = "类型：1. 小程序团购商品 2.小程序分销商品二维码", required = true)
    })
    public ResponseEntity<FileSystemResource> save(String content, Integer type) throws WxErrorException {

        String page;
        if (Objects.equals(type, QRCodeType.GROUP.value()) || Objects.equals(type, QRCodeType.DISTRIBUTION.value())) {
            page = "pages/detail/detail";
        } else {
            // 无法获取页面信息
            throw new LuckException("无法获取页面信息");
        }
        Map<String, Object> map = new HashMap<>(16);
        map.put("type", type);
        map.put("content", content);
        String ticket = IdUtil.simpleUUID();
        QrcodeTicket qrcodeTicket = new QrcodeTicket();
        qrcodeTicket.setContent(Json.toJsonString(map));
        qrcodeTicket.setType(type);
        qrcodeTicket.setTicket(ticket);
        qrcodeTicket.setTicketUrl(page);
        qrcodeTicketService.save(qrcodeTicket);
        File file = wxConfig.getWxMaService().getQrcodeService().createWxaCodeUnlimit(ticket, page);
        return ResponseEntity.ok(new FileSystemResource(file));
    }


    /**
     * 根据Ticket获取content
     */
    @GetMapping("/getContent")
    @Operation(summary = "根据Ticket获取保存的内容", description = "小程序里的scene就是你要的Ticket咯")
    @Parameters(@Parameter(name = "ticket", description = "小程序里的scene就是你要的Ticket咯", required = true))
    public ServerResponseEntity<QrcodeTicketVO> getContent(String ticket) {

        QrcodeTicketVO qrcodeTicketVO = qrcodeTicketService.getByTicket(ticket);
        if (Objects.isNull(qrcodeTicketVO)) {
            // 二维码已过期
            throw new LuckException("二维码已过期");
        }
        if (qrcodeTicketVO.getExpireTime() != null && qrcodeTicketVO.getExpireTime().getTime() < System.currentTimeMillis()) {
            throw new LuckException("二维码已过期");
        }
        return ServerResponseEntity.success(qrcodeTicketVO);
    }

}
