package shop.alien.store.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreAliPayLog;
import shop.alien.store.service.AliService;
import shop.alien.store.util.ali.AliApi;
import shop.alien.store.util.ali.AliSms;
import shop.alien.util.ali.AliOSSUtil;
import shop.alien.util.common.AlipayTradeAppPay;
import shop.alien.util.common.AlipayTradeRefund;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author ssk
 * @version 1.0
 * @date 2024/12/11 17:34
 */
@Slf4j
@Api(tags = {"二期-阿里接口"})
@ApiSort(9)
@CrossOrigin
@RestController
@RequestMapping("/ali")
@RequiredArgsConstructor
public class AliController {

    private final AliApi aliPayConfig;

    private final AliSms aliSmsConfig;

    private final AliService aliService;

    private final AlipayTradeRefund alipayTradeRefund;

    private final AliOSSUtil aliOSSUtil;
    private final AliApi aliApi;

    @ApiOperation("阿里回调")
    @ApiOperationSupport(order = 1)
    @GetMapping("/notify")
    public void notify(HttpServletRequest request, HttpServletResponse response) {
        log.info("AliController.notify");
        aliService.notify(request);
    }

    @ApiOperation("身份证二要素核验")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "idCard", value = "身份证号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/getIdInfo")
    public R getIdInfo(String name, String idCard) {
        log.info("AliController.getIdInfo?name={}&idCard={}", name, idCard);
        if (aliPayConfig.getIdInfo(name, idCard)) {
            return R.success("身份验证成功");
        }
        return R.fail("身份验证失败");
    }

    @ApiOperation("单笔转账接口")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "收款人姓名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "idCard", value = "收款人身份证号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "phone", value = "收款人手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "money", value = "收款金额", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/pay")
    public R pay(String name, String idCard, String phone, String money) {
        log.info("AliController.pay?name={}&idCard={}&phone={}&money={}", name, idCard, phone, money);
        StoreAliPayLog pay = aliPayConfig.pay(name, idCard, phone, money);
        if (null != pay) {
            return R.data(pay);
        }
        return R.fail("转账失败");
    }

    @ApiOperation("发送短信")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/sendSms")
    public R sendSms(String phone) {
        Integer code = aliSmsConfig.sendSms(phone);
        log.info("AliController.sendSms?phone={}&code={}", phone, code);
        if (code != null) {
            return R.data(code);
        }
        return R.fail("短信发送失败");
    }

    @ApiOperation("银行卡核验")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "idCard", value = "身份证号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "bankCard", value = "银行卡号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/validateBankCardInfo")
    public R validateBankCardInfo(String name, String idCard, String bankCard, String phone) {
        log.info("AliController.getBankCardInfo?name={}&idCard={}&bankCard={}&phone={}", name, idCard, bankCard, phone);
        if (aliPayConfig.validateBankCardInfo(name, idCard, bankCard, phone)) {
            return R.success("银行卡信息验证成功");
        }
        return R.fail("银行卡信息验证失败");
    }

    @ApiOperation("H5人脸核身初始化")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "idCard", value = "身份证号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/h5FaceInit")
    public R h5FaceInit(String name, String idCard) {
        log.info("AliController.h5FaceInit?name={}&idCard={}", name, idCard);
        JSONObject jsonObject = aliPayConfig.h5FaceInit(name, idCard);
        if (null != jsonObject) {
            return R.data(jsonObject);
        }
        return R.fail("H5人脸核身初始化失败");
    }

    @ApiOperation("H5人脸核身结果查询")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({@ApiImplicitParam(name = "certifyId", value = "认证单据号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/h5FaceResult")
    public R h5FaceResult(String certifyId) {
        log.info("AliController.h5FaceResult?certifyId={}", certifyId);
        JSONObject jsonObject = aliPayConfig.h5FaceResult(certifyId);
        if (null != jsonObject) {
            return R.data(jsonObject);
        }
        return R.fail("H5人脸核身初始化失败");
    }

    @ApiOperation("跳转支付宝人脸核身初始化")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "idCard", value = "身份证号", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/jumpToAliPayInit")
    public R jumpToAliPayInit(String name, String idCard) {
        log.info("AliController.jumpToAliPayInit?name={}&idCard={}", name, idCard);
        JSONObject jsonObject = aliPayConfig.jumpToAliPayInit(name, idCard);
        if (null != jsonObject) {
            return R.data(jsonObject);
        }
        return R.fail("跳转支付宝人脸核身初始化失败");
    }

    @ApiOperation("跳转支付宝人脸核身开始认证")
    @ApiOperationSupport(order = 9)
    @ApiImplicitParams({@ApiImplicitParam(name = "certifyId", value = "认证ID", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/jumpToAliPay")
    public R jumpToAliPay(String certifyId) {
        log.info("AliController.jumpToAliPay?certifyId={}", certifyId);
        JSONObject jsonObject = aliPayConfig.jumpToAliPay(certifyId);
        if (null != jsonObject) {
            return R.data(jsonObject);
        }
        return R.fail("跳转支付宝人脸核身开始认证失败");
    }

    @ApiOperation("跳转支付宝人脸核身查询记录")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({@ApiImplicitParam(name = "certifyId", value = "认证ID", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/jumpToAliPayResult")
    public R jumpToAliPayResult(String certifyId) {
        log.info("AliController.jumpToAliPayResult?certifyId={}", certifyId);
        JSONObject jsonObject = aliPayConfig.jumpToAliPayResult(certifyId);
        if (null != jsonObject) {
            return R.data(jsonObject);
        }
        return R.fail("跳转支付宝人脸核身查询记录失败");
    }

    @GetMapping("/userPay")
    public String userPay() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return AlipayTradeAppPay.executeAlipayPayment(dateFormat.format(new Date()), "0.01", "测试支付");
    }

    @GetMapping("/returnPay")
    public void returnPay(String orderNo, String refundAmount) {
        alipayTradeRefund.processRefund(orderNo, refundAmount, "退款");
    }

    @ApiOperation("跳转支付宝人脸核身查询记录")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "File", paramType = "query", required = true),
            @ApiImplicitParam(name = "ossFilePath", value = "oss中的文件路径", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/uploadFile")
    public R<String> uploadFile(@RequestParam(value = "file") MultipartFile multipartFile, @RequestParam(value = "ossFilePath") String ossFilePath) {
        return R.data(aliOSSUtil.uploadFile(multipartFile, ossFilePath));
    }

    @ApiOperation("支付")
    @ApiOperationSupport(order = 12)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "price", value = "金额", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "subject", value = "类目", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/zhifu")
    public R<JSONObject> zhifu(@RequestParam(value = "price") String price, @RequestParam(value = "subject") String subject) {
        return R.data(aliApi.promotionPackagePay(price, subject));
    }

}
