package com.cyksj.service.coupon.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.mapper.EquipmentActivityReduceMapper;
import com.cyksj.model.entity.EquipmentActivityReduce;
import com.cyksj.service.coupon.EquipmentActivityReduceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 项目名: yhlxj2
 * 文件名: EquipmentActivityReduceServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/1/30 11:05
 */
@Service
@RequiredArgsConstructor
public class EquipmentActivityReduceServiceImpl extends ServiceImpl<EquipmentActivityReduceMapper,EquipmentActivityReduce> implements EquipmentActivityReduceService {
	private final EquipmentActivityReduceMapper equipmentActivityReduceMapper;

	@Override
	public void saveOrUpdateEquipmentAcReduceConfig(EquipmentActivityReduce equipmentActivityReduce) {
		checkParams(equipmentActivityReduce);
		this.saveOrUpdate(equipmentActivityReduce);
	}

	public void checkParams(EquipmentActivityReduce equipmentActivityReduce) {
		String name = equipmentActivityReduce.getName();
		BigDecimal f = equipmentActivityReduce.getF();
		BigDecimal q = equipmentActivityReduce.getQ();
		BigDecimal tq = equipmentActivityReduce.getTq();
		Date st = equipmentActivityReduce.getSt();
		Date et = equipmentActivityReduce.getEt();
		if (ObjectUtil.hasEmpty(name, f, q, tq, st, et)) {
			throw BusinessRuntimeException.getInstance("请补充必填项..");
		}
		BigDecimal zero = BigDecimal.ZERO;
		if (f.compareTo(zero) < 0 || q.compareTo(zero) < 0 || tq.compareTo(zero) < 0) {
			throw BusinessRuntimeException.getInstance("满减金额不可小于0");
		}

		Integer count = equipmentActivityReduceMapper.checkRepeatTimeDuration(equipmentActivityReduce.getId(), st, et);
		if (count > 0) {
			throw BusinessRuntimeException.getInstance("存在活动时间重叠的记录..");
		}
	}
}
