package com.ktg.iscs.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSON;
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.framework.websocket.WebSocketAndroid;
import com.ktg.iscs.domain.IsLockCabinet;
import com.ktg.iscs.domain.IsLockCabinetSlots;
import com.ktg.iscs.domain.dto.hardwareApi.SendDataDTO;
import com.ktg.iscs.mapper.IsLockCabinetMapper;
import com.ktg.iscs.service.IIsLockCabinetService;
import com.ktg.iscs.service.IIsLockCabinetSlotsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 锁控机柜Service业务层处理
 *
 * @author cgj
 * @date 2025-05-14
 */
@Service
public class IsLockCabinetServiceImpl extends ServiceImpl<IsLockCabinetMapper, IsLockCabinet> implements IIsLockCabinetService {

    @Autowired
    private IsLockCabinetMapper isLockCabinetMapper;
    @Autowired
    private IIsLockCabinetSlotsService iIsLockCabinetSlotsService;

    @Override
    public Page<IsLockCabinet> getIsLockCabinetPage(Page<IsLockCabinet> page, IsLockCabinet isLockCabinet) {
        /*Page<IsLockCabinet> result = page(page, Wrappers.<IsLockCabinet>lambdaQuery()
                .like(StringUtils.isNotBlank(isLockCabinet.getCabinetCode()), IsLockCabinet::getCabinetCode, isLockCabinet.getCabinetCode())
                .like(StringUtils.isNotBlank(isLockCabinet.getCabinetName()), IsLockCabinet::getCabinetName, isLockCabinet.getCabinetName())
                .eq(StringUtils.isNotBlank(isLockCabinet.getIsOnline()), IsLockCabinet::getIsOnline, isLockCabinet.getIsOnline())
                .eq(isLockCabinet.getWorkareId() != null, IsLockCabinet::getWorkareId, isLockCabinet.getWorkareId())
                .eq(isLockCabinet.getWorkstationId() != null, IsLockCabinet::getWorkstationId, isLockCabinet.getWorkstationId())
                .eq(StringUtils.isNotBlank(isLockCabinet.getStatus()), IsLockCabinet::getStatus, isLockCabinet.getStatus())
                .orderByDesc(IsLockCabinet::getCabinetId));*/
        Page<IsLockCabinet> result = isLockCabinetMapper.getIsLockCabinetPage(page, isLockCabinet);
        return result;
    }

    @Override
    public Boolean deleteIsLockCabinetByCabinetIds(String cabinetIds) {
        Assert.notBlank(cabinetIds, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(cabinetIds);
        List<Long> ids = Arrays.asList(longIds);
        // 开始检测下面有没有正在使用的仓位信息，有的话不能删
        List<IsLockCabinetSlots> list = iIsLockCabinetSlotsService.list(Wrappers.<IsLockCabinetSlots>lambdaQuery()
                .in(IsLockCabinetSlots::getCabinetId, ids));
        Assert.isTrue(list.isEmpty(), "请先清理柜子下的仓位信息！");
        return removeBatchByIds(ids);
    }

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

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

    @Override
    public Boolean sendDataToLoto(SendDataDTO dto) {
        WebSocketAndroid.sendMessage(dto.getLotoSerialNumber(), JSON.toJSONString(dto));
        return true;
    }

}
