package com.fs.app.controller.store;


import com.alibaba.fastjson.JSON;
import com.fs.common.config.FSSysConfig;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.redis.RedisCache;
import com.fs.company.service.ICompanyService;
import com.fs.company.service.ICompanyUserService;
import com.fs.erp.service.IErpOrderService;
import com.fs.hisStore.service.*;
import com.fs.huifuPay.domain.HuiFuResult;
import com.fs.huifuPay.sdk.opps.core.utils.HuiFuUtils;
import com.fs.live.service.ILiveOrderService;
import com.fs.ybPay.domain.OrderCallback;
import com.fs.ybPay.domain.OrderResult;
import com.fs.pay.pay.dto.OrderQueryDTO;
import com.fs.pay.pay.util.PayUtil;
import com.fs.pay.service.IPayService;
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.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

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

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

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IFsStoreOrderScrmService orderService;
    @Autowired
    private IFsStoreOrderStatusScrmService orderStatusService;
    @Autowired
    private IFsUserBillScrmService billService;
    @Autowired
    private IFsUserScrmService userService;
    @Autowired
    private ApplicationEventPublisher publisher;

    @Autowired
    private FSSysConfig sysConfig;
    @Autowired
    IErpOrderService erpOrderService;

    @Autowired
    private IFsStoreOrderItemScrmService storeOrderItemService;

    @Autowired
    private IFsStorePaymentScrmService storePaymentService;
    @Autowired
    private IFsExpressScrmService expressService;
    @Autowired
    private ILiveOrderService liveOrderService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    IPayService ybPayService;

    @Autowired
    private ICompanyUserService companyUserService;

    @ApiOperation("汇付第三方支付回调")
    @PostMapping(value="/hfPayNotify")
    public String hfPayNotify(@RequestParam String resp_desc, @RequestParam String resp_code, @RequestParam String sign, @RequestParam String resp_data) throws Exception {
        //验证
        HuiFuResult o = JSON.parseObject(resp_data, HuiFuResult.class);
        logger.info("汇付支付回调:"+o);
        if(o.getResp_code().equals("00000000") && o.getNotify_type().equals("1")){
            String[] order=o.getReq_seq_id().split("-");
            switch (order[0]) {
                case "store":
                    try {
                        HuiFuUtils.updateDivItem(order[1]);
                    } catch (Exception e) {
                        logger.error("-------分账明细回调错误{}", e.getMessage());
                    }
                    return orderService.payConfirm(1,null,order[1], o.getHf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
                case "store_remain":
                    try {
                        HuiFuUtils.updateDivItem(order[1]);
                    } catch (Exception e) {
                        logger.error("-------分账明细回调错误{}", e.getMessage());
                    }
                    return orderService.payRemainConfirm( order[1], o.getHf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
                case "payment":
                    try {
                        HuiFuUtils.updateDivItem(order[1]);
                    } catch (Exception e) {
                        logger.error("-------分账明细回调错误{}", e.getMessage());
                    }
                    return storePaymentService.payConfirm(order[1],o.getHf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
                case "live":
                    liveOrderService.payConfirm(1,null,order[1], o.getHf_seq_id(),o.getOut_trans_id(),o.getParty_order_id());
//                    try {
//                        HuiFuUtils.updateDivItem(orderId[1]);
//                    } catch (Exception e) {
//                        logger.error("-------分账明细回调错误{}", e.getMessage());
//                    }
                    break;
            }

        }
        return "ok";
    }

    @ApiOperation("易宝第三方支付回调")
    @PostMapping(value="/ybPayNotify")
    public String ybPayNotify(@RequestBody Map<String, String> params, @RequestBody OrderCallback o) throws Exception {
        //验证
        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";
    }



}
