package com.slodon.b2b2c.mq;

import com.slodon.b2b2c.core.constant.StarterConfigConst;
import com.slodon.b2b2c.core.constant.VideoConst;
import com.slodon.b2b2c.model.video.AuthorModel;
import com.slodon.b2b2c.model.video.LiveStoreBindMemberModel;
import com.slodon.b2b2c.video.example.AuthorExample;
import com.slodon.b2b2c.video.example.LiveStoreBindMemberExample;
import com.slodon.b2b2c.video.pojo.Author;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * mq处理解绑视频带货会员
 */
@Slf4j
@Component
public class MqVideoConsumer {

    @Resource
    private LiveStoreBindMemberModel liveStoreBindMemberModel;
    @Resource
    private AuthorModel authorModel;

    /**
     * 解绑视频带货会员
     *
     * @param storeId 店铺id
     */
    @RabbitListener(queues = StarterConfigConst.MQ_QUEUE_NAME_UNBIND_VIDEO_MEMBER, containerFactory = StarterConfigConst.MQ_FACTORY_NAME_SINGLE_PASS_ERR)
    public void unbindVideoMemberConsumer(Long storeId) {
        try {
            //删除店铺下的绑定会员
            LiveStoreBindMemberExample example = new LiveStoreBindMemberExample();
            example.setStoreId(storeId);
            liveStoreBindMemberModel.deleteLiveStoreBindMemberByExample(example);

            //清除在该店铺的店铺信息
            AuthorExample authorExample = new AuthorExample();
            authorExample.setStoreId(storeId);
            Author author = new Author();
            author.setRoleType(VideoConst.ROLE_TYPE_1);
            author.setStoreId(0L);
            author.setStoreName("");
            author.setVendorId(0L);
            author.setVendorName("");
            author.setUpdateTime(new Date());
            authorModel.updateAuthorByExample(author, authorExample);
        } catch (Exception e) {
            log.error("解绑视频带货会员失败，店铺id：{}", storeId, e);
        }
    }
}
