package com.beagle.informix.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beagle.informix.entity.mybatis.Vccinfo;
import com.beagle.informix.mapper.VccinfoMapper;
import com.beagle.informix.service.InformixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author lzz
 * date 2023/5/29
 * @version 1.0
 * description:
 */
@Service("informixMybatisService")
@Slf4j
public class InformixMybatisServiceImpl extends ServiceImpl<VccinfoMapper, Vccinfo> implements InformixService {
    @Resource
    private ResourceLoader resourceLoader;
    @Override
    public void doConnectJob() throws Exception{
//        for (int i = 0; i < 100; i++) {
//            this.baseMapper.selectTest();
//        }
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 1; i++) {
            executorService.submit(()->{
                System.out.println(this.baseMapper.selectTest());
            });
        }
    }
    private void insertMethod() throws Exception{
        Vccinfo vccinfo = new Vccinfo();
        vccinfo.setVId(3);
        vccinfo.setEffective(1);
        vccinfo.setVName("mybatis");
        vccinfo.setIvrMax(1);
        vccinfo.setAgentMax(1);
        vccinfo.setUpdateKey("mybatis key");
        // 组装clob
        vccinfo.setTClob("测试clob数据！@#@Ddsfsdferewr");
        // 组装blob数据
        // 处理blob字段
        org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:EnableLoopback.exe");
        File inputStream = resource.getFile();
        try (FileInputStream fis = new FileInputStream(inputStream)){
            vccinfo.setTBlob(fis.readAllBytes());
        }
        // insert
        // 如果使用实体保存，则需要在blob字段上新增BlobTypeHandler转换
        // 如果使用xml的insert保存，也需要在属性上设置BlobTypeHandler转换
        baseMapper.insertData(vccinfo);
    }

    private void updateMethod() throws Exception {
        LambdaQueryWrapper<Vccinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Vccinfo::getVId, "ibatis");
        Vccinfo vccinfo = new Vccinfo();
        vccinfo.setVName("mybatis-update");
        baseMapper.update(vccinfo, queryWrapper);
    }

    private void queryMethod() {
        List<Vccinfo> vccinfoList =  this.baseMapper.queryList(1);
        log.info("queryAll：{}", JSONUtil.toJsonStr(vccinfoList));
    }
    private void delete() {
        LambdaQueryWrapper<Vccinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Vccinfo::getVId, "ibatis");
        baseMapper.delete(queryWrapper);
    }

}
