package com.ktg.iscs.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.constant.UserConstants;
import com.ktg.common.core.text.Convert;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.StringUtils;
import com.ktg.common.utils.obj.BeanUtils;
import com.ktg.iscs.domain.IsHardware;
import com.ktg.iscs.domain.IsHardwareType;
import com.ktg.iscs.domain.dto.hardware.GetListDTO;
import com.ktg.iscs.domain.vo.hardware.IsHardwareTypeVO;
import com.ktg.iscs.mapper.IsHardwareTypeMapper;
import com.ktg.iscs.service.IIsHardwareService;
import com.ktg.iscs.service.IIsHardwareTypeService;
import com.ktg.system.strategy.AutoCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 硬件类型Service业务层处理
 *
 * @author cgj
 * @date 2024-10-16
 */
@Service
public class IsHardwareTypeServiceImpl extends ServiceImpl<IsHardwareTypeMapper, IsHardwareType> implements IIsHardwareTypeService
{
    @Autowired
    private IsHardwareTypeMapper isHardwareTypeMapper;
    @Autowired
    private AutoCodeUtil autoCodeUtil;
    @Autowired
    private IIsHardwareService iIsHardwareService;

    /**
     * 查询硬件类型
     *
     * @param id 硬件类型主键
     * @return 硬件类型
     */
    @Override
    public IsHardwareType selectIsHardwareTypeById(Long id)
    {
        return isHardwareTypeMapper.selectIsHardwareTypeById(id);
    }

    /**
     * 查询硬件类型列表
     *
     * @param isHardwareType 硬件类型
     * @return 硬件类型
     */
    @Override
    public List<IsHardwareType> selectIsHardwareTypeList(IsHardwareType isHardwareType)
    {
        return isHardwareTypeMapper.selectIsHardwareTypeList(isHardwareType);
    }

    /**
     * 新增硬件类型
     *
     * @param isHardwareType 硬件类型
     * @return 结果
     */
    @Override
    public int insertIsHardwareType(IsHardwareType isHardwareType)
    {
        if (StringUtils.isBlank(isHardwareType.getHardwareTypeCode())) {
            isHardwareType.setHardwareTypeCode(autoCodeUtil.genSerialCode(UserConstants.HARDWARE_TYPE_CODE,null));
        }
        /*if(isHardwareType.getParentTypeId()!= null){
            IsHardwareType parent = getOne(Wrappers.<IsHardwareType>lambdaQuery()
                    .eq(IsHardwareType::getId, isHardwareType.getParentTypeId()));
            if(StringUtils.isNotNull(parent)){
                isHardwareType.setAncestors(parent.getAncestors()+","+parent.getId());
            }
        }*/
        isHardwareType.setCreateTime(DateUtils.getNowDate());
        return isHardwareTypeMapper.insertIsHardwareType(isHardwareType);
    }

    /**
     * 修改硬件类型
     *
     * @param isHardwareType 硬件类型
     * @return 结果
     */
    @Override
    public int updateIsHardwareType(IsHardwareType isHardwareType)
    {
        isHardwareType.setUpdateTime(DateUtils.getNowDate());
        return isHardwareTypeMapper.updateIsHardwareType(isHardwareType);
    }

    /**
     * 批量删除硬件类型
     *
     * @param ids 需要删除的硬件类型主键
     * @return 结果
     */
    @Override
    public int deleteIsHardwareTypeByIds(String ids)
    {
        Assert.notBlank(ids, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(ids);
        for (Long longId : longIds) {
            List<IsHardware> list = iIsHardwareService.list(Wrappers.<IsHardware>lambdaQuery()
                    .eq(IsHardware::getHardwareTypeId, longId));
            if (!list.isEmpty()) {
                Assert.isTrue(false, list.get(0).getHardwareTypeName() + "类型下已配置了设备，不能删除！");
            }
        }
        // 判断下面有没有子类，有则不能删除
        List<IsHardwareType> hardwareTypeList = list(Wrappers.<IsHardwareType>lambdaQuery().in(IsHardwareType::getParentTypeId, longIds));
        Assert.isTrue(hardwareTypeList.isEmpty(), "选中的类别下存在子类，不可删除！");
        return isHardwareTypeMapper.deleteIsHardwareTypeByIds(longIds);
    }

    /**
     * 删除硬件类型信息
     *
     * @param id 硬件类型主键
     * @return 结果
     */
    @Override
    public int deleteIsHardwareTypeById(Long id)
    {
        return isHardwareTypeMapper.deleteIsHardwareTypeById(id);
    }

    @Override
    public List<IsHardwareTypeVO> getIsHardwareTypeList(GetListDTO getListDTO) {
        List<IsHardwareType> list = list(Wrappers.<IsHardwareType>lambdaQuery()
                .like(StringUtils.isNotBlank(getListDTO.getHardwareTypeName()), IsHardwareType::getHardwareTypeName, getListDTO.getHardwareTypeName())
                .like(StringUtils.isNotBlank(getListDTO.getHardwareTypeCode()), IsHardwareType::getHardwareTypeCode, getListDTO.getHardwareTypeCode())
                .eq(StringUtils.isNotBlank(getListDTO.getEnableFlag()), IsHardwareType::getEnableFlag, getListDTO.getEnableFlag()));
        return BeanUtils.toBean(list, IsHardwareTypeVO.class);
    }

    @Override
    public Page<IsHardwareType> getIsHardwareTypePage(Page<IsHardwareType> page, GetListDTO getListDTO) {
        Page<IsHardwareType> result = page(page, Wrappers.<IsHardwareType>lambdaQuery()
                .like(org.apache.commons.lang3.StringUtils.isNotBlank(getListDTO.getHardwareTypeName()), IsHardwareType::getHardwareTypeName, getListDTO.getHardwareTypeName())
                .like(org.apache.commons.lang3.StringUtils.isNotBlank(getListDTO.getHardwareTypeCode()), IsHardwareType::getHardwareTypeCode, getListDTO.getHardwareTypeCode())
                .eq(org.apache.commons.lang3.StringUtils.isNotBlank(getListDTO.getEnableFlag()), IsHardwareType::getEnableFlag, getListDTO.getEnableFlag())
                .orderByDesc(IsHardwareType::getId));
        return result;
    }
}
