package cn.estsh.i3plus.core.apiservice.daoimpl;

import cn.estsh.i3plus.core.apiservice.dao.ITestDao;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * @Description : 性能测试
 * @Reference :
 * @Author : yunhao
 * @CreateDate : 2019-02-20 21:17
 * @Modify:
 **/
@Service
public class TestDaoImpl implements ITestDao {
    public static final Logger LOGGER = LoggerFactory.getLogger(UserPermissionDaoImpl.class);

    @Autowired
    private EntityManager entityManager;

    public static final String TEXT = "党的十八大提出，倡导富强、民主、文明、和谐，倡导自由、平等、公正、法治，倡导爱国、敬业、诚信、友善，积极培育和践行社会主义核心价值观。富强、民主、文明、和谐是国家层面的价值目标，自由、平等、公正、法治是社会层面的价值取向，爱国、敬业、诚信、友善是公民个人层面的价值准则，这24个字是社会主义核心价值观的基本内容。";

    @Override
    public void insertBatch(int num, int waitTime) {
        long t1 = System.currentTimeMillis();
        SysTool sysTool;
        StringBuffer sb = new StringBuffer();
        sb.append("insert into sys_tool (id,name,tool_type_name_rdd,tool_type_id,tool_ip,tool_port,tool_description) values ");
        SnowflakeIdMaker snf = new SnowflakeIdMaker();
        for (int i = 0; i < num * 100; i++) {
            sysTool = new SysTool();
            sysTool.setId(snf.nextId() + System.currentTimeMillis());
            sysTool.setName(RandomStringUtils.random(10, TEXT));
            sysTool.setToolTypeNameRdd(RandomStringUtils.random(10, TEXT));
            sysTool.setToolTypeId(System.currentTimeMillis());
            sysTool.setToolIp(RandomStringUtils.random(10, TEXT));
            sysTool.setToolPort((int) (Math.random() * i));
            sysTool.setToolDescription(RandomStringUtils.random(10, TEXT));
            sysTool.setCreateDatetime(TimeTool.getNowTime(true));
            sysTool.setModifyDatetime(TimeTool.getNowTime(true));

            sb.append("("+sysTool.getId()+",'"+sysTool.getName()+"','"+sysTool.getToolTypeNameRdd()+"',"+sysTool.getToolTypeId()+",'"
                    +sysTool.getToolIp()+"',"+sysTool.getToolPort()+",'"+sysTool.getToolDescription()+"'),");


        }
        sb.deleteCharAt(sb.length() - 1);
        Query sql = entityManager.createNativeQuery(sb.toString());
        LOGGER.info("插入耗时：" + (System.currentTimeMillis() - t1) + ",操作数：" + sql.executeUpdate());
    }

    @Override
    public List<Object> queryToolAndToolType(Pager pager) {
        String hql = "select t,tt from SysTool as t " +
                " left join SysToolType as tt on t.toolTypeId = tt.id " +
                " where 1=1";

        return entityManager.createQuery(hql).setFirstResult(pager.getStartRow())
                .setMaxResults(pager.getPageSize()).getResultList();
    }
}
