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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.account.dto.extend.ShopExtendDTO;
import com.czg.exception.CzgException;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.account.entity.ShopExtend;
import com.czg.account.service.ShopExtendService;
import com.czg.service.account.mapper.ShopExtendMapper;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 店铺扩展信息 服务层实现。
 *
 * @author zs
 * @since 2025-02-19
 */
@Service
public class ShopExtendServiceImpl extends ServiceImpl<ShopExtendMapper, ShopExtend> implements ShopExtendService {

    @Override
    public Boolean edit(Long shopId, ShopExtendDTO shopExtendDTO) {
        ShopExtend shopExtend = queryChain().eq(ShopExtend::getShopId, shopId).eq(ShopExtend::getAutoKey, shopExtendDTO.getAutokey()).one();
        if (shopExtend == null) {
            throw new CzgException("key有误");
        }

        BeanUtil.copyProperties(shopExtendDTO, shopExtend);
        if (shopExtendDTO.getValue() == null) {
            shopExtend.setValue(null);
        }
        mapper.updateByQuery(shopExtend, false, new QueryWrapper().eq(ShopExtend::getShopId, shopId).eq(ShopExtend::getAutoKey, shopExtendDTO.getAutokey()));
        return true;
    }

    @Override
    public List<ShopExtend> listInfo(Long shopId, String autoKey) {
        QueryWrapper queryWrapper = new QueryWrapper().eq(ShopExtend::getShopId, shopId);
        if (StrUtil.isNotBlank(autoKey)) {
            queryWrapper.eq(ShopExtend::getAutoKey, autoKey);
        }
        List<ShopExtend> shopExtends = list(queryWrapper);
        shopExtends = checkAndInitialize(shopExtends, shopId);
        return shopExtends;
    }

    public List<ShopExtend> checkAndInitialize(List<ShopExtend> shopExtendList, long shopId) {
        List<ShopExtend> newRecords = new ArrayList<>();
        List<String> requiredAutoKeys = Arrays.asList("index_bg", "my_bg", "member_bg", "shopinfo_bg", "ticket_logo");

        if (shopExtendList.isEmpty()) {
            for (String key : requiredAutoKeys) {
                ShopExtend newRecord = new ShopExtend();
                newRecord.initAutoKey(key, shopId);
                newRecords.add(newRecord);
            }
            mapper.insertBatchSelective(newRecords, 50);
            return newRecords;
        } else {
            for (ShopExtend shopExtend : shopExtendList) {
                switch (shopExtend.getAutoKey()) {
                    case "index_bg":
                        shopExtend.setName("首页");
                        break;
                    case "my_bg":
                        shopExtend.setName("个人中心");
                        break;
                    case "member_bg":
                        shopExtend.setName("会员卡");
                        break;
                    case "shopinfo_bg":
                        shopExtend.setName("商品列表");
                        break;
                    case "ticket_logo":
                        shopExtend.setName("标签打印logo");
                        break;
                }
            }
            Set<String> existingAutoKeys = shopExtendList.stream()
                    .map(ShopExtend::getAutoKey)
                    .collect(Collectors.toSet());

            List<String> missingAutoKeys = requiredAutoKeys.stream()
                    .filter(key -> !existingAutoKeys.contains(key))
                    .toList();

            for (String key : missingAutoKeys) {
                ShopExtend newRecord = new ShopExtend();
                newRecord.initAutoKey(key, shopId);
                newRecords.add(newRecord);
            }

            if (!newRecords.isEmpty()) {
                mapper.insertBatchSelective(newRecords, 50);
            }

            shopExtendList.addAll(newRecords);
            return shopExtendList;
        }
    }
}
