package com.ruoyi.cms.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Arrays;
import java.util.Objects;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.cms.constant.CommonConstant;
import com.ruoyi.cms.domain.JobApply;
import com.ruoyi.cms.domain.SubwayStation;
import com.ruoyi.cms.mapper.SubwayStationMapper;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.SubwayLineMapper;
import com.ruoyi.cms.domain.SubwayLine;
import com.ruoyi.cms.service.ISubwayLineService;

/**
 * 地铁线路Service业务层处理
 *
 * @author Lishundong
 * @date 2024-11-12
 */
@Service
public class SubwayLineServiceImpl extends ServiceImpl<SubwayLineMapper,SubwayLine> implements ISubwayLineService
{
    @Autowired
    private SubwayLineMapper subwayLineMapper;
    @Autowired
    private SubwayStationMapper subwayStationMapper;
    /**
     * 查询地铁线路
     *
     * @param lineId 地铁线路主键
     * @return 地铁线路
     */
    @Override
    public SubwayLine selectSubwayLineByLineId(Long lineId)
    {
        SubwayLine subwayLine = subwayLineMapper.selectById(lineId);
        List<SubwayStation> subwayStations = subwayStationMapper.selectList(Wrappers.<SubwayStation>lambdaQuery().eq(SubwayStation::getLineId, subwayLine.getLineId()));
        subwayLine.setSubwayStationList(subwayStations);
        return subwayLine;
    }

    /**
     * 查询地铁线路列表
     *
     * @param subwayLine 地铁线路
     * @return 地铁线路
     */
    @Override
    public List<SubwayLine> selectSubwayLineList(SubwayLine subwayLine)
    {
        return subwayLineMapper.selectSubwayLineList(subwayLine);
    }

    /**
     * 新增地铁线路
     *
     * @param subwayLine 地铁线路
     * @return 结果
     */
    @Override
    public int insertSubwayLine(SubwayLine subwayLine)
    {
        Long count = subwayLineMapper.selectCount(Wrappers.<SubwayLine>lambdaQuery().eq(SubwayLine::getLineName, subwayLine.getLineName()));
        if(count>0){
            throw new ServiceException("已存在相同线路名称");
        }
        int insert = subwayLineMapper.insert(subwayLine);
        if(insert>0){
            removeSubwayCache();
        }
        return insert;
    }
    public static void removeSubwayCache()
    {
        SpringUtils.getBean(RedisCache.class).deleteObject(CommonConstant.SUBWAY_LINE_CACHE);
    }

    public static void setSubwayCache(List<SubwayLine> subwayLineList)
    {
        SpringUtils.getBean(RedisCache.class).setCacheObject(CommonConstant.SUBWAY_LINE_CACHE, subwayLineList);
    }

    public static List<SubwayLine> getSubwayCache()
    {
        JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(CommonConstant.SUBWAY_LINE_CACHE);
        if (StringUtils.isNotNull(arrayCache))
        {
            return arrayCache.toList(SubwayLine.class);
        }
        return null;
    }
    /**
     * 修改地铁线路
     *
     * @param subwayLine 地铁线路
     * @return 结果
     */
    @Override
    public int updateSubwayLine(SubwayLine subwayLine)
    {
        Long count = subwayLineMapper.selectCount(Wrappers.<SubwayLine>lambdaQuery().eq(SubwayLine::getLineName, subwayLine.getLineName()));
        if(count>1){
            throw new ServiceException("已存在相同线路名称");
        }
        int i = subwayLineMapper.updateById(subwayLine);
        if(i>0){
            removeSubwayCache();
        }
        return i;
    }

    /**
     * 批量删除地铁线路
     *
     * @param lineId 需要删除的地铁线路主键
     * @return 结果
     */
    @Override
    public int deleteSubwayLineByLineIds(Long lineId)
    {
        Long count = subwayStationMapper.selectCount(Wrappers.<SubwayStation>lambdaQuery().eq(SubwayStation::getLineId, lineId));
        if(count>0){
            throw new ServiceException("该地铁线路下已有站点，请删除其它站点后再删除该线路");
        }
        int i = subwayLineMapper.deleteById(lineId);
        removeSubwayCache();
        return i;
    }

    @Override
    public List<SubwayLine> appSubway() {
        List<SubwayLine> subwayCache = getSubwayCache();
        if(Objects.isNull(subwayCache))
        {
        List<SubwayLine> subwayLineList = subwayLineMapper.appSubway();
        setSubwayCache(subwayLineList);
        return subwayLineList;
        }
        return subwayCache;
    }
}
