package com.caimei.www.controller.authorized.document;

import com.caimei.www.pojo.JsonModel;
import com.caimei.www.pojo.document.OssAuthorization;
import com.caimei.www.service.page.DocumentAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * Description
 *
 * @author : plf
 * @date : 2020/11/10
 */
@RestController
@RequestMapping("/document")
public class DocumentAuthController {

    private DocumentAuthService documentAuthService;

    @Autowired
    public void setDocumentAuthService(DocumentAuthService documentAuthService) {
        this.documentAuthService = documentAuthService;
    }

    /**
     * 获取图形验证码
     */
    @GetMapping("/getImgVerifyCode")
    public Mono<JsonModel> getImgVerifyCode() {
        return documentAuthService.getImgVerifyCode();
    }

    /**
     * oss短信登录,发送短信验证
     */
    @GetMapping("/note")
    public Mono<JsonModel> ossNote(String mobile, String imgCode, String token) {
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(imgCode) || StringUtils.isEmpty(token)) {
            return Mono.just(JsonModel.error("参数异常"));
        }
        return documentAuthService.ossNote(mobile, imgCode, token);
    }

    /**
     * oss手机短信验证码登录
     */
    @PostMapping("/mobileCodeLogin")
    public ResponseEntity<JsonModel> mobileCodeLogin(OssAuthorization authorization) {
        return documentAuthService.mobileCodeLogin(authorization);
    }

    /**
     * 资料列表
     */
    @GetMapping("/dataList")
    public Mono<JsonModel> dataList(@CookieValue(value = "authorizationMobile", required = false) String mobile, String name, Integer pageNum, Integer pageSize) {
        Boolean isLogin = documentAuthService.getAuthorizationCookie(mobile);
        if (!isLogin) {
            return Mono.just(JsonModel.error(-99, "请登录"));
        }
        return documentAuthService.dataList(name, pageNum, pageSize);
    }

    /**
     * 资料详情
     */
    @GetMapping("/dataDetails")
    public Mono<JsonModel> dataDetails(@CookieValue(value = "authorizationMobile", required = false) String mobile, Integer pdfId) {
        Boolean isLogin = documentAuthService.getAuthorizationCookie(mobile);
        if (!isLogin) {
            return Mono.just(JsonModel.error(-99, "请登录"));
        }
        if (pdfId == null) {
            return Mono.just(JsonModel.error("参数异常"));
        }
        return documentAuthService.dataDetails(pdfId);
    }

    /**
     * 更多资料
     */
    @GetMapping("/moreData")
    public Mono<JsonModel> moreData(@CookieValue(value = "authorizationMobile", required = false) String mobile, Integer archiveId) {
        Boolean isLogin = documentAuthService.getAuthorizationCookie(mobile);
        if (!isLogin) {
            return Mono.just(JsonModel.error(-99, "请登录"));
        }
        if (archiveId == null) {
            return Mono.just(JsonModel.error("参数异常"));
        }
        return documentAuthService.moreData(archiveId);
    }

}
