package com.pj.task;

import com.alibaba.fastjson.JSON;
import com.pj.api.dto.CompanyDto;
import com.pj.api.dto.PaymentDto;
import com.pj.api.dto.PaymentTwoDto;
import com.pj.dto.DataDto;
import com.pj.rabbitmq.MessageSender;

import com.pj.utils.CryptoUtil;
import com.pj.xml.MessageXML;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class PaymentService {

    @Autowired
    private MessageSender messageSender;

    public static void main(String[] args) {
        String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\n" +
                "<message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"\" title=\"二级市场\">  \n" +
                "  <headers> \n" +
                "    <item key=\"sn\">7E97BB9B-4DDE-4D6B-B639-FC053368DD76</item>  \n" +
                "    <item key=\"date\">2024-08-26 15:15:18 285</item>  \n" +
                "    <item key=\"organization\">861501</item>  \n" +
                "    <item key=\"bankCode\">MRCCCNB1</item>  \n" +
                "    <item key=\"operation\">MKT003</item> \n" +
                "  </headers>  \n" +
                "  <plain><payload xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"responseDTO\" code=\"101\" source=\"d6d0ded2-eb2e-4c8d-bdf0-e04b43474057\" note=\"卖家账户认证通过\">\n" +
                "    <results xsi:type=\"vendorDTO\" name=\"李正红\" papersType=\"0001\" papersNo=\"532325199706101330\" nationality=\"0001\" accountNo=\"6223692312105747\" bankCode=\"MRCCCNB1\" tel=\"13658831034\"/>\n" +
                "  </payload></plain>\n" +
                "</message>\n";
        String headersStr = xmlStr.substring(xmlStr.indexOf("<headers>"), xmlStr.indexOf("</headers>")+10);
        System.out.println(headersStr);
        /*String a = CryptoUtil.decrypt("106940ae6c70fcdf19561fad1fb9bdca6e4bccacc84a7a474dd8316fdcfc2b060d7b8ea013bba87694e8c9425632e662497b189a117d441db59f602cb11f3726b50f646da8e2cbbd90a4427da1a63c079294584f8348ae744bea8c871faaf868b1a0bcbe861ef6a4656caada1349710c7a5cea48c90bac115ee62fbff5a7fa8d7d20c5c4e2833d3cf018d3d4aaeb5283f355e82078b42e9623b517f601bd30709e95a73c2c25ac30b142346620abf6b6525b5b0ba411725140d311346b0162fb9bb88d6721d3f4f74d4c22c28ae5e069d5");
        String b = CryptoUtil.decrypt("102efe99a286571fb3f88605063f5cdc8eafa83ea7e148db3ae1bb324a9e2e24a427ea1c51be27bcc8fc747f63cf01cd62887e16de38c135cd7eff69f760abab9f07f7565a963bd19de79d6bb9511385970020de2373df36927332a6efe4b2aed3dcf0dab450210ec0af36e85ada754eb157664e1a152a66725eeafcd60349afb0df99e8d828cf97bba1f7592f06c504f51bb5bb1b1ca2ac70d9d6199e1c9a5667a9a55988daa1f7b6c6b9bd09a4ab3c77ed1236424aba3df4b97d6ba41fed733d096de1fb286550ee7cb5c0fcf275a3a9aa16b0e99be93d7945a89e464bfba97b");
        String c = CryptoUtil.decrypt("107d621b69cad5beabb4aaffd465d16a29dea25e85b52a8c8eba4097d863d4ffe6768ec045afa6bc9ef65c315197f2b7fd427df5588ff34b5ef032353a9bf6e23e4be43b44343a2ea49215d95b7aca0cc4629be31a024b9f48a7debb7332fe54aca0fe3830e42c17026a46567307ae54e53595ce58b7b08526245373da86100af832947550467121f31f39943dc505576798f974d259fbe9f095d95389cf28d383bdbe7a26bee3bec8004f8c159ed3e888a5cd2f5fe83df9392c553b583d2a2ab1e210e1638aaebf5accba0f1b11670e713655f84bfb7c660f8908e053cc7a1b986954f181223d4ee8344201ea44ef1769fb1d4f809e100613e45dc57c312a9642");
        String d = CryptoUtil.decrypt("1078433bd7958c67b3e43a5964e98b93f23c0cbc2c34560afd74fb9fdceed2eb440bfeab935bd87661b0f11682ef191cdd5dbc8e6f6fc04f4ce9f17e6145ce4d1b8f48c9a82200912e43dc1ccbbc1c57b41cd38fb84419e4b4463ef677576814d6c6f9988a3c6645af3fb4232457bf6ff234e54fb344436293ec5fbe89e9493cc91efef7c8c1ecbab992b4e3ed263c137b24edee9d67165c056ec1952b7e6075dfc48d447cca08f7ddd56276a9973570456be48c10e5f8e4761b488fa7719b67270cb365608a3184d0405e5ddbca31bca0583e09fe41a4a26c477fc05a89f9d28d50848538f67e00a5b55d3455a1b0c9f74fa99d3dc78646c9ccc898923bdd3c95d9c1688d9b1b85c5864c729249d0a470c464a4c77bcd1ea59ebe12ffeba0860b240d0d0933a14b45e8f7ef583e4bf64fd0ef5f5ffaa1c89cc60e1d2ab4c85a510f355f062d3e7e7a0fd9ed2fd95a88213ac9e05e081ee16f73eb0d3e0f9beb6c");
        System.out.println(a+"\n");
        System.out.println(b+"\n");
        System.out.println(c+"\n");
        System.out.println(d);*/
    }
    public void testSend() {
        System.out.println("开始发送----------\n");
        //PaymentDto paymentDto = new PaymentDto();
        PaymentTwoDto paymentDto = new PaymentTwoDto();
        paymentDto.setFieldCode("861501");
        //paymentDto.setTradeNo("YYSC20240112171255");
        paymentDto.setCurrCode("RMB");
        paymentDto.setResalePrice(BigDecimal.ZERO);
        paymentDto.setTotalPrice(BigDecimal.valueOf(8000));
        paymentDto.setDeclTime("2024-01-17 10:11:55 876");
        paymentDto.setBorderName("李正红");
        paymentDto.setBorderidno("532325199706101330");
        paymentDto.setBankCode("6223692312105747");
        paymentDto.setBorderTel("13658831034");
        paymentDto.setOwnerName("云南君弛网络服务有限公司");
        paymentDto.setOwnerIdtype("0008");
        paymentDto.setBankAccount("0600055566570132");
        paymentDto.setOwnerTel("18117247531");
        //paymentDto.setShopSccd("78509372-8");
        //paymentDto.setNationality("0001");
        paymentDto.setBank("MRCCCNB1");

        paymentDto.setCodeTs("001");
        paymentDto.setGoodsName("大闸蟹");
        paymentDto.setGoodsUnit("吨");
        paymentDto.setDeclPrice(10000.0);
        paymentDto.setBuyQty(1.0);
        paymentDto.setGrossWt(0.8);
        paymentDto.setTotalPrice(BigDecimal.valueOf(0));

        paymentDto.setOrderNo("EJSCDD2408261611120098");
        paymentDto.setResalePrice(BigDecimal.valueOf(8450.05));
        paymentDto.setPcName("云南跨境贸易公司");
        paymentDto.setDutyParagraph("12312312-3");
        paymentDto.setPcBankAccount("0100028613124012");
        paymentDto.setPcContact("15195882701");

        List<CompanyDto> companyDtoList = new ArrayList<>();
        CompanyDto companyDto = new CompanyDto();
        companyDto.setName("云南君弛网络服务有限公司");
        companyDto.setUsci("78509372-8");
        companyDto.setBankAccount("0600055566570132");
        companyDto.setContact("18117247531");
        companyDto.setChargesPrice(BigDecimal.valueOf(150));
        companyDtoList.add(companyDto);
        paymentDto.setCompanyDtoList(companyDtoList);

        PaymentDto paymentDto1 = new PaymentDto();
        paymentDto1.setVoyageNo("111");//车辆批号
        paymentDto1.setVeNo("云A12345");//车牌号
        paymentDto1.setTradeNo("YYSC20240112171255");
        paymentDto1.setFieldCode("861501");
        //String xml = MessageXML.SXB023(paymentDto1);
        //String xml = MessageXML.SXB013(paymentDto);
        //String xml = MessageXML.MKT006_NoCommission(paymentDto);
        String xml = MessageXML.MKT007_NoCommission(paymentDto);
        //String xml = MessageXML.MKT004(paymentDto);
        /*String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\n" +
                "<message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"\" title=\"一级市场\">  \n" +
                "  <headers> \n" +
                "    <item> \n" +
                "      <key>sn</key>  \n" +
                "      <value>353447DB-86A3-4D32-9351-54B6D8211252</value> \n" +
                "    </item>  \n" +
                "    <item> \n" +
                "      <key>date</key>  \n" +
                "      <value>2024-01-10 14:34:26 582</value> \n" +
                "    </item>  \n" +
                "    <item> \n" +
                "      <key>organization</key>  \n" +
                "      <value>861501</value> \n" +
                "    </item>  \n" +
                "    <item> \n" +
                "      <key>operation</key>  \n" +
                "      <value>SXB002</value> \n" +
                "    </item>  \n" +
                "    <item> \n" +
                "      <key>bankCode</key>  \n" +
                "      <value>MRCCCNB1</value> \n" +
                "    </item> \n" +
                "  </headers>  \n" +
                "  <plain>10eb95c1c32131a58c6900c0c2f4dcd75327e09effa261c05db4a6b567943e69c6c01c7fc02bec3fa814bb8e70878b96b7e541dcd92bcb5cc51bef0775d65f756ce57f94083c40bbf3ee9424e77642448265a9f4929692c0f40c815ee78fd5e433f05be5af3a1f6f7b23103f246d1003f29343a4b461602b07828f6113ebff280d6ead8e8d67d0af03e3a6c77943111ef84128a3c6f9064f74508a23504cfdbc26a835f9ae505069432285f4031ea3251b2a480d9a629f5d31e2588753ed376b3afd6141371af75a7353b252546153819905d5d71462379a00a77195df6653134bb6a613cfbc23aa8747495df0da526be04adbc0c18fb932c1d02bed56213e8123</plain>\n" +
                "</message>";*/
        //messageSender.sendToLevelOneMQ(xml);
        messageSender.sendToLevelTwoMQ(xml);
        System.out.println("结束发送----------");
    }

    /**
     * ※ 结关报文
     * 报文名称：CXB001.xml  数据流向：海关数据中心--->银行
     * @param paymentDto 海关结关信息
     */
    public void sendCXB001(PaymentDto paymentDto) {
        log.info("send data to 银行:{}", JSON.toJSONString(paymentDto));
        String xml = MessageXML.CXB001(paymentDto);
        messageSender.sendToCustomsMQ(xml);
        log.info("send mq msg finish:{}", JSON.toJSONString(xml));
    }

    /**
     * ※ 订单扣款
     * 报文名称：SXB010.xml  数据流向：一级市场--->银行
     * @param paymentDto 一级市场订单信息
     */
    public void sendSXB010(PaymentDto paymentDto) {
        log.info("send data to 银行:{}", JSON.toJSONString(paymentDto));
        String xml = MessageXML.SXB010(paymentDto);
        messageSender.sendToLevelOneMQ(xml);
        //mq发送信息
        log.info("send mq msg finish:{}", JSON.toJSONString(xml));
    }

    /**
     * ※ 订单退款
     * 报文名称：SXB013.xml  数据流向：一级市场--->银行
     * @param paymentDto 一级市场订单信息
     */
    public void sendSXB013(PaymentDto paymentDto) {
        log.info("send data to 银行:{}", JSON.toJSONString(paymentDto));
        String xml = MessageXML.SXB013(paymentDto);
        messageSender.sendToLevelOneMQ(xml);
        log.info("send mq msg finish:{}", JSON.toJSONString(xml));
    }

    /**
     * ※ 通关车批信息同步(车出一级市场时)
     * 报文名称：SXB023.xml  数据流向：一级市场--->银行
     * @param paymentDto 一级市场订单信息
     */
    public void sendSXB023(PaymentDto paymentDto) {
        log.info("send data to 银行:{}", JSON.toJSONString(paymentDto));
        String xml = MessageXML.SXB023(paymentDto);
        messageSender.sendToLevelOneMQ(xml);
        log.info("send mq msg finish:{}", JSON.toJSONString(xml));
    }

    /**
     * ※ 订单扣款（无手续费）
     * 报文名称：SXB023.xml  数据流向：二级市场--->银行
     * @param paymentTwoDto 订单信息
     */
    public void sendMKT006_NoCommission(PaymentTwoDto paymentTwoDto) {
        log.info("send data to 银行:{}", JSON.toJSONString(paymentTwoDto));
        String xml = MessageXML.MKT006_NoCommission(paymentTwoDto);
        messageSender.sendToLevelTwoMQ(xml);
        log.info("send mq msg finish:{}", JSON.toJSONString(xml));
    }

    /**
     * ※ 订单扣款（+手续费）
     * 报文名称：SXB023.xml  数据流向：二级市场--->银行
     * @param paymentTwoDto 订单信息
     */
    public void sendMKT006(PaymentTwoDto paymentTwoDto) {
        log.info("send data to 银行:{}", JSON.toJSONString(paymentTwoDto));
        String xml = MessageXML.MKT006(paymentTwoDto);
        messageSender.sendToLevelTwoMQ(xml);
        log.info("send mq msg finish:{}", JSON.toJSONString(xml));
    }


}
