package com.xinelu.manage.service.coupon.impl;

import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.utils.SecurityUtils;
import com.xinelu.common.utils.codes.GenerateSystemCodeUtil;
import com.xinelu.manage.domain.coupon.Coupon;
import com.xinelu.manage.mapper.coupon.CouponMapper;
import com.xinelu.manage.service.coupon.ICouponService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


/**
 * 优惠券信息Service业务层处理
 *
 * @author xinyilu
 * @date 2023-02-23
 */
@Service
public class CouponServiceImpl implements ICouponService {
    @Resource
    private CouponMapper couponMapper;
    @Resource
    private GenerateSystemCodeUtil generateSystemCodeUtil;

    /**
     * 查询优惠券信息
     *
     * @param id 优惠券信息主键
     * @return 优惠券信息
     */
    @Override
    public Coupon selectCouponById(Long id) {
        return couponMapper.selectCouponById(id);
    }

    /**
     * 查询优惠券信息列表
     *
     * @param coupon 优惠券信息
     * @return 优惠券信息
     */
    @Override
    public List<Coupon> selectCouponList(Coupon coupon) {
        return couponMapper.selectCouponList(coupon);
    }

    /**
     * 新增优惠券信息
     *
     * @param coupon 优惠券信息
     * @return 结果
     */
    @Override
    public AjaxResult insertCoupon(Coupon coupon) {
        //判断优惠券名称是否重复
        int titleCount = couponMapper.selectCouponByTitle(coupon.getCouponTitle());
        if (titleCount > 0) {
            return AjaxResult.error("优惠劵名称重复，请重新输入！");
        }
        if (coupon.getCouponReductionDays() <= 0) {
            return AjaxResult.error("优惠券有效期要大于0");
        }
        if (coupon.getCouponPrice().compareTo(BigDecimal.ZERO) <= 0 || coupon.getCouponConsumePrice().compareTo(BigDecimal.ZERO) <= 0) {
            return AjaxResult.error("'优惠券面额'或'优惠券适用门槛'要大于0");
        }
        if (coupon.getCouponPrice().compareTo(coupon.getCouponConsumePrice()) == 0) {
            return AjaxResult.error("优惠劵面额与优惠券门槛不能相等，请重新输入！");
        }
        if (coupon.getCouponPrice().compareTo(coupon.getCouponConsumePrice()) > 0) {
            return AjaxResult.error("优惠劵面额不能大于优惠券门槛，请重新输入！");
        }
        coupon.setCreateTime(LocalDateTime.now());
        coupon.setCreateBy(SecurityUtils.getUsername());
        coupon.setCouponCode(Constants.COUPON_CODE + generateSystemCodeUtil.generateSystemCode(Constants.COUPON_CODE));
        return AjaxResult.success(couponMapper.insertCoupon(coupon));
    }

    /**
     * 修改优惠券信息
     *
     * @param coupon 优惠券信息
     * @return 结果
     */
    @Override
    public AjaxResult updateCoupon(Coupon coupon) {
        Coupon excuseCoupon = couponMapper.selectCouponById(coupon.getId());
        if (Objects.isNull(excuseCoupon)) {
            return AjaxResult.error("优惠券信息不存在！");
        }
        if (coupon.getCouponReductionDays() <= 0) {
            return AjaxResult.error("优惠券有效期要大于0");
        }
        if (coupon.getCouponPrice().compareTo(BigDecimal.ZERO) <= 0 || coupon.getCouponConsumePrice().compareTo(BigDecimal.ZERO) <= 0) {
            return AjaxResult.error("'优惠券面额'或'优惠券适用门槛'要大于0");
        }
        if (coupon.getCouponPrice().compareTo(coupon.getCouponConsumePrice()) == 0) {
            return AjaxResult.error("优惠劵面额与优惠券门槛不能相等，请重新输入！");
        }
        if (coupon.getCouponPrice().compareTo(coupon.getCouponConsumePrice()) > 0) {
            return AjaxResult.error("优惠劵面额不能大于优惠券门槛，请重新输入！");
        }
        //判断优惠券名称是否重复
        if (StringUtils.isNotBlank(excuseCoupon.getCouponTitle()) && !excuseCoupon.getCouponTitle().equals(coupon.getCouponTitle())) {
            int titleCount = couponMapper.selectCouponByTitle(coupon.getCouponTitle());
            if (titleCount > 0) {
                return AjaxResult.error("优惠劵名称重复，请重新输入！");
            }
        }
        coupon.setUpdateTime(LocalDateTime.now());
        coupon.setUpdateBy(SecurityUtils.getUsername());
        return AjaxResult.success(couponMapper.updateCoupon(coupon));
    }

    /**
     * 批量删除优惠券信息
     *
     * @param ids 需要删除的优惠券信息主键
     * @return 结果
     */
    @Override
    public int deleteCouponByIds(Long[] ids) {
        return couponMapper.deleteCouponByIds(ids);
    }

    /**
     * 删除优惠券信息信息
     *
     * @param id 优惠券信息主键
     * @return 结果
     */
    @Override
    public int deleteCouponById(Long id) {
        return couponMapper.deleteCouponById(id);
    }
}
