package co.yixiang.yshop.module.product.service.productcoupon;

import co.yixiang.yshop.module.product.controller.app.coupon.vo.AppCouponDetailRespVO;
import co.yixiang.yshop.module.product.convert.storeproductcoupon.StoreProductCouponConvert;
import co.yixiang.yshop.module.product.dal.dataobject.productcoupon.ProductCouponDO;
import co.yixiang.yshop.module.product.dal.mysql.productcoupon.ProductCouponMapper;
import co.yixiang.yshop.module.product.enums.coupon.CouponReceiveTypeEnum;
import co.yixiang.yshop.module.product.enums.coupon.CouponScopeEnum;
import co.yixiang.yshop.module.product.service.productcouponrelation.AppCouponRelationService;
import co.yixiang.yshop.module.product.service.storeproduct.StoreProductService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * App优惠券 Service 实现类
 *
 * @author moxiangrong
 */
@Service
@Validated
public class AppCouponServiceImpl extends ServiceImpl<ProductCouponMapper, ProductCouponDO> implements AppCouponService {

    @Resource
    private StoreProductService productService;
    @Resource
    private AppCouponRelationService appCouponRelationService;


    @Override
    public List<AppCouponDetailRespVO> receiveList(Long productId, Long uid) {
        List<ProductCouponDO> couponDOS = this.baseMapper.receiveList(productId);
        if (CollectionUtils.isEmpty(couponDOS)) return null;
        List<AppCouponDetailRespVO> couponDetailList = StoreProductCouponConvert.INSTANCE.convertList(couponDOS);
        // 用户登录时 过滤已领取优惠券
        Predicate<AppCouponDetailRespVO> predicate = getPredicate(uid);

        couponDetailList = couponDetailList.stream()
                .filter(predicate)
                .sorted(Comparator.comparing(AppCouponDetailRespVO::getCouponType, Comparator.reverseOrder())
                        /*.thenComparing(AppCouponDetailRespVO::getDiscount, Comparator.naturalOrder())
                        .thenComparing(AppCouponDetailRespVO::getCouponValue, Comparator.reverseOrder())*/
                ).collect(Collectors.toList());
        return couponDetailList;
    }

    @NotNull
    private Predicate<AppCouponDetailRespVO> getPredicate(Long uid) {
        Predicate<AppCouponDetailRespVO> predicate = item -> true;
        if (Objects.nonNull(uid)) {
            predicate = item -> {
                //过滤用户已经领取的优惠券
                if (CouponReceiveTypeEnum.LIMITATION.getValue().equals(item.getReceiveType())) {
                    long receivedCount = appCouponRelationService.getReceivedCount(item.getId(), uid);
                    return receivedCount < item.getLimitNumber();
                }
                return true;
            };
        }
        return predicate;
    }

    @Override
    public List<Long> getCouponProductList(Long id) {
        List<Long> productIdList = new ArrayList<>();
        ProductCouponDO couponDO = getById(id);
        if (CouponScopeEnum.PRODUCT.getValue().equals(couponDO.getCouponScope())) {
            String[] productIdArr = couponDO.getScopeValues().split(",");
            for (String s : productIdArr) productIdList.add(Long.parseLong(s));
        }
        return productIdList;
    }
}
