package com.huashe.park.core.service.impl;

import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huashe.park.common.EnhancedIDGenerator;
import com.huashe.park.core.mapper.ConsPileHoleInfoMapper;
import com.huashe.park.core.redis.RedisProxy;
import com.huashe.park.core.service.IConsPileHoleInfoService;
import com.huashe.park.domain.dto.cons.PileRealtimeIndex;
import com.huashe.park.domain.entity.ConsPileHoleInfo;

/**
 * 施工桩点信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-13
 */
@Service
public class ConsPileHoleInfoServiceImpl implements IConsPileHoleInfoService {
    @Autowired
    private ConsPileHoleInfoMapper consPileHoleInfoMapper;

    @Autowired
    private EnhancedIDGenerator byte8Id;

    @Autowired
    private RedisProxy redisProxy;

    private static final String CACHE_KEY = "consPileHoleInfo:byteId:%s";

    /**
     * 查询施工桩点信息
     * 
     * @param id 施工桩点信息主键
     * @return 施工桩点信息
     */
    @Override
    public ConsPileHoleInfo selectConsPileHoleInfoById(Long id) {
        return consPileHoleInfoMapper.selectConsPileHoleInfoById(id);
    }

    @Override
    public ConsPileHoleInfo selectConsPileHoleInfoByteId(String byteKey) {
        String redisKey = String.format(CACHE_KEY, byteKey);
        if (redisProxy.hasKey(redisKey)) {
            return redisProxy.getCacheObject(redisKey);
        }
        ConsPileHoleInfo consPileHoleInfo = consPileHoleInfoMapper.selectConsPileHoleInfoByByteId(byteKey);
        if (ObjectUtils.isNotEmpty(consPileHoleInfo)) {
            redisProxy.setCacheObject(redisKey, consPileHoleInfo);
        }
        return consPileHoleInfo;
    }

    @Override
    public List<PileRealtimeIndex> selectConsPileHoleRealtimeIndex(ConsPileHoleInfo consPileHoleInfo) {
        return consPileHoleInfoMapper.selectConsPileHoleRealtimeIndex(consPileHoleInfo);
    }

    /**
     * 查询施工桩点信息列表
     * 
     * @param consPileHoleInfo 施工桩点信息
     * @return 施工桩点信息
     */
    @Override
    public List<ConsPileHoleInfo> selectConsPileHoleInfoList(ConsPileHoleInfo consPileHoleInfo) {
        return consPileHoleInfoMapper.selectConsPileHoleInfoList(consPileHoleInfo);
    }

    @Override
    public List<ConsPileHoleInfo> selectNonConsPileHoleInfo(ConsPileHoleInfo consPileHoleInfo) {
        return consPileHoleInfoMapper.selectNonConsPileHoleInfo(consPileHoleInfo);
    }

    /**
     * 新增施工桩点信息
     * 
     * @param consPileHoleInfo 施工桩点信息
     * @return 结果
     */
    @Override
    public int insertConsPileHoleInfo(ConsPileHoleInfo consPileHoleInfo) {
        consPileHoleInfo.setByteId(byte8Id.generate());
        return consPileHoleInfoMapper.insertConsPileHoleInfo(consPileHoleInfo);
    }

    /**
     * 修改施工桩点信息
     * 
     * @param consPileHoleInfo 施工桩点信息
     * @return 结果
     */
    @Override
    public int updateConsPileHoleInfo(ConsPileHoleInfo consPileHoleInfo) {
        return consPileHoleInfoMapper.updateConsPileHoleInfo(consPileHoleInfo);
    }

    /**
     * 批量删除施工桩点信息
     * 
     * @param ids 需要删除的施工桩点信息主键
     * @return 结果
     */
    @Override
    public int deleteConsPileHoleInfoByIds(Long[] ids) {
        return consPileHoleInfoMapper.deleteConsPileHoleInfoByIds(ids);
    }

    /**
     * 删除施工桩点信息信息
     * 
     * @param id 施工桩点信息主键
     * @return 结果
     */
    @Override
    public int deleteConsPileHoleInfoById(Long id) {
        return consPileHoleInfoMapper.deleteConsPileHoleInfoById(id);
    }

    @Override
    public void deleteConsPileHoleInfoByConsUnitId(Long id) {
        consPileHoleInfoMapper.deleteConsPileHoleInfoByConsUnitId(id);
    }

    @Override
    public void importConsPileHole(List<ConsPileHoleInfo> consPileHoleInfo) {
        consPileHoleInfo.forEach((d) -> {
            d.setByteId(byte8Id.generate());
        });
        consPileHoleInfoMapper.batchInsertConsPileHoleInfo(consPileHoleInfo);
    }
}
