/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.mall.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.enums.BusinessSceneEnum;
import org.springblade.common.enums.CouponStateEnum;
import org.springblade.modules.api.vo.CouponQuantityVO;
import org.springblade.modules.mall.entity.Coupon;
import org.springblade.modules.mall.entity.CouponUser;
import org.springblade.modules.mall.service.ICouponService;
import org.springblade.modules.mall.vo.CouponUserVO;
import org.springblade.modules.mall.mapper.CouponUserMapper;
import org.springblade.modules.mall.service.ICouponUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.system.entity.UserApp;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;


/**
 * 用户优惠券关联表 服务实现类
 *
 * @author BladeX
 * @since 2023-05-04
 */
@Service
public class CouponUserServiceImpl extends ServiceImpl<CouponUserMapper, CouponUser> implements ICouponUserService {

	@Resource
	private ICouponService couponService;

	@Override
	public IPage<CouponUserVO> selectCouponUserPage(IPage<CouponUserVO> page, CouponUserVO couponUser) {
		return page.setRecords(baseMapper.selectCouponUserPage(page, couponUser));
	}

	@Override
	public CouponQuantityVO quantity(Long userId) {
		return baseMapper.quantity(userId);
	}

	@Override
	public IPage<CouponUserVO> userCouponList(IPage<CouponUserVO> page, Integer state, Long userId) {
		return page.setRecords(this.baseMapper.selectUserCouponList(page, userId, state));
	}


	@Transactional(rollbackFor = Exception.class)
	@Override
	public void receive(Long couponId, Long userId) {
		Coupon coupon = couponService.getById(couponId);
		Objects.requireNonNull(coupon, "优惠券信息不存在");

		CouponUser one = this.lambdaQuery().eq(CouponUser::getCouponId, couponId).eq(CouponUser::getUserId, userId).one();
		Assert.isNull(one, "请勿重复领取");

		CouponUser couponUser = new CouponUser();
		couponUser.setUserId(userId);
		couponUser.setCouponId(couponId);
		couponUser.setState(CouponStateEnum.UNUSED.getValue());
		// 保存用户优惠券信息
		this.save(couponUser);

		// 新人福利优惠券领取过后，修改用户领取状态
		if (BusinessSceneEnum.NEW.getValue().equals(coupon.getBusinessScene())) {
			UserApp userApp = new UserApp();
			userApp = userApp.selectOne(Wrappers.<UserApp>lambdaQuery().eq(UserApp::getUserId, userId));
			userApp.setReceiveNewBenefits(true);
			userApp.updateById();
		}
	}

	@Override
	public void use(String orderNo, Long couponUserId) {
		lambdaUpdate().set(CouponUser::getState, CouponStateEnum.USED.getValue())
				.set(CouponUser::getUserTime, LocalDateTime.now())
				.set(CouponUser::getOrderNo, orderNo)
				.eq(CouponUser::getId, couponUserId).update();
	}


}
