package com.ktg.iscs.service.impl;

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.redis.RedisCache;
import com.ktg.common.utils.StringUtils;
import com.ktg.iscs.domain.IsLotoStation;
import com.ktg.iscs.domain.IsMap;
import com.ktg.iscs.domain.IsMapPoint;
import com.ktg.iscs.domain.dto.hardwareApi.UpdateSwitchStatusDTO;
import com.ktg.iscs.mapper.IsMapMapper;
import com.ktg.iscs.service.IIsLotoStationService;
import com.ktg.iscs.service.IIsMapPointService;
import com.ktg.iscs.service.IIsMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ktg.common.constant.Constants.SYS_ATTR;

/**
 * 地图参数Service业务层处理
 *
 * @author cgj
 * @date 2025-03-11
 */
@Service
public class IsMapServiceImpl extends ServiceImpl<IsMapMapper, IsMap> implements IIsMapService {

    @Autowired
    private IsMapMapper isMapMapper;
    @Autowired
    private IIsMapPointService iIsMapPointService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IIsLotoStationService isLotoStationService;

    @Override
    public Page<IsMap> getIsMapPage(Page<IsMap> page, IsMap isMap) {
        Page<IsMap> result = page(page, Wrappers.<IsMap>lambdaQuery()
                .like(StringUtils.isNotBlank(isMap.getName()), IsMap::getName, isMap.getName())
                .orderByDesc(IsMap::getId));
        return result;
    }

    @Override
    public IsMap selectIsMapById(Long id) {
        IsMap map = getById(id);
        if (map != null) {
            IsMapPoint point = new IsMapPoint();
            point.setMapId(id);
            List<IsMapPoint> list = iIsMapPointService.getIsMapPointList(point);
            // 查询lotostation信息
            IsLotoStation lotoStation = isLotoStationService.getOne(Wrappers.<IsLotoStation>lambdaQuery()
                    .eq(IsLotoStation::getMapId, id));
            if (lotoStation != null && StringUtils.isNotBlank(lotoStation.getLotoSerialNumber())) {
                List<UpdateSwitchStatusDTO> switchStatusList = redisCache.getCacheList(SYS_ATTR + lotoStation.getLotoSerialNumber() + ":sys.points.switch.records");
                if (!switchStatusList.isEmpty() && !list.isEmpty()) {
                    for (IsMapPoint isMapPoint : list) {
                        for (UpdateSwitchStatusDTO updateSwitchStatusDTO : switchStatusList) {
                            if (StringUtils.isNotBlank(isMapPoint.getPointSerialNumber())
                                    && StringUtils.isNotBlank(updateSwitchStatusDTO.getPointSerialNumber())
                                    && isMapPoint.getPointSerialNumber().equals(updateSwitchStatusDTO.getPointSerialNumber())) {
                                isMapPoint.setSwitchStatus(updateSwitchStatusDTO.getSwitchStatus());
                                isMapPoint.setSwitchLastUpdateTime(updateSwitchStatusDTO.getSwitchLastUpdateTime());
                            }
                        }
                    }
                }
                map.setPointList(list);
            }
        }
        return map;
    }

}
