package com.ruoyi.xq.controller.app;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.xq.domain.AuthOrder;
import com.ruoyi.xq.dto.app.pay.OrderCreateVo;
import com.ruoyi.xq.dto.common.faceauth.CommitAuthRecordResp;
import com.ruoyi.xq.enums.common.SystemConfigEnum;
import com.ruoyi.xq.manager.SystemConfigManager;
import com.ruoyi.xq.service.AuthOrderService;
import com.ruoyi.xq.service.CardAuthRecordService;
import com.ruoyi.xq.tencent.CreateAuthResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
@Tag(name = "实名认证订单相关")
public class AuthOrderAppController {

    @Autowired
    private AuthOrderService authOrderService;
    @Autowired
    private SystemConfigManager systemCOnfigManager;
    @Autowired
    private CardAuthRecordService cardAuthRecordService;

    @PostMapping("/order/create")
    @Operation(summary = "生成实名认证订单")
    @Log(title = "生成实名认证订单", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<OrderCreateVo> createAuthOrder(){
        Long userId = LoginHelper.getUserId();
        AuthOrder authOrder = authOrderService.createVipOrder(userId);
        OrderCreateVo result = new OrderCreateVo();
        result.setPrice(authOrder.getPrice());
        result.setOrderNo(authOrder.getOrderNo());
        result.setOrderName(authOrder.getOrderName());
        return R.ok(result);
    }


    @PostMapping("/price")
    @Operation(summary = "获取实名认证价格")
    public R<Map<String,Object>> getAuthOrderPrice(){
        BigDecimal authCardPrice = systemCOnfigManager.getSystemConfigOfBigDecimal(SystemConfigEnum.AUTH_CARD_PRICE);
        Map<String,Object> map = new HashedMap<>();
        map.put("price",authCardPrice);
        return R.ok(map);
    }

    /**
     * 实名核身鉴权
     *
     * @return
     */
    @Operation(summary = "实名核身鉴权")
    @GetMapping(value = "/detectAuth")
    @Log(title = "实名核身鉴权", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<CreateAuthResp> detectAuth(String orderNo,String redirectUrl) {
        CreateAuthResp authRecord = cardAuthRecordService.createAuthRecord(orderNo, redirectUrl);
        return R.ok(authRecord);
    }

    /**
     * 获取实名核身结果信息
     *
     * @param bizToken
     * @return
     */
    @Operation(summary = "获取实名核身结果信息")
    @GetMapping(value = "/getDetectInfo")
    @Log(title = "获取实名核身结果信息", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<CommitAuthRecordResp> getDetectInfo(@RequestParam String bizToken) {
        CommitAuthRecordResp commitAuthRecordResp = cardAuthRecordService.commitAuthRecord(bizToken);
        return R.ok(commitAuthRecordResp);
    }

}
