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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.goods_center.entity.dto.IntegralDTO;
import com.fzm.mall.server.admin.goods_center.entity.dto.IntegralExchangeDTO;
import com.fzm.mall.server.admin.goods_center.entity.dto.OnOffDTO;
import com.fzm.mall.server.admin.goods_center.entity.dto.StockDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.IntegralExchangeVO;
import com.fzm.mall.server.admin.goods_center.enums.status.SpuStatusEnum;
import com.fzm.mall.server.admin.goods_center.mapper.IntegralExchangeMapper;
import com.fzm.mall.server.admin.goods_center.model.IntegralExchange;
import com.fzm.mall.server.admin.goods_center.service.IIntegralExchangeService;
import com.fzm.mall.server.admin.util.UUIDUtil;
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;

/**
 * @author wangp
 * @date 2021/3/25 16:06
 * @description 积分兑换服务实现类
 * @since JDK 1.8
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IntegralExchangeServiceImpl extends ServiceImpl<IntegralExchangeMapper, IntegralExchange> implements IIntegralExchangeService {

    private final DozerBeanMapper beanMapper;

    @Override
    public void add(IntegralExchangeDTO ieDTO, String merchantId) {
        IntegralExchange integralExchange = beanMapper.map(ieDTO, IntegralExchange.class);
        integralExchange.setGoodsId(UUIDUtil.getUUID());
        integralExchange.setStatus(SpuStatusEnum.ON.getStatus());
        boolean save = save(integralExchange);
        if (!save) {
            throw new MyException(MallResponseError.SAVE_ERROR);
        }
    }

    @Override
    public IPage<IntegralExchangeVO> page(String merchantId, String goodsId, String goodsName, Long addTimeHead, Long addTimeTail, Integer pageNum, Integer pageSize) {
        IPage<IntegralExchangeVO> page = new Page<>(pageNum, pageSize);
        return baseMapper.page(page, merchantId, goodsId, goodsName, addTimeHead, addTimeTail);
    }

    @Override
    public void onOff(OnOffDTO onOffDTO) {
        UpdateWrapper<IntegralExchange> uw = new UpdateWrapper<>();
        uw.lambda()
                .set(IntegralExchange::getStatus, onOffDTO.getType())
                .eq(IntegralExchange::getGoodsId, onOffDTO.getGoodsId());
        if (!update(uw)) {
            throw new MyException(MallResponseError.UPDATE_ERROR);
        }
    }

    @Override
    public void stock(StockDTO stockDTO) {
        String goodsId = stockDTO.getGoodsId();
        Integer stock = stockDTO.getStock();

        UpdateWrapper<IntegralExchange> uw = new UpdateWrapper<>();
        uw.lambda()
                .set(IntegralExchange::getStock, stock)
                .eq(IntegralExchange::getGoodsId, goodsId);
        if (!update(uw)) {
            throw new MyException(MallResponseError.UPDATE_ERROR);
        }
    }

    @Override
    public void integral(IntegralDTO integralDTO) {
        String goodsId = integralDTO.getGoodsId();
        Integer integral = integralDTO.getIntegral();

        UpdateWrapper<IntegralExchange> uw = new UpdateWrapper<>();
        uw.lambda()
                .set(IntegralExchange::getPNum, integral)
                .eq(IntegralExchange::getGoodsId, goodsId);
        if (!update(uw)) {
            throw new MyException(MallResponseError.UPDATE_ERROR);
        }
    }

    @Override
    public void delete(String goodsIds) {
        UpdateWrapper<IntegralExchange> uw = new UpdateWrapper<>();
        uw.lambda().set(IntegralExchange::getStatus, SpuStatusEnum.DELETE.getStatus())
                .eq(IntegralExchange::getGoodsId, goodsIds);
        if (!update(uw)) {
            throw new MyException(MallResponseError.UPDATE_ERROR);
        }
    }

    @Override
    @Transactional
    public void deleteBatch(List<String> goodsIds) {
        for (String goodsId : goodsIds) {
            UpdateWrapper<IntegralExchange> uw = new UpdateWrapper<>();
            uw.lambda()
                    .set(IntegralExchange::getStatus, SpuStatusEnum.DELETE.getStatus())
                    .eq(IntegralExchange::getGoodsId, goodsId);
            if (!update(uw)) {
                throw new MyException(MallResponseError.UPDATE_ERROR);
            }
        }
    }

    @Override
    public IntegralExchange getByGoodsId(String goodsId) {
        QueryWrapper<IntegralExchange> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(IntegralExchange::getGoodsId, goodsId)
                .ne(IntegralExchange::getStatus,
                        SpuStatusEnum.DELETE.getStatus());
        return getOne(qw);
    }
}

