package cn.estsh.i3plus.core.apiservice.serviceimpl.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ISysToolService;
import cn.estsh.i3plus.core.apiservice.dao.ITestDao;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
import cn.estsh.i3plus.pojo.platform.bean.SysTool;
import cn.estsh.i3plus.pojo.platform.bean.SysToolType;
import cn.estsh.i3plus.pojo.platform.repository.SysToolRepository;
import cn.estsh.i3plus.pojo.platform.repository.SysToolTypeRepository;
import cn.estsh.i3plus.pojo.platform.sqlpack.CoreHqlPack;
import cn.estsh.impp.framework.boot.util.ValidatorBean;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @Description : 硬件服务接口实现
 * @Reference :
 * @Author : yunhao
 * @Date : 2018-10-25 20:29
 * @Modify :
 **/
@Service
public class SysToolService implements ISysToolService {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysToolService.class);

    @Autowired
    private SysToolRepository toolRDao;

    @Autowired
    private SysToolTypeRepository toolTypeRDao;

    @Autowired
    private ITestDao testDao;

    @Override
    @ApiOperation(value = "新增硬件信息")
    public SysTool insertSysTool(SysTool tool) {
        // 查询硬件类型名称
        //todo 压测关闭校验提高处理速度
//        LOGGER.info("硬件类型 TOOL_TYPE :{}", tool);
//        SysToolType toolType = toolTypeRDao.getById(tool.getToolTypeId());
//        ValidatorBean.checkNotNull(toolType);
//
//        tool.setToolTypeNameRdd(toolType.getName());
//
//        LOGGER.info("硬件 TOOL tool:{}",tool);
        return toolRDao.insert(tool);
    }

    @Override
    @ApiOperation(value = "删除硬件信息",notes = "根据 ID 删除硬件信息")
    public int deleteSysToolById(Long id) {
        LOGGER.info("硬件 TOOL id:{}",id);
       return toolRDao.deleteByProperty("id",id);
    }

    @Override
    @ApiOperation(value = "更新硬件信息")
    public void updateSysTool(SysTool tool) {
        // 查询硬件类型名称
        LOGGER.info("硬件类型 TOOL_TYPE :{}", tool);
        SysToolType toolType = toolTypeRDao.getById(tool.getToolTypeId());
        ValidatorBean.checkNotNull(toolType);

        SysTool newSysTool = toolRDao.getById(tool.getId());
        newSysTool.setName(tool.getName());
        newSysTool.setToolConnType(tool.getToolConnType());
        newSysTool.setToolDataType(tool.getToolDataType());
        newSysTool.setToolIp(tool.getToolIp());
        newSysTool.setToolPort(tool.getToolPort());
        newSysTool.setToolDescription(tool.getToolDescription());
        newSysTool.setToolTypeId(tool.getToolTypeId());
        newSysTool.setToolTypeNameRdd(toolType.getName());
        newSysTool.setToolOperating(tool.getToolOperating());

        LOGGER.info("硬件 TOOL :{}",newSysTool);
        toolRDao.update(newSysTool);
    }

    @Override
    public void insertBatch(int num,int waitTime){
        try {
            testDao.insertBatch(num,waitTime);
            Thread.sleep(waitTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    @ApiOperation(value = "查询设备信息",notes = "查询所有设备信息")
    public List<SysTool> listSysTool() {
        LOGGER.info("硬件 TOOL list");
        return toolRDao.list();
    }

    @Override
    @ApiOperation(value = "查询设备信息",notes = "根据ID 查询设备信息")
    public SysTool getSysToolById(Long id) {
        LOGGER.info("硬件 TOOL id:{}",id);
        return toolRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "查询设备信息",notes = "根据条件 查询设备信息(分页)")
    public ListPager querySysToolByPager(SysTool tool, Pager pager) {
        LOGGER.info("硬件 TOOL SysTool:{}，Pager:{}",tool,pager);
        if(tool == null) {
            pager = PagerHelper.getPager(pager, toolRDao.listCount());
            return new ListPager(toolRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysTool(tool);
            pager = PagerHelper.getPager(pager, toolRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(toolRDao.findByHqlWherePage(hqlPack + tool.orderBy(),pager),pager);
        }
    }

    @Override
    @ApiOperation(value = "更新设备信息",notes = "更新设备状态信息")
    public void updateSysToolStatusById(Long id, Integer status) {
        LOGGER.info("硬件 TOOL id:{}，status：{}",id,status);
        toolRDao.updateByProperties("id",id,"toolStatus", status);
    }

    @Override
    @ApiOperation(value = "删除硬件信息",notes = "根据 ID 批量删除硬件信息")
    public int deleteSysToolByIds(Long[] ids) {
        LOGGER.info("消息 SYS_MESSAGE ids:{}", Arrays.toString(ids));
        return toolRDao.deleteByIds(ids);
    }

    @Override
    @ApiOperation(value = "更新设备信息",notes = "根据 ID 批量修改硬件状态信息")
    public int updateSysToolStatusByIds(Long[] ids, Integer status) {
        LOGGER.info("硬件 TOOL ids：{}，status：{}",ids,status);
        StringBuffer where = new StringBuffer();
        HqlPack.getInPack(StringUtils.join( ids,","), "id", where);

        return toolRDao.updateByHqlWhere(where.toString(), "toolStatus", status);
    }

    public List<SysTool> findSysToolByTop(int topNum) {
        return toolRDao.findByHqlTopWhere("",topNum);
    }

    @Override
    public List<SysTool> findSysToolByIdInterval(long min, long max) {
        return toolRDao.findByHqlWhere(CoreHqlPack.packHqlByInterval("id",min,max));
    }
}
