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

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.opration.entity.dto.PopularizeCommissionSettingDTO;
import com.fzm.mall.server.admin.opration.entity.vo.PopularizeCommissionSettingVO;
import com.fzm.mall.server.admin.opration.model.PopularizeCommission;
import com.fzm.mall.server.admin.opration.mapper.PopularizeCommissionMapper;
import com.fzm.mall.server.admin.opration.service.IPopularizeCommissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.user_center.entity.dto.MemberConfigDTO;
import com.fzm.mall.server.admin.user_center.entity.vo.MemberConfigVO;
import com.fzm.mall.server.admin.user_center.model.SysMember;
import lombok.RequiredArgsConstructor;
import org.dozer.DozerBeanMapper;
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-07-19
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PopularizeCommissionServiceImpl extends ServiceImpl<PopularizeCommissionMapper, PopularizeCommission> implements IPopularizeCommissionService {

    private final DozerBeanMapper beanMapper;

    @Override
    public void save(List<PopularizeCommissionSettingDTO> dtoList) {
        dtoList.forEach(v -> {
            PopularizeCommission popularizeCommission = beanMapper.map(v, PopularizeCommission.class);
            UpdateWrapper<PopularizeCommission> uw = new UpdateWrapper<>();
            uw.lambda().eq(PopularizeCommission::getSort, popularizeCommission.getSort())
                    .eq(PopularizeCommission::getUpdateTime, popularizeCommission.getUpdateTime());
            boolean update = update(popularizeCommission, uw);
            if (!update) {
                throw new MyException(MallResponseError.UPDATE_ERROR);
            }
        });
    }

    @Override
    public List<PopularizeCommissionSettingVO> listSelfDefine() {
        List<PopularizeCommissionSettingVO> configVOList = list().stream()
                .map(v -> beanMapper.map(v, PopularizeCommissionSettingVO.class))
                .collect(Collectors.toList());
        return configVOList;
    }

}
