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.security.AESEncodeUtil;
import com.hfkj.entity.BsGasOrder;
import com.hfkj.entity.BsOrderRefund;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.service.order.BsOrderRefundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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;

/**
 * @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;

    @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) {
                    // 退款
                    orderRefundService.tradeOrderChildRefund(gasOrder.getChannelOrderNo(), false,1, data.getString("refundFailReason"));
                }
            }

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

}

