package com.ruoyi.sim.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.sim.mapper.SimMapper;
import com.ruoyi.sim.domain.Sim;

/**
 * 模拟器Service业务层处理
 *
 * @author tom
 * @date 2024-12-13
 */
@Service
public class SimService {

    @Autowired
    private SimMapper simMapper;

    /**
     * 查询模拟器
     *
     * @param simId 模拟器主键
     * @return 模拟器
     */
    public Sim selectSimBySimId(Long simId) {
        return simMapper.selectSimBySimId(simId);
    }

    /**
     * 查询模拟器列表
     *
     * @param sim 模拟器
     * @return 模拟器
     */
    public List<Sim> selectSimList(Sim sim) {
        return simMapper.selectSimList(sim);
    }

    /**
     * 新增模拟器
     *
     * @param sim 模拟器
     * @return 结果
     */
    public int insertSim(Sim sim) {
        sim.setCreateTime(DateUtils.getNowDate());
        return simMapper.insertSim(sim);
    }

    /**
     * 修改模拟器
     *
     * @param sim 模拟器
     * @return 结果
     */
    public int updateSim(Sim sim) {
        sim.setUpdateTime(DateUtils.getNowDate());
        return simMapper.updateSim(sim);
    }

    /**
     * 批量删除模拟器
     *
     * @param simIds 需要删除的模拟器主键
     * @return 结果
     */
    public int deleteSimBySimIds(Long[] simIds) {
        return simMapper.deleteSimBySimIds(simIds);
    }

    /**
     * 删除模拟器信息
     *
     * @param simId 模拟器主键
     * @return 结果
     */
    public int deleteSimBySimId(Long simId) {
        return simMapper.deleteSimBySimId(simId);
    }

    // -------------------------------- tom add  --------------------------------

    public List<Sim> listAll() {
        return selectSimList(new Sim());
    }

    public List<Sim> listAllEnable() {
        List<Sim> listR = new ArrayList<Sim>();
        listAll()
                .stream()
                .filter(Objects::nonNull)
                .filter(s -> !Sim.State.DISABLE.equals(s.getSimState()))
                .forEach(listR::add);
        return listR;
    }

    public int updateAllEnableState(String simState) {
        List<Sim> list = listAllEnable();
        for (Sim sim : list) {
            sim.setSimState(simState);
            simMapper.updateSim(sim);
        }
        return list.size();
    }

    public List<String> listSimTypes() {
        return Sim.TYPE_SET.stream().toList();
    }

    public boolean checkSimTypeOk(String simType) {
        return Sim.TYPE_SET.contains(simType);
    }

    public boolean checkState(String simState) {
        return Sim.STATE_SET.contains(simState);
    }

    public int updateSimStateBySimId(Long simId, String simState) {
        // check
        if (checkState(simState)) {

        }
        //
        Sim q = selectSimBySimId(simId);
        if (Objects.isNull(q)) {
            return 0;
        }
        q.setSimState(simState);
        return updateSim(q);
    }

    public boolean isSimStateBySimId(Long simId, String simState) {
        // check
        if (checkState(simState)) {

        }
        Sim q = selectSimBySimId(simId);
        if (Objects.isNull(q)) {
            return false;
        }
        return (q.getSimState().equals(simState));
    }

    public boolean isSimDisable(Long simId) {
        return isSimStateBySimId(simId, Sim.State.DISABLE);
    }

    public int updateSimSnBySimId(Long simId, String simSn) {
        // check
        if (StringUtils.isEmpty(simSn)) {
            return 0;
        }
        //
        Sim q = selectSimBySimId(simId);
        if (Objects.isNull(q)) {
            return 0;
        }
        q.setSimSn(simSn);
        q.setUpdateTime(DateUtils.getNowDate());
        return updateSim(q);
    }

    public int updateAllEnableStateInit() {
        List<Sim> list = simMapper.selectSimList(new Sim());
        int count = 0;
        for (Sim o : list) {
            if (o == null) {
                continue;
            }
            if (!Objects.equals(o.getSimState(), Sim.State.DISABLE)) {
                o.setSimState(Sim.State.ENABLE_INIT);
            }
            updateSim(o);
            count = count + 1;
        }
        return count;
    }

    @Async("tp-log")
    public void updateLastSentTime(Sim s) {
        if (s == null) {
            return;
        }
        s.setLastSentTime(DateUtils.getNowDate());
        updateSim(s);
    }

    @Async("tp-log")
    public void updateLastReceivedTime(Sim s) {
        if (s == null) {
            return;
        }
        s.setLastReceivedTime(DateUtils.getNowDate());
        updateSim(s);
    }
}
