package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMer;
import com.hfkj.entity.BsMerPlatformNo;
import com.hfkj.entity.BsMerWithdrawal;
import com.hfkj.model.ResponseData;
import com.hfkj.service.BsMerPlatformNoService;
import com.hfkj.service.BsMerService;
import com.hfkj.service.BsMerWithdrawalService;
import com.hfkj.sysenum.MerWithdrawalStatusEnum;
import com.hfkj.sysenum.PlatformTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @className: BsMerWithdrawalController
 * @author: HuRui
 * @date: 2023/6/8
 **/
@Controller
@Api(value = "商户结算信息")
@RequestMapping(value = "/merWithdrawal")
public class BsMerWithdrawalController {

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

    @Resource
    private BsMerWithdrawalService merWithdrawalService;
    @Resource
    private BsMerPlatformNoService merPlatformNoService;
    @Resource
    private BsMerService merService;

    @RequestMapping(value="/notify",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "提现通知")
    public ResponseData notify(@RequestParam(value = "cupNo" , required = false) String cupNo,
                               @RequestBody String reqBody) {
        try {
            log.info("提款通知：", reqBody);
            log.info("cupNo：", cupNo);
            System.out.println(reqBody);
            JSONObject respData = JSONObject.parseObject(reqBody);

            BsMerPlatformNo merPlatform = merPlatformNoService.getPlatformNoByCupNo(cupNo);
            // 查询提款
            BsMerWithdrawal merWithdrawal = merWithdrawalService.getDetailByDrawJnl(respData.getString("drawJnl"));
            if (merWithdrawal == null) {
                merWithdrawal = new BsMerWithdrawal();
                merWithdrawal.setPlatformType(PlatformTypeEnum.type1.getNumber());
                if (merPlatform != null) {
                    merWithdrawal.setMerId(merPlatform.getMerId());
                    merWithdrawal.setMerNo(merPlatform.getMerNo());
                    merWithdrawal.setPlatformNo(merPlatform.getPlatformNo());
                    merWithdrawal.setCupNo(merPlatform.getCupNo());
                    // 查询商户
                    BsMer mer = merService.getMer(merPlatform.getMerId());
                    if (mer != null) {
                        merWithdrawal.setMerName(mer.getMerName());
                    }
                }
            }
            merWithdrawal.setDrawJnl(respData.getString("drawJnl"));
            merWithdrawal.setDrawAmt(respData.getBigDecimal("drawAmt"));
            merWithdrawal.setDrawFee(respData.getBigDecimal("drawFee"));
            merWithdrawal.setDrawMode(respData.getString("drawMode").equals("D0")?1:2);
            merWithdrawal.setBatchAutoSettle(respData.getString("batchAutoSettle").equals("01")?1:2);
            merWithdrawal.setBatchNo(respData.getString("batchNo"));
            merWithdrawal.setAcctNo(respData.getString("acctNo"));
            merWithdrawal.setAcctName(respData.getString("acctName"));
            merWithdrawal.setSettleNo(respData.getString("settleNo"));
            merWithdrawal.setBankNo(respData.getString("bankNo"));
            merWithdrawal.setBankName(respData.getString("nbkName"));
            merWithdrawal.setCreateTime(DateUtil.format(respData.getString("createdTime"), "yyyy-MM-dd HH:mm:ss"));
            if (StringUtils.isNotBlank(respData.getString("completeTime"))) {
                merWithdrawal.setCompleteTime(DateUtil.format(respData.getString("completeTime"), "yyyy-MM-dd HH:mm:ss"));
            }

/*          DRAW.ACCEPTED 提款已受理
            DRAW.FREEZE 提款冻结
            DRAW.PROCESSING 提款处理中
            DRAW.SUCCESS 提款成功
            DRAW.FAILED 提款失败*/
            String drawState = respData.getString("drawState");
            if (drawState.equals("DRAW.ACCEPTED")) {
                merWithdrawal.setDrawStatus(MerWithdrawalStatusEnum.status1.getNumber());
            } else if (drawState.equals("DRAW.FREEZE")) {
                merWithdrawal.setDrawStatus(MerWithdrawalStatusEnum.status2.getNumber());
            } else if (drawState.equals("DRAW.PROCESSING")) {
                merWithdrawal.setDrawStatus(MerWithdrawalStatusEnum.status3.getNumber());
            } else if (drawState.equals("DRAW.SUCCESS")) {
                merWithdrawal.setDrawStatus(MerWithdrawalStatusEnum.status4.getNumber());
            } else if (drawState.equals("DRAW.FAILED")) {
                merWithdrawal.setDrawStatus(MerWithdrawalStatusEnum.status5.getNumber());
            }
            merWithdrawalService.editData(merWithdrawal);
            return ResponseMsgUtil.success(null);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getDetailByDrawJnl",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据提款流水号查询详情")
    public ResponseData getDetailByDrawJnl(@RequestParam(value = "drawJnl" , required = true) String drawJnl) {
        try {

            return ResponseMsgUtil.success(merWithdrawalService.getDetailByDrawJnl(drawJnl));

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
