package cn.pluss.platform.ys.settle;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.MakeMoney;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.settle.SettleService;
import cn.pluss.platform.util.DateUtils;
import cn.pluss.platform.util.ParametersUtil;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.ys.YsConfig;
import cn.pluss.platform.ys.YsService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 银盛代付service
 */
@Service("ysSettleService")
@Slf4j
public class YsSettleServiceImpl implements SettleService {

    @Resource
    private YsService ysService;

    @Resource
    private YsConfig ysConfig;

    /**
     * 银盛提现接口
     * @param makeMoney
     * @return
     */
    @Override
    public MakeMoney withdraw(MakeMoney makeMoney) {

        MsgException.checkNull(makeMoney.getPlanAmount(),"应付提现金为空");

        MsgException.checkBlank(makeMoney.getBillId(),"提现单号为空");

        if(StringUtil.isEmpty(makeMoney.getSettDate()))makeMoney.setSettDate(DateUtils.formatDateDefault(new Date(),"yyyyMMdd"));

        JSONObject reqData = new JSONObject();
        reqData.put("merchantNo",makeMoney.getMerchantId());
        reqData.put("merchantOrderNo",makeMoney.getBillId());
        reqData.put("tradeAmount",makeMoney.getPlanAmount());
        reqData.put("tradeTime",DateUtils.formatDateDefault(new Date(),"yyyyMMddHHmmss"));
        reqData.put("shopDate",makeMoney.getSettDate());
        reqData.put("orderDesc","转账");
        reqData.put("backUrl", ParametersUtil.domain+"/wap/notify/ysCallBack");

        try {
            JSONObject respData = ysService.requestApiCommon(YsConfig.SETTLE_TRADE, reqData, YsService.TYPE_PAY);
            if(YsConfig.RESULT_SUCCESS_CODE.equals(respData.getString("status"))){
                makeMoney.setRemark(respData.getString("statusMeg"));
                makeMoney.setThirdStatus(respData.getString("status"));
                makeMoney.setState("1");
                makeMoney.setSettType("D0");
                makeMoney.setChannelNo(respData.getString("ysTradeNo"));
                makeMoney.setMercFee(respData.getBigDecimal("payeeFee").setScale(2).doubleValue());
            }else if(YsConfig.YS_RESULT_ACCEPT_CODE.equals(respData.getString("status"))){
                makeMoney.setRemark(respData.getString("statusMeg"));
                makeMoney.setThirdStatus(respData.getString("status"));
                makeMoney.setState("0");
                makeMoney.setSettType("D0");
                makeMoney.setChannelNo(respData.getString("ysTradeNo"));
                makeMoney.setMercFee(respData.getBigDecimal("payeeFee").setScale(2).doubleValue());
            }else{
                makeMoney.setFailReason(respData.getString("statusMeg"));
                makeMoney.setThirdStatus(respData.getString("status"));
                if(YsConfig.YS_RESULT_DFREVERSE_CODE.equals(respData.getString("status"))){
                    makeMoney.setState("2");
                }else{
                    makeMoney.setState("-1");
                }
            }
            return makeMoney;
        }catch (Exception e){
            e.printStackTrace();
            MsgException.throwException(e.getMessage());
        }
        return null;
    }

    /**
     * 提现结果查询
     * @param makeMoney
     * @return
     */
    @Override
    public JSONObject query(MakeMoney makeMoney) {

        MsgException.checkNull(makeMoney.getBillId(),"提现单号为空！");
        JSONObject reqData = new JSONObject();
        reqData.put("merchantNo","826440360108002");
        reqData.put("merchantOrderNo",makeMoney.getBillId());

        try {
            JSONObject respData = ysService.requestApiCommon(YsConfig.SETTLE_TRADE_QUERY, reqData, YsService.TYPE_PAY);

            String status = respData.getString("status");

            JSONObject data = new JSONObject();

            if(YsConfig.RESULT_SUCCESS_CODE.equals(status) || YsConfig.YS_RESULT_ACCEPT_CODE.equals(status)){
                if(YsConfig.RESULT_SUCCESS_CODE.equals(status)){
                    data.put("state","1");
                }else{
                    data.put("state","0");
                }
                data.put("thirdStatus",status);
                data.put("remark",respData.getString("statusMeg"));
                data.put("channelNo",respData.getString("ysTradeNo"));
                data.put("mercFee",respData.getBigDecimal("payeeFee").setScale(2));
            }else{
                if(YsConfig.YS_RESULT_DFREVERSE_CODE.equals(respData.getString("status"))){
                    data.put("state","2");
                }else{
                    data.put("state","-1");
                }
                data.put("thirdStatus",status);
                data.put("failReason",respData.getString("statusMeg"));
            }
            return ResultGenerator.genSuccessJsonResult("查询成功！",data);
        }catch (Exception e){
            e.printStackTrace();
            MsgException.throwException(e.getMessage());
        }
        return ResultGenerator.genFailJsonResult("查询失败！");
    }
}
