package com.ruoyi.dezhi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.dezhi.domain.BBusiDezhiVideoPackage;
import com.ruoyi.dezhi.mapper.BBusiDezhiVideoPackageMapper;
import com.ruoyi.dezhi.service.IBBusiDezhiVideoPackageService;
import com.ruoyi.system.service.ISysConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 视频包Service业务层处理
 * 
 * @author mingxue
 * @date 2021-07-22
 */
@Service
public class BBusiDezhiVideoPackageServiceImpl implements IBBusiDezhiVideoPackageService 
{
    @Autowired
    private BBusiDezhiVideoPackageMapper bBusiDezhiVideoPackageMapper;
    @Autowired
    private ISysConfigService configService;
    /**
     * 查询视频包
     * 
     * @param id 视频包ID
     * @return 视频包
     */
    @Override
    public BBusiDezhiVideoPackage selectBBusiDezhiVideoPackageById(Long id)
    {
        return bBusiDezhiVideoPackageMapper.selectBBusiDezhiVideoPackageById(id);
    }

    @Override
    public BBusiDezhiVideoPackage selectBBusiDezhiVideoPackageByPackId(String packId) {
        return bBusiDezhiVideoPackageMapper.selectBBusiDezhiVideoPackageByPackId(packId);
    }

    /**
     * 查询视频包列表
     * 
     * @param bBusiDezhiVideoPackage 视频包
     * @return 视频包
     */
    @Override
    public List<BBusiDezhiVideoPackage> selectBBusiDezhiVideoPackageList(BBusiDezhiVideoPackage bBusiDezhiVideoPackage)
    {
        return bBusiDezhiVideoPackageMapper.selectBBusiDezhiVideoPackageList(bBusiDezhiVideoPackage);
    }

    /**
     * 新增视频包
     * 
     * @param bBusiDezhiVideoPackage 视频包
     * @return 结果
     */
    @Override
    public int insertBBusiDezhiVideoPackage(BBusiDezhiVideoPackage bBusiDezhiVideoPackage)
    {
        return bBusiDezhiVideoPackageMapper.insertBBusiDezhiVideoPackage(bBusiDezhiVideoPackage);
    }

    /**
     * 修改视频包
     * 
     * @param bBusiDezhiVideoPackage 视频包
     * @return 结果
     */
    @Override
    public int updateBBusiDezhiVideoPackage(BBusiDezhiVideoPackage bBusiDezhiVideoPackage)
    {
        return bBusiDezhiVideoPackageMapper.updateBBusiDezhiVideoPackage(bBusiDezhiVideoPackage);
    }

    /**
     * 批量删除视频包
     * 
     * @param ids 需要删除的视频包ID
     * @return 结果
     */
    @Override
    public int deleteBBusiDezhiVideoPackageByIds(Long[] ids)
    {
        return bBusiDezhiVideoPackageMapper.deleteBBusiDezhiVideoPackageByIds(ids);
    }

    /**
     * 删除视频包信息
     * 
     * @param id 视频包ID
     * @return 结果
     */
    @Override
    public int deleteBBusiDezhiVideoPackageById(Long id)
    {
        return bBusiDezhiVideoPackageMapper.deleteBBusiDezhiVideoPackageById(id);
    }

    @Override
    public BBusiDezhiVideoPackage selectByPack(String packId) {
        BBusiDezhiVideoPackage query = new BBusiDezhiVideoPackage();
        query.setPackId(packId);
        List<BBusiDezhiVideoPackage> list = this.selectBBusiDezhiVideoPackageList(query);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    @Override
    public List<Object> getvideoCourse(Integer subject){
        String json = configService.selectConfigByKey("video_courses");
        JSONArray arr = JSON.parseArray(json);
        List<Object> list = new ArrayList<Object>(arr.size());
        for (int i = 0; i < arr.size(); i++) {
            JSONObject row = arr.getJSONObject(i);
            JSONArray subjects = row.getJSONArray("subjects");
            if (null != subjects && subjects.contains(subject)) {
                row.remove("subjects");
                list.add(row);
            }
        }

        return list;
    }
}
