package com.fs.live.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.fs.live.domain.Live;
import com.fs.live.service.ILiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.live.mapper.LiveWatchConfigMapper;
import com.fs.live.domain.LiveWatchConfig;
import com.fs.live.service.ILiveWatchConfigService;

/**
 * 直播观看奖励设置Service业务层处理
 *
 * @author fs
 * @date 2025-07-08
 */
@Service
public class LiveWatchConfigServiceImpl implements ILiveWatchConfigService {

    @Autowired
    private ILiveService liveService;
    @Autowired
    private LiveWatchConfigMapper baseMapper;


    /**
     * 查询直播观看奖励设置
     *
     * @param id 直播观看奖励设置主键
     * @return 直播观看奖励设置
     */
    @Override
    public LiveWatchConfig selectLiveWatchConfigById(Long id)
    {
        return baseMapper.selectLiveWatchConfigById(id);
    }

    /**
     * 根据直播ID查询直播观看奖励设置
     *
     * @param id 直播ID
     * @return 直播观看奖励设置
     */
    @Override
    public String selectLiveWatchConfigByLiveId(Long id) {
        Live live = liveService.selectLiveByLiveId(id);
        if(live == null){
            return null;
        }
        return live.getConfigJson();
    }

    /**
     * 查询直播观看奖励设置列表
     *
     * @param liveWatchConfig 直播观看奖励设置
     * @return 直播观看奖励设置
     */
    @Override
    public List<LiveWatchConfig> selectLiveWatchConfigList(LiveWatchConfig liveWatchConfig)
    {
        return baseMapper.selectLiveWatchConfigList(liveWatchConfig);
    }

    /**
     * 新增直播观看奖励设置
     *
     * @return 结果
     */
    @Override
    public int insertLiveWatchConfig(String userId, String jsonConfig ,Long liveId)
    {
        Live live = liveService.selectLiveByLiveId(liveId);
        if(live == null){
            return 0;
        }
        JSONObject jsonObject = JSON.parseObject(jsonConfig);
        jsonObject.put("liveId", live.getLiveId());
        jsonConfig = jsonObject.toJSONString();
        live.setConfigJson(jsonConfig);
        return liveService.updateLiveEntity(live);
    }

    /**
     * 修改直播观看奖励设置
     *
     * @param jsonConfig 直播观看奖励设置
     * @return 结果
     */
    @Override
    public int updateLiveWatchConfig(String jsonConfig, Long liveId)
    {
        Live live = liveService.selectLiveByLiveId(liveId);
        if(live == null){
            return 0;
        }
        JSONObject jsonObject = JSON.parseObject(jsonConfig);
        jsonObject.put("liveId", live.getLiveId());
        jsonConfig = jsonObject.toJSONString();
        live.setConfigJson(jsonConfig);
        return liveService.updateLiveEntity(live);
    }

    /**
     * 批量删除直播观看奖励设置
     *
     * @param ids 需要删除的直播观看奖励设置主键
     * @return 结果
     */
    @Override
    public int deleteLiveWatchConfigByIds(Long[] ids)
    {
        return baseMapper.deleteLiveWatchConfigByIds(ids);
    }

    /**
     * 删除直播观看奖励设置信息
     *
     * @param id 直播观看奖励设置主键
     * @return 结果
     */
    @Override
    public int deleteLiveWatchConfigById(Long id)
    {
        return baseMapper.deleteLiveWatchConfigById(id);
    }
}
