package com.czg.service.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.czg.market.dto.MkDistributionLevelConfigDTO;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.czg.market.entity.MkDistributionLevelConfig;
import com.czg.market.service.MkDistributionLevelConfigService;
import com.czg.service.market.mapper.MkDistributionLevelConfigMapper;
import org.springframework.stereotype.Service;

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

/**
 * 分销员等级配置 服务层实现。
 *
 * @author ww
 * @since 2025-10-25
 */
@Service
public class MkDistributionLevelConfigServiceImpl extends ServiceImpl<MkDistributionLevelConfigMapper, MkDistributionLevelConfig> implements MkDistributionLevelConfigService{

    @Override
    public void updateByShopId(List<MkDistributionLevelConfigDTO> levelConfigList,  Long shopId, Long id) {
        int level = 1;
        List<Long> idList = new ArrayList<>();
        for (MkDistributionLevelConfigDTO item : levelConfigList) {
            MkDistributionLevelConfig config = BeanUtil.copyProperties(item, MkDistributionLevelConfig.class);
            config.setDistributionConfigId(id);
            config.setShopId(shopId);
            config.setLevel(level++);
            saveOrUpdate(config);
            idList.add(config.getId());
        }

        if (!idList.isEmpty()) {
            remove(new QueryWrapper().eq(MkDistributionLevelConfig::getDistributionConfigId, id).notIn(MkDistributionLevelConfig::getId, idList));
        }
    }
}
