package com.bjtdba.merchant.controller;

import com.alibaba.fastjson.JSONObject;
import com.bjtdba.entity.merchant.Merchant;
import com.bjtdba.merchant.dao.HfUserOrderDao;
import com.bjtdba.merchant.dao.MerchantDao;
import com.bjtdba.merchant.dao.MerchantIntentionDao;
import com.bjtdba.merchant.service.MerchantMemberService;
import com.bjtdba.merchant.service.MerchantSettleInService;
import com.huifu.adapay.core.AdapayCore;
import com.huifu.adapay.core.util.AdapaySign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用与回调，gateWay不做拦截
 */
@RestController
@RequestMapping("huifuService")
public class HuifuController {

    @Autowired
    private MerchantIntentionDao merchantIntentionDao;

    @Autowired
    private HfUserOrderDao hfUserOrderDao;

    @Autowired
    private MerchantDao merchantDao;

    @Autowired
    private MerchantSettleInService merchantSettleInService;

    @Autowired
    private MerchantMemberService merchantMemberService;

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

    @PostMapping("/huifuWx")
    public void callbackWx(HttpServletRequest request) {
        try {
            //验签请参data
            String data = request.getParameter("data");
            //验签请参sign
            String sign = request.getParameter("sign");
            //验签标记
            boolean checkSign;
            //验签请参publicKey
            String publicKey = AdapayCore.PUBLIC_KEY;
            log.info("验签请参：data={}sign={}"+request.toString());
            //验签
            checkSign = AdapaySign.verifySign(data, sign, publicKey);
            log.info("微信回调1data"+data);
            log.info("微信回调checkSign"+checkSign);
            if(checkSign){
                //验签成功逻辑
                log.info("成功返回数据data:"+data);
                JSONObject jsonObject = JSONObject.parseObject(data);
                String member_id = (String) jsonObject.get("member_id");
                String audit_state = (String) jsonObject.get("audit_state");
                String order_no = (String) jsonObject.get("order_no");
                String settle_account_id = (String) jsonObject.get("id");
                if (settle_account_id == null || settle_account_id.isEmpty()) {
                    settle_account_id = (String) jsonObject.get("settle_account_id");
                }
                log.info("汇付返回+++++"+jsonObject.toJSONString());
                int uid =  hfUserOrderDao.selectByOrderNo(order_no);
                if(audit_state.equals("A")){  //audit_desc
                    //A-待审核
                    int status = 0;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("B")){
                    //B-审核失败
                    int status = 2;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("C")){
                    //C-开户失败
                    int status = 2;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("D")){
                    //D-开户成功但未创建结算账户
                    int status = 2;
                    //merchantDao.updateHuiByUid(null,member_id,status,uid);
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("E")){
                    //E-开户和创建结算账户成功
                    log.info("微信回调成功1");
                    //int status = 1;
                    merchantDao.updateHuiByUid(null,member_id,uid);
                    //merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                    /**
                     * 微信提交成功以后在提交支付宝。该期间不进行任何状态变化
                     * 1、使用用户uid 查询商户ID
                     */
                    List<Merchant> merchant= merchantDao.selectListByUId(uid);
                    merchantMemberService.insterMerchantMember(merchant.get(0).getMer_id(),2,member_id,settle_account_id);//保存商户支付id
                    if(merchant.size()==1){
                        //调用第二个微信的注册方法
                        merchantSettleInService.executeCreateHuiFuWx2(1,merchant.get(0).getMer_id());
                    }
                }
                log.info("member_id="+member_id+"audit_state="+audit_state);
            }else {
                log.info("微信回调失败");
            }
        }catch (Exception e){
            log.info("异步回调开始，参数，request={}");
        }
        return ;

    }
    //微信2-----------------------------------------------------------------------
    @PostMapping("/huifuWx2")
    public void callbackWx2(HttpServletRequest request) {
        try {
            //验签请参data
            String data = request.getParameter("data");
            //验签请参sign
            String sign = request.getParameter("sign");
            //验签标记
            boolean checkSign;
            //验签请参publicKey
            String publicKey = AdapayCore.PUBLIC_KEY;
            log.info("验签请参：data={}sign={}"+request.toString());
            //验签
            checkSign = AdapaySign.verifySign(data, sign, publicKey);
            log.info("微信回调2data"+data);
            log.info("微信回调checkSign"+checkSign);
            if(checkSign){
                //验签成功逻辑
                log.info("成功返回数据data:"+data);
                JSONObject jsonObject = JSONObject.parseObject(data);
                String member_id = (String) jsonObject.get("member_id");
                String audit_state = (String) jsonObject.get("audit_state");
                String order_no = (String) jsonObject.get("order_no");
                String settle_account_id = (String) jsonObject.get("id");
                if (settle_account_id == null || settle_account_id.isEmpty()) {
                    settle_account_id = (String) jsonObject.get("settle_account_id");
                }
                log.info("汇付返回+++++"+jsonObject.toJSONString());
                int uid =  hfUserOrderDao.selectByOrderNo(order_no);
                if(audit_state.equals("A")){  //audit_desc
                    //A-待审核
                    int status = 0;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("B")){
                    //B-审核失败
                    int status = 2;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("C")){
                    //C-开户失败
                    int status = 2;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("D")){
                    //D-开户成功但未创建结算账户
                    int status = 2;
                    //merchantDao.updateHuiByUid(null,member_id,status,uid);
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("E")){
                    //E-开户和创建结算账户成功
                    log.info("微信回调成功");
                    //int status = 1;
                    merchantDao.updateHuiByUid(null,member_id,uid);
                    //merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                    /**
                     * 微信提交成功以后在提交支付宝。该期间不进行任何状态变化
                     * 1、使用用户uid 查询商户ID
                     */
                    List<Merchant> merchant= merchantDao.selectListByUId(uid);
                    merchantMemberService.insterMerchantMember(merchant.get(0).getMer_id(),3,member_id,settle_account_id);//保存商户支付id
                    if(merchant.size()==1){
                        //这块调用阿里
                        merchantSettleInService.executeCreateHuiFuAli(1,merchant.get(0).getMer_id());
                    }
                }
                log.info("member_id="+member_id+"audit_state="+audit_state);
            }else {
                log.info("微信回调失败");
            }
        }catch (Exception e){
            log.info("异步回调开始，参数，request={}");
        }
        return ;

    }
    //---------------------------------------------------------------------------
    @PostMapping("/huifuAli")
    public void callbackAli(HttpServletRequest request) {
        try {
            //验签请参data
            String data = request.getParameter("data");
            //验签请参sign
            String sign = request.getParameter("sign");
            //验签标记
            boolean checkSign;
            //验签请参publicKey
            String publicKey = AdapayCore.PUBLIC_KEY;
            log.info("验签请参：data={}sign={}"+request.toString());
            //验签
            checkSign = AdapaySign.verifySign(data, sign, publicKey);
            log.info("阿里回调"+data);
            if(checkSign){
                //验签成功逻辑
                log.info("成功阿里返回数据data:"+data);
                JSONObject jsonObject = JSONObject.parseObject(data);
                String member_id = (String) jsonObject.get("member_id");
                String audit_state = (String) jsonObject.get("audit_state");
                String order_no = (String) jsonObject.get("order_no");
                String settle_account_id = (String) jsonObject.get("id");
                if (settle_account_id == null || settle_account_id.isEmpty()) {
                    settle_account_id = (String) jsonObject.get("settle_account_id");
                }
                log.info("汇付返回阿里+++++"+jsonObject.toJSONString());
                int uid =  hfUserOrderDao.selectByOrderNo(order_no);
                if(audit_state.equals("A")){  //audit_desc
                    //A-待审核
                    int status = 0;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("B")){
                    //B-审核失败
                    int status = 2;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("C")){
                    //C-开户失败
                    int status = 2;
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("D")){
                    //D-开户成功但未创建结算账户
                    int status = 2;
                    merchantDao.updateHuiByUid(member_id,null,uid);
                    merchantIntentionDao.updateResultByUid(jsonObject.get("audit_desc").toString(),status,uid);
                }else if(audit_state.equals("E")){
                    //E-开户和创建结算账户成功
                    log.info("开店成功");
                    int status = 1;
                    merchantDao.updateHuiByUid(member_id,null,uid);
                    List<Merchant> merchant= merchantDao.selectListByUId(uid);
                    merchantMemberService.insterMerchantMember(merchant.get(0).getMer_id(),1,member_id,settle_account_id);//保存商户支付id
                    merchantIntentionDao.updateResultByUid("开店成功",status,uid);
                }
                log.info("member_id="+member_id+"audit_state="+audit_state);
            }else {
                log.info("阿里回调失败");
            }
        }catch (Exception e){
            log.info("异步回调开始，参数，request={}");
        }
        return ;

    }
}
