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.utils.StringUtils;
import com.ktg.iscs.domain.IsLockCabinetSlots;
import com.ktg.iscs.mapper.IsLockCabinetSlotsMapper;
import com.ktg.iscs.service.IIsLockCabinetSlotsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 锁控机柜-仓位Service业务层处理
 *
 * @author cgj
 * @date 2025-05-14
 */
@Service
public class IsLockCabinetSlotsServiceImpl extends ServiceImpl<IsLockCabinetSlotsMapper, IsLockCabinetSlots> implements IIsLockCabinetSlotsService {

    @Autowired
    private IsLockCabinetSlotsMapper isLockCabinetSlotsMapper;

    @Override
    public Page<IsLockCabinetSlots> getIsLockCabinetSlotsPage(Page<IsLockCabinetSlots> page, IsLockCabinetSlots isLockCabinetSlots) {
        Page<IsLockCabinetSlots> result = page(page, Wrappers.<IsLockCabinetSlots>lambdaQuery()
                .like(StringUtils.isNotBlank(isLockCabinetSlots.getSlotCode()), IsLockCabinetSlots::getSlotCode, isLockCabinetSlots.getSlotCode())
                .eq(isLockCabinetSlots.getCabinetId() != null, IsLockCabinetSlots::getCabinetId, isLockCabinetSlots.getCabinetId())
                .eq(StringUtils.isNotBlank(isLockCabinetSlots.getSlotType()), IsLockCabinetSlots::getSlotType, isLockCabinetSlots.getSlotType())
                .eq(StringUtils.isNotBlank(isLockCabinetSlots.getIsOccupied()), IsLockCabinetSlots::getIsOccupied, isLockCabinetSlots.getIsOccupied())
                .eq(StringUtils.isNotBlank(isLockCabinetSlots.getStatus()), IsLockCabinetSlots::getStatus, isLockCabinetSlots.getStatus())
                .orderByDesc(IsLockCabinetSlots::getSlotId));
        return result;
    }

    @Override
    public Boolean insertIsLockCabinetSlots(IsLockCabinetSlots isLockCabinetSlots) {
        // 开始判断编码是否重复
        if (StringUtils.isNotBlank(isLockCabinetSlots.getSlotCode())) {
            List<IsLockCabinetSlots> list = list(Wrappers.<IsLockCabinetSlots>lambdaQuery()
                    .eq(IsLockCabinetSlots::getSlotCode, isLockCabinetSlots.getSlotCode()));
            Assert.isTrue(list.isEmpty(), "该编码已被使用，请更换！");
        }
        return save(isLockCabinetSlots);
    }

    @Override
    public Boolean updateIsLockCabinetSlots(IsLockCabinetSlots isLockCabinetSlots) {
        Assert.notNull(isLockCabinetSlots.getSlotId(), "主键id不能为空！");
        // 开始判断编码是否重复
        if (StringUtils.isNotBlank(isLockCabinetSlots.getSlotCode())) {
            List<IsLockCabinetSlots> list = list(Wrappers.<IsLockCabinetSlots>lambdaQuery()
                    .eq(IsLockCabinetSlots::getSlotCode, isLockCabinetSlots.getSlotCode())
                    .ne(IsLockCabinetSlots::getCabinetId, isLockCabinetSlots.getCabinetId()));
            Assert.isTrue(list.isEmpty(), "该编码已被使用，请更换！");
        }
        return updateById(isLockCabinetSlots);
    }

}
