package com.xzzn.ems.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.xzzn.common.utils.DateUtils;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.EmsFaultIssueLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xzzn.ems.mapper.EmsDeviceChangeLogMapper;
import com.xzzn.ems.domain.EmsDeviceChangeLog;
import com.xzzn.ems.service.IEmsDeviceChangeLogService;

/**
 * 设备状态变更记录Service业务层处理
 *
 * @author xzzn
 * @date 2025-11-15
 */
@Service
public class EmsDeviceChangeLogServiceImpl implements IEmsDeviceChangeLogService
{
    @Autowired
    private EmsDeviceChangeLogMapper emsDeviceChangeLogMapper;

    /**
     * 查询设备状态变更记录
     *
     * @param id 设备状态变更记录主键
     * @return 设备状态变更记录
     */
    @Override
    public EmsDeviceChangeLog selectEmsDeviceChangeLogById(Long id)
    {
        return emsDeviceChangeLogMapper.selectEmsDeviceChangeLogById(id);
    }

    /**
     * 查询设备状态变更记录列表
     *
     * @param emsDeviceChangeLog 设备状态变更记录
     * @return 设备状态变更记录
     */
    @Override
    public List<EmsDeviceChangeLog> selectEmsDeviceChangeLogList(EmsDeviceChangeLog emsDeviceChangeLog)
    {
        return emsDeviceChangeLogMapper.selectEmsDeviceChangeLogList(emsDeviceChangeLog);
    }

    /**
     * 新增设备状态变更记录
     *
     * @param emsDeviceChangeLog 设备状态变更记录
     * @return 结果
     */
    @Override
    public int insertEmsDeviceChangeLog(EmsDeviceChangeLog emsDeviceChangeLog)
    {
        emsDeviceChangeLog.setCreateTime(DateUtils.getNowDate());
        return emsDeviceChangeLogMapper.insertEmsDeviceChangeLog(emsDeviceChangeLog);
    }

    /**
     * 修改设备状态变更记录
     *
     * @param emsDeviceChangeLog 设备状态变更记录
     * @return 结果
     */
    @Override
    public int updateEmsDeviceChangeLog(EmsDeviceChangeLog emsDeviceChangeLog)
    {
        emsDeviceChangeLog.setUpdateTime(DateUtils.getNowDate());
        return emsDeviceChangeLogMapper.updateEmsDeviceChangeLog(emsDeviceChangeLog);
    }

    /**
     * 批量删除设备状态变更记录
     *
     * @param ids 需要删除的设备状态变更记录主键
     * @return 结果
     */
    @Override
    public int deleteEmsDeviceChangeLogByIds(Long[] ids)
    {
        return emsDeviceChangeLogMapper.deleteEmsDeviceChangeLogByIds(ids);
    }

    /**
     * 删除设备状态变更记录信息
     *
     * @param id 设备状态变更记录主键
     * @return 结果
     */
    @Override
    public int deleteEmsDeviceChangeLogById(Long id)
    {
        return emsDeviceChangeLogMapper.deleteEmsDeviceChangeLogById(id);
    }

    @Override
    public void dealSyncData(String content, String operateType) {
        if (StringUtils.isEmpty(content)) {
            return;
        }

        EmsDeviceChangeLog changeLog = JSON.parseObject(content, EmsDeviceChangeLog.class);
        String logId = changeLog.getLogId();
        if (checkLogIsExist(logId)) {
            return;
        }
        switch(operateType) {
            case "INSERT":
                insertEmsDeviceChangeLog(changeLog);
                break;
            default:
                break;
        }
    }

    private boolean checkLogIsExist(String logId) {
        EmsDeviceChangeLog changeLog = emsDeviceChangeLogMapper.selectDeviceChangeLogByLogId(logId);
        if (changeLog != null) {
            return true;
        }
        return false;
    }
}
