package com.fs.app.controller;


import com.alibaba.fastjson.JSON;
import com.fs.common.core.controller.BaseController;
import com.fs.erp.service.IErpOrderService;
import com.fs.huifuPay.domain.HuiFuResult;
import com.fs.pay.pay.domain.OrderCallback;
import com.fs.pay.pay.domain.OrderResult;
import com.fs.pay.pay.dto.OrderQueryDTO;
import com.fs.pay.pay.service.PayService;
import com.fs.pay.pay.util.PayUtil;
import com.fs.store.domain.*;
import com.fs.store.service.*;
import com.fs.pay.service.dto.PayNotifyDTO;
import com.fs.pay.service.dto.RefundNotifyDTO;
import com.fs.pay.service.dto.TradeOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api("第三方支付接口")
@RestController
@RequestMapping("/app/pay")
public class PayController {

    protected final Logger logger = LoggerFactory.getLogger(PayController.class);

    @Autowired
    private IFsStoreOrderService orderService;

    @Autowired
    private IFsStorePaymentService storePaymentService;

    @Autowired
    private PayService ybPayService;


    @ApiOperation("易宝第三方支付回调")
    @PostMapping(value="/ybPayNotify")
    public String ybPayNotify(@RequestBody Map<String, String> params, @RequestBody OrderCallback o) {
        //验证
        String sign = PayUtil.sign(params);
        String sg = params.get("sign");
        if (!sg.equals(sign)){
            return "fail";
        }
        OrderQueryDTO orderQueryDTO = new OrderQueryDTO();
        orderQueryDTO.setLowOrderId(o.getLowOrderId());
        OrderResult orderResult = ybPayService.getOrder(orderQueryDTO);
        String[] order=o.getLowOrderId().split("-");
        switch (order[0]) {
            case "store":
                return orderService.payConfirm(1,null,order[1], o.getUpOrderId(),orderResult.getBankTrxId(),orderResult.getBankOrderId());
            case "store_remain":
                return orderService.payRemainConfirm( order[1], o.getUpOrderId(),orderResult.getBankTrxId(),orderResult.getBankOrderId());
            case "payment":
                return storePaymentService.payConfirm(order[1],o.getUpOrderId(),orderResult.getBankTrxId(),orderResult.getBankOrderId());
        }
        return "SUCCESS";
    }
    @ApiOperation("汇付第三方支付回调")
    @PostMapping(value="/hfPayNotify")
    public String hfPayNotify(@RequestParam String resp_desc, @RequestParam String resp_code, @RequestParam String sign, @RequestParam String resp_data) {
        //验证
        HuiFuResult o = JSON.parseObject(resp_data, HuiFuResult.class);
        logger.info("汇付支付回调:"+o);
        if(o.getResp_code().equals("00000000")){
            String[] order=o.getReq_seq_id().split("-");
            switch (order[0]) {
                case "store":
                    return orderService.payConfirm(1,null,order[1], o.getHf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
                case "store_remain":
                    return orderService.payRemainConfirm( order[1], o.getHf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
                case "payment":
                    return storePaymentService.payConfirm(order[1],o.getHf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
            }

        }
        return "ok";
    }


    @ApiOperation("小雨点第三方支付回调")
    @PostMapping(value="/payNotify")
    public String payNotify(@RequestBody PayNotifyDTO dto) {
            if("SUCCESS".equals(dto.getOrderStatus())) {
                //订单号
                String tradeNo = dto.getMerchOrderNo();
                List<TradeOrder> tradeOrders= JSON.parseArray(dto.getTradeOrders(),TradeOrder.class);
                if(tradeOrders!=null&&tradeOrders.size()>0){
                    for(TradeOrder tradeOrder:tradeOrders){
                        String[] order=tradeOrder.getSubMerchOrderNo().split("-");
                        switch (order[0]) {
                            case "store":
                                orderService.payConfirm(1,null,order[1], tradeNo,dto.getBankTransactionId(),dto.getBankSerialNo());
                                return "success";
                            case "store_remain":
                                orderService.payRemainConfirm( order[1], tradeNo,dto.getBankTransactionId(),dto.getBankSerialNo());
                                return "success";
                            case "payment":
                                return storePaymentService.payConfirm(order[1], tradeNo,dto.getBankTransactionId(),dto.getBankSerialNo());
                        }
                    }
                }
            }
            return "success";
    }
    @ApiOperation("小雨点第三方退款回调")
    @PostMapping(value="/refundNotify")
    public String refundNotify(@RequestBody RefundNotifyDTO dto) throws Exception {
        if("SUCCESS".equals(dto.getOrderStatus())) {
            String[] orderIds=dto.getOrigMerchOrderNo().split("-");
            FsStorePayment storePayment=null;
            switch (orderIds[0]) {
                case "store":
                    storePayment=storePaymentService.selectFsStorePaymentByCode(orderIds[1]);
                    if(storePayment!=null){
                        storePayment.setRefundMoney(BigDecimal.valueOf(Double.parseDouble(dto.getAmount())));
                        storePayment.setStatus(-1);
                        storePayment.setRefundTime(new Date());
                        storePaymentService.updateFsStorePayment(storePayment);
                    }
                    return "success";
                case "store_remain":
                    storePayment=storePaymentService.selectFsStorePaymentByCode(orderIds[1]);
                    if(storePayment!=null){
                        storePayment.setRefundMoney(BigDecimal.valueOf(Double.parseDouble(dto.getAmount())));
                        storePayment.setStatus(-1);
                        storePayment.setRefundTime(new Date());
                        storePaymentService.updateFsStorePayment(storePayment);
                    }
                    return "success";
                case "doctor":
                   return "success";
                case "payment":
                    storePaymentService.refund(orderIds[1]);
                    return "success";
            }
            return "success";
        }
        return "success";
    }

}
