package selling.sunshine.controller;

import com.alibaba.fastjson.JSONObject;
import com.pingplusplus.model.Event;
import com.pingplusplus.model.Webhooks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import selling.sunshine.model.Charge;
import selling.sunshine.model.CustomerOrder;
import selling.sunshine.model.CustomerOrderBill;
import selling.sunshine.model.OrderItemStatus;
import selling.sunshine.service.BillService;
import selling.sunshine.service.ChargeService;
import selling.sunshine.service.OrderService;
import selling.sunshine.service.ToolService;
import selling.sunshine.utils.ResponseCode;
import selling.sunshine.utils.ResultData;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sunshine on 8/3/16.
 */
@RestController
@RequestMapping("/reimburse")
public class ReimburseController {
    private Logger logger = LoggerFactory.getLogger(ReimburseController.class);

    @Autowired
    private ChargeService chargeService;

    @Autowired
    private BillService billService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private ToolService toolService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/inform")
    public ResultData inform(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultData result = new ResultData();
        JSONObject webhooks = toolService.getParams(request);
        JSONObject reimburse = webhooks.getJSONObject("data").getJSONObject("object");
        String dealId = reimburse.getString("order_no");
        if (StringUtils.isEmpty(dealId)) {
            result.setResponseCode(ResponseCode.RESPONSE_ERROR);
            return result;
        }
        Event event = Webhooks.eventParse(webhooks.toString());
        if ("refund.succeeded".equals(event.getType())) {
            response.setStatus(200);
        } else {
            response.setStatus(500);
        }
        if (dealId.startsWith("CUO")) {
            Map<String, Object> condition = new HashMap<>();
            condition.put("billId", dealId);
            ResultData fetchResponse = billService.fetchCustomerOrderBill(condition);
            if (fetchResponse.getResponseCode() == ResponseCode.RESPONSE_OK) {
                CustomerOrderBill bill = ((List<CustomerOrderBill>) fetchResponse.getData()).get(0);
                CustomerOrder order = bill.getCustomerOrder();
                if (order.getStatus() == OrderItemStatus.REFUNDING) {
                    order.setStatus(OrderItemStatus.REFUNDED);
                    orderService.updateCustomerOrder(order);
                }
            }
        }
        return result;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/apply")
    public ResultData reimburse(String orderId) {
        ResultData result = new ResultData();
        Map<String, Object> condition = new HashMap<>();
        condition.put("orderId", orderId);
        condition.put("status", 1);
        ResultData response = billService.fetchCustomerOrderBill(condition);
        if (response.getResponseCode() != ResponseCode.RESPONSE_OK) {
            result.setResponseCode(ResponseCode.RESPONSE_ERROR);
            result.setDescription(response.getDescription());
            return result;
        }
        CustomerOrderBill bill = ((List<CustomerOrderBill>) response.getData()).get(0);
        condition.clear();
        condition.put("orderNo", bill.getBillId());
        response = chargeService.fectchCharge(condition);
        if (response.getResponseCode() != ResponseCode.RESPONSE_OK) {
            result.setResponseCode(ResponseCode.RESPONSE_ERROR);
            result.setDescription(response.getDescription());
            return result;
        }
        Charge charge = ((List<Charge>) response.getData()).get(0);
        response = chargeService.reimburse(charge);
        if (response.getResponseCode() != ResponseCode.RESPONSE_OK) {
            result.setResponseCode(ResponseCode.RESPONSE_ERROR);
            result.setDescription(response.getDescription());
            return result;
        }
        condition.clear();
        condition.put("orderId", orderId);
        response = orderService.fetchCustomerOrder(condition);
        if (response.getResponseCode() == ResponseCode.RESPONSE_OK) {
            CustomerOrder current = ((List<CustomerOrder>) response.getData()).get(0);
            current.setStatus(OrderItemStatus.REFUNDING);
            orderService.updateCustomerOrder(current);
        }
        return result;
    }
}

@@ -148,16 +148,55 @@
 bhooks;%0A
+import org.apache.shiro.SecurityUtils;%0A
 import o
@@ -208,24 +208,24 @@
 f4j.Logger;%0A
-
 import org.s
@@ -635,338 +635,44 @@
 del.
-Charge;%0Aimport selling.sunshine.model.CustomerOrder;%0Aimport selling.sunshine.model.CustomerOrderBill;%0Aimport selling.sunshine.model.OrderItemStatus;%0Aimport selling.sunshine.service.BillService;%0Aimport selling.sunshine.service.ChargeService;%0Aimport selling.sunshine.service.OrderService;%0Aimport selling.sunshine.service.ToolService
+*;%0Aimport selling.sunshine.service.*
 ;%0Aim
@@ -1367,24 +1367,75 @@
 olService;%0A%0A
+    @Autowired%0A    private LogService logService;%0A%0A
     @Respons
@@ -3076,16 +3076,44 @@
 imburse(
+HttpServletRequest request, 
 String o
@@ -3160,32 +3160,381 @@
 w ResultData();%0A
+        User user = (User) SecurityUtils.getSubject();%0A        Admin admin = user.getAdmin();%0A        BackOperationLog backOperationLog = new BackOperationLog(%0A                admin.getUsername(), toolService.getIP(request), %22%E7%AE%A1%E7%90%86%E5%91%98%22 + admin.getUsername() + %22%E5%8F%91%E8%B5%B7%E5%AF%B9%E5%AE%A2%E6%88%B7%E8%AE%A2%E5%8D%95:%22 + orderId + %22%E9%80%80%E6%AC%BE%E5%A4%84%E7%90%86%22);%0A        logService.createbackOperationLog(backOperationLog);%0A%0A
         Map%3CStri

add operation log of reimburse