package com.cyksj.service.market.draw.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.mapper.manage.coupon.CouponMapper;
import com.cyksj.mapper.market.draw.LuckyDrawMapper;
import com.cyksj.model.entity.Coupon;
import com.cyksj.model.entity.LuckyDraw;
import com.cyksj.model.views.GoodsDonSkuView;
import com.cyksj.service.market.draw.LuckyDrawService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

/*
 *项目名: netflix
 *文件名: LuckyDrawServiceImpl
 *创建者: JavaZou
 *创建时间:2022/12/27 16:05
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class LuckyDrawServiceImpl implements LuckyDrawService {
	private final LuckyDrawMapper luckyDrawMapper;

	private final CouponMapper couponMapper;

	private final BeanSearcher beanSearcher;

	@Override
	public void saveLuckyDraw(LuckyDraw luckyDraw) throws Exception {
		//校验参数
		checkLuckyDraw(luckyDraw);
		luckyDrawMapper.insert(luckyDraw);
	}

	@Override
	public void putStatus(Long id) throws Exception {
		LuckyDraw luckyDraw = luckyDrawMapper.selectById(id);
		if (luckyDraw == null || !luckyDraw.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该奖品已不存在");
		}
		if (!luckyDraw.getStatus()) {
			//剔除相同位置的奖品
			changeLuckyDrawIndex(luckyDraw);
		}
		luckyDraw.setStatus(!luckyDraw.getStatus());
		luckyDrawMapper.updateById(luckyDraw);
	}

	@Override
	public void updateLuckyDraw(LuckyDraw luckyDraw) throws Exception {
		if (luckyDraw.getId() == null || luckyDraw.getId() < 1) {
			throw BusinessRuntimeException.getInstance("奖品id为空");
		}
		LuckyDraw entity = luckyDrawMapper.selectById(luckyDraw.getId());
		if (entity == null || !entity.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该奖品已不存在");
		}
		if (entity.getStatus()) {
			changeLuckyDrawIndex(luckyDraw);
		}
		BeanUtil.copyProperties(luckyDraw, entity);
		//校验参数
		checkLuckyDraw((entity));
		luckyDrawMapper.updateById(entity);
	}

	public void checkLuckyDraw(LuckyDraw luckyDraw) throws Exception {
		HashSet<Integer> set = Jsons.parseObject(luckyDraw.getPosition(), HashSet.class);
		for (Integer index : set) {
			if (index < 1 || index > 9) {
				throw BusinessRuntimeException.getInstance("奖品位置错误");
			}
		}
		luckyDraw.setPosition(set.toString());
		if (luckyDraw.getType() == LuckyDraw.Type.coupon) {
			//优惠券是否存在
			if (luckyDraw.getCouponId() == null || luckyDraw.getCouponId() < 1) {
				throw BusinessRuntimeException.getInstance("选择的优惠券不存在");
			}
			Long couponId = luckyDraw.getCouponId();
			Coupon coupon = couponMapper.selectById(couponId);
			if (coupon == null) {
				throw BusinessRuntimeException.getInstance("选择的优惠券不存在");
			}
		} else if (luckyDraw.getType() == LuckyDraw.Type.points) {
			if (luckyDraw.getPoints() == null || luckyDraw.getPoints() < 0) {
				throw BusinessRuntimeException.getInstance("积分为空");
			}
		} else if (luckyDraw.getType() == LuckyDraw.Type.duration) {
			if (luckyDraw.getDays() == null || luckyDraw.getDays() < 1) {
				throw BusinessRuntimeException.getInstance("时长要大于0");
			}
			if (luckyDraw.getDrawType() != LuckyDraw.DrawType.specific) {
				throw BusinessRuntimeException.getInstance("该奖池下不允许配置该奖品类型");
			}
		} else {
			GoodsDonSkuView goodsDonSkuView = beanSearcher.searchFirst(GoodsDonSkuView.class, MapUtils.builder()
					.field(GoodsDonSkuView::getGoodsId, luckyDraw.getGoodsId()).op(Operator.Equal)
					.field(GoodsDonSkuView::getSkuId, luckyDraw.getSkuId()).op(Operator.Equal)
					.field(GoodsDonSkuView::getGoodsDonStatus, true).op(Operator.Equal)
					.field(GoodsDonSkuView::getGoodsDonSkuStatus, true).op(Operator.Equal).build());
			if (goodsDonSkuView == null) {
				throw BusinessRuntimeException.getInstance("所选平台或规格 不存在或已下架");
			}
		}
	}

	public void changeLuckyDrawIndex(LuckyDraw entity) throws Exception {
		//先找出所有已上架的商品
		List<LuckyDraw> luckyDraws = luckyDrawMapper.selectList(Wrappers.lambdaQuery(LuckyDraw.class)
				.eq(LuckyDraw::getStatus, true)
				.eq(LuckyDraw::getDrawType, entity.getDrawType())
				.ne(LuckyDraw::getId, entity.getId()));
		List<Integer> self = Jsons.parseList(entity.getPosition(), Integer.class);
		for (LuckyDraw luckyDraw : luckyDraws) {
			List<Integer> list = Jsons.parseList(luckyDraw.getPosition(), Integer.class);
			for (Integer selfIndex : self) {
				if (list.contains(selfIndex)) {
					if (list.size() == 1) {
						luckyDraw.setStatus(false);
					} else {
						list.removeIf(i -> i.equals(selfIndex));
						luckyDraw.setPosition(list.toString());
					}
					luckyDrawMapper.updateById(luckyDraw);
				}
			}
		}
	}
}
