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.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.exception.MyException;
import com.fzm.mall.server.admin.goods_center.entity.dto.PreSaleDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.PreSaleVO;
import com.fzm.mall.server.admin.goods_center.enums.status.CommonStatusEnum;
import com.fzm.mall.server.admin.goods_center.model.PreSale;
import com.fzm.mall.server.admin.goods_center.mapper.PreSaleMapper;
import com.fzm.mall.server.admin.goods_center.service.IPreSaleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 16:06
 * @description 商品预售 服务实现类
 * @since JDK 1.8
 */
@Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PreSaleServiceImpl extends ServiceImpl<PreSaleMapper, PreSale> implements IPreSaleService {

    private final DozerBeanMapper beanMapper;

    @Override
    public void add(PreSaleDTO preSaleDTO, String goodsId) {
        ObjectUtil.checkObjFieldsIsNull(preSaleDTO);

        if (preSaleDTO.getSaleStartTime() < 0
                || preSaleDTO.getSaleEndTime() < 0
                || preSaleDTO.getDrawStartTime() < 0
                || preSaleDTO.getDrawEndTime() < 0
                || preSaleDTO.getDeposit().compareTo(BigDecimal.ZERO) < 0) {
            throw new MyException(MallResponseError.VALUE_LT_0_ERROR);
        }

        PreSale preSale = beanMapper.map(preSaleDTO, PreSale.class);
        preSale.setPreSaleId(UUIDUtil.getUUID());
        preSale.setGoodsId(goodsId);
        if (preSale.getDepositType().equals(1)) {
            preSale.setDeposit(preSale.getDeposit().divide(new BigDecimal(100)));
        }
        if (!save(preSale)) {
            throw new MyException(MallResponseError.SAVE_ERROR);
        }
    }

    @Override
    public void edit(PreSaleDTO preSaleDTO, String goodsId) {
        ObjectUtil.checkObjFieldsIsNull(preSaleDTO);

        if (preSaleDTO.getSaleStartTime() < 0
                || preSaleDTO.getSaleEndTime() < 0
                || preSaleDTO.getDrawStartTime() < 0
                || preSaleDTO.getDrawEndTime() < 0
                || preSaleDTO.getDeposit().compareTo(BigDecimal.ZERO) < 0) {
            throw new MyException(MallResponseError.VALUE_LT_0_ERROR);
        }

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

        PreSale preSale = beanMapper.map(preSaleDTO, PreSale.class);
        preSale.setDeposit(preSale.getDeposit().divide(new BigDecimal(100)));

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

    @Override
    public PreSaleVO getByGoodsId(String goodsId) {
        Integer status = CommonStatusEnum.EFFECTIVE.getStatus();

        QueryWrapper<PreSale> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(PreSale::getGoodsId, goodsId)
                .eq(PreSale::getStatus, status);
        PreSale preSale = getOne(qw);

        if (preSale.getDepositType().equals(1)) {
            preSale.setDeposit(preSale.getDeposit().multiply(new BigDecimal(100)));
        }

        return beanMapper.map(preSale, PreSaleVO.class);
    }
}
