package com.yami.shop.distribution.comment.listener;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.esotericsoftware.minlog.Log;
import com.yami.shop.bean.event.OpenShopInitEvent;
import com.yami.shop.bean.event.ProdChangeEvent;
import com.yami.shop.bean.model.Product;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.Json;
import com.yami.shop.distribution.comment.model.DistributionAuditingSet;
import com.yami.shop.distribution.comment.service.DistributionAuditingSetService;
import com.yami.shop.distribution.comment.service.DistributionRecruitSetService;
import com.yami.shop.distribution.comment.vo.DistributionAuditingConditionsVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component("distributionProdChangeListener")
@AllArgsConstructor
public class ProdChangeListener {

    private final DistributionAuditingSetService distributionAuditingSetService;

    @EventListener(ProdChangeEvent.class)
    public void ProdChangeEvent(ProdChangeEvent event) {
        // 删除的商品的时候，好像不需要删除分销的设置
        // 因为删除了商品，有购买过依旧可以判断这个商品用户是否购买过
        // 暂时不执行下满的代码
        // 删除商品时，处理分销设置
//        handleDistributionAfterDeleteProd(event);


    }

    /**  删除商品时，处理分销设置 */
    private void handleDistributionAfterDeleteProd(ProdChangeEvent event) {
        Product product = event.getProduct();
        Long prodId = product.getProdId();
        if (Objects.isNull(prodId)) {
            return;
        }
        // 店铺分销员审核设置
        DistributionAuditingSet distributionAuditingSet = distributionAuditingSetService.getByShopId();
        DistributionAuditingConditionsVO auditingConditions = distributionAuditingSet.getAuditingConditions();
        List<Long> prodIds = auditingConditions.getProdList();
        if (Objects.isNull(prodIds)) {
            return;
        }
        if (!prodIds.contains(prodId)) {
            return;
        }
        // 删除的商品在 分销设置->申请条件设置->申请条件->购买指定商品的列表中
        // 删除商品时也删除掉这个列表中的商品
        prodIds.remove(prodId);
        auditingConditions.setProdList(prodIds);
        auditingConditions.setProductDtoList(null);
        distributionAuditingSet.setAuditingConditions(auditingConditions);
        distributionAuditingSet.setAuditingConditionsJson(Json.toJsonString(distributionAuditingSet.getAuditingConditions()));
        if (Objects.isNull(distributionAuditingSet.getAuditingConditionsJson())) {
            return;
        }
        boolean update = distributionAuditingSetService.update(new LambdaUpdateWrapper<DistributionAuditingSet>()
                .set(DistributionAuditingSet::getAuditingConditionsJson, distributionAuditingSet.getAuditingConditionsJson())
                .set(DistributionAuditingSet::getUpdateTime, new Date())
                .eq(DistributionAuditingSet::getAuditingSetId, distributionAuditingSet.getAuditingSetId()));
        if (update) {
            distributionAuditingSetService.removeCacheByShopId(Constant.PLATFORM_SHOP_ID);
        }
        String okStr = update ? "成功" : "失败";
        Log.info("删除商品prodId = " + prodId + "时，同时也去除在分销设置中的商品prodId = "+ prodId + " ，更新" + okStr);
    }

}
