package com.cweb.controller.pay;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.security.AESEncodeUtil;
import com.hai.config.CommonSysConst;
import com.hai.config.WxOrderConfig;
import com.hai.dao.HighGasOrderRefundMapper;
import com.hai.entity.HighChildOrder;
import com.hai.entity.HighGasOrderRefund;
import com.hai.entity.HighOrder;
import com.hai.model.OrderRefundModel;
import com.hai.service.HighOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

@Controller
@RequestMapping(value = "/qianzhu")
@Api(value = "千猪回调")
public class QzOrderController {

    private static Logger log = LoggerFactory.getLogger(QzOrderController.class);

    @Resource
    private HighOrderService highOrderService;


    @RequestMapping(value = "/orderNotify", method = RequestMethod.POST)
    @ApiOperation(value = "千猪订单回调")
    public void orderNotify(@RequestBody String reqBodyStr,HttpServletRequest request, HttpServletResponse response) {
        try {
            log.info(reqBodyStr);
            JSONObject dataObject = JSONObject.parseObject(reqBodyStr, JSONObject.class);
            HighOrder order = highOrderService.getOrderByOrderNo(dataObject.getString("orderNo"));
            if (order != null) {
                JSONObject data = dataObject.getJSONObject("data");
                for (HighChildOrder childOrder : order.getHighChildOrderList()) {
                    // 4：KFC肯德基  5：电影票
                    if (childOrder.getGoodsType() == 4) {
                        // 10:已出票 -5:已取消 -3:部分取消 -10:退款
                        if (dataObject.getInteger("eventType") == -5) {
                            // 微信退款
                            OrderRefundModel orderRefundModel = WxOrderConfig.orderToRefund(order.getPaySerialNo(), data.getBigDecimal("cancelReason"), data.getBigDecimal("cancelReason"));
                            if(orderRefundModel.getResult_code().equals("SUCCESS")) {
                                childOrder.setChildOrdeStatus(5);
                                order.setOrderStatus(5);
                                order.setCancelTime(new Date());
                                order.setCancelRemarks(data.getString("cancelReason"));
                                highOrderService.updateOrderDetail(order);
                            }
                        }

                        if (dataObject.getInteger("eventType") == -10) {
                            // 微信退款
                            OrderRefundModel orderRefundModel = WxOrderConfig.orderToRefund(order.getPaySerialNo(), data.getBigDecimal("refundAmount"), data.getBigDecimal("refundAmount"));
                            if(orderRefundModel.getResult_code().equals("SUCCESS")) {
                                childOrder.setChildOrdeStatus(4);
                                order.setOrderStatus(4);
                                order.setRefundTime(new Date());
                                order.setRefundPrice(data.getBigDecimal("refundAmount"));
                                highOrderService.updateOrderDetail(order);
                            }
                        }
                    }

                    if (childOrder.getGoodsType() == 5) {

                        // 5：已支付(默认不发) 10:已出票 -5:已取消  15：交易成功
                        if (dataObject.getInteger("eventType") == -5) {
                            childOrder.setChildOrdeStatus(5);
                            order.setOrderStatus(5);
                            order.setCancelTime(new Date());
                            order.setCancelRemarks(data.getString("cancelReason"));
                        }

                        if (dataObject.getInteger("eventType") == 15) {
                            childOrder.setChildOrdeStatus(3);
                            order.setOrderStatus(3);
                            order.setFinishTime(new Date());
                        }
                    }
                }

                highOrderService.updateOrder(order);

                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=utf-8");
                PrintWriter writer= response.getWriter();
                writer.write("success");
            }
        } catch (Exception e) {
            log.error("WechatPayController --> wechatNotify() error!", e);
        }
    }

}
