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.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.AuctionDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.AuctionVO;
import com.fzm.mall.server.admin.goods_center.enums.status.AuctionStatusEnum;
import com.fzm.mall.server.admin.goods_center.mapper.AuctionMapper;
import com.fzm.mall.server.admin.goods_center.model.Auction;
import com.fzm.mall.server.admin.goods_center.service.IAuctionService;
import com.fzm.mall.server.admin.util.ObjectUtil;
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.math.BigDecimal;

/**
 * @author wangp
 * @date 2021/3/15 14:17
 * @description 商品拍卖 服务实现类
 * @since JDK 1.8
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AuctionServiceImpl extends ServiceImpl<AuctionMapper, Auction> implements IAuctionService {

    private final DozerBeanMapper beanMapper;

    @Override
    public void add(AuctionDTO auctionDTO, String goodsId) {
        ObjectUtil.checkObjFieldsIsNull(auctionDTO);

        if (auctionDTO.getBuyerDeposit().compareTo(BigDecimal.ZERO) < 0
                || auctionDTO.getPriceIncrease().compareTo(BigDecimal.ZERO) < 0) {
            throw new MyException(MallResponseError.VALUE_LT_0_ERROR);
        }

        Auction auction = beanMapper.map(auctionDTO, Auction.class);
        auction.setAuctionId(UUIDUtil.getUUID());
        auction.setGoodsId(goodsId);
        if (!save(auction)) {
            throw new MyException(MallResponseError.SAVE_ERROR);
        }
    }

    @Override
    public void edit(AuctionDTO auctionDTO, String goodsId) {
        ObjectUtil.checkObjFieldsIsNull(auctionDTO);

        if (auctionDTO.getBuyerDeposit().compareTo(BigDecimal.ZERO) < 0
                || auctionDTO.getPriceIncrease().compareTo(BigDecimal.ZERO) < 0) {
            throw new MyException(MallResponseError.VALUE_LT_0_ERROR);
        }

        UpdateWrapper<Auction> uw = new UpdateWrapper<>();
        uw.lambda().eq(Auction::getGoodsId, goodsId);

        Auction auction = beanMapper.map(auctionDTO, Auction.class);

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

    @Override
    public AuctionVO getByGoodsId(String goodsId) {
        Integer status = AuctionStatusEnum.DELETE.getStatus();

        QueryWrapper<Auction> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(Auction::getGoodsId, goodsId)
                .ne(Auction::getStatus, status);
        Auction auction = getOne(qw);

        return beanMapper.map(auction, AuctionVO.class);
    }
}
