package com.fzm.mall.server.admin.general_model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fzm.mall.server.admin.enums.setting.GoodsConfigureEnum;
import com.fzm.mall.server.admin.general_model.model.PlatformSetting;
import com.fzm.mall.server.admin.general_model.mapper.PlatformSettingMapper;
import com.fzm.mall.server.admin.general_model.service.IPlatformSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.goods_center.service.ISpuService;
import com.fzm.mall.server.admin.home_page.model.Merchant;
import com.fzm.mall.server.admin.home_page.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 平台事件设置 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-08-17
 */
@Service
@Transactional
public class PlatformSettingServiceImpl extends ServiceImpl<PlatformSettingMapper, PlatformSetting> implements IPlatformSettingService {

    @Autowired
    private IMerchantService merchantService;

    @Override
    public String queryByEvent(String event) {
        LambdaQueryWrapper<PlatformSetting> qw = new LambdaQueryWrapper<>();
        qw.eq(PlatformSetting::getSetEvent, event);
        return getOne(qw).getSetResult();
    }

    @Override
    public void delayDelivery(String delayDelivery) {
        LambdaUpdateWrapper<PlatformSetting> uw = new LambdaUpdateWrapper<>();
        uw.set(PlatformSetting::getSetResult, delayDelivery);
        uw.eq(PlatformSetting::getSetEvent, "user_delay_delivery");
        update(uw);

        //商户不可延迟提货
        if (Integer.valueOf(delayDelivery).equals(GoodsConfigureEnum.IMMEDIATE_DELIVERY.status())) {
            List<Merchant> merchants = merchantService.list().stream()
                    .peek(v -> v.setDelayDelivery(GoodsConfigureEnum.IMMEDIATE_DELIVERY.status()))
                    .collect(Collectors.toList());
            merchantService.updateBatchById(merchants);
        }
    }
}
