package com.slodon.b2b2c.model.live;

import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.write.live.LiveBroadWriteMapper;
import com.slodon.b2b2c.live.dto.LiveBroadcastDto;
import com.slodon.b2b2c.live.example.LiveBroadcastExample;
import com.slodon.b2b2c.live.pojo.LiveBroadcast;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class LiveBroadModel {

    @Autowired
    private LiveBroadWriteMapper liveBroadWriteMapper;

    public void broadcastRoomCreate(LiveBroadcastDto liveBroadcastDto) {

        LiveBroadcast live_broadcast = new LiveBroadcast();
        try {
            PropertyUtils.copyProperties(live_broadcast,liveBroadcastDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        liveBroadWriteMapper.insert(live_broadcast);
    }

    public void broadcastRoomEdit(LiveBroadcast live_broadcast) {
        liveBroadWriteMapper.updateByPrimaryKeySelective(live_broadcast);
    }

    /**
     * 根据条件获取直播公共信息表（SPU）列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<LiveBroadcast> getLivesList(LiveBroadcastExample example, PagerInfo pager) {
        List<LiveBroadcast> goodsList;
        if (pager != null) {
            pager.setRowsCount(liveBroadWriteMapper.countByExample(example));
            goodsList = liveBroadWriteMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            goodsList = liveBroadWriteMapper.listByExample(example);
        }
        return goodsList;
    }
}
