
package com.tmerclub.cloud.multishop.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.user.feign.PopupFeignClient;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.JumpType;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.multishop.constant.HomeStatus;
import com.tmerclub.cloud.multishop.constant.RenovationType;
import com.tmerclub.cloud.multishop.mapper.ShopRenovationMapper;
import com.tmerclub.cloud.multishop.model.ShopRenovation;
import com.tmerclub.cloud.multishop.service.ShopRenovationService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 店铺装修信息
 *
 * @author Orange
 * @date 2022-08-12 11:27:18
 */
@Service
public class ShopRenovationServiceImpl implements ShopRenovationService {

    @Autowired
    private ShopRenovationMapper shopRenovationMapper;
    @DubboReference
    private PopupFeignClient popupFeignClient;

    @Override
    public PageVO<ShopRenovation> page(PageDTO pageDTO, ShopRenovation shopRenovation) {
        return PageUtil.doPage(pageDTO, () -> shopRenovationMapper.list(shopRenovation));
    }

    @Override
    @Cacheable(cacheNames = CacheNames.GET_BY_SHOP_RENOVATION_ID_KEY, key = "#renovationId")
    public ShopRenovation getByRenovationId(Long renovationId) {
        return shopRenovationMapper.getByRenovationId(renovationId);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.GET_SHOP_HOME_RENOVATION_KEY, key = "#shopId + ':' + #renovationType")
    public ShopRenovation getShopHomeRenovation(Long shopId, Integer renovationType) {
        return shopRenovationMapper.getByShopIdAndRenovationType(shopId, renovationType);
    }

    @Override
    public void save(ShopRenovation shopRenovation) {
        shopRenovationMapper.save(shopRenovation);
    }

    @Override
    public void update(ShopRenovation shopRenovation) {
        shopRenovationMapper.update(shopRenovation);
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void deleteById(Long renovationId, Integer renovationType, Long shopId) {
        shopRenovationMapper.deleteById(renovationId);
        if (Objects.equals(renovationType, RenovationType.H5.value())) {
            // 失效弹窗
            ServerResponseEntity<Void> response = popupFeignClient.deleteLink(renovationId, shopId, JumpType.MINI_PAGE.value(), null);
            if (!response.isSuccess()) {
                throw new LuckException(response.getMsg());
            }
        }
    }

    @Override
    public void removeCache(Long shopId, Integer renovationType, Long renovationId) {
        List<String> key = new ArrayList<>(Constant.INITIAL_CAPACITY);
        key.add(CacheNames.GET_SHOP_HOME_RENOVATION_KEY + CacheNames.UNION + shopId + CacheNames.UNION_KEY + renovationType);
        key.add(CacheNames.GET_BY_SHOP_RENOVATION_ID_KEY + CacheNames.UNION + renovationId);
        RedisUtil.del(key);
    }

    @Override
    public void updateToHomePage(ShopRenovation shopRenovation) {
        // 取消设置为主页
        if (Objects.equals(shopRenovation.getHomeStatus(), HomeStatus.YES.value())) {
            shopRenovation.setHomeStatus(HomeStatus.NO.value());
            update(shopRenovation);
            return;
        }

        ShopRenovation search = new ShopRenovation();
        search.setShopId(shopRenovation.getShopId());
        search.setRenovationType(shopRenovation.getRenovationType());
        search.setHomeStatus(HomeStatus.YES.value());
        // 设为主页的装修列表
        List<ShopRenovation> shopRenovationList = shopRenovationMapper.list(search);
        // 先取消已有的主页
        shopRenovationMapper.updateHomeStatus(shopRenovation.getShopId(), shopRenovation.getRenovationType());
        // 设置当前模板为主页
        shopRenovation.setHomeStatus(HomeStatus.YES.value());
        update(shopRenovation);
        // 清除旧的主页缓存
        if (CollUtil.isNotEmpty(shopRenovationList)) {
            List<String> key = new ArrayList<>(Constant.INITIAL_CAPACITY);
            for (ShopRenovation renovation : shopRenovationList) {
                key.add(CacheNames.GET_BY_SHOP_RENOVATION_ID_KEY + CacheNames.UNION + renovation.getRenovationId());
            }
            RedisUtil.del(key);
        }
    }
}
