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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.user_center.entity.dto.MerchantConfigDTO;
import com.fzm.mall.server.admin.user_center.entity.vo.MerchantConfigVO;
import com.fzm.mall.server.admin.user_center.model.MerchantConfig;
import com.fzm.mall.server.admin.user_center.mapper.MerchantConfigMapper;
import com.fzm.mall.server.admin.user_center.service.IMerchantConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangp
 * @date 2021/3/24 14:33
 * @description 商户设置 服务实现类
 * @since JDK 1.8
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MerchantConfigServiceImpl extends ServiceImpl<MerchantConfigMapper, MerchantConfig> implements IMerchantConfigService {

    private final DozerBeanMapper beanMapper;

    @Override
    public void init(Integer level) {
        MerchantConfig merchantConfig = new MerchantConfig();
        merchantConfig.setLevel(level);
        merchantConfig.setMembershipFee(BigDecimal.ZERO);
        merchantConfig.setServiceFee(BigDecimal.ZERO);
        merchantConfig.setDrawProportion(BigDecimal.ZERO);
        boolean save = save(merchantConfig);
        if (!save) {
            throw new MyException(MallResponseError.SAVE_ERROR);
        }
    }

    @Override
    public void edit(List<MerchantConfigDTO> merchantConfigDTOList) {
        for (MerchantConfigDTO dto : merchantConfigDTOList) {
            MerchantConfig merchantConfig = beanMapper.map(dto, MerchantConfig.class);
            merchantConfig.setDrawProportion(dto.getDrawProportion().divide(new BigDecimal(100)));

            UpdateWrapper<MerchantConfig> uw = new UpdateWrapper<>();
            uw.lambda()
                    .eq(MerchantConfig::getLevel, dto.getLevel());
            boolean update = update(merchantConfig, uw);

            if (!update) {
                throw new MyException(MallResponseError.UPDATE_ERROR);
            }
        }
    }

    @Override
    public List<MerchantConfigVO> listSelfDefine() {
        List<MerchantConfigVO> configVOList = list().stream()
                .map(v1 -> beanMapper.map(v1, MerchantConfigVO.class))
                .peek(v2 -> v2.setDrawProportion(
                        v2.getDrawProportion().multiply(new BigDecimal(100))))
                .collect(Collectors.toList());
        return configVOList;
    }

    @Override
    public boolean merchantSettingsExist(Integer level) {
        QueryWrapper<MerchantConfig> qw = new QueryWrapper<>();
        qw.lambda().eq(MerchantConfig::getLevel, level);
        return getOne(qw) != null;
    }
}
