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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.finance_center.entity.dto.PayModelDTO;
import com.fzm.mall.server.admin.finance_center.entity.vo.PayModeVO;
import com.fzm.mall.server.admin.finance_center.enums.status.PayModeStatusEnum;
import com.fzm.mall.server.admin.finance_center.model.MerchantPayMode;
import com.fzm.mall.server.admin.finance_center.mapper.MerchantPayModeMapper;
import com.fzm.mall.server.admin.finance_center.service.IMerchantPayModeService;
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.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wangp
 * @date 2021/4/26 15:53
 * @description 服务实现类
 * @since JDK 1.8
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MerchantPayModeServiceImpl extends ServiceImpl<MerchantPayModeMapper, MerchantPayMode> implements IMerchantPayModeService {
    private final DozerBeanMapper beanMapper;

    @Override
    public void add(List<PayModelDTO> dtos, String merchantId) {
        Integer delete = PayModeStatusEnum.DELETE.getStatus();
        Integer normal = PayModeStatusEnum.NORMAL.getStatus();
        boolean update = true;
        boolean save = true;

        QueryWrapper<MerchantPayMode> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(MerchantPayMode::getMerchantId, merchantId)
                .ne(MerchantPayMode::getStatus, delete);
        List<MerchantPayMode> list = list(qw);

        if (list.size() > 0) {
            list = list.stream().peek(v -> v.setStatus(delete))
                    .collect(Collectors.toList());
            update = updateBatchById(list);
        }

        List<MerchantPayMode> payModes = dtos.stream().map(v -> {
            MerchantPayMode payMode = beanMapper.map(v, MerchantPayMode.class);
            payMode.setMerchantId(merchantId);
            payMode.setStatus(normal);
            return payMode;
        }).collect(Collectors.toList());

        save = saveBatch(payModes);

        if (!update || !save) {
            throw new MyException(MallResponseError.SAVE_ERROR);
        }
    }

    @Override
    public List<PayModeVO> queryPayMode(String merchantId) {
        QueryWrapper<MerchantPayMode> qw = new QueryWrapper<>();
        qw.lambda().eq(MerchantPayMode::getMerchantId, merchantId)
                .eq(MerchantPayMode::getStatus,
                        PayModeStatusEnum.NORMAL.getStatus());
        List<PayModeVO> vos = list(qw).stream()
                .map(v -> beanMapper.map(v, PayModeVO.class))
                .collect(Collectors.toList());
        return vos;
    }
}
