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

import cn.estsh.i3plus.core.api.iservice.busi.ISysToolTypeService;
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.enumutil.CommonEnumUtil;
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.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description : 硬件类型接口实现
 * @Reference :
 * @Author : yunhao
 * @Date : 2018-10-28 12:35
 * @Modify :
 **/
@Service
public class SysToolTypeService implements ISysToolTypeService {
    public static final Logger LOGGER = LoggerFactory.getLogger(SysToolTypeService.class);

    @Autowired
    private SysToolTypeRepository toolTypeRDao;

    @Autowired
    private SysToolRepository toolRDao;

    @Override
    @ApiOperation(value = "新增硬件类型")
    public void insertSysToolType(SysToolType toolType) {
        LOGGER.info("硬件类型 TOOL_TYPE toolType:{}",toolType);
        toolTypeRDao.insert(toolType);
    }

    @Override
    @ApiOperation(value = "删除硬件类型",notes = "根据 ID 删除硬件类型信息")
    public void deleteSysToolTypeById(Long id) {
        LOGGER.info("硬件类型 TOOL_TYPE id:{}",id);
        long count = toolRDao.findByPropertyCount("toolTypeId",id);
        if(count >= 1){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("存在关联硬件无法删除！")
                    .setErrorSolution("请重新操作")
                    .build();
        }

        toolTypeRDao.deleteById(id);
    }

    @Override
    @ApiOperation(value = "更新硬件类型")
    public void updateSysToolType(SysToolType toolType) {
        LOGGER.info("硬件类型 TOOL_TYPE toolType:{}",toolType);
        toolTypeRDao.update(toolType);

        // 修改硬件中的类型名称冗余
        toolRDao.updateByProperties("toolTypeId",toolType.getId(),"toolTypeNameRdd",toolType.getName());
    }

    @Override
    @ApiOperation(value = "查询硬件类型")
    public List<SysToolType> listSysToolType() {
        LOGGER.info("硬件类型 TOOL_TYPE list");
        return toolTypeRDao.list();
    }

    @Override
    @ApiOperation(value = "查询硬件类型",notes = "根据 ID 查询硬件类型信息")
    public SysToolType getSysToolTypeById(Long id) {
        LOGGER.info("硬件类型 TOOL_TYPE id:{}",id);
        return toolTypeRDao.getById(id);
    }

    @Override
    @ApiOperation(value = "查询硬件类型",notes = "查询类型信息 复杂化查询")
    public ListPager querySysToolTypeByPager(SysToolType toolType, Pager pager) {
        LOGGER.info("硬件类型 TOOL_TYPE SysToolType:{}，Pager:{}",toolType,pager);
        if(toolType == null) {
            pager = PagerHelper.getPager(pager, toolTypeRDao.listCount());
            return new ListPager(toolTypeRDao.listPager(pager),pager);
        }else {
            String hqlPack = CoreHqlPack.packHqlSysToolType(toolType);
            pager = PagerHelper.getPager(pager, toolTypeRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(toolTypeRDao.findByHqlWherePage(hqlPack + toolType.orderBy(),pager),pager);
        }
    }

    @Override
    @ApiOperation(value = "删除硬件类型",notes = "根据 ID 批量删除硬件类型信息")
    public void deleteSysToolTypeByIds(Long[] ids) {
        LOGGER.info("硬件类型 TOOL_TYPE ids:{}", ids);
        List<SysTool> count = toolRDao.findByHqlWhere(CoreHqlPack.packHqlIds("toolTypeId",ids));
        if(count.size() > 0){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("【" + count.get(0).getToolTypeNameRdd() + "】 - 存在关联硬件无法删除！")
                    .setErrorSolution("请重新操作")
                    .build();
        }else {
            toolTypeRDao.deleteByIds(ids);
        }
    }
}
