package org.springblade.gateway.point_gateway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.jsonwebtoken.lang.Assert;
import org.springblade.common.enums.AppConstant;
import org.springblade.gateway.point_gateway.enums.PointTypeEnum;
import org.springblade.gateway.point_gateway.excetion.PointException;
import org.springblade.gateway.point_gateway.service.PointExchangeService;
import org.springblade.sing.goods.entity.GoodsOrder;
import org.springblade.sing.goods.entity.PointGoods;
import org.springblade.sing.goods.enums.GoodsRecordType;
import org.springblade.sing.goods.service.IGoodsOrderService;
import org.springblade.sing.goods.service.IPointGoodsService;
import org.springblade.sing.point.entity.PointRecord;
import org.springblade.sing.point.entity.UserPufaPoint;
import org.springblade.sing.point.service.IPointRecordService;
import org.springblade.sing.point.service.IUserPufaPointService;
import org.springblade.sing.user.entity.UserAddress;
import org.springblade.sing.user.service.IUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Optional;

@Service
public class PointExchangeServiceImpl implements PointExchangeService {

	@Autowired
	private IPointGoodsService pointGoodsService;
	@Autowired
	private IPointRecordService pointRecordService;
	@Autowired
	private IUserPufaPointService userPufaPointService;
	@Autowired
	private IUserAddressService userAddressService;
	@Autowired
	private IGoodsOrderService goodsOrderService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean exchange(Integer type,String phone, Long goodsId, Long addressId ,Long num) {
		System.out.println(phone);
		UserPufaPoint user = userPufaPointService.getOne(new LambdaQueryWrapper<UserPufaPoint>().eq(UserPufaPoint::getPhone,phone));
		Assert.notNull(user,"用户不存在");
		PointGoods goods = pointGoodsService.getById(goodsId);
		Assert.notNull(goods,"商品不存在");
		UserAddress address = userAddressService.getById(addressId);
		Assert.notNull(address,"请添加收货地址");

		Boolean isLimit = Optional.ofNullable(goods.getIsLimit()).orElse(false);
		if (isLimit){
			int size = goodsOrderService.list(new LambdaQueryWrapper<GoodsOrder>().eq(GoodsOrder::getPhone, phone).eq(GoodsOrder::getPointGoodsId, goodsId)).size();
			if ((num + size) > goods.getLimitBuy()){
				throw new PointException("超过限购数量");
			}

		}

		if (goods.getRemain() < num) {
			throw new PointException("库存不足");
		}

		if (user.getPufaPoint().compareTo(BigDecimal.ZERO)<=0){
			throw new PointException("积分不足");
		}


		//商品所需积分
		BigDecimal exchangeNum = BigDecimal.valueOf(num);
		BigDecimal exchangePoint = goods.getPoint().multiply(exchangeNum);

		if (user.getPufaPoint().compareTo(exchangePoint) < 0){
			throw new PointException("积分不足");
		}
		user.setPufaPoint(user.getPufaPoint().subtract(exchangePoint));

		Assert.isTrue(userPufaPointService.saveOrUpdate(user),"兑换失败");

		//添加商品记录表
		GoodsOrder goodsOrder = new GoodsOrder();

		goodsOrder.setUserId(user.getUserId());
		goodsOrder.setPointGoodsId(goodsId);
		goodsOrder.setAddressId(addressId);
		goodsOrder.setNum(num);
		goodsOrder.setType(GoodsRecordType.POINT_GOODS_EXCHANGE);
		goodsOrder.setUsePoint(exchangePoint);
		goodsOrder.setGoodsImgUrl(goods.getImgUrl());
		goodsOrder.setGoodsName(goods.getName());

		String consigneeAddress = address.getProvinceName() + "-" + address.getCityName()
			+ "-" + address.getAreaName() + " " + address.getAddress();
		goodsOrder.setAddress(consigneeAddress);
		goodsOrder.setPhone(phone);
		goodsOrder.setConsignee(address.getConsignee());
		goodsOrder.setGoodsStatus(AppConstant.GOODS_STATUS.待发货);
		goodsOrder.setGoodsPoint(goods.getPoint());

		Assert.isTrue(goodsOrderService.save(goodsOrder),"兑换失败");

		//添加积分消费
		PointRecord pointRecord = new PointRecord();
		pointRecord.setPoint(goods.getPoint());
		pointRecord.setUserId(user.getId());
		pointRecord.setPointGoodsId(goodsId);
		pointRecord.setPointType(PointTypeEnum.PUFA_POINT_EXCHANGE);
		Assert.isTrue(pointRecordService.save(pointRecord),"兑换失败");

		//更新商品库存
		goods.setRemain(goods.getRemain() - num);
		pointGoodsService.updateById(goods);

		return true;
	}
}
