package com.hzya.frame.seeyon.cbs8.service.impl;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.seeyon.cbs8.dao.ITransactionDetailDao;
import com.hzya.frame.seeyon.cbs8.entity.TransactionDetailEntity;
import com.hzya.frame.seeyon.cbs8.service.IPaymentService;
import com.hzya.frame.seeyon.cbs8.service.ITransactionDetailService;
import com.hzya.frame.stringutil.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author xiangerlin
 * @Date 2024/6/24 11:08
 **/
@Service(value = "OATransactionDetailServiceImpl")
public class TransactionDetailServiceImpl extends BaseService<TransactionDetailEntity,String> implements ITransactionDetailService {

    private ITransactionDetailDao transactionDetailDao;

    @Autowired
    public void setTransactionDetailDao(ITransactionDetailDao dao) {
        this.transactionDetailDao = dao;
        this.dao=dao;
    }

    /**
     * 只返回交易流水号
     *
     * @param entity
     * @return
     */
    @DS("#entity.dataSourceCode")
    @Override
    public List<TransactionDetailEntity> querySerialNumber(TransactionDetailEntity entity) {
        List<TransactionDetailEntity> list = transactionDetailDao.queryList(entity, "TransactionDetailEntity_list_serialNumber");;
        return list;
    }

    /**
     * 保存交易明细，通过rest接口的方式
     *
     * @param entity
     * @throws Exception
     */
    @Override
    public void restSave(TransactionDetailEntity entity) {
        String field0001= StringUtil.nullConvert(entity.getAccountNo());//我方银行账号
        String field0002=StringUtil.nullConvert(entity.getAccountName());//我方户名
        String field0003=StringUtil.nullConvert(entity.getOpenBank());//我方开户行
        String field0004=StringUtil.nullConvert(entity.getBankType());//我方银行类型
        String field0005=StringUtil.nullConvert(entity.getTransactionSerialNumber());//交易流水号
        String field0006=StringUtil.nullConvert(entity.getBankTransactionDate());//交易日期
        String field0007=StringUtil.nullConvert(entity.getBankSerialNumber());//银行流水号
        String field0008=StringUtil.nullConvert(entity.getCurrency());//币种
        String field0009=StringUtil.nullConvert(entity.getIncurredAmount());//收款金额
        String field0010=StringUtil.nullConvert(entity.getPurpose());//用途
        String field0011=StringUtil.nullConvert(entity.getDigest());//摘要
        String field0012=StringUtil.nullConvert(entity.getOppositeAccount());//对方账号
        String field0013=StringUtil.nullConvert(entity.getOppositeName());//对方户名
        String field0014=StringUtil.nullConvert(entity.getOppositeOpeningBank());//对方开户行
        String field0015=StringUtil.nullConvert(entity.getRemark());//备注
        String data = StrUtil.format(getXmlTemplate(),field0001,field0002,field0003,field0004,field0005,field0006,field0007,field0008,field0009,field0010,field0011,field0012,field0013,field0014,field0015);
        Map<String, String> headerMap = MapBuilder.<String, String>create(true)
                .put("apiCode", "8000240006")
                .put("publicKey","ZJYAorA7JuRDfrVjywcx78BFcqlLwthgXNC65TXxxQMUHuxCe7eDIk+3zDUT+v578prj")
                .put("secretKey","a54vt9Wx7gdBig+4JCkZ/lISIIL2m4ZEyZkXtW0uQVBDHS+V4SVgT6xhNblacri/j3JzOP8MtA1LSGvL+2BWG8c/o7DKi92S4mr3zcGearA=")
                .put("appId","800024")
                .build();
        JSONObject paramsTemplate = new JSONObject();
        paramsTemplate.put("loginName", "hzya_rest");
        paramsTemplate.put("dataXml", data);
        String params = JSONObject.toJSONString(paramsTemplate);
        logger.info("保存交易明细到OA底表请求参数:{}",params);
        String body = HttpRequest.post("http://127.0.0.1:9999/kangarooDataCenterV3/entranceController/externalCallInterface").addHeaders(headerMap).body(params).timeout(60000).execute().body();
        logger.info("保存交易明细到OA底表响应参数:{}",body);
    }

    /**
     * 无流程表单模版
     *  transaction 交易明细
     *  payApply 支付申请日志
     * @return
     */
    private String getXmlTemplate(){
        return "<forms  version=\"2.1\"><formExport><summary  id=\"-6287716442449165745\" name=\"formmain_0233\"/><definitions><column  id=\"field0001\" type=\"0\" name=\"我方银行账户\" isNullable=\"false\" length=\"100\"/><column  id=\"field0002\" type=\"0\" name=\"我方户名\" isNullable=\"false\" length=\"100\"/><column  id=\"field0003\" type=\"0\" name=\"我方开户行\" isNullable=\"false\" length=\"100\"/><column  id=\"field0004\" type=\"0\" name=\"我方银行类型\" isNullable=\"false\" length=\"100\"/><column  id=\"field0005\" type=\"0\" name=\"交易流水号\" isNullable=\"false\" length=\"100\"/><column  id=\"field0006\" type=\"3\" name=\"交易日期\" isNullable=\"false\" length=\"255\"/><column  id=\"field0007\" type=\"0\" name=\"银行流水号\" isNullable=\"false\" length=\"100\"/><column  id=\"field0008\" type=\"0\" name=\"币种\" isNullable=\"false\" length=\"100\"/><column  id=\"field0009\" type=\"4\" name=\"收款金额\" isNullable=\"false\" length=\"20\"/><column  id=\"field0010\" type=\"0\" name=\"用途\" isNullable=\"false\" length=\"800\"/><column  id=\"field0011\" type=\"0\" name=\"摘要\" isNullable=\"false\" length=\"256\"/><column  id=\"field0012\" type=\"0\" name=\"对方账号\" isNullable=\"false\" length=\"100\"/><column  id=\"field0013\" type=\"0\" name=\"对方户名\" isNullable=\"false\" length=\"100\"/><column  id=\"field0014\" type=\"0\" name=\"对方开户行\" isNullable=\"false\" length=\"100\"/><column  id=\"field0015\" type=\"0\" name=\"备注\" isNullable=\"false\" length=\"800\"/></definitions><values><column  name=\"我方银行账户\"><value><![CDATA[{}]]></value></column><column  name=\"我方户名\"><value><![CDATA[{}]]></value></column><column  name=\"我方开户行\"><value><![CDATA[{}]]></value></column><column  name=\"我方银行类型\"><value><![CDATA[{}]]></value></column><column  name=\"交易流水号\"><value><![CDATA[{}]]></value></column><column  name=\"交易日期\"><value><![CDATA[{}]]></value></column><column  name=\"银行流水号\"><value><![CDATA[{}]]></value></column><column  name=\"币种\"><value><![CDATA[{}]]></value></column><column  name=\"收款金额\"><value><![CDATA[{}]]></value></column><column  name=\"用途\"><value><![CDATA[{}]]></value></column><column  name=\"摘要\"><value><![CDATA[{}]]></value></column><column  name=\"对方账号\"><value><![CDATA[{}]]></value></column><column  name=\"对方户名\"><value><![CDATA[{}]]></value></column><column  name=\"对方开户行\"><value><![CDATA[{}]]></value></column><column  name=\"备注\"><value><![CDATA[{}]]></value></column></values><subForms/></formExport></forms>";
    }
}
