package com.yueqian.xingyun.basedata.listeners.app;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yueqian.xingyun.basedata.entity.Address;
import com.yueqian.xingyun.basedata.enums.AddressEntityType;
import com.yueqian.xingyun.basedata.events.DeleteStoreCenterEvent;
import com.yueqian.xingyun.basedata.service.address.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class AddressForDeleteStoreCenterListener implements
    ApplicationListener<DeleteStoreCenterEvent> {

  @Autowired
  private AddressService addressService;

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void onApplicationEvent(DeleteStoreCenterEvent event) {

    Wrapper<Address> deleteWrapper = Wrappers.lambdaQuery(Address.class)
        .eq(Address::getEntityId, event.getEntity().getId())
        .eq(Address::getEntityType, AddressEntityType.SC);
    addressService.remove(deleteWrapper);
  }
}
