package com.bweb.controller.notify;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.gas.newlink.NewLinkGasService;
import com.hfkj.channel.gas.newlink.NewLinkRequestService;
import com.hfkj.common.exception.BaseException;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.pay.util.SignatureUtil;
import com.hfkj.common.security.AESEncodeUtil;
import com.hfkj.common.utils.HttpsUtils;
import com.hfkj.entity.BsAgentApiParam;
import com.hfkj.entity.BsGasOrder;
import com.hfkj.entity.BsOrderRefund;
import com.hfkj.openapi.model.request.RequestOrderRefundModel;
import com.hfkj.openapi.model.response.OrderRefundNotifyModel;
import com.hfkj.openapi.service.ApiGasOrderService;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.service.order.BsOrderRefundService;
import com.hfkj.sysenum.gas.GasOrderCreateType;
import com.hfkj.sysenum.gas.OrderOilStatus;
import com.hfkj.sysenum.order.OrderRefundStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @className: NewLinkNotify
 * @author: HuRui
 * @date: 2024/7/26
 **/
@Controller
@RequestMapping(value = "/newLinkNotify")
@Api(value = "加油订单业务")
public class NewLinkNotify {

    @Resource
    private NewLinkGasService newLinkGasService;
    @Resource
    private BsOrderRefundService orderRefundService;
    @Resource
    private BsGasOrderService gasOrderService;
    @Resource
    private ApiGasOrderService apiGasOrderService;

    @RequestMapping(value="/merChange",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "商户变更")
    public String merChange(@RequestBody JSONObject body) {
        try {
            System.out.println("================团油商户变更通知==================");
            System.out.println(body);
            String bodyData = body.getString("data");
            // 解密参数
            JSONObject data = JSONObject.parseObject(AESEncodeUtil.aesDecryptByBytes(AESEncodeUtil.base64Decode(bodyData), NewLinkRequestService.APP_SECRET));
            if ("001".equals(data.getString("type"))) {
                // 油站更新
                // newLinkGasService.merChange(data.getString("value"));
            }
        } catch (Exception e) {
            System.out.println("变更异常：" + e.getMessage());
        } finally {
            System.out.println("================团油商户变更通知==================");
        }
        return "success";
    }


    @RequestMapping(value="/orderRefund",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "订单退款")
    public String orderRefund(@RequestBody JSONObject body) {
        try {
            System.out.println("================团油交易退款通知==================");
            System.out.println(body);
            String bodyData = body.getString("data");
            // 解密参数
            JSONObject data = JSONObject.parseObject(AESEncodeUtil.aesDecryptByBytes(AESEncodeUtil.base64Decode(bodyData), NewLinkRequestService.APP_SECRET));
/*            if (data.getBoolean("refundResult").equals(true)) {
                // 查询加油订单
                BsGasOrder gasOrder = gasOrderService.getDetailByOrderNo(data.getString("thirdOrderNo"));
                if (gasOrder != null) {
                    if (gasOrder.getCreateType().equals(GasOrderCreateType.TYPE1.getNumber())) {
                        // 退款
                        orderRefundService.refundMoney(orderRefundService.getRefundByChildOrderNo(gasOrder.getOrderChildNo()));
                        
                    } else if (gasOrder.getCreateType().equals(GasOrderCreateType.TYPE2.getNumber())) {
                        // 退款
                        RequestOrderRefundModel requestOrderRefund = new RequestOrderRefundModel();
                        requestOrderRefund.setOrderNo(gasOrder.getOrderNo());
                        requestOrderRefund.setRefundReason(data.getString("refundFailReason"));
                        apiGasOrderService.refundOrder(gasOrder.getAgentId(), requestOrderRefund);

                    }
                }
            }*/

        } catch (Exception e) {
            System.out.println("变更异常：" + e.getMessage());
        } finally {
            System.out.println("================团油交易退款通知==================");
        }
        return "success";
    }

}

