package com.czg.service.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.czg.account.entity.ShopConfig;
import com.czg.account.entity.ShopInfo;
import com.czg.account.service.ShopConfigService;
import com.czg.exception.CzgException;
import com.czg.service.RedisService;
import com.czg.service.account.mapper.ShopConfigMapper;
import com.czg.service.account.mapper.ShopInfoMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.cache.annotation.Cacheable;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 店铺配置扩展
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 1.0 2025-04-03
 */
@DubboService
public class ShopConfigServiceImpl extends ServiceImpl<ShopConfigMapper, ShopConfig> implements ShopConfigService {

    @Resource
    private ShopInfoMapper shopInfoMapper;
    @Resource
    private RedisService redisService;


    @Override
    @Cacheable(cacheNames = "shopInfo",key = "#id")
    public ShopInfo getShopInfoAndConfig(Long id) throws CzgException {
        ShopInfo shopInfo = shopInfoMapper.selectOneById(id);
        if (shopInfo == null) {
            throw new CzgException("店铺不存在");
        }
        ShopConfig shopConfig = getById(shopInfo.getId());
        BeanUtil.copyProperties(shopConfig, shopInfo);
        return shopInfo;
    }

    @Override
    public boolean editConfig(Long shopId, ShopConfig shopConfig) {
        ShopConfig info = getById(shopId);
        if (info == null) {
            info = new ShopConfig();
            BeanUtil.copyProperties(shopConfig, info);
            info.setId(shopId);
            info.setMainId(shopInfoMapper.selectOneByQuery(new QueryWrapper().eq(ShopInfo::getId, shopId)).getMainId());
            info.setId(shopId);
            save(info);
        }else  {
            BeanUtil.copyProperties(shopConfig, info);
            updateById(info);
        }

        return true;
    }

    @Override
    public void editStatusByShopIdList(Long mainShopId, Integer isEnable,  boolean onyUpValid, String property, String useShopType, List<Long> shopIdList) {
        ShopConfig shopConfig = getById(mainShopId);
        if (shopConfig == null) {
            shopConfig = new ShopConfig();
            shopConfig.setId(mainShopId);
            save(shopConfig);
        }
        if (shopIdList == null) {
            shopIdList = new ArrayList<>();
        }
        // 防止报错
        Set<Long> childShopIdList = shopInfoMapper.selectListByQuery(new QueryWrapper().eq(ShopInfo::getMainId, mainShopId)
                .select(ShopInfo::getId)).stream().map(ShopInfo::getId).collect(Collectors.toSet());
        childShopIdList.add(mainShopId);
        List<Long> extistList = new ArrayList<>(list(new QueryWrapper().in(ShopConfig::getId, childShopIdList)).stream().map(ShopConfig::getId).toList());
        extistList.add(mainShopId);
        childShopIdList.stream().filter(id -> !extistList.contains(id)).forEach(id -> {
            save(new ShopConfig().setId(id).setMainId(id.equals(mainShopId) ? null : mainShopId).setIsEnableDiscount(0).setIsProductSuggest(0));
        });

        if (isEnable == 0 && !onyUpValid) {
            updateChain().in(ShopConfig::getId, childShopIdList).set(property, 0).update();
        }else {
            if ("all".equals(useShopType)) {
                updateChain().in(ShopConfig::getId, childShopIdList).set(property, 1).update();
            }else {
                if (shopIdList.isEmpty()) {
                    updateChain().eq(ShopConfig::getId, mainShopId).set(property, 1).update();
                }else {
                    if (!onyUpValid) {
                        updateChain().notIn(ShopConfig::getId, shopIdList).set(property, 0).update();
                    }
                    updateChain().in(ShopConfig::getId, shopIdList).set(property, 1).update();
                }
            }
        }

        extistList.forEach(item -> redisService.del("shopInfo::" + item));
        redisService.del("shopInfo::" + mainShopId);
    }

}
