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.common.utils.StringUtils;
import com.ktg.iscs.domain.IsKey;
import com.ktg.iscs.domain.vo.key.IsKeyVO;
import com.ktg.iscs.mapper.IsKeyMapper;
import com.ktg.iscs.service.IIsKeyService;
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-11-19
 */
@Service
public class IsKeyServiceImpl extends ServiceImpl<IsKeyMapper, IsKey> implements IIsKeyService
{
    @Autowired
    private IsKeyMapper isKeyMapper;
    @Autowired
    private AutoCodeUtil autoCodeUtil;

    /**
     * 查询钥匙
     *
     * @param keyId 钥匙主键
     * @return 钥匙
     */
    @Override
    public IsKey selectIsKeyByKeyId(Long keyId)
    {
        return isKeyMapper.selectIsKeyByKeyId(keyId);
    }

    @Override
    public IsKey selectIsKeyByNfc(String nfc) {
        return isKeyMapper.selectIsKeyByNfc(nfc);
    }

    /**
     * 查询钥匙列表
     *
     * @param isKey 钥匙
     * @return 钥匙
     */
    @Override
    public List<IsKey> selectIsKeyList(IsKey isKey)
    {
        return isKeyMapper.selectIsKeyList(isKey);
    }

    /**
     * 新增钥匙
     *
     * @param isKey 钥匙
     * @return 结果
     */
    @Override
    public int insertIsKey(IsKey isKey)
    {
        Assert.notBlank(isKey.getKeyName(), "名称不能为空！");
        // 1.检查钥匙名称是否重复
        List<IsKey> list = list(Wrappers.<IsKey>lambdaQuery().eq(IsKey::getKeyName, isKey.getKeyName()));
        Assert.isFalse(!list.isEmpty(), "该钥匙名称已被使用!");
        // 2.检查钥匙编码是否重复
        List<IsKey> isKeys = list(Wrappers.<IsKey>lambdaQuery().eq(IsKey::getKeyCode, isKey.getKeyCode()));
        Assert.isFalse(!isKeys.isEmpty(), "该钥匙编码已被使用!");
        // 3.检查nfc是否重复
        if (StringUtils.isNotBlank(isKey.getKeyNfc())) {
            List<IsKey> isKeys1 = list(Wrappers.<IsKey>lambdaQuery().eq(IsKey::getKeyNfc, isKey.getKeyNfc()));
            Assert.isFalse(!isKeys1.isEmpty(), "该NFC已被使用!");
        }
        // 4.检查MAC是否重复
        if (StringUtils.isNotBlank(isKey.getMacAddress())) {
            List<IsKey> isKeys2 = list(Wrappers.<IsKey>lambdaQuery().eq(IsKey::getMacAddress, isKey.getMacAddress()));
            Assert.isFalse(!isKeys2.isEmpty(), "该MAC已被使用!");
        }
        isKey.setCreateTime(DateUtils.getNowDate());
        return isKeyMapper.insertIsKey(isKey);
    }

    /**
     * 修改钥匙
     *
     * @param isKey 钥匙
     * @return 结果
     */
    @Override
    public int updateIsKey(IsKey isKey)
    {
        // 1.检查钥匙名称是否重复
        Assert.notBlank(isKey.getKeyName(), "名称不能为空！");
        List<IsKey> list = list(Wrappers.<IsKey>lambdaQuery()
                .eq(IsKey::getKeyName, isKey.getKeyName())
                .ne(IsKey::getKeyId, isKey.getKeyId()));
        Assert.isFalse(!list.isEmpty(), "该钥匙名称已被使用!");
        // 2.检查钥匙编码是否重复
        List<IsKey> isKeys = list(Wrappers.<IsKey>lambdaQuery()
                .eq(IsKey::getKeyCode, isKey.getKeyCode())
                .ne(IsKey::getKeyId, isKey.getKeyId()));
        Assert.isFalse(!isKeys.isEmpty(), "该钥匙编码已被使用!");
        // 3.检查nfc是否重复
        if (StringUtils.isNotBlank(isKey.getKeyNfc())) {
            List<IsKey> isKeys1 = list(Wrappers.<IsKey>lambdaQuery()
                    .eq(IsKey::getKeyNfc, isKey.getKeyNfc())
                    .ne(IsKey::getKeyId, isKey.getKeyId()));
            Assert.isFalse(!isKeys1.isEmpty(), "该NFC已被使用!");
        }
        // 4.检查MAC是否重复
        if (StringUtils.isNotBlank(isKey.getMacAddress())) {
            List<IsKey> isKeys2 = list(Wrappers.<IsKey>lambdaQuery()
                    .eq(IsKey::getMacAddress, isKey.getMacAddress())
                    .ne(IsKey::getKeyId, isKey.getKeyId()));
            Assert.isFalse(!isKeys2.isEmpty(), "该MAC已被使用!");
        }
        isKey.setUpdateTime(DateUtils.getNowDate());
        return isKeyMapper.updateIsKey(isKey);
    }

    /**
     * 批量删除钥匙
     *
     * @param keyIds 需要删除的钥匙主键
     * @return 结果
     */
    @Override
    public int deleteIsKeyByKeyIds(String keyIds)
    {
        Assert.notBlank(keyIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(keyIds);
        return isKeyMapper.deleteIsKeyByKeyIds(longIds);
    }

    /**
     * 删除钥匙信息
     *
     * @param keyId 钥匙主键
     * @return 结果
     */
    @Override
    public int deleteIsKeyByKeyId(Long keyId)
    {
        return isKeyMapper.deleteIsKeyByKeyId(keyId);
    }

    @Override
    public Page<IsKeyVO> getIsKeyPage(Page<IsKey> page, IsKey isKey) {
        return isKeyMapper.getIsKeyPage(page, isKey);
    }

    @Override
    public Boolean insertIsKeyByCabinet(IsKey isKey) {
        Assert.isFalse(StringUtils.isBlank(isKey.getKeyNfc()), "请给我钥匙的nfc！");
        Assert.isFalse(StringUtils.isBlank(isKey.getMacAddress()), "请给我钥匙的mac地址！");
        // 0.检查钥匙的nfc是否存在，存在就不允许添加
        List<IsKey> list = list(Wrappers.<IsKey>lambdaQuery()
                .eq(IsKey::getKeyNfc, isKey.getKeyNfc()));
        Assert.isTrue(list.isEmpty(), "该nfc的钥匙已存在！");
        // 0.1检查mac是否重复
        List<IsKey> list1 = list(Wrappers.<IsKey>lambdaQuery()
                .eq(IsKey::getMacAddress, isKey.getMacAddress()));
        Assert.isTrue(list1.isEmpty(), "该mac的钥匙已存在！");
        // 1.自动生成名称
        String keyName = "钥匙-" + isKey.getKeyNfc();
        // 2.自动生成编码
        String keyCode = autoCodeUtil.genSerialCode("KEY_CODE", null);
        isKey.setKeyName(keyName);
        isKey.setKeyCode(keyCode);
        isKey.setCreateTime(DateUtils.getNowDate());
        return save(isKey);
    }
}
