package com.ruoyi.cms.service.impl;

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

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.cms.constant.CommonConstant;
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.CommercialAreaMapper;
import com.ruoyi.cms.domain.CommercialArea;
import com.ruoyi.cms.service.ICommercialAreaService;

/**
 * 商圈Service业务层处理
 *
 * @author Lishundong
 * @date 2024-11-12
 */
@Service
public class CommercialAreaServiceImpl extends ServiceImpl<CommercialAreaMapper,CommercialArea> implements ICommercialAreaService
{
    @Autowired
    private CommercialAreaMapper commercialAreaMapper;

    /**
     * 查询商圈
     *
     * @param commercialAreaId 商圈主键
     * @return 商圈
     */
    @Override
    public CommercialArea selectCommercialAreaByCommercialAreaId(Long commercialAreaId)
    {
        return commercialAreaMapper.selectById(commercialAreaId);
    }

    /**
     * 查询商圈列表
     *
     * @param commercialArea 商圈
     * @return 商圈
     */
    @Override
    public List<CommercialArea> selectCommercialAreaList(CommercialArea commercialArea)
    {
        return commercialAreaMapper.selectCommercialAreaList(commercialArea);
    }
    public static void removeCommercialAreaCache()
    {
        SpringUtils.getBean(RedisCache.class).deleteObject(CommonConstant.COMMERICIAL_AREA);
    }

    public static void setCommercialAreaCache(List<CommercialArea> commercialAreaCache)
    {
        SpringUtils.getBean(RedisCache.class).setCacheObject(CommonConstant.COMMERICIAL_AREA, commercialAreaCache);
    }

    public static List<CommercialArea> getCommercialAreaCache()
    {
        JSONArray arrayCache = SpringUtils.getBean(RedisCache.class).getCacheObject(CommonConstant.COMMERICIAL_AREA);
        if (StringUtils.isNotNull(arrayCache))
        {
            return arrayCache.toList(CommercialArea.class);
        }
        return null;
    }
    /**
     * 新增商圈
     *
     * @param commercialArea 商圈
     * @return 结果
     */
    @Override
    public int insertCommercialArea(CommercialArea commercialArea)
    {
        Long aLong = commercialAreaMapper.selectCount(Wrappers.<CommercialArea>lambdaQuery().eq(CommercialArea::getCommercialAreaName, commercialArea.getCommercialAreaName()));
        if (aLong>0){
            throw new ServiceException("该商圈名已存在");
        }
        int insert = commercialAreaMapper.insert(commercialArea);
        if(insert>0){
            removeCommercialAreaCache();
        }
        return insert;
    }

    /**
     * 修改商圈
     *
     * @param commercialArea 商圈
     * @return 结果
     */
    @Override
    public int updateCommercialArea(CommercialArea commercialArea)
    {
        Long aLong = commercialAreaMapper.selectCount(Wrappers.<CommercialArea>lambdaQuery().eq(CommercialArea::getCommercialAreaName, commercialArea.getCommercialAreaName()).notIn(CommercialArea::getCommercialAreaId,commercialArea.getCommercialAreaId()));
        if (aLong>0){
            throw new ServiceException("该商圈名已存在");
        }
        int i = commercialAreaMapper.updateById(commercialArea);
        if(i>0){
            removeCommercialAreaCache();
        }
        return i;
    }

    /**
     * 批量删除商圈
     *
     * @param commercialAreaIds 需要删除的商圈主键
     * @return 结果
     */
    @Override
    public int deleteCommercialAreaByCommercialAreaIds(Long[] commercialAreaIds)
    {
        int i = commercialAreaMapper.deleteBatchIds(Arrays.asList(commercialAreaIds));
        if(i>0){
            removeCommercialAreaCache();
        }
        return i;
    }

    @Override
    public List<CommercialArea> appCommercialArea() {
        List<CommercialArea> commercialAreaCache = getCommercialAreaCache();
        if(Objects.isNull(commercialAreaCache))
        {
            List<CommercialArea> commercialAreaList = commercialAreaMapper.selectCommercialAreaList(null);
            setCommercialAreaCache(commercialAreaList);
            return commercialAreaList;
        }
        return commercialAreaCache;
    }
}
