package com.beagle.informix.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * @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;
    @Resource
    private VccinfoMapper vccinfoMapper;
    @Override
    @Transactional
    public void doConnectJob() throws Exception{
        insertMethod();
//         updateMethod();
//        queryMethod();
//        delete();
    }
    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 =  vccinfoMapper.queryList(1);
        log.info("queryAll：{}", JSONUtil.toJsonStr(vccinfoList));
    }
    private void delete() {
        LambdaQueryWrapper<Vccinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Vccinfo::getVId, "ibatis");
        baseMapper.delete(queryWrapper);
    }

}
