package com.glxp.mipsdl.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.glxp.mipsdl.client.ClientProcessor;
import com.glxp.mipsdl.dao.basic.BasicBussinessTypeDao;
import com.glxp.mipsdl.entity.basic.BasicBussinessTypeEntity;
import com.glxp.mipsdl.entity.njxyy.BasicCorpEntity;
import com.glxp.mipsdl.req.base.UdiwmsQueryUdiInvProductsRequest;
import com.glxp.mipsdl.res.BaseResponse;
import com.glxp.mipsdl.thirddao.njxyy.TestDataMapper;
import com.glxp.mipsdl.util.ResultVOUtils;
//import com.sun.tools.javac.Main;
import com.glxp.mipsdl.util.TestDLL;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class TestController {


    @Resource
    TestDataMapper testDataMapper;
    @Resource
    BasicBussinessTypeDao bussinessTypeDao;


    @GetMapping("/test/nj")
    public BaseResponse filterInv() {
        for (int i = 0; i < 100000; i++) {
            List<BasicCorpEntity> data = testDataMapper.selectList(new QueryWrapper<BasicCorpEntity>().last("limit 10"));
            log.info(i+"");
        }
        return ResultVOUtils.success();
    }

    @GetMapping("/test/ct")
    public BaseResponse filterCt() {
        for (int i = 0; i < 100000; i++) {
            List<BasicBussinessTypeEntity> data = bussinessTypeDao.selectList(new QueryWrapper<BasicBussinessTypeEntity>());
            log.info(i+"");
        }
        return ResultVOUtils.success();
    }
    @PostMapping  ("/test/scyh")
    public BaseResponse scyh(@RequestBody Map map) {


       return ResultVOUtils.success(gmEcbEncryptKe((String) map.get("key"), (String) map.get("code")));
    }
    public String gmEcbEncryptKe(String key ,String code){
        try {
            Thread.sleep(10000);
            System.out.println("H0grHchTXHPKna8Va/k9zenxltDjrZhhr8rHAXYbXqp4virHfevi1Oafh377WlsU9bDn9R1HgpChs9kS6z92PkT3MvnwsmGhq1TPcbPP9k2w9fRj4EmMcDUUwsWPgu8TSEUdK9n6Z1IzYwf2RjlYHCYO5WgiPbyv80zuXZ4KrwuTlxnPRz3Xqr0wFldMsiTT".length());
//         key ="74F8DDC4212A8DFAAD4F23DFBF37BC1CAAB24D3CAD3BC7867972D2A1C5EB6D6B1C300237F18077D87175058D24027A7341FA466A1B96A95851278B67F4F80668";
//         code ="H51072700564";
//        ByteByReference  data = new ByteByReference(10);
            Pointer data = new Memory(128);

//        Pointer keyData = new Memory(key.length()+1);
//        keyData.write(0, key.getBytes(), 0, key.length());
//
//        Pointer codeData = new Memory(code.length()+1);
//        keyData.write(0, code.getBytes(), 0, code.length());
//
//        IntByReference intByReference = new IntByReference(key.length());
//        System.out.println("data"+data.getValue());
            int ss = TestDLL.instance.gm_ecb_encrypt_key(key,code,key.length(),data);
            System.out.println("asdas"+ss);
//        for (ByteByReference datum : data) {
//            try {
//                System.out.println("data"+datum.getValue());
//
//            }catch (Exception e){
//
//            }
//
//        }
            System.out.println(data.getString(0));
            String str =  data.getString(0);
            Native.free(Pointer.nativeValue(data));

            Pointer.nativeValue(data, 0);      //避免Memory对象被GC时重复执行Nativ.free()方法

//        Native.free(Pointer.nativeValue(keyData));
//        Pointer.nativeValue(keyData, 0);      //避免Memory对象被GC时重复执行Nativ.free()方法
//
//        Native.free(Pointer.nativeValue(codeData));
//        Pointer.nativeValue(codeData, 0);      //避免Memory对象被GC时重复执行Nativ.free()方法
//
//        Native.free(Pointer.nativeValue(intByReference.getPointer()));
//        Pointer.nativeValue(intByReference.getPointer(), 0);      //避免Memory对象被GC时重复执行Nativ.free()方法


            return str;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
