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.core.text.Convert;
import com.ktg.common.utils.DateUtils;
import com.ktg.iscs.domain.IsIsolationPoint;
import com.ktg.iscs.domain.IsLockType;
import com.ktg.iscs.domain.vo.lockType.IsLockTypeVO;
import com.ktg.iscs.mapper.IsLockTypeMapper;
import com.ktg.iscs.service.IIsIsolationPointService;
import com.ktg.iscs.service.IIsLockTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物资类型Service业务层处理
 *
 * @author cgj
 * @date 2024-11-12
 */
@Service
public class IsLockTypeServiceImpl extends ServiceImpl<IsLockTypeMapper, IsLockType> implements IIsLockTypeService
{
    @Autowired
    private IsLockTypeMapper isLockTypeMapper;
    @Autowired
    private IIsIsolationPointService iIsIsolationPointService;

    /**
     * 查询物资类型
     *
     * @param lockTypeId 物资类型主键
     * @return 物资类型
     */
    @Override
    public IsLockType selectIsLockTypeByLockTypeId(Long lockTypeId)
    {
        return isLockTypeMapper.selectIsLockTypeByLockTypeId(lockTypeId);
    }

    /**
     * 查询物资类型列表
     *
     * @param isLockType 物资类型
     * @return 物资类型
     */
    @Override
    public List<IsLockType> selectIsLockTypeList(IsLockType isLockType)
    {
        return isLockTypeMapper.selectIsLockTypeList(isLockType);
    }

    /**
     * 新增物资类型
     *
     * @param isLockType 物资类型
     * @return 结果
     */
    @Override
    public int insertIsLockType(IsLockType isLockType)
    {
        // 1.检查挂锁类型名称是否重复
        List<IsLockType> list = list(Wrappers.<IsLockType>lambdaQuery()
                .eq(IsLockType::getLockTypeName, isLockType.getLockTypeName()));
        Assert.isFalse(!list.isEmpty(), "该挂锁类型名称已被使用!");
        // 2.检查挂锁类型编码是否重复
        List<IsLockType> isLockTypes = list(Wrappers.<IsLockType>lambdaQuery()
                .eq(IsLockType::getLockTypeCode, isLockType.getLockTypeCode()));
        Assert.isFalse(!isLockTypes.isEmpty(), "该挂锁类型编码已被使用!");
        isLockType.setCreateTime(DateUtils.getNowDate());
        return isLockTypeMapper.insertIsLockType(isLockType);
    }

    /**
     * 修改物资类型
     *
     * @param isLockType 物资类型
     * @return 结果
     */
    @Override
    public int updateIsLockType(IsLockType isLockType)
    {
        // 1.检查挂锁类型名称是否重复
        List<IsLockType> list = list(Wrappers.<IsLockType>lambdaQuery()
                .eq(IsLockType::getLockTypeName, isLockType.getLockTypeName())
                .ne(IsLockType::getLockTypeId, isLockType.getLockTypeId()));
        Assert.isFalse(!list.isEmpty(), "该挂锁类型名称已被使用!");
        // 2.检查挂锁类型编码是否重复
        List<IsLockType> isLockTypes = list(Wrappers.<IsLockType>lambdaQuery()
                .eq(IsLockType::getLockTypeCode, isLockType.getLockTypeCode())
                .ne(IsLockType::getLockTypeId, isLockType.getLockTypeId()));
        Assert.isFalse(!isLockTypes.isEmpty(), "该挂锁类型编码已被使用!");
        isLockType.setUpdateTime(DateUtils.getNowDate());
        return isLockTypeMapper.updateIsLockType(isLockType);
    }

    /**
     * 批量删除物资类型
     *
     * @param lockTypeIds 需要删除的物资类型主键
     * @return 结果
     */
    @Override
    public int deleteIsLockTypeByLockTypeIds(String lockTypeIds)
    {
        Assert.notBlank(lockTypeIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(lockTypeIds);
        // 判断下面有没有子类，有则不能删除
        List<IsLockType> lockTypeList = list(Wrappers.<IsLockType>lambdaQuery().in(IsLockType::getParentTypeId, longIds));
        Assert.isTrue(lockTypeList.isEmpty(), "选中的类别下存在子类，不可删除！");
        // 判断有没有点位使用该类别，有的话不能删
        List<IsIsolationPoint> pointList = iIsIsolationPointService.list(Wrappers.<IsIsolationPoint>lambdaQuery()
                .in(IsIsolationPoint::getLockTypeId, longIds));
        Assert.isTrue(pointList.isEmpty(), "选中的类别下有被隔离点正在使用的，不可删除！");
        return isLockTypeMapper.deleteIsLockTypeByLockTypeIds(longIds);
    }

    /**
     * 删除物资类型信息
     *
     * @param lockTypeId 物资类型主键
     * @return 结果
     */
    @Override
    public int deleteIsLockTypeByLockTypeId(Long lockTypeId)
    {
        return isLockTypeMapper.deleteIsLockTypeByLockTypeId(lockTypeId);
    }

    @Override
    public Page<IsLockTypeVO> getIsLockTypePage(Page<IsLockType> page, IsLockType isLockType) {
        return isLockTypeMapper.getIsLockTypePage(page, isLockType);
    }
}
