package com.beagle.informix.repository.impl;

import com.beagle.informix.entity.jpa.Vccinfo;
import com.beagle.informix.repository.VccinfoDao;

import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;
import java.util.List;

/**
 * @author lzz
 * date 2023/5/30
 * @version 1.0
 * description:
 */
@Repository
public class VccinfoDaoImpl implements VccinfoDao {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Vccinfo getVccInfoById(Integer vId) {
        return entityManager.find(Vccinfo.class, vId);
    }

    @Override
    public List<Vccinfo> getAllVccInfo() {
        String sql = "FROM Vccinfo as vInfo ORDER BY vInfo.effective";
        return (List<Vccinfo>) entityManager.createQuery(sql).getResultList();
    }

    @Override
    public void addVccinfo(Vccinfo vccinfo) {
        entityManager.persist(vccinfo);
//        entityManager.createNativeQuery("insert  into  cti_vccinfo_new (vccid ,vccname, effective, agentmax,ivrmax,updatekey,tclob,tblob)\n" +
//                        "        VALUES(?,?,?,?,?,?,?,?")
//                .setParameter(1, vccinfo.getVId())
//                .setParameter(2, vccinfo.getVName())
//                .setParameter(3, vccinfo.getEffective())
//                .setParameter(4, vccinfo.getAgentMax())
//                .setParameter(5, vccinfo.getIvrMax())
//                .setParameter(6, vccinfo.getUpdateKey())
//                .setParameter(7, vccinfo.getTClob())
//                .setParameter(8, vccinfo.getTBlob()).executeUpdate();

    }

    @Override
    @Transactional
    public void updateVccinfo(Vccinfo vccinfo) {
        // 提示异常 Caused by: java.sql.SQLException: Code-set conversion function failed due to illegal sequence or invalid value.
        /*Vccinfo old = entityManager.find(Vccinfo.class, "jpa");
        old.setVName("informix—name");
        entityManager.merge(old);*/

        // 第一步：准备好jpql
// ?1表示第一个参数，?2表示第二个参数
        String jpql = "update Vccinfo s set s.vName  = ?1 where s.vId = ?2";
        EntityTransaction transaction = null;

        try {
//            transaction = entityManager.getTransaction();
//            transaction.begin();

            // 第二步：设置jpql的参数
            Query query = entityManager.createQuery(jpql);
            query.setParameter(1, "赵六666999");
            query.setParameter(2, "%jpa%");

            // 第三步：执行jpql
            int rows = query.executeUpdate();
            System.out.println("影响的行数：" + rows);

            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
//            transaction.rollback();
        } finally {
            entityManager.close();
        }
    }

    @Override
    public void deleteVccinfo(Integer vId) {
        entityManager.remove(getVccInfoById(vId));
    }
}
